Skip to content

Commit 68aa889

Browse files
authored
Merge pull request #319 from meshy/customers-create-plan-quantity
Allow customers to be created with non-1 quantities of a plan
2 parents a03b861 + 6b513ef commit 68aa889

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

docs/reference/actions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Args:
7878
`settings.PINAX_STRIPE_DEFAULT_PLAN`.
7979
- charge_immediately: whether or not the user should be immediately
8080
charged for the subscription. Defaults to `True`.
81+
- quantity: the quantity of the subscription. Defaults to `1`.
8182

8283
Returns: `pinax.stripe.models.Customer` object that was created
8384

pinax/stripe/actions/customers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def can_charge(customer):
2727
return False
2828

2929

30-
def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_immediately=True):
30+
def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_immediately=True, quantity=1):
3131
"""
3232
Creates a Stripe customer.
3333
@@ -39,6 +39,7 @@ def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_imme
3939
plan: a plan to subscribe the user to
4040
charge_immediately: whether or not the user should be immediately
4141
charged for the subscription
42+
quantity: the quantity (multiplier) of the subscription
4243
4344
Returns:
4445
the pinax.stripe.models.Customer object that was created
@@ -49,6 +50,7 @@ def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_imme
4950
email=user.email,
5051
source=card,
5152
plan=plan,
53+
quantity=quantity,
5254
trial_end=trial_end
5355
)
5456
try:

pinax/stripe/tests/test_actions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,31 @@ def test_customer_create_user_with_plan(self, CreateMock, SyncMock, CreateAndPay
193193
self.assertTrue(SyncMock.called)
194194
self.assertTrue(CreateAndPayMock.called)
195195

196+
@patch("pinax.stripe.actions.invoices.create_and_pay")
197+
@patch("pinax.stripe.actions.customers.sync_customer")
198+
@patch("stripe.Customer.create")
199+
def test_customer_create_user_with_plan_and_quantity(self, CreateMock, SyncMock, CreateAndPayMock):
200+
Plan.objects.create(
201+
stripe_id="pro-monthly",
202+
name="Pro ($19.99/month each)",
203+
amount=19.99,
204+
interval="monthly",
205+
interval_count=1,
206+
currency="usd"
207+
)
208+
CreateMock.return_value = dict(id="cus_YYYYYYYYYYYYY")
209+
customer = customers.create(self.user, card="token232323", plan=self.plan, quantity=42)
210+
self.assertEqual(customer.user, self.user)
211+
self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
212+
_, kwargs = CreateMock.call_args
213+
self.assertEqual(kwargs["email"], self.user.email)
214+
self.assertEqual(kwargs["source"], "token232323")
215+
self.assertEqual(kwargs["plan"], self.plan)
216+
self.assertEqual(kwargs["quantity"], 42)
217+
self.assertIsNotNone(kwargs["trial_end"])
218+
self.assertTrue(SyncMock.called)
219+
self.assertTrue(CreateAndPayMock.called)
220+
196221
@patch("stripe.Customer.retrieve")
197222
def test_purge(self, RetrieveMock):
198223
customer = Customer.objects.create(

0 commit comments

Comments
 (0)