How to Handle Payment Race Conditions in My Marketplace?

0
5
Asked By MaverickQuest87 On

I'm building a reseller marketplace platform where I allow users to buy products, but I've run into a tricky issue with race conditions in my payment process. Here's how it works:

When a user clicks "Buy," I reserve the stock and create a pending transaction. Paystack then processes their payment and sends a webhook to confirm, at which point I release the reservation and decrease the actual stock. If a payment isn't received within an hour, a cron job marks the transaction as expired and releases the reservation. The problem comes when a user abandons checkout and returns later to complete their payment. If they do this two hours later, Paystack sends an old transaction reference to my server, which sees the transaction as expired. This creates an issue because my stock and reservation counts get messed up, leading to overselling.

I'm considering options like shortening the payment window or rejecting expired transactions, but I'm open to any smarter solutions you might have. What advice do you have to prevent these race conditions?

1 Answer

Answered By TechieTom123 On

Have you thought about validating the reservation when the user clicks 'Buy'? You could periodically check the reservation status from their browser, which would let you revoke the reservation if needed.

CuriousCoder56 -

That's a solid idea! Do you think opening a websocket to actively listen for those updates would be the best route?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.