Stripe subscriptions: Modify when period ends instead of now - stripe-payments

Context: There is a Stripe plan "Starter". The customer subscribes to the "Starter" plan. Within a subscription period the user wants to up- or downgrade the subscription (in my case: the quantity will be increased or decreased). Programmatically I want to achieve this with a subscription update.
Problem: Stripe wants to make the update immediately. But I want to have it changed at the end of the period.
Alternatives:
I saw this Stackoverflow asking exact the same question, but is six years ago -> Stripe: downgrade a user at "period end". The proposed solutions are not really reflecting the desired solution and also do not work for me.
I saw that Stripe has a subscription schedule API. Is this probably related to the problem solution?
I'm relly looking forward for (high-level) solutions to solve this problem. Thank you in advance.

So you want to update the subscription details at the end of the current period. In that case, you may want to use the subscription webhook events mentioned here
"When the subscription period elapses, invoice.created event is sent, indicating the creation of a draft invoice."
"About an hour after the invoice is created, it is finalized (changes are no longer permitted), and an invoice.finalized event is sent."
So you can listen to that invoice.created event and then update the subscription.

This is what I discussed with the Stripe support channel:
I understand that scheduling is not the best fit for you, as you'd
have to release the schedule once the update is made so the new
subscription rolls over, and this doesn't cancel the subscription, but
start a new one.
What you could do, is to cancel the first subscription at_period_end,
and then start the next one when the event
customer.subscription.deleted comes through:
https://stripe.com/docs/api/events/types#event_types-customer.subscription.deleted
If you don't want to use Webhooks, you can also cancel at period end
and then generate the new subscription at the same time using the
billing cycle anchor
https://stripe.com/docs/api/subscriptions/create#create_subscription-billing_cycle_anchor.
This will only work though for subscriptions with the same interval,
or if the second subscription interval is shorter than the first.
I implemented the webhook solution. It works perfectly for me.

Related

Stripe Testing - Charge user right away even if subscription due date is next month

I have created a project that allows users to subscribe to our service monthly or yearly.
Now, I want to test our Stripe integration by attaching a working credit card, then changing that credit card into a "decline card" which I found here (4000000000000341).
Now, I wanted to test that flow. I can see that the user's upcoming invoice is still next month, but obviously, I cannot wait for next month since I want to test it right away.
I was Googling and I see that they mentioned that I should put a trial date, I tried that by modifying the user's subscription in the Stripe dashboard, but seems not to work. Maybe I'm missing something or did something wrong? The current flow of our subscription basis is we don't have a trial period, so I am not sure if putting a trial date is applicable to us.
Any help/tips is greatly appreciated! Thank you!
You should use test clocks.
You can simply advance the clock's time to one month later so that the upcoming invoice will be ready for you.
I believe the upcoming invoice won't be paid successfully as you are using a decline test card. You can read this doc to learn more about invoice payment handling.

Stripe API: Can we Update single upcoming invoice of a subscription in stripe?

So, I've created a subscription on stripe using:
stripe.subscriptions.create
Is there a way to update the single upcoming invoice ?
I want to update the payment_date i.e. due_at of that upcoming invoice.
For example:
if its May 10, I want to shift this to 01 May.
I tried looking in the docs but couldn't find anything related to this issue.
Any solution or suggestion is appreciated.
I'm guessing you are using collection_method: "send_invoice" when creating the subscription? If so, there are two places where you can set the due date for an invoice:
Set it when creating the subscription with days_until_due.
Or set it on the invoice directly. All invoices are created in draft status for one hour, so you have the time to edit them if needed. You could listen to the webhook event invoice.created, and then update the days_until_due or due_date of the invoice.

Stripe events for subscription and one time payment

I have a custom code to execute for subscriptions and another one for one-time payments.
I have already written both codes and they are working fine. Now I would like to move each code to the right section where it must be executed. So I would like to ask you
if the payment_intent.succeeded only fires when we have a one time payment and the invoice.payment_succeeded event only fires when we have a subscription ? If that's the case I will be able to place my codes when I capture those 2 events.

Stripe CLI: testing action when trial period ends

I'd like to be able to test locally that the correct actions are taken when the trial period for a user ends.
I set up the Stripe CLI, and start listening:
stripe listen --forward-to http://localhost:8000/api/stripe-webhook
When a trial comes to an end, the customer.subscription.updated event will occur.
I trigger this event using the Stripe CLI:
stripe trigger customer.subscription.updated
On my server side I can see that payload['data']['previous_attributes'] contains a metadata array with foo = null. When a trial ends, we should get previous_attributes containing something like: { "status": "trialing"...
I'm wondering if it's possible to trigger an event using the Stripe CLI that mimics a user trial ending (or maybe even just a way to set the previous_attributes to similar values that'd occur in a live environment when a trial ends)? If it's possible to do that then it'd be a little easier to test locally that the appropriate action occurs when a trial ends without pushing to a prod-like environment.
The easiest way is probably to just use trial_end to set a very short trial period either when creating a subscription, or on an existing subscription, and wait a couple of minutes for it to end, at which point it will generate the appropriate events.
stripe subscriptions update sub_HpOl9vDMzIEfGf --trial-end=`date -v +5M +%s`
https://stripe.com/docs/billing/testing#trials
https://stripe.com/docs/api/subscriptions/update#update_subscription-trial_end

Is customer.subscription.updated a good way to handle the status of a subscription?

I viewed this question, but am still not 100% sure about how to handle recurring subscriptions. I have the following question:
If I have a webhook for the customer.subscription.updated, is that
enough to keep track of a recurring subscription? For example, according to
the question above, when the subscription billing period changes,
customer.subscription.updated is called. Will it also be called if, for example,
a payment fails, and the status changes to past due?
Is it alright to use just the customer.subscription.updated webhook, or is it necessary to use the webhooks such as invoice.payment_succeeded for keeping track of the subscription? Thank you for any help.
You'll probably want to also keep track of invoice.payment_failed - and possibly invoice.payment_action_required - so you know when those happen; otherwise you should be good, ya. You can read more about it here:
https://stripe.com/docs/billing/webhooks

Resources