PayPal智能支付按钮 — 如何创建订单服务器端并将订单 ID 传递给PayPal按钮设置?



我正在集成PayPal Checkout并按照他们的文档了解如何在此处设置客户端SDK。

我不想在客户端设置订单项目和总和,而是使用服务器端设置步骤4末尾提到的订单。使用他们的 PHP SDK,我可以毫无问题地设置订单,但我正在努力让事情发挥作用的地方是如何将这个创建的订单传递给客户端。创建的订单的返回对象具有启动捕获它的 ID 和 URL,但遵循链接实际上提供了与单击PayPal按钮不同的 UX,因此我仍然想使用该按钮,但与创建的订单一起使用。

使用他们的示例代码:

paypal.Buttons({
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
alert('Transaction completed by ' + details.payer.name.given_name);
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.

本质上,我有一个来自服务器端调用的订单 ID,不需要使用 "createOrder" 调用,但是如何将我的订单 ID 传递给paypal.Buttons()设置,以便当用户单击呈现的PayPal按钮时,他们被发送以批准我创建的订单?

PS:我知道您之后验证并捕获订单,但是为什么有人想在他们的客户端Javascript中设置订单费用?对我来说,这似乎是一个根本错误的想法。我在这里错过了什么吗?

解决方案相当明显,但不知何故,我在之前查看时并没有偶然发现它。createOrder需要返回订单 ID,可以按照文档完成:

method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.orderID; // Use the same key name for order ID on the client and server
});

最新更新