How do I tell my app that when it is in production, and paypal triggers the 'return_url' it should look for '/success' on the server side rather than in the client?
I have set up an express server to handle PayPal payment requests. When a post request is served to 'localhost:5000/pay' on express I handle the logic and am redirected to PayPal where the user enters their details. They are then redirected to 'localhost:5000/success' where the payment is executed using the token returned by paypal. This works fine locally, but not on heroku. The issue is the return to the '/success' page.
As you can see from the code below, paypal asks me to reference a return_url (the url served for a successful transaction). In heroku the server is not running on 'locahost:5000' so it just throws an error. Alternatively serving the actual url followed by '/success' doesn't trigger the proxy I have set up in react and so it tries to find '/success' in my client side - which of course isn't there and triggers a 404.
The code to create payment:
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": `http://localhost:5000/success`,
"cancel_url": `http://localhost:5000/cancel`
},
"transactions": [{
"item_list": {
"items": content
},
"amount": {
"currency": "USD",
"total": productSelectTotal,
"details":{
"subtotal":productSubTotal,
"shipping":shipping
}
},
"description": "first tea added to the store"
}]
}
execution of payment once redirected to server/success
app.get('/success', (req, res)=> {
const payerId = req.query.PayerID
const paymentId = req.query.paymentId
const execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"item_list": {
"items": content
},
"amount": {
"currency":"USD",
"total": productSelectTotal,
"details":{
"subtotal":productSubTotal,
"shipping":shipping
}
}
}]
}
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
console.log(JSON.stringify(payment));
res.send('Payment Successful')
}
});
})