Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kanak09's avatar

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 :

  1. 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.

  2. 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)

0 likes
6 replies
kanak09's avatar

Hello, My question is not how to store data in session, but how to process all the logic and the best practice to make things clean

martinbean's avatar
Level 80

@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.

1 like
kanak09's avatar

@martinbean THANKS for the reply. I understand your point and it makes sense. To explain the context my app is a kind of e-learning (so not really physical product to buy) where a manager can enroll one or many students to one or many courses at a time.

In term of "user experience", don't you think it's annoying if you take time to set the enrollments (select courses and pick students) then the checkout button is disabled and a message says "you don't have enough credits, please refill". So you have to click , leave the page and come back by your own after successfull refill .

In another hand, doing your way is technically simpler.

martinbean's avatar

In term of "user experience", don't you think it's annoying if you take time to set the enrollments (select courses and pick students) then the checkout button is disabled and a message says "you don't have enough credits, please refill". So you have to click , leave the page and come back by your own after successfull refill .

@kanak09 Not really. I’d rather know up front that I don’t have enough credits so I can rectify that. I’d find it more frustrating clicking a “checkout” button, only to be redirected to a “buy credits” page, or some other page with an error message, where I now have to go through the buy credits flow any way instead of just doing it myself beforehand.

Please or to participate in this conversation.