#tudor_ang

1 messages · Page 1 of 1 (latest)

sacred gyroBOT
obsidian ruin
#

👋 how may I help?

shrewd knoll
#

Hello i want to ask if how i use stripe is a good practice

obsidian ruin
#

sure

shrewd knoll
#

i create a saas app with MERN and on user registration (when a user create account) i also create a new customer in stripe for that user

#

and when he start his subscription i listen on stripe webhooks to change in my mongodb isPrmium flag to true from false

#

and if the webhook fails for that user i change to false the isPremium

#

is this a good way of doing it?

obsidian ruin
#

I don't see any major flaws

#

but what events are you listening to (just out of curiosity)

shrewd knoll
#

on success i have this


case 'invoice.payment_succeeded':
        if (dataObject['billing_reason'] == 'subscription_create') {
          // The subscription automatically activates after successful payment
          // Set the payment method used to pay the first invoice
          // as the default payment method for that subscription
          const subscription_id = dataObject['subscription']
          const payment_intent_id = dataObject['payment_intent']

          // Retrieve the payment intent used to pay the subscription
          const payment_intent = await stripe.paymentIntents.retrieve(payment_intent_id);

          try {
            const subscription = await stripe.subscriptions.update(
              subscription_id,
              {
                default_payment_method: payment_intent.payment_method,
              },
            );

            console.log("Default payment method set for subscription:" + payment_intent.payment_method);
            const dbUser = await User.findOne({ email: dataObject.customer_email })

            dbUser.isPremium = true;
            dbUser.subscriptionId = subscription.id;

            await dbUser.save();

          } catch (err) {
            console.log(err);
            console.log(`⚠️  Falied to update the default payment method for subscription: ${subscription_id}`);
          }
        };
#

on fail i still build my logic but i will do it here

  case 'invoice.payment_failed':
        // TODO: handle case where subscription renew fails 
        // If the payment fails or the customer does not have a valid payment method,
        //  an invoice.payment_failed event is sent, the subscription becomes past_due.
        // Use this webhook to notify your user that their payment has
        // failed and to retrieve new card details.
        break;

#

what do you think?

obsidian ruin
#

I would use invoice.paid instead of invoice.payment_succeeded

shrewd knoll
#

why?

obsidian ruin
#

there's a slight difference

shrewd knoll
#

whats the difference?

#

i read the docs but i am not sure i understand

obsidian ruin
#

both would fire on the occasion where the payment attempt has succeeded

#

but in the case if the invoice was marked as paid-out-of-band (meaning paid outside of Stripe) then invoice.payment_suceeded won't be fired

#

it's just a precaution matter to when/if this happens

shrewd knoll
#

oh i understand

#

ok i will change this

#

and i have one more question

#

when the subscription automaticaly renew

#

this webhooks will trigger automatically right?

#

1 month later

obsidian ruin
#

yes

#

but the billing_reason would be different

#

subscription_cycle if it's a renewal

#

          try {
            const subscription = await stripe.subscriptions.update(
              subscription_id,
              {
                default_payment_method: payment_intent.payment_method,
              },
            );```
I just saw that bit of code
#

and it's unnecessary

#

on_subscription

#

that would do this for you

shrewd knoll
#

and with my code will not work?

obsidian ruin
#

why do you need to introduce extra code if we have a way to achieve this natively with a param?

shrewd knoll
#

i am not sure what i have to replace

obsidian ruin
#

you just need to remove this code from here

#

and in the subscription creation call

#

you just need to add an extra param

#

the one I sent you

shrewd knoll
#

and how i can test this if is working?

obsidian ruin
#

what do you mean? you want to test whether the param is working?

shrewd knoll
#

if the subscription renew is working

#

so i dont have to wait 1 month for an account to renew

obsidian ruin
shrewd knoll
#

thanks

#

i will look over it

#

and if fails how i know to remove premium from a user?

#

in my db

obsidian ruin
#

do you mean if the renew doesn't really occur?

shrewd knoll
#

lets say if he dont have money on card

#

and he cant pay automaticaly

#

how i know from what user to remove premium

#

and on what webhook

obsidian ruin
shrewd knoll
#

yeah but i want to change in my mongodb

obsidian ruin
#

you can use customer.subscription.deleted

shrewd knoll
#

i can search the user by customerid right?

obsidian ruin
#

yes

shrewd knoll
#

and on what webhook i can do this?

obsidian ruin
#

on most if not all of subscription event objects you have the customer property

shrewd knoll
#

awesome

#

i will start to make this

#

i will be back if i fail

#

thanks for the help