Immediately charge for subscription changes - stripe-payments

We are using Stripe payment processing for our customer's subscriptions. When users change plans, we want them to be charged immediately for the prorated difference. Stripe does this automatically when plans have different billing intervals but when changing between plans with the same interval, Stripe defers payment till the next billing period.
The way we handle this now is to create an invoice after updating the subscription, but if the billing fails for it fails, we need to do a rollback.
stripe.subscriptions.update(/* change plan */);
if (plans_have_different_billing_intervals) {
try {
stripe.invoices.create(/* for all pending charges */);
} catch (err) {
/* rollback subscription to old plan */
}
}
Overall, this feels wrong and convoluted. Is there an easier/cleaner way to implement this that we aren't seeing?

You can use billing_cycle_anchor set to now when updating a subscription to force it to change the billing cycle and thus always charge the customer immediately. Personally I think this makes a lot more sense. Also this works well with prorating.
await stripe.subscriptions.update(subscription.id, {
billing_cycle_anchor: 'now',
items: [{ id: itemId, plan: planId }]
});
https://stripe.com/docs/billing/subscriptions/billing-cycle#changing

To immediately charge your users the prorated price difference when the user upgrades, use the proration_behavior='always_invoice' option when modifying the user's subscription. Quoting from the docs:
... In order to always invoice immediately for prorations, pass always_invoice.
Python example:
stripe.Subscription.modify(
subscription_id,
items=[{
'id': item_id,
'plan': plan_id,
}],
# Charge the prorated amount immediately.
proration_behavior='always_invoice',
)
This immediately charges the user a prorated amount when the user changes to a more expensive plan. However, when the user changes to a cheaper plan, the user will be given a prorated credit, making the user's next bill cheaper by the prorated amount.
Scenarios
In these scenarios, suppose the billing date is on the first day of each month. Further suppose that the month has 30 days.
Scenario 1: The user is subscribed to a $5 monthly plan. The user decides to upgrade to a $30 plan on the 22nd day of the month. With 8 days left in the month, the unused portion of the $5 plan is approximately (8 / 30) * 5 = $1.33, and the prorated amount for the $30 plan is (8 / 30) * 30 = $8.00. So the user will be charged 8.00 - 1.33 = $6.67 immediately.
Scenario 2: The user is subscribed to a $30 monthly plan. The user decides to downgrade to a $5 plan on the 22nd day of the month. With 8 days left in the month, the unused portion of the $30 plan is approximately (8 / 30) * 30 = $8.00, and the prorated amount for the $5 plan is (8 / 30) * 5 = $1.33. In this case, the user will not be charged, but the user's next bill (on the next billing date) will be reduced by 8.00 - 1.33 = $6.67.
The example calculations above are only approximate because Stripe prorates to the exact second, not by day.
Side notes
dearsina's answer is correct, but involves at least three API calls rather than one. To explicitly follow the steps in that answer, you could do this instead:
# Step 1 in dearsina's answer: Modify the subscription.
modified_subscription = stripe.Subscription.modify(
subscription_id,
items=[{
'id': item_id,
'plan': plan_id,
}],
)
# Steps 2 and 3: Create and retrieve invoice.
invoice = stripe.Invoice.create(customer=modified_subscription.customer)
# Steps 4 and 5: Finalize and pay invoice.
stripe.Invoice.pay(invoice.id)
But why do this when you could do all the steps in one API call?

To take payment for a change of quantity or subscription plan, you need to do the following:
Update subscription with the new changes.
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.
Retrieve the newly created invoice
Finalise the invoice
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.
As OP says, it gets particularly complicated if the payment fails. The way I see it, you have three choices:
Ignore the failed payment, give the customer access to the service, and hope that they'll pay the next time payment is attempted.
Roll-back the subscription upgrade, warn the customer that payment failed.
Put the entire subscription on hold until the customer pays.
I'm only mentioning #3 because at times, the subscription upgrade process may be complicated. And the customer, having spent time upgrading their subscription, wouldn't want the work to be undone, for what could be a completely harmless reason (their credit card had expired for example).
It may be easier, from a development perspective, to mark a subscription as unpaid, than to roll back or to freeze (and then unfreeze) certain parts of the subscription. But you'll then run foul of barring the customer from a service that they have already paid for (their existing subscription) because they couldn't pay for the additional service.

You can use webhooks in Stripe. After creating an invoice, you can charge the invoice right away (invoice now). If the invoice payment fails, you can use a webhook to rollback to the old account.
Check this guide for more information about Stripe Subscriptions: https://www.truespotmedia.com/testing-webhooks-in-stripe-with-php/

Related

How to reset monthly SaaS usage on an annual Stripe plan?

I am currently in the process of creating the billing system for my first SaaS. A simple tool to generate videos. There will be a free, mid, and pro tier. The main difference will be the number of minutes you can generate per month.
I created 3 Stripe products for the tiers. The free product consists of one price ($0) with a monthly charge interval. The mid and pro tier consists of 2 prices, a monthly and annual charge interval.
When a user signs up for an account, my backend automatically creates a Stripe customer and subscribe it to a free plan. A user can upgrade their account to a mid or pro tier. The plan will be downgraded to a free tier when the user cancels or if the payment failed.
I reset the number of available render minutes after each successful payment, at the start of the billing month. I do this by listening to the successful payment Stripe webhooks. Even when the user is on the free tier, the webhook still gets fired since it is a plan.
The problem is that this method would not work for annual plans.
Which made me think that the method that I went for may not be the correct one.
Do you know if this is a good method if there is a better one and if there is a workaround for annual subscriptions?
Thank you very much for your time.
I think for the annual billing you'll likely want to just set up a cron job or similar that runs daily, and that looks at which annual subscriptions' 'billing day anniversary' (i.e., billing date is Aug 7, so billing day anniversary is the 7th of each month) and resets the counts - unless it is the billing date, in which case leave it to your webhook.

