Add credits to each subscription

How to add monthly credits based on the subscription? Without affecting user credits in case they bought extra credits
6 Replies
kapa.ai
kapa.ai3mo ago
Hi @atomic-peter. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To add monthly credits based on the subscription without affecting user credits in case they bought extra credits, you can follow these steps: 1. Create a Stripe product for the subscription that includes monthly credits. When creating a Stripe product, you can choose a normal one-time payment for a product (instead of a subscription) and then when the payment goes through, you give that user +30 credits (or however many credits you want to give) source (https://discord.com/channels/686873244791210014/1205328655300038766). 2. In your Wasp code, you can handle the Stripe webhook event for successful payments. In the event handler, you can increment the user's credits. Here's an example of how you might do this:
if (line_items?.data[0]?.price?.id === process.env.CREDITS_PRICE_ID) {
console.log('Credits purchased: ');
await context.entities.User.updateMany({
where: {
stripeId: userStripeId,
},
data: {
credits: {
increment: 10, // Increment by however many credits the subscription includes
},
},
});
}
if (line_items?.data[0]?.price?.id === process.env.CREDITS_PRICE_ID) {
console.log('Credits purchased: ');
await context.entities.User.updateMany({
where: {
stripeId: userStripeId,
},
data: {
credits: {
increment: 10, // Increment by however many credits the subscription includes
},
},
});
}
source (https://discord.com/channels/686873244791210014/1205328655300038766) 3. Make sure to set up the Stripe CLI and ensure that you're receiving events to the webhook. You can check the "stripe testing" section in the open SaaS docs for more information on this source (https://discord.com/channels/686873244791210014/1205328655300038766). Remember, the credits increment should not affect the user's existing credits. The increment operation will add to the existing credits, not replace them. Please note that this is a high-level overview and you might need to adjust the code to fit your specific use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @atomic-peter, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
atomic-peter
atomic-peterOP3mo ago
what if i want to combine a subscription plan with limited credits per month, is that posible?
NEROX
NEROX3mo ago
@kapa.ai what if i want to combine a subscription plan with limited credits per month, is that posible?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, it's possible to combine a subscription plan with limited credits per month. You can do this by setting up a subscription product in Stripe that includes a certain number of credits per month. When setting up the Stripe webhook in your Wasp application, you can handle the event for successful subscription payments. In the event handler, you can increment the user's credits by the number of credits included in the subscription. Here's an example of how you might do this:
if (line_items?.data[0]?.price?.id === process.env.SUBSCRIPTION_PRICE_ID) {
console.log('Subscription purchased');
await context.entities.User.updateMany({
where: {
stripeId: userStripeId,
},
data: {
credits: {
increment: 10, // Increment by the number of credits included in the subscription
},
},
});
}
if (line_items?.data[0]?.price?.id === process.env.SUBSCRIPTION_PRICE_ID) {
console.log('Subscription purchased');
await context.entities.User.updateMany({
where: {
stripeId: userStripeId,
},
data: {
credits: {
increment: 10, // Increment by the number of credits included in the subscription
},
},
});
}
source (https://discord.com/channels/686873244791210014/1205328655300038766) This will add the subscription credits to the user's account each time a subscription payment is successful. Please note that this is a high-level overview and you might need to adjust the code to fit your specific use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
p4j4r5
p4j4r53mo ago
I m working on something similar Monthly subscription - Max 100 A items and total 100 items B Credit purchase - if user on free plan, Max A items changes to 50 ( with expiry of 2 months) and total 50 items B - If a monthly subscriber purchases credit purchase because he ran out of B, then Max A still remain at 100, while B increase it by 50. Seems this is possible , by tweaking plan effect and passing those to be updated by the method updateUserStripePaymentDetails in webhook.ts
atomic-peter
atomic-peterOP3mo ago
great! thanks man
Want results from more Discord servers?
Add your server