Search code examples
stripe-payments

Stripe immediate payment on each quantity update


We are using Stripe as the payment gateway, and we have a yearly plan i.e .billing cycle of the plan is 1 year.

Within the billing cycle, the user can update opt for more seats which will result in increasing the quantity for the subscribed plan.

Subscription subscription = Subscription.retrieve(paymentDetails.getSubscriptionId());
int currentQuentity = subscription.getQuantity();
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("quantity", (currentQuentity + changeInQuantity));
subscription.update(updateParams);

So for any update stripe refunds(if quantity reduces) and charges(if quantity increases) at the end of billing cycle prorated.

In our business logic, we need immediate payment (charges or refunds) on each quantity update rather then on end of billing cycle. Is there any way in the stripe to achieve so.


Solution

  • I spent a lot of time trying to figure this one out, I hope I can save someone else some time. To take payment for a change of quantity or subscription plan, you need to do the following:

    1. Update subscription with the new changes.
    2. Create an invoice. The thing that confused me by this is that Stripe magically knows to only invoice for the updated subscription items, and not for upcoming billing cycle items.

    3. Retrieve the newly created invoice

    4. Finalise the invoice
    5. Take payment for the invoice This assumes that your customer has a payment method stored.

    Only when all of the steps have been completed, you've successfully charged for the subscription change (and charged for the change only). You can do it all of it in one go. The customer should get an invoice email from Stripe at the end of it.

    What makes matters complicated for me, is that reading from the documentation, I got the impression that Stripe will automatically do all of this for me as long as the subscription is set to "billing": "charge_automatically". This is not the case, and in Stripe's defence, they mention it at the bottom of the documentation page related to upgrading and downgrading plans:

    If you want the customer to immediately pay the price difference when switching to a more expensive plan on the same billing cycle, you need to generate an invoice after making the switch.