#tudor_ang
1 messages · Page 1 of 1 (latest)
👋 how may I help?
Hello i want to ask if how i use stripe is a good practice
sure
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?
I don't see any major flaws
but what events are you listening to (just out of curiosity)
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?
I would use invoice.paid instead of invoice.payment_succeeded
why?
there's a slight difference
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
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
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
you can use instead this param https://stripe.com/docs/api/subscriptions/create#create_subscription-payment_settings-save_default_payment_method
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
on_subscription
that would do this for you
and with my code will not work?
why do you need to introduce extra code if we have a way to achieve this natively with a param?
i am not sure what i have to replace
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
and how i can test this if is working?
what do you mean? you want to test whether the param is working?
if the subscription renew is working
so i dont have to wait 1 month for an account to renew
thanks
i will look over it
and if fails how i know to remove premium from a user?
in my db
do you mean if the renew doesn't really occur?
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
yeah but i want to change in my mongodb
you can use customer.subscription.deleted
i can search the user by customerid right?
yes
and on what webhook i can do this?
on most if not all of subscription event objects you have the customer property
then you can use https://stripe.com/docs/api/customers/retrieve to retrieve the customer info
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.