#jcube_code

1 messages ยท Page 1 of 1 (latest)

loud iceBOT
#

๐Ÿ‘‹ 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.

thorn agateBOT
#

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.

rare granite
#

hi! you have to use two API calls

#

one to create the Schedule with just from_subscription, and then you update it

bitter hill
#

Do i have to cancel the existing subscription ?

rare granite
#

no

bitter hill
rare granite
#

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"
]);
bitter hill
#

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

rare granite
#

it depends what you want.

bitter hill
#

When user downgrades he the current higher plan should be applicable until next month

rare granite
#

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.

bitter hill
#

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
rare granite
#

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

bitter hill
#

`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
rare granite
#

in this case it's pointless to pass it really

bitter hill
#

Then whats best approach for what i want to do?

rare granite
#

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

rare granite
bitter hill
#

I have to pass two phases?

rare granite
#

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

bitter hill
#

The current phase and then the phase i want to start from next month

rare granite
#

yes

bitter hill
rare granite
#

customer.subscription.updated

bitter hill
#

Okay got it thank you

#

If i don't mention iterations and it will run for forever until user cancels?

rare granite
#

if using end_behavior:"release", yes.

bitter hill
#

Alright one last question ๐Ÿ™‚. I have asked too many dumb questions lol

#

Can there be more than one schedules?

rare granite
#

across your account yes, but each subscription can only have one schedule

bitter hill
#

Or something like conflicting schedules?

bitter hill
#

Can I see schedules on dashboard?

rare granite
#

yes

bitter hill
#
            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

rare granite
#

can you explain/elaborate?

bitter hill
rare granite
#

and what did you expect to happen instead?

bitter hill
#

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

rare granite
#

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.

bitter hill
#

I'm doing proration_behaviour always

bitter hill
rare granite
#

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.

bitter hill
#

How do i get hosted invoice page?

bitter hill
#

I mean I want to know if an invoice for update failed so that I can prompt user and then listen for web hook

worn rain
#

Hi! I'm taking over from my colleague. Please, give me a moment to catch up.

bitter hill
#

Alright

worn rain
bitter hill
#

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

worn rain
#

You could listen to invoice.payment_failed and grab the hosted_invoice_url from the event payload.

bitter hill
#
            items: [
                {
                    id: id,
                    deleted: true
                }, {
                    price: 'price_1OqG9PSA2e71Dz91HaJFV0xb'
                }
            ],
            proration_behavior:  "always_invoice"
        })```
#

How to know if this update was successful

worn rain
#

You will receive the invoice.paid webhook event.

loud iceBOT
bitter hill
#

@wicked ingot Can you help me please.

#

If you can read the previous chat it would help

wicked ingot
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

#

would you mind summarizing please

bitter hill
#

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

wicked ingot
#

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

bitter hill
#

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

wicked ingot
#

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

bitter hill
#

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

bitter hill
wicked ingot
#

you shouldn't rely on that

bitter hill
#

Then what's the best approach for me?

wicked ingot
#

webhooks are more adequate

bitter hill
#

and if an invoice fails and i receive invoice.payment_failed then how do i retrieve the hosted URL for that invoice?

#

using id?

wicked ingot
#

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

bitter hill
#

A lazy question to ask but how would i know in the invoice.paid if the user has downgraded or upgraded

wicked ingot
#

if the billing_reason, subscription_update then you would retrieve the subscription and check what's the new price

bitter hill
#

Okay

#

Thanks

#

Will my customer receive an email when his payment is failed for upgrade from stripe?

wicked ingot
#

it depends on the settings on your dashboard

bitter hill