Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Tiered Pricing Plans #629

Open
wants to merge 14 commits into
base: original
Choose a base branch
from
Prev Previous commit
Next Next commit
Add Tests for Syncing Plans with Tiers
  • Loading branch information
jksimoniii committed Feb 1, 2019
commit f1258314ac3e5f2e16d4ea13de588000cb2dea9d
2 changes: 1 addition & 1 deletion pinax/stripe/actions/plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def sync_plan(plan, event=None):
utils.update_with_defaults(obj, defaults, created)

if plan["tiers"]:
obj.tiers.all().delete()
obj.tiers.all().delete() # delete all tiers, since they don't have ids in Stripe
obj.tiers.set([models.Tier(
plan=obj,
amount=utils.convert_amount_for_db(tier["amount"], plan["currency"]),
Expand Down
63 changes: 61 additions & 2 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,10 +1234,40 @@ def test_sync_plans(self, PlanAutoPagerMock):
"tiers_mode": None,
"tiers": None
},
{
"id": "tiered1",
"object": "plan",
"amount": None,
"created": 1448121054,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": False,
"metadata": {},
"name": "The Simple Plan",
"statement_descriptor": "ALTMAN",
"trial_period_days": 3,
"billing_scheme": "tiered",
"tiers_mode": 'test',
"tiers": [
{
"amount": None,
"flat_amount": 14900,
"up_to": 100
},
{
"amount": 100,
"flat_amount": None,
"up_to": None
}
],
},
]
plans.sync_plans()
self.assertTrue(Plan.objects.all().count(), 2)
self.assertEqual(Plan.objects.all().count(), len(PlanAutoPagerMock.return_value))
self.assertEqual(Plan.objects.get(stripe_id="simple1").amount, decimal.Decimal("9.99"))
self.assertTrue(Plan.objects.filter(stripe_id="tiered1").exists())
self.assertEqual(Plan.objects.get(stripe_id="tiered1").tiers.count(), 2)

@patch("stripe.Plan.auto_paging_iter", create=True)
def test_sync_plans_update(self, PlanAutoPagerMock):
Expand Down Expand Up @@ -1276,13 +1306,42 @@ def test_sync_plans_update(self, PlanAutoPagerMock):
"tiers_mode": None,
"tiers": None
},
{
"id": "tiered1",
"object": "plan",
"amount": None,
"created": 1448121054,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": False,
"metadata": {},
"name": "The Simple Plan",
"statement_descriptor": "ALTMAN",
"trial_period_days": 3,
"billing_scheme": "tiered",
"tiers_mode": 'test',
"tiers": [
{
"amount": None,
"flat_amount": 14900,
"up_to": 100
},
{
"amount": 100,
"flat_amount": None,
"up_to": None
}
],
},
]
plans.sync_plans()
self.assertTrue(Plan.objects.all().count(), 2)
self.assertEqual(Plan.objects.all().count(), len(PlanAutoPagerMock.return_value))
self.assertEqual(Plan.objects.get(stripe_id="simple1").amount, decimal.Decimal("9.99"))
PlanAutoPagerMock.return_value[1].update({"amount": 499})
plans.sync_plans()
self.assertEqual(Plan.objects.get(stripe_id="simple1").amount, decimal.Decimal("4.99"))
self.assertEqual(Plan.objects.get(stripe_id="tiered1").tiers.count(), 2)

def test_sync_plan(self):
"""
Expand Down