I am using PayPal payment in my React application, where I am trying to make a refund for users who choose to withdraw from the tournament. And I used to perform refund at my backend node js.
well, coming to the issue. I am not able to perform the refund when I have more than one refunds to make. The following is the code I have used to make refunds to multiple transactions. This is the SDK I use https://github.com/paypal/Checkout-NodeJS-SDK.
For async, I have referred this blog https://zellwk.com/blog/async-await-in-loops/
I have no errors in log.
app.post('/tournament/users/withdraw',VerifyRoute,(request, response)=>{
const {withDrawUsers,tournament_id,registration_fee,payer_info}=request.body;
....
....
to_withdraw.map(async(withDrawUsers)=>{
function client() {
let clientId = "xyz";
let clientSecret = "abc";
let environment = new checkoutNodeJssdk.core.SandboxEnvironment(clientId, clientSecret);
return new checkoutNodeJssdk.core.PayPalHttpClient(environment);
}
async function refundOrder(captureId,amount ,debug=false) {
try {
const request = new checkoutNodeJssdk.payments.CapturesRefundRequest(captureId);
request.requestBody({
"amount": {
"value": amount,
"currency_code": "USD"
}
});
const response = await client().execute(request);
}
catch (error) {
errorLogger.error("Error refunding the order "+error);
}
}
(async() => await refundOrder(withDrawUsers.transaction_id,(registration_fee*(withDrawUsers.noOfUsers)).toString(),false))();
})
});
});