我如何告诉我的应用程序何时在生产中,并且Paypal触发“ return_url”,它应该在服务器端而不是客户端中查找“ / success”?
我已经设置了快递服务器来处理PayPal付款请求。当快递请求送达至'localhost:5000 / pay'时,我会处理逻辑并重定向到PayPal,用户在其中输入其详细信息。然后将它们重定向到“ localhost:5000 / success”,在此使用Paypal返回的令牌执行付款。这在本地工作正常,但在heroku上则不行。问题是返回到“ /成功”页面。
从下面的代码中可以看到,贝宝(Paypal)要求我引用return_url(用于成功交易的URL)。在heroku中,服务器未在'locahost:5000'上运行,因此它仅引发错误。另外,在实际的网址后加'/ success'不会触发我已在react中设置的代理,因此它会尝试在客户端找到'/ success'-当然不存在并触发404。
创建付款的代码:
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"
}]
}
付款执行一旦重定向到服务器/成功
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')
}
});
})
答案 0 :(得分:0)
您可以使用以下网址动态获得速递网址:How to get the full url in Express?
只需对其进行修改以在return_url上连接'/ success'
答案 1 :(得分:0)
最终,我通过在节点服务器中添加路线/ api / ...文件夹解决了该问题。当rect拨打电话时,它改为使用api / paypal / success,因此没有任何混乱。