#jcube_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1216692979788681347
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- jcube_api, 2 hours ago, 27 messages
hi! you have to use two API calls
one to create the Schedule with just from_subscription, and then you update it
Do i have to cancel the existing subscription ?
no
Okay so first i create a subscription schedule from and then use the returned object?
yes
this is PHP but hopefully it explains the idea.
// schedule a change from gold to silver at the end of the period
$sched = \Stripe\SubscriptionSchedule::create([
'from_subscription' => $subscription->id,
]);
\Stripe\SubscriptionSchedule::update($sched->id, [
'phases' => [
[
"start_date" => $subscription->current_period_start,
"end_date" => $subscription->current_period_end,
'items' => [
[
'price' => $gold->id,
'quantity' => 1,
],
],
],
[
"start_date" => $subscription->current_period_end,
'items' => [
[
'price' => $silver->id,
'quantity' => 1,
],
],
],
],
"end_behavior" => "release"
]);
Thank you!!
I had one more question
if(price_id == "price_1OsJmuSA2e71Dz91jqdMYH0V"){
await stripe.subscriptions.update(sub_id, {
items: [
{
id: id,
deleted: true
}, {
price: 'price_1OqG9PSA2e71Dz91HaJFV0xb'
}
],
proration_behavior: "always_invoice"
}) ````
I'm using update method to upgrade
Is it a good practice or shall I make schedule for this?
I want it to be applied immediately
When user upgrades it should reflect and charged immediately
it depends what you want.
When user downgrades he the current higher plan should be applicable until next month
if you want the change to only take place at the end of the current billing period, you need to use a Schedule. If you're ok with it changing immediately, then you can use subscription.update yes.
and from next month only charge for lower plan
`const subscriptionSchedule = await stripe.subscriptionSchedules.create
({
from_subscription : sub_id,
})
await stripe.subscriptionSchedules.update(subscriptionSchedule.id,
{
start_date: period_end,
end_behavior: 'release',
phases : [
{
items: [
{
price: 'price_1OsJmuSA2e71Dz91jqdMYH0V',
quantity: 1,
},
],
iterations:12,
},
],
})` Can you check this , I'm using period end of original subscription as a start date
I don't think that works but you can try it
the approach I posted above does work though. You need to a) create the schedule b) update it with c) one phase that represents the current state and d) another phase that represents the next billing cycle that is using the new Price
`phases : [
{
items: [
{
price: 'price_1OsJmuSA2e71Dz91jqdMYH0V',
quantity: 1,
},
],
iterations:12,
start_date : period_end
},
],` What does iterations 12 mean? Does it mean user will be charged for next 12 months
in this case it's pointless to pass it really
Then whats best approach for what i want to do?
it just means the phase repeats for the next 12 months, but because you use end_behavior: release" it doesn't really matter since it's already doing that
I explained it above and gave an example
please review this ^^
I have to pass two phases?
yes
as I explained earlier:
ou need to a) create the schedule b) update it with c) one phase that represents the current state and d) another phase that represents the next billing cycle that is using the new Price
The current phase and then the phase i want to start from next month
yes
What web hook will i receive If use this method on next billing method when subscription is updated?
customer.subscription.updated
Okay got it thank you
If i don't mention iterations and it will run for forever until user cancels?
if using end_behavior:"release", yes.
Alright one last question ๐. I have asked too many dumb questions lol
Can there be more than one schedules?
across your account yes, but each subscription can only have one schedule
Or something like conflicting schedules?
Okay that answers my question thanks!!!
Can I see schedules on dashboard?
yes
items: [
{
id: id,
deleted: true
}, {
price: 'price_1OqG9PSA2e71Dz91HaJFV0xb'
}
],
proration_behavior: "always_invoice"
})````
When I'm using this it says payment past due
How to charge the difference from user for upgrade
can you explain/elaborate?
When user upgrades I'm using this to update his current subscription. But on dashboard I saw it says payment has failed for update. payment was initiated which requires an additional user action
and what did you expect to happen instead?
Actually I want to charge customer for difference amount for upgrade?
I'm doing payment gateways for first time so please spare me if i ask dumb questions
sounds like you're doing that(you used proration_behavior: "always_invoice" which charges for the prorated changes in the pricing plans) and then that was indeed charged, and the charge failed, which can happen.
Yes I guess so
I'm doing proration_behaviour always
How do I confirm if payment indeed charged?
the page you're looking at in the screenshot already explains that.
it explains that a payment was attempted and that payment failed due to an action(3D Secure/OTP etc) being required. The next step is to have the customer visit the Hosted Invoice Page for the Invoice that has just been create,d and complete the payment.
How do i get hosted invoice page?
And what web-hook will i receive for this invoice
I mean I want to know if an invoice for update failed so that I can prompt user and then listen for web hook
Hi! I'm taking over from my colleague. Please, give me a moment to catch up.
Alright
Each Invoice has hosted_invoice_url: https://docs.stripe.com/api/invoices/object#invoice_object-hosted_invoice_url
invoice.paid
I'm updating an subscription using subscriptions.update now how do i get the hosted url for that?
You can read the previous chat to get context.
Basically I want to upgrade user subscription on event immediately but if the payment is not paid he should be prompted
And if he downgrades to cheaper plan there should be a subscription schedule to change price id from next month onwards
You could listen to invoice.payment_failed and grab the hosted_invoice_url from the event payload.
items: [
{
id: id,
deleted: true
}, {
price: 'price_1OqG9PSA2e71Dz91HaJFV0xb'
}
],
proration_behavior: "always_invoice"
})```
How to know if this update was successful
You will receive the invoice.paid webhook event.
@wicked ingot Can you help me please.
If you can read the previous chat it would help
๐ taking over for my colleague. Let me catch up.
would you mind summarizing please
Actually I want to implement a subscription such that there are two plans cheaper plan and expensive plan, when a user upgrades from cheaper plan to expensive plan he should be charged immediately and when his payment is successful I want to update it in my database. Now In case if user is subscribed to expensive plan and he downgrades the cheaper plan should be active from next billing cycle and when next billing cycle arrives i want to update in my db that user has now downgraded
I used subscription.update method for going from cheaper plan to expensive plan
I suggest using invoice.paid and checking the billing_reason
so basically when you go from cheaper to expensive it's easy, it will generate an invoice automatically and you can choose whether you want to change the billing_cycle_anchor or keep it as is
for the other scenario
you can use subscription schedules
and set the next phase to be for 1 iteration, and set the end_behavior to release
Now there's a problem with update if an invoice fails, if invoice fails how do i send user the hosted invoice for a difference amount
in all cases, on the webhook end, you would use invoice.paid and based on the billing_reason you know whether there was a subscription_update or not
you can listen to invoice.payment_failed
But I'm not using sockets on my web app so I want to know when i update the subscription using subscription.update method if it was failed then i would immediately return the invoice url prompting the user that payment failed
getting me or shall i rephrase?
you shouldn't rely on that
Then what's the best approach for me?
webhooks are more adequate
and if an invoice fails and i receive invoice.payment_failed then how do i retrieve the hosted URL for that invoice?
using id?
it would be on the object that you receive
but it's always best to retrieve the invoice with the ID and check the status, sometimes things change asynchronously
so it's always good to retrieve the latest version of the object
A lazy question to ask but how would i know in the invoice.paid if the user has downgraded or upgraded
if the billing_reason, subscription_update then you would retrieve the subscription and check what's the new price
Okay
Thanks
Will my customer receive an email when his payment is failed for upgrade from stripe?
it depends on the settings on your dashboard
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Let me go through it. Can you keep this thread open