I'm currently designing voucher system to be implemented in our company e-commerce application.

I'm currently have voucher table in database which contains following information :

  • Unique voucher code
  • Voucher remaining count -> How many times this voucher can be used in purchase transaction
  • Other columns

Currently, customer input the voucher code in checkout page, before payment ( using credit card and internet banking ) happens.

I'm using hosted payment gateway, which means I redirect customer to payment gateway website. There customer input and complete the payment and get redirected back to our website

My question is about when should I decrement the voucher remaining count information?

I have consider two alternatives, as follows :

  • Customer input the voucher code and push validate voucher code button. There will be ajax call to validate the voucher code, get discounted transaction amount and then decrement the voucher remaining count. The weakness of this solution is that, when payment fails or stock suddenly runs out, the voucher is already used thus customer get angry.
  • Customer input the voucher code and push validate voucher code. there will be ajax call just to get the validate the code and discounted transaction amount because of voucher usage. We decrement the voucher remaining count after payment is successfully committed. The weakness of this solutions is that, if after payment, the voucher remaining count already become zero because of another usage from different customer, it means that the we have charged the wrong transaction amount and suffered loss.

Is there any other better alternatives that those two above or should I just choose one of above solution?

2 Answers 2


You can associate vouchers with orders as Muhammad suggested and use ajax to check availability. Count all vouchers for open and paid orders. Don't count vouchers for cancelled orders. This way someone might not be able to use a voucher, but later someone else might. If that first someone complains about that, make sure, you have one extra stock item that you can let go for the discounted rate and everybody is happy ;)


You don't need to add voucher remaining column. Instead, use maximum allowed vouchers field only and store all created vouchers in another table.

Customer can start creating new voucher if stock available (Maximum allowed vouchers value - Total counts in voucher created table) is greater than zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.