I am working on PHP project which is having multiple payment gateway. We do have cron job in which automated payment of multiple users initiate from our server towards multiple payment gateways. It is tokenization payment so, we send token to payment gateway and after authenticate token for that user, payment gateway respond either success or failure. It is basic foreach loop in programming so one record process at the time and payment request done by PHP CURL.
Sometimes, we are facing random issue like, after send request from our server, if payment gateway does not respond during specific time period, server end up with 504 time out. So queue of entire payment process stopped, also there is no acknowledgement of ongoing payment process, either payment success of failure.
Due to this abandon process, when server initiate process next time, it again fetch same records and initiate payment process again. Due to that, it end up with multiple payment deducted from user's bank account.
As a part of solution I have been applying timeout in payment request in CURL. Is there any other steps I have to perform to avoid 504 timeout issue and multiple payment deduction from user's account?
I would appreciate for any recommendation, technical suggestion or online documentation.
Thanks in advance.