Stripe Proration - Pay Upfront?

Problem:
I had my first user upgrade to a higher priced subscription plan, under the proration settings, the user is set to be charged a higher amount next month ($84.95 instead of $67).
Question:
How can I charge the user the difference ($17.95) upfront, so they are charged this amount when they press upgrade, and then on the next billing cycle get charged just the normal amount ($67)
Is there an option to enable this in Stripe?
As ceejayoz mentioned, the solution here is to generate a one off invoice right after the upgrade.
Generating a one-off invoice pulls in any pending invoice items that would have been added to the regularly scheduled invoice.

Stripe - after changing plan, don't bill for the new subscription until current cycle ends

I want to let user to change their subscription plan. However, they won't be refunded after switching. If i set the prorate to false, stripe will bill for the new subscription right away.
For example, if the user is subscribed to a yearly plan in Jun 2018, and wants to change to monthly in Oct 2018. They will be charged with monthly plan after Jun2019, as current cycle hasn't ended yet.
How can I do with stripe api when i am updating the current subscription?
in the stripe api docs (https://stripe.com/docs/api/subscriptions/update)
"if you set prorate to false when switching between different billing intervals (monthly to yearly, for example), we won't generate any credits for the old subscription's unused time—although we will still reset the billing date and will bill immediately for the new subscription."
In 2019 Stripe shipped support for Subscription Schedules which allow you to control multiple sequential phases on a Subscription. You can read more about this in the docs: https://stripe.com/docs/billing/subscriptions/subscription-schedules
With this API, you can now explicitly schedule a future change of a Subscription. The idea would be to use the Create SubscriptionSchedule API a schedule from your Subscription and plan a second phase to move the Subscription to the new price on the next billing cycle.
Past answer, which still work but less optimized:
One solution for this would be to follow those steps:
Mark the current subscription to cancel at the end of the current period so that you don't attempt to charge that customer again.
Create a new subscription to the monthly plan but put the customer on a trial period until the end of the current subscription (June 2019 in your example). This allows you to not charge him until the other subscription ends so that you switch the customer to the monthly plan as expected.

Recurring billing with Stripe - will Subscriptions suffice?

My application allows a customer to purchase credits for later in-app use.
I want to enable customers to buy credits throughout the month, and only get billed at the end of the month.
Should I be using a Stripe Subscription at an amount that equals the price of one credit, and change the quantity according to the number of credits the customer purchased?
(After a successful invoice - I'll reset the subscription quantity to 0)
Is there a better solution? Perhaps some clever method of using Stripe Checkout?
Your proposed approach sounds reasonable.
I've not tried it but an alternative I can think of would use a free plan and the Invoice Items part of the API.
Create a free plan with amount with amount field set to 0. As far as I can see from the docs, all the subscription lifecycle webhooks should be triggered for Customers who're subscribed to the plan.
Through the month, create InvoiceItems for the Customer. According to the docs, at the end of the billing cycle, those InvoiceItems are added to the customer's invoice.
Sometimes you want to add a charge or credit to a customer but only actually charge the customer's card at the end of a regular billing cycle. This is useful for combining several charges to minimize per-transaction fees or having Stripe tabulate your usage-based billing totals.
Beyond that, you'll want to consider if you should have Stripe generate/email your invoices.
I don't think this is the way to go, because subscriptions are billed the first time among other things.
The recommanded way is: stripe doc: Place a hold on a card
summary
You first ask authorization for a certain amount (the max amount). Eg: 1000$
Your custommer buy 50 credits in the month
At the end of the month you charge the customer for 50$ (it can not be greater that the maximum you authorized in the first step)
That's all :)

Stripe Payments Charge

Is it possible to control the charge date in Stripe? For example, we need to charge:
One-time charge ($5) that needs to be charged on the purchase date.
One-time set up fee ($99) that is charged after 30 days from the purchase date.
Recurring charge ($79) that needs to be charged at the end of the term (It can be either Monthly or recurring)
Is that possible to have all these charges in the same subscription? If so how to do it with Stripe APIs?
When you set up a subscription with Stripe, you define price and interval they are charged. There is an option to have a trial period, so that you can have the first charge delayed - but beyond that there are not other options. Note that subscription plan details (price, interval, etc.) are, by design, not editable
In order to implement the business rules you have listed, you will need to implement that yourself. You can do so by:
create a subscription plan with X day trial (I'm not 100% sure if that's what you mean by 'to be charged at the end of the term').
creating the customer and saving a card for that customer.
charge the one-time charge on the purchase date.
start the subscription for the user on the purchase date (or 30 days later, depending on what is meant by 'end of the term').
then charge the 'set up fee' charge 30 days later.
There may be some services (such as Azure's Logic App Service) that could help you implement this business process but you won't be able to do it with just Stripe.

Resources