#meteograms-si
1 messages ยท Page 1 of 1 (latest)
@bleak palm it means we don't return it at the time that CheckoutSession is created
i.e. the change is we defer the creation of the SetupIntent until the customer actually uses the Checkout page(which improves latency on the API request creating the session and produces less dangling objects for Sessions that are never actioned).
That's my understanding at least. I might actually be wrong(what I said is definitely true for PaymentIntent, but I didn't look yet into that specific change for setup_intent on Checkout's API).
It's also possible that in your example you didn't test with the latest API version(the version you use to e.g. make a request does not influence what version the webhook event will use since that's defined by your endpoint settings). What's the evt_xxx you tested on?
Just tested again now... checkout.session.completed event with id evt_1Lc3mYHkIcPWRyQ1WGaQj54c
Ah "api_version":"2020-03-02"
yeah then it would be that, events are versioned according to the settings of the endpoint so they would still contain fields removed from newer versions if the endpoint is using an older version
Ah, OK, confusing... for me anyway!
Seems like we can't update the API version of the webhook... have to remove and re-add?
yep, it can only be set on creation
OK got the new webhook endpoint set up, and for "id":"evt_1Lc4S5HkIcPWRyQ1qybYHuOm" I see "type":"checkout.session.completed", "api_version":"2022-08-01", and indeed "setup_intent":null
Will now see how this impacts the processing of this event...
Hi! I'm taking over this thread. Let me know if you have any questions.
Thanks @frigid tusk. In my handling of the checkout.session.completed event, I am getting the payment_method via the setup_intent and using that to set the default_payment_method for the customer to this card. But setup_intent is now null in the latest API. Any idea how to get the payment method from the checkout.session.completed event now?
Can you share the event ID?
evt_1Lc4S5HkIcPWRyQ1qybYHuOm
Thanks! Give me a few minutes to look into this.
Note that when creating a subscription with Checkout, the default payment method will be automatically set on the subscription.
Yep... up till now I've also been setting this as default for the Customer
And if you want to retrieve the Payment Method, you could find it on the subscription object in default_payment_method.
And to get the subscription object from the checkout.session.completed event the best way is stripe.subscriptions.retrieve(event.data.object.subscription?
Yes, that should work!
When I try, I get "error processing checkout.session.completed event ReferenceError: Cannot access 'subscription' before initialization"
what's the full code and stack trace?
The stack trace doesn't really offer much help... and the code is quite straightforward... I think the error message is self-explanatory... just can't access subscription details when processing the checkout session completed event? Here goes anyway...
error processing checkout.session.completed event ReferenceError: Cannot access 'subscription' before initialization at StripeHelper.getPaymentMethodId (file:///usr/src/app/lib/StripeHelper.js:357:70) at StripeHelper.processCheckoutSessionCompletedEvent (file:///usr/src/app/lib/StripeHelper.js:545:48) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async StripeHelper.webhookHandler (file:///usr/src/app/lib/StripeHelper.js:709:17)
then flow is as follows (extracts)...
event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret)
type = event.type
object = event.data.object
if (type == 'checkout.session.completed') then ...
if (object.mode == 'subscription') then ...
subscription = object.subscription
subscription = await stripe.subscriptions.retrieve(subscription)
That's when the error occurs
sounds like you didn't initialise the library
like you didn't do const stripe = require("stripe")(STRIPE_KEY,{apiVersion:"2022-08-01"}) for example
you probably do that in other scripts but forgot to do it in the webhook endpoint script, perhaps
No, all other parts of the webhook processing is fine... it really is just an erro on this one call...
ok then maybe you need to do let subscription = object.subscription instead of just using a bare variable
again, sharing the full code would really help here
@bleak palm are you still facing this error?
Just about to pick this up again...
overall it feels like a JS error (like strange variable hoisting behaviour).
overall
const subscriptionId = object.subscription
let subscription = await stripe.subscriptions.retrieve(subscriptionId)
would work.
Or you could just do
let session = await stripe.checkout.sessions.retrieve(event.data.object.id, {expand:["subscription"]})
and work with that instead.
Of course, you're right. Like a dumbass ๐ I was using const subscription = object.subscription and then again initialising the same const with const subscription = await stripe.subscriptions.retrieve(subscription). Just using const subscriptionId for the former and it works.
๐