我正在尝试将 paypal 添加到我的网站。我正在使用他们的 REST API,我想做的是让用户确定他们要存入的金额。我可以在创建付款时获得总计,但我不确定如何在没有复杂的对象数组系统的情况下将其传递到执行部分。有任何想法吗?
以下是我如何创建订单和执行订单的示例:
router.post('/pay', (req, res) => {
console.log(req.body.amount_to_deposit)
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": `${domain}success`,
"cancel_url": `${domain}cancel`
},
"transactions": [{
"custom": req.user.steamid,
"item_list": {
"items": [{
"name": "Deposit",
"sku": "001",
"price": "25",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "25"
},
"description": `Deposit UserID:${req.user.steamid}`
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
});
router.get('/success', (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
console.log(req.query)
const execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": "USD",
"total": "25"
}
}]
};
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
if (payment.transactions[0].custom) {
console.log(payment.transactions[0].custom)
}
res.send('Success');
}
});
});
我可以使用 body 参数获取用户想要支付的金额,但是如何将其发送到成功路径,从而执行支付 json 对象?
忽然笑
相关分类