[英]How to access Node Express server using proxy once deployed to Heroku?
我怎么告诉我的应用程序,当它在生产中,并且paypal触发'return_url'它应该在服务器端而不是在客户端中寻找'/ success'?
我已经设置了一个快递服务器来处理PayPal付款请求。 当一个帖子请求被提供给'localhost:5000 / pay'时,我处理逻辑并被重定向到PayPal,用户输入他们的详细信息。 然后将它们重定向到'localhost:5000 / success',使用paypal返回的令牌执行付款。 这在本地工作正常,但不是在heroku上。 问题是返回'/ success'页面。
从下面的代码中可以看出,paypal要求我引用return_url(为成功的事务服务的url)。 在heroku中,服务器没有在'locahost:5000'上运行,所以它只是抛出一个错误。 或者,服务实际网址后跟'/ success'不会触发我在响应中设置的代理,因此它会尝试在我的客户端找到'/ 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')
}
});
})
您可以使用以下方式动态获取您的URL: 如何在Express中获取完整的URL?
只需调整它就可以在return_url上连接'/ success'
我最终通过在节点服务器中添加routes / api / ...文件夹来解决问题。 当rect打电话时,它改为api / paypal /成功,并没有混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.