#Adi
1 messages · Page 1 of 1 (latest)
Hi! Let me help you with this.
You can just stop listening to the checkout.session.completed, and only listen to the invoice.paid event.
but I am passing meta data to checkout session i.e priceId and userId and based on that I update customer object in DB
if (event.type === 'checkout.session.completed') {
const eventData = event.data.object;
const { customer, subscription, metadata } = eventData;
const { userId, priceId } = metadata;
let credits = 500;
// $39 plan
if (priceId === STRIPE_BASIC_PLAN) {
credits = 2000
} // $129 plan
else if (priceId === STRIPE_STANDARD_PLAN) {
credits = 10000
}
await usersCollection.updateOne({ userId: userId }, { $set: { "priceId": priceId, "subscription": subscription, "customer": customer, "trial": false, "credits": credits } });
logger.info("checkout.session.completed event: ", eventData);
This code will work only when customer is making payment for first time... for 2nd /3rd month charge how can I add those credits? I know what logic to write but not sure about which webhook event I should listen
You can put metadata in subscription_data.metadata so it is always present on the Subscription object.
is there any other way?? Actually I have some customers who have made purchase last month but now on 28th March they will pay for 2nd month and I want to handle this case of adding credits for 2nd month successful payments
If I add logic in invoice paid event then lets say if I have new customer making purchase. In that case checkout.session and invoice paid both will get triggered and it will add extra credits to user
Are you getting my point?
I get your point. That's because implementation was not correct from the very beginning.
To fix this you will need to retrieve the metadata from the original Checkout Session and copy it to the Subscription object. You can do it every time, but it might be easier to run a one-time migration. You can find the Checkout Session by searching by a Subscription ID: https://stripe.com/docs/api/checkout/sessions/list#list_checkout_sessions-subscription
You can also listen to customer.subscription.updated event, but you will still need to search for the original Checkout Session to fetch the metadata.
Is there any reference or sample code available in nodejs Javascript which handles all these events and logic?
I can refer them
Is there any way to understand if invoice object https://stripe.com/docs/api/invoices/object
of customer is from 1st time payment or subsequent payment? event that information can help me
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Yes, you can change the language at the top
I'm checking but I don't see a straightforward way.
I found one option: when user pays for first time billing reason value is subscription_create but for 2nd or 3rd month it will be different right?
Yeah, that's one way.
Not sure what will be the reason in the subsequent invoices tho
Let me check
if billing_reason has value subscription_create then I don't run my logic on customer object. If it not then I can run logic on customer object based on priceId and customerId present in invoice object
Can you also let me know what are all possible values of billing reason field ?
I checked and the next billing_reason will be subscription_cycle
ok got it then it will work in my case. But what if someone upgrades plan or downgrades it?
That's why it's best to listen to customer.subscription.updated event: https://stripe.com/docs/api/events/types#event_types-customer.subscription.updated
You get the data.previous_attributes property so you can check what was updated: https://stripe.com/docs/api/events/object#event_object-data-previous_attributes
yes when plan upgrade and downgrade happens I make changes accordingly in customer.subscription.updated webhook event but now How can I distinguish between invoice paid event and subscription.updated event because during plan change both will be triggered and only relying on invoice.paid billing_reason field will not work