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?