#benjamin-fazli_api

1 messages ยท Page 1 of 1 (latest)

last hingeBOT
#

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

๐Ÿ“ 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.

autumn mason
#

Hi there ๐Ÿ‘‹ can you tell me more about what you're trying to accomplish? It sounds like you're interested in finding the ID of a Checkout Session, which may or may not be in the body of the Event depending on what type of Event(s) you're listening to.

wide jewel
#

Hi

#

Yes, so on event: invoice.payment_failed I want to get the session id, so I can expire a session.

#

The real reason why im doing it is because I try to cancel the incomplete subscriptions, but you cannot do that without canceling the session firstly.

autumn mason
#

What mode Checkout Sessions are you creating here, subscription?

wide jewel
#

import { FREE_TRIAL_DAYS, PLAN_PERIODS } from '@/data/constants'
import { STRIPE_REDIRECT_URL } from '@/data/constants/Envs'

const SubscriptionBuilder = (props: SubscriptionBuilderFunctionProps): unknown => {
    const { 
        user,
        period,
        monthlyPlanId,
        yearlyPlanId,
        code
    } = props

    const planId = period === PLAN_PERIODS.MONTHLY ? monthlyPlanId : yearlyPlanId
    const usedTrial = user.Subscription?.UsedTrial

    const stripe_payload: StripePayloadType = {
        mode: 'subscription',
        payment_method_types: ['card'],
        success_url: `${STRIPE_REDIRECT_URL}/?success=true`,
        cancel_url: `${STRIPE_REDIRECT_URL}/?success=false`,
        customer: user.StripeCustomerId,
        allow_promotion_codes: true,
        subscription_data: {
            payment_behavior: 'allow_incomplete'
        },
        line_items: [ 
            { 
                price: planId, 
                quantity: 1
            } 
        ]
    }

    if (code) {
        const codeFormatted = code?.toLocaleLowerCase().trim()

        if (!stripe_payload.metadata) stripe_payload.metadata = {}
        stripe_payload.metadata.referrer = codeFormatted
    }

    if (!usedTrial) stripe_payload.subscription_data.trial_period_days = FREE_TRIAL_DAYS

    return stripe_payload
}

export default SubscriptionBuilder```
#

Yes, im doing it via subscription mode

autumn mason
#

Gotcha, then you should get the ID of the associated Subscription from the Invoice object contained in the Event, in its subscription field:
https://docs.stripe.com/api/invoices/object#invoice_object-subscription

Then you can use that ID as a filter when making a request to list Checkout Sessions, again via the subscription parameter, which should return just the related Checkout Session for you to work with:
https://docs.stripe.com/api/checkout/sessions/list#list_checkout_sessions-subscription

Let me know if that doesn't let you do what you're hoping though.

wide jewel
#

I don't want to do but I cannot cancel that incomplete subscription on the same checkout for some reasons, can you recommend something?

autumn mason
#

Sorry, I'm not sure I understand. You don't want to do what?

wide jewel
#

So I use stripe checkout to handle subscriptions

#

The problem is that incomplete subscriptions that expire within 24 horus are created if a payment fails. Wich in my case I don't want that, and there is not option to disable it on Stripe nor via configs.

#

So what I tried to do is, when a failed payment happens if it creates a incomplete subscription to delete it immeditly within that webhook call and delete the invoice also, but in this case doesn't allow me to delete the subscription while the user session is still active

#

So I do not know what to do

#

Is that clear?

autumn mason
#

So you're trying to cancel the Checkout Session instead? Did the advice I shared previously work for that?

wide jewel
#

Yes it did work

#

But its kinda bad, because I expire the session, wich is not the best practice

autumn mason
#

It's the only approach to do what you're describing. Do you have context you can share on why you want to immediately cancel everything, instead of letting the customer try again/try a different payment method?

wide jewel
#

We are using Stripe portal, then it prompts to pay the invoices for some reaons, for subscriptions that did went through

#

We simply do not want to create incomplete sessions, and only care when a payment goes through

#

Then the second problem is that, it tries to charge the user for all the subsriptions, wich if 3 times failed, 3 attemps for charges

#

This should be an optional feature, not a default enabled non disable option

autumn mason
#

I'm having trouble following that, as it sounds like it is mixing several subscription related features.

If you cancel a Checkout Session because a Subsription's first payment failed, you're going to invalidate the session that the customer may still be actively using.

wide jewel
#

But why I just can't cancel it?

#

Its not that if the payment goes through that subscription is going active, only if the user pays the specific bill

#

In this case everytime the user fails to pay it creates multple insitances of subscription

#

We can have a meeting if you want for this case

autumn mason
#

Its not that if the payment goes through that subscription is going active, only if the user pays the specific bill
Is this what you're seeing in your testing? Because it doesn't sound right.

The Checkout Session will create one Subscription. If the first payment fails, that subscription becomes incomplete while it waits to see if the payment for the first Invoice will be paid. A single Checkout Session shouldn't be creating multiple Subscriptions. I'd be interested in taking a closer look at those objects if that's what you're seeing.

wide jewel
#

I will do a video in some time and sent it to you