Reputation: 7064
On signing up a user to a subscription, I would like to charge a different rate if the user has credit in my system. However, when the user signs up for the subscription, Stripe is charging them right away instead of waiting for me to mutate the invoice object on the invoice created webhook.
My understanding based on the docs is that charging the user will wait until after invoice create returns with a 200. However, my invoice created webhook is returning 500's so I know this isn't true.
My current code looks like this:
stripe.Subscription.create(
customer=user.stripe_customer_id,
plan=subscription_plan.stripe_product_id,
trial_period_days=trial_period,
idempotency_key=key)
My webhook looks like:
def invoice_created(request, *args, **kwargs):
user = request.user
invoice = request.data['data']['object']
stripe_customer = stripe.Customer.retrieve(user.stripe_customer_id)
if request.user.credit:
stripe_customer.add_invoice_item(
amount=-user.credit,
currency='usd',
description='Applied ${:.2f} of credit for subscription.'.format(user.credit),
invoice=invoice['id'],
)
return Response(status=200)
When I try to update the invoice, it says the invoice can't be modified. The error I'm getting is:
stripe.error.InvalidRequestError: Request "": Invalid invoice: This invoice is no longer editable
What am I supposed to do here?
Upvotes: 1
Views: 454
Reputation: 8737
My understanding based on the docs is that charging the user will wait until after invoice create returns with a 200.
This is not true for the first Invoice of a Subscription, which is always charged immediately.
If you want to add items to that first one, you'll want to create the Invoice Items on the Customer first, before you create the Subscription.
Upvotes: 2