请看这个例子-> https://developer.paypal.com/demo/checkout/#/pattern/client
那里的 createOrder 函数返回这个 ->
{"id":"8GS56714S6789541X","intent":"CAPTURE","status":"CREATED","purchase_units":[{"reference_id":"default","amount":{"currency_code":"USD","value":"88.44"},"payee":{"email_address":"barco.03-facilitator@gmail.com","merchant_id":"YQZCHTGHUK5P8"}}],"create_time":"2021-02-04T02:47:04Z","links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/8GS56714S6789541X","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/checkoutnow?token=8GS56714S6789541X","rel":"approve","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/8GS56714S6789541X","rel":"update","method":"PATCH"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/8GS56714S6789541X/capture","rel":"capture","method":"POST"}]}
我想从中提取 id 字段,然后将其传递给 onApprove,我想在其中使用它传递给另一个函数。
这是我在上面的示例链接中尝试更改以实现我想要的内容,但我做错了,因为我什至再也看不到贝宝按钮了,哈哈
let orderID = 0;
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) { //this gives back the response I posted above
return actions.order.create({
purchase_units: [{
amount: {
value: '88.44'
}
}]
}).then((details) => {
orderID = details.id;
})
},
// Finalize the transaction
onApprove: function() {
console.log(orderID);
letsAuthorize(orderID); //this is some function that I wanna pass the orderID to
}
}).render('#paypal-button-container');
请注意,在示例中,他们也在捕获 onApprove 但我不想捕获。我只想从 createOrder 响应中获取 orderID,然后将其传递给 onApprove 中的函数。
(简而言之,我想获取 createorder 在其响应中发送的 orderid 并将其传递给 onApprove 函数,在该函数中我想用它来调用另一个函数,该函数将转到我的授权后端实现)