One way is to store the data in session.
Help building a Credit system behaviour
Hello the community,
I need to implement a credit system (meaning user pays real money to buy credit package, and then uses credit on the website for some actions). I already implemented the workflow with stripe gateway and it works pretty fine. I can buy credits and spend them when I submit form to buy the product.
My big struggle is to make things clean for the Balance check before validating the purchase. Here are my interrogations :
-
Having a Middleware on checkout page to check the credit balance is a good idea ? If yes, I would like to pass a parameter to the middleware to indicate on the page the minimum number of credits required for the user to continue. With this information I would like to display a message like "you don't have enought credits, you need to buy at least XXX credits" , and also pre-select the best matching package.
-
Considering the middleware or whatever other solution to block the submit if the balance is not enough, how to follow up the request to the all the redirections until balance is refilled ? I understand how a middleware works to follow the request and redirects to the "buy credits page" but I don't understand in my scenario how to make difference with "normal balance refilled".
To summarize how to make difference between these 2 scenarios : SCENARIO 1 : -> User wants to buy credits -> go to "Buy credits" page -> select credit package, make payment (stripe) -> success payment page
SCENARIO 2 -> User wants to buy product -> submit form -> balance is not enough, middleware intercept request and redirect to "Buy credits" page -> select credit package, make payment -> HERE HOW display success payment page for the refill with a continue button to return to the purchase page, using saved form data ?
IDEA : I was thinking of using Session variables to save the cart, but don't know what are the best practice for this kind of behaviour.
Thank you very much for the help.
ps : I know there are third parties plugin for this kind of behaviour but i don't want this (there is always a feature i dont want, or that doen't work the way i need, so have to make my own)
@kanak09 I wouldn’t automatically redirect to a “buy credits” page if the user does not have enough credits.
First, you should be indicating on the product itself if the user does not have enough credits for the product, and disabling any “check out” or “buy now” buttons.
Second, you should then be only creating checkout sessions if the user does have enough credits. You should then listen to webhooks for checkout sessions being completed, and deducting credits when a session is completed successfully.
In the event the user initiates multiple checkout sessions and their credits goes into a negative balance, you can simple refund the charge for the session that would have taken the user over.
Please or to participate in this conversation.