#ashish_subscription-renewal

1 messages ยท Page 1 of 1 (latest)

violet laurelBOT
#

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

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

blissful hound
#

Hey @digital mountain ! What does "payment is getting failed" mean exactly? You shared a request id which is great but it's just a manual click on a TestClock in the Dashboard. So having a more specific question with clearer details would help

digital mountain
#

There is no log of that payment but I managed to get event id evt_1Pkpr7A9qVxHukS4QzwIsuWt

blissful hound
#

yeah sorry I am asking for a lot of details and you shared one id with no info. Sorry there are dozens of ways to use Stripe and I need you to provide a bit of a summary of waht the issue is here

digital mountain
#

I wanted to create a update subscription endpoint. I want any changes in the subscription to happen only after the current billing cycle. So I did the following created a schedule with start date as end date of current plan and cancelled the current plan by setting end at current billing cycle

#

That's working fine, the existing plan gets cancelled and then the new plan starts but the payment gets failed

#

This is latest event id which says event invoice payment failed
evt_1PkqIaA9qVxHukS4gkyo8cJx

#

Thanks for your time and help

blissful hound
#

ashish_subscription-renewal

digital mountain
#

I checked await stripe.paymentMethods.list({
customer: customerId,});
In the response I can see the card

blissful hound
#

okay so that worked?

digital mountain
#

No, it didn't. There is card associated to that customer yet it's failing

blissful hound
#

Okay so you didn't read the doc I just shared yet. Make sure to carefully go through it first

digital mountain
#

I read the doc but couldn't understand where I'm going wrong. Sorry I pretty new to this, please bear with me

#

Here is the code I used, if that could give you more idea
paymentRoutes.post('/update-subscription-plans', async (c) => {
try {
const { customerId, subscriptionId, newPriceId } = await c.req.json();

    // Fetch the current subscription
    const subscription = await stripe.subscriptions.retrieve(subscriptionId);
    const isInTrial = subscription.trial_end && subscription.trial_end > Math.floor(Date.now() / 1000);
    const currentPeriodEnd = isInTrial ? subscription.trial_end : subscription.current_period_end;

    const schedules = await stripe.subscriptionSchedules.list({
        customer: customerId,
      });

      const existingSchedule = schedules.data.find(schedule => schedule.subscription === subscriptionId);

      const newPhase = {
        items: [{ price: newPriceId }],
      };
      let result;
      if (existingSchedule) {
        result = await stripe.subscriptionSchedules.update(existingSchedule.id, {
            phases: [
                ...existingSchedule.phases.map(phase => ({
                  items: phase.items.map(item => ({ price: item.price })),
                  start_date: phase.start_date,
                  end_date: phase.end_date,
                })),
                newPhase,
              ],
        });
      } else {
        // Create a new schedule if no existing schedule for the subscription
        result = await stripe.subscriptionSchedules.create({
          customer: customerId,
          start_date: currentPeriodEnd!-1,
          end_behavior:'release',
          phases: [
            {
                items:[{price:newPriceId}],
                proration_behavior:'none'
            },
          ],
        });
        await stripe.subscriptions.update(subscriptionId, {
            cancel_at_period_end: true,
          })
blissful hound
#

You have a Customer cus_123. That Customer has a saved card called a PaymentMethod pm_ABC
That PaymentMethod is not the default. You have to be explicit, as the developer, so that when you try to charge that card you tell us "please charge pm_ABC on Customer cus_123"

#

My guess is you created a brand new SubscriptionSchedule and forgot to tell us about which card to use

digital mountain
violet laurelBOT
digital mountain
#

Hey @blissful hound, thanks for pointing out. But there is only one issue for the first time schedule it is creating a new subscription id but for subsequent changes it is using the new subscription id only

#

Anyway to change this behavior to use only one subscription id through out?

undone seal
#

Hi, I'm stepping in and catching up.

digital mountain
undone seal
#

Can you share the specific request ids and the new subscription id that is being created? It seems like you're just attempting to update an exsiting subscription but you're saying that action is creating a new subscription?

digital mountain
#

I'm not sure of the request ids. But here is the customer id: cus_Qc1aIZuM1UzHxD. Which could give you more idea of what happened.
old subscription id: sub_1PknX5A9qVxHukS4q2wwtpIy
new subscription id for all subsequent changes: sub_1PkqtZA9qVxHukS42qhu9hKG

#

I even shared the code I was using above, if that helps in anyway

undone seal
#

Looking

undone seal
#

Ok, it seems like you're creating a subscription, then setting the cancel end period to true: https://dashboard.stripe.com/test/logs/req_NWXrm3nofS6wKH. Then, you're updating the schedules? I'm trying to reproduce this on my end but having a very hard time understanding the steps you took. Can you lay out the exact steps you're taking? Like steps 1, create a subscription.. Step 2, update the subscription etc with much more details?

digital mountain
undone seal
#

no, not the code but explicitly stating what you're doing so I can reproduce this on my end.

digital mountain
#

Oh ok

#

First I'm checking whether there is any schedule associated with given customer id and subscription id.
If it exists then I'm calling schedulesUpdate. If it's not there then I'm calling createSchedule with customerId which is creating a new subscription

undone seal
#

The issue is that I need to replicate this on my end to understand what is going on, and it is hard to follow what is happening with the amount of update requests you've made on the above example.

digital mountain
#

๐Ÿ˜… . I'll try to narrow down

undone seal
#

Thank you!

digital mountain
#

One question

#

When we do stripe.subscriptions.retrieve() does it give the past scheduled plans too or the one which gonna executed?

undone seal
digital mountain
#

I need some more time to narrow down. I'll be back asap at max by tmrw