Payment for Credits / Credit Balance System
Hello, Im currently going over the documentation and will try to create some stuff using the Open SaaS.
Can anyone point me in the right direction to have payments turn into credits? The idea would be to have a credit system running where each user buys credits and then uses thoses credits to run apps.
Thank you, great job on this!!
23 Replies
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
Will try, I understand the mechanics, just not much of a developer myself.
here is a link to the lines in the code where we gave an example of Crediting a user with a product payment instead of a subscription: https://github.com/wasp-lang/open-saas/blob/main/app/src/server/webhooks/stripe.ts#L66-L82
GitHub
open-saas/app/src/server/webhooks/stripe.ts at main · wasp-lang/ope...
A free, open-source SaaS app starter for React & Node.js with superpowers. Production-ready. Community-driven. - wasp-lang/open-saas
Having issues with windows wsl but managed to boot everything in a vm, will try to work it this way.
DEV Community
Supercharge your Windows Development: The Ultimate Guide to WSL 🚀📟
Hi! I’m Boris! I’m a software engineer working professionally in insurance, teaching other...
yeah having an issue where it cant install a distro, tried every trick in the book
huh that's tricky. Let us know what's the exact error and maybe somebody will have an idea
any documentation explaining what exactly I need to configure on Stripe to achieve this? Credits is exactly what I need
Wohooo @until, you just became a Waspeteer level 1!
ended up creating a vm to circumvent this
Stripe themselves have great documentation.
Yeah I've created a few products just unsure on where I configure productID X to be the one that lets the users buy credits on wasp side
@Vinny (@Wasp)
I've created a CREDITS_PRICE_ID in the .env file.
I've then replaced subscripton_price_id places with credits_price_id and commented 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: {
// hasPaid: true,
// datePaid: new Date(),
// },
// });
// }
Im getting the following console error:
[Server!] Error: You must provide at least one recurring price in
subscription
mode when using prices.
I understand this but I wanted the app to run solely on non-subscription credits.
(I do not mind running a subscription model on the side, just dont want it to be mandatory). What do I have to change?
Regarding the following on main.wasp operations:
action stripeCreditsPayment {
fn: import { stripeCreditsPayment } from "@server/actions.js",
entities: [User]
}
Do I need to uncomment this?
Ok almost got it. Changed method to "payment". Credits not incrementing on user though
Tried a new install, didnt change a thing, users dont get any sort of credit incrementMake sure you’re running the stripe cli and that you’re receiving events to the webhook
Check “stripe testing” in the open SaaS docs
@Vinny (@Wasp) would code in coverlettergpt.xyz be a good example to check out?
aha I see now it is using monthly subscription
No. It’s set up the same way
Yeah I checked it and the events are showing up. I think I'm going to do a whole new install and go from there
Ok the payment does change the status of the user to a subscription based planned. Now I will be trying credits
I'll be enabling this:
// action stripeCreditsPayment {
// fn: import { stripeCreditsPayment } from "@server/actions.js",
// entities: [User]
// }
// action updateUser {
// fn: import { updateUser } from "@server/actions.js",
// entities: [User]
// }
Removing "Disabled:True" from this:
{
name: 'Credits',
id: 'credits',
href: '',
price: '$2.95',
description: 'Buy credits to use for your projects.',
features: ['10 credits', 'Use them any time', 'No expiration date'],
disabled: true,
},
and enabling this:
/**
* and here is an example of handling a different type of product
* make sure to configure it in the Stripe dashboard first!
*/
// 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,
// },
// },
// });
// }
Also added a one off payment id to the env file as CREDITS_PRICE_ID
Cant seem to make it work
Wohooo @until, you just became a Waspeteer level 2!
can you share the error youre getting?
actually not getting any error, it just ignores the one off payment of credits and always redirects to the subscription mode (which works fine)
no matter what button I click
Are you importing the correct action on the client?
e.g.
stripeCreditsPayment
?I managed to make it work by adding the following
await context.entities.User.update({
where: {
id: context.user.id,
},
data: {
checkoutSessionId: session?.id ?? null,
stripeId: customer.id ?? null,
// Added credits increment here
credits: {
increment: 10,
},
},
});
But I did this by basically replacing the subscription mode :/
Now I need to get both to work ^^
I'm very new at this.
ok interesting. I geuss there is no session.id nor customer.id being passed on that event
oh I just added the increment of credits
the rest was the same
changed the stripe method to payment and used a one off payment stripe product id
now I want to have both options in but one thing at a time I guess