【发布时间】:2020-05-05 09:39:10
【问题描述】:
我需要为我目前开发的预订服务集成支付功能。
我使用 React.js 作为前端,使用 NodeJS/Express 作为后端。
我已经在我的快递服务器 /payment 上设置了一个路由,在 /payment 路由中,我基本上采用了一个包含一些客户数据的数据对象,然后我通过 axios POST 请求将数据发送到 /apiURL,然后我得到一个响应并向 /apiURL/responseID 发送另一个 POST 请求,然后得到 API 公司建议我设置一个回调 URL(这应该是第一次调用中的数据参数),API 将向其发送回发客户完成付款后请求。
所以我的问题是,如何提取此回调响应数据?我可以以某种方式等待它并在同一条路线上提取吗?
由于我使用 React,我是否应该使用 React Router 制作响应回调 URL 的组件?
或者我是否必须制作一个服务器端变量,以便在回调到来时读取并更改?我第一次处理这样一个更大的项目,我真的不知道该怎么做。
我可以阅读有关此问题的一些伪代码或文章会很棒。
/干杯
这是有问题的路线
server.express.post("/payment", async (req, res) => {
// payment header
const paymentHeader = {...}
const body = {
"payment": {
...
"urls": {
"hostUrls": ["https://example.com"],
"paymentUrl": "http://example.com/perform-payment",
"completeUrl": "https://example.com/payment-completed",
"cancelUrl": "https://example.com/payment-canceled",
"callbackUrl": "https://example.com/payment-callback",
"logoUrl": "https://example.com/logo.png",
"termsOfServiceUrl": "https://example.com/terms.pdf"
},
...
}
}
axios
.post(
`${process.env.PAYMENT_URL}/psp/swish/payments`,
body,
paymentHeader
)
.then(response => {
console.log('2d post /payment')
axios.post(`${process.env.PAYMENT_URL}${response.data.payment.id}/sales`, {
"transaction": {
"msisdn": "+46739000001"
},
paymentHeader
}
).then(response => console.log(response.data)).catch(err => console.log(err.response.data))
})
.catch(err => console.log(err.response.data));
});
【问题讨论】:
-
为了更好的帮助,我鼓励您提供您的方法的基本代码示例。
-
这个问题的答案是什么?我正在努力解决类似的问题。
标签: reactjs api express routes react-router