#Adi

1 messages ยท Page 1 of 1 (latest)

deft jacinthBOT
nova kettle
#

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

halcyon blade
#
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);
        } 
wraith violet
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

halcyon blade
#

Check these 2 cases:

  1. 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
  2. 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?

wraith violet
#

credits isn't something Stripe related

#

it's really in your logic

#

const totalCredits = currentCredits + credits;

#

this line ^ to be more specific

halcyon blade
#

but I cannot detect plan upgrade/downgrade in stripe

#

is there any specific webhook for that?

halcyon blade
#

credits I want to carryforward every month not reset them

wraith violet
#

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

halcyon blade
#

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?

wraith violet
#

no. invoice.paid is the event to use

halcyon blade
#

ok got it.