#Adi
1 messages ยท Page 1 of 1 (latest)
Not sure I follow but can you elaborate which Doc you are following for "credits per plan"? There is only credit balance per Customer AFAIK
if (event.type === 'customer.subscription.updated') {
const eventData = event.data.object;
const { plan, customer } = eventData;
const { id } = plan;
let credits = 500;
if (id === STRIPE_BASIC_PLAN) {
credits = 2000;
}
else if (id === STRIPE_STANDARD_PLAN) {
credits = 10000;
}
const user = await usersCollection.findOne({ customer: customer });
const { credits: currentCredits } = user;
const totalCredits = currentCredits + credits;
logger.info(`[SUBSCRIPTION_UPDATED]: Previous credits: ${currentCredits} & new credits: ${credits} & total credits: ${totalCredits}`);
await usersCollection.updateOne({ customer: customer }, { $set: { "priceId": id, "credits": totalCredits } });
logger.info("customer.subscription.updated", eventData);
}
๐ taking over for my colleague. Let me catch up.
Check these 2 cases:
- User checkout for $50 plan which has 2k credits and then next day he upgrades plan to $100 then total credits for him will be 2k + 10k
- User checkout directly for $100 plan then he gets 10k credits
In first case user is getting more credits for same amount
is there any way to handle it?
credits isn't something Stripe related
it's really in your logic
const totalCredits = currentCredits + credits;
this line ^ to be more specific
but I cannot detect plan upgrade/downgrade in stripe
is there any specific webhook for that?
any suggestions if this logic can be fixed for same webhook event
credits I want to carryforward every month not reset them
actually there is a couple of ways
you can either listen to customer.subscription.updated or even better in invoice.paid event you can look into billing_reason
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
ok understood. Let me go through docs.
I had one follow up question: User paid $50 on 1st March then I update credits of user object which is in db. Now next month 1st April do I need to listen to invoice paid webhook event to update credits every month? Currently every $50 plan will give 2k credits
or is there some other webhook event which I should listen to?
no. invoice.paid is the event to use
ok got it.