W
Wasp8mo ago
Arnab

Credit functionality

Hi all - Love this project. Thanks for making this amazing community. I'm trying to figure out how to use the credit functionality properly. For example, I'd like to -1 a credit for an uploaded image. Where is the credit logic and and how do I call the function to modify the credit amounts? (im a n00b coder)
10 Replies
kapa.ai
kapa.ai8mo ago
Hi @Arnab. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hello and welcome to the Wasp community! Based on the information provided in the knowledge sources, it seems like the credit functionality is handled through the interaction with the database using Prisma. In the Discord support thread: Payment for Credits / Credit Balance System > Answers Part 4 (https://discord.com/channels/686873244791210014/1205328655300038766), a community member shared a code snippet where they increment the user's credits by 10. Here's the code snippet:
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,
},
},
});
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,
},
},
});
In your case, if you want to decrement the credits by 1 when an image is uploaded, you would need to call a similar function but instead of incrementing by 10, you would decrement by 1. Here's how you could do it:
await context.entities.User.update({
where: {
id: context.user.id,
},
data: {
credits: {
decrement: 1,
},
},
});
await context.entities.User.update({
where: {
id: context.user.id,
},
data: {
credits: {
decrement: 1,
},
},
});
Please note that you would need to call this function in the appropriate place in your code, for example, after successfully uploading an image. If you're new to coding, I would recommend going through the Wasp documentation (https://wasp-lang.dev/docs) and the Prisma documentation (https://www.prisma.io/docs/) to get a better understanding of how to interact with the database using Prisma in Wasp. I hope this helps! Let me know if you have any other questions. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Arnab, 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?
Arnab
ArnabOP8mo ago
Does the Wasp project use "useAuth" or is it better use props for credit management?
miho
miho8mo ago
@Vinny (@Wasp) one for you when you have the time
Vinny (@Wasp)
Vinny (@Wasp)8mo ago
Hey Arnab. 1) follow the stripe integraton instructions and when you create a new product in the stripe dashboard, have it be a one-time purchase product instead of subscription --> 2) configure the amount of credits you want to give your user for that purchase in your stripe webhook. In the example we give you with open saas, that product gives the user 10 credits 3) like you guessed, use useAuth and check the user's credit amount on your server before allowing them to proceed. You will see examples of this, along with error detection and returing credits to the user in case of an error, here
Arnab
ArnabOP8mo ago
Very helpful. My concern was about using the prop method (as done in /account) to check credits or useAuth. Perhaps I'll use the prop method for displaying the available credits in the nav bar and useAuth for +/- or credits. Is this the right way to think of it? Also instead of creating a new api end point for verification I can use Wasp's abstraction to handle requests? Sorry in advance I'm still a n00b!
terrence
terrence8mo ago
I did some simple way, just when you call upload image, after it's successfully done, update your credit with decrement 1, it's very simple and straightforward
Arnab
ArnabOP8mo ago
Ok nice. Where is the logic for the decrement? actions.ts?
miho
miho8mo ago
https://github.com/search?q=repo%3Awasp-lang%2Fopen-saas%20decrement&type=code This way you can find any other logic you might be interested
GitHub
Build software better, together
GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 420 million projects.
From An unknown user
From An unknown user
From An unknown user
Arnab
ArnabOP8mo ago
Perfect! Now that makes sense. Thank you!
MEE6
MEE68mo ago
Wohooo @Arnab, you just became a Waspeteer level 2!
Want results from more Discord servers?
Add your server