【发布时间】:2019-05-23 11:45:01
【问题描述】:
我如何告诉我的应用当它在生产中并且贝宝触发“return_url”时它应该在服务器端而不是在客户端寻找“/success”?
我已经设置了一个快递服务器来处理 PayPal 付款请求。当 post 请求通过快递发送到“localhost:5000/pay”时,我处理逻辑并被重定向到用户输入详细信息的 PayPal。然后将它们重定向到“localhost:5000/success”,使用 paypal 返回的令牌执行付款。这在本地工作正常,但在 heroku 上不行。问题是返回到“/success”页面。
从下面的代码可以看出,paypal 要求我引用一个 return_url(为成功交易提供的 url)。在 heroku 中,服务器没有在 'locahost:5000' 上运行,所以它只是抛出一个错误。或者,提供后跟“/success”的实际 url 不会触发我在 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')
}
});
})
【问题讨论】:
标签: node.js reactjs express heroku