skolind
skolind

Reputation: 1754

Avoid double requests to database

I've built an eCommerce solution on Nuxt.js (vue.js and node.js) with a headless cms called Cockpit. I am using a third-party payment gateway that, when payment is valid, redirects the user to a callback url.

On my callback url I have multiple actions going on: Create shipment, Add order to Cockpit and post order data to an invoice software. Also confirmation mails and shipping labels are generated. Labels and mails are sent twice too, and this is annoying for the customer + it costs money to generate the shipping labels. Everything is posting and working as intended.

My issue is, that the payment gateway sometimes call my callback url twice within milliseconds, and therefore creating a duplicate order in Cockpit, since first request haven't finished before the second one is started, and therefore my check for double orders fails, because in theory no order with that transaction id exists yet.

To check for double orders I do the following - this is done as soon as the user hits the callback url:

My question finally is - do you guys have any suggestions to how I can avoid and eliminate those double orders? Maybe some of you tried something like this before?

I am sorry for the long post, but I feel it's necessary for this to be a valid question on StackOverflow

Upvotes: 0

Views: 246

Answers (1)

user1092803
user1092803

Reputation: 3277

It seems that the problem is not on your side: the first question is "why the third-party payment gateway call your callback two times ? Does it make 2 payment ?" I think you should try to understand this before anything else, with the help of the payment gateway guys, since this is not normal.

In the meantime, or if you are not be able to solve on the other side, you can try to implement some sort of locking for the current order or a queue for the callback function so that if a second callback for the same order arrive, it can be blocked, discarded or stopped until the first one end (and, at this time, I suppose that the second call will find the order already processed so no more double print and email)

Upvotes: 1

Related Questions