#m4rt1ns_1993

1 messages · Page 1 of 1 (latest)

hazy vaultBOT
placid walrus
#

Hi! Let me help you with this.

#

What doesn't work exactly?

wheat jay
#

So I have an application where users pay a subscription and they can generate one report (included on the subscription). If they want to generate an extra report, we need to invoice them. Basically what we do is update their subscription, push forward the subscription end dae, and charge the immediately. When we update the subscription, we sent a metadata field to the subscription which is extraAnnualReport : true.

#

Then two things can happen, or the payment is successful or declined. In both scenarios we need to delete the metadata of the subscription.

#

If the payment is declined, I'm handling it here and it works just fine:

case 'invoice.payment_failed': { //Payment failed, show payment modal const { extraAnnualReport } = data?.['subscription_details']?.['metadata'] try { // If the payment fails, we need to check if the payment is for extra annual report by checking subscription metadata // If it is, it means the users was trying to generate a new report, but the payment failed -> Don't block the access // If it fails for other reasons, block the access if (!extraAnnualReport) { await updateOrganisationValue(orgId, { subscriptionEndDate: '', paymentMethod: 'Stripe', hasSubscription: false, }) } } catch (error) { console.log('Error Handling Event: ', error.message) } finally { // Remove metadata from subscription if (extraAnnualReport) { await stripe.subscriptions.update(data.subscription, { metadata: '' }, stripeAccId) } } break }

#

In this case I even get a new request on Stripe, for the await stripe.subscriptions.update(data.subscription, { metadata: '' }, stripeAccId) call

#

If the payment is succesful I'm handling it here, annd it doesn't work...

case 'invoice.paid': { const { extraAnnualReport } = data?.['subscription_details']?.['metadata'] try { // Then define and call a function to handle the event payment_intent.succeeded const sub = await stripe.subscriptions.retrieve(data.subscription, stripeAccId) console.log({ sub }) const subscriptionEndDate = sub && format(fromUnixTime(sub['current_period_end']), 'yyyy-MM-dd') await updateOrganisationValue(orgId, { subscriptionEndDate: subscriptionEndDate, paymentMethod: 'Stripe', hasSubscription: true, stripePriceId: sub?.plan?.id, stripeProductId: sub?.plan?.product, }) } catch (err) { console.log('Error Handling Event: ', err.message) } finally { // Remove metadata from subscription if (extraAnnualReport) { await stripe.subscriptions.update(data.subscription, { metadata: '' }) } } break }

#

Why it works for one case and not for the other?

placid walrus
#

Are you talking about this line?

await stripe.subscriptions.update(data.subscription, { metadata: '' })
forest forge
#

didn't we help you with this earlier and determine that it works fine, just that you then do a subsequent API call that puts the metadata back?

wheat jay
#

yes the fnnally statement

#

But I' not doing a subsequent API call... I mean I simply update the subscription here
await updateStripeSubscription(customerId, { billing_cycle_anchor: 'now', proration_behavior: 'none', metadata: { extraAnnualReport: true } })

#

And If im doing subsequent API calls, why it works when the payment is declined but not when it's succesful?

forest forge
#

I mean I simply update the subscription here
and that call adds metadata to the subscription, right?

wheat jay
#

Yeah...

#

But then we need to remove it

forest forge
#

await stripe.subscriptions.update("sub_xxx", { metadata: '' }) will work 100% of the time

wheat jay
#

I'm not saying it doesn't work

#

I'm saying that I don't understand what am I doing wrong...

#

And why it works if the payment is declined and not when its succesful

forest forge
#

I can't really understand the question. You said "Why it works for one case and not for the other?" but then you said "I'm not saying it doesn't work"

can you share a crisp example of what "it" is, what it looks like when it works, what it looks like when it doesn't? with screenshots/requst IDs req_xxx, subscription IDs sub_xxx, etc?

#

also remember what I said earlier, calling await stripe.subscriptions.update will also trigger a customer.subscription.updated event, so it will also trigger any code you have listening to that

#

so just removing the metadata will trigger an event

#

and then maybe your code doesn't handle that well and get confused and makes another update call, and that call sets metadata again

wheat jay
#

In my case what I've said for a couple of times is that in my code I'm able to remove the metadta when the payment is declined, but not when the payment is succesful... By saying this, it means that it DOES WORK when the payment is declined but it DOESN'T WORK when the payment is successful

forest forge
#

then whatever is happening in your code that leads to a subsequent API call that updates the Subscription again to add metadata, only happens in the case when the payment is successful.

wheat jay
#

That I already know

forest forge
#

I see, so what's the question? it's hard for us to debug your code, you are best placed to trigger that code in test mode and adds logs/breakpoints and compare with your Stripe Developer Logs to undestand what is happening.

wheat jay
#

The question I already asked you

forest forge
#

and I think the answer I gave is there is something happening in your code that you to debug

#

if it helps, looking at your accounts logs I see two different Node servers

#

one that uses Stripe/v1 NodeBindings/13.5.0 of our library, and one that uses Stripe/v1 NodeBindings/8.222.0

#

what I think could be happening is you have another server running somewhere that you have forgotten about, and that server is listening to events and triggering API calls, and it's those API calls that are creating the requests that add the metadata back

wheat jay
#

Ok, Ill look into that