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

Allow customers to be created with non-1 quantities of a plan #319

Merged
merged 2 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Args:
`settings.PINAX_STRIPE_DEFAULT_PLAN`.
- charge_immediately: whether or not the user should be immediately
charged for the subscription. Defaults to `True`.
- quantity: the quantity of the subscription. Defaults to `1`.

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

Expand Down
4 changes: 3 additions & 1 deletion pinax/stripe/actions/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def can_charge(customer):
return False


def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_immediately=True):
def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_immediately=True, quantity=1):
"""
Creates a Stripe customer.

Expand All @@ -39,6 +39,7 @@ def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_imme
plan: a plan to subscribe the user to
charge_immediately: whether or not the user should be immediately
charged for the subscription
quantity: the quantity (multiplier) of the subscription

Returns:
the pinax.stripe.models.Customer object that was created
Expand All @@ -49,6 +50,7 @@ def create(user, card=None, plan=settings.PINAX_STRIPE_DEFAULT_PLAN, charge_imme
email=user.email,
source=card,
plan=plan,
quantity=quantity,
trial_end=trial_end
)
try:
Expand Down
25 changes: 25 additions & 0 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,31 @@ def test_customer_create_user_with_plan(self, CreateMock, SyncMock, CreateAndPay
self.assertTrue(SyncMock.called)
self.assertTrue(CreateAndPayMock.called)

@patch("pinax.stripe.actions.invoices.create_and_pay")
@patch("pinax.stripe.actions.customers.sync_customer")
@patch("stripe.Customer.create")
def test_customer_create_user_with_plan_and_quantity(self, CreateMock, SyncMock, CreateAndPayMock):
Plan.objects.create(
stripe_id="pro-monthly",
name="Pro ($19.99/month each)",
amount=19.99,
interval="monthly",
interval_count=1,
currency="usd"
)
CreateMock.return_value = dict(id="cus_YYYYYYYYYYYYY")
customer = customers.create(self.user, card="token232323", plan=self.plan, quantity=42)
self.assertEqual(customer.user, self.user)
self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
_, kwargs = CreateMock.call_args
self.assertEqual(kwargs["email"], self.user.email)
self.assertEqual(kwargs["source"], "token232323")
self.assertEqual(kwargs["plan"], self.plan)
self.assertEqual(kwargs["quantity"], 42)
self.assertIsNotNone(kwargs["trial_end"])
self.assertTrue(SyncMock.called)
self.assertTrue(CreateAndPayMock.called)

@patch("stripe.Customer.retrieve")
def test_purge(self, RetrieveMock):
customer = Customer.objects.create(
Expand Down