问题
Please see this example -> https://developer.paypal.com/demo/checkout/#/pattern/client
The createOrder func there returns this ->
{"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"}]}
I want to extract the id field from it and then pass it on to onApprove, in which I want to use it to pass to another function.
This is what I tried changing in the example link above to achieve what I want but I'm doing it very wrong because I can't even see the paypal button anymore lol
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');
Please note that in the example, they are also capturing onApprove but I don't want to capture. I only want to get the orderID from the createOrder response and then pass it into the function in onApprove.
(in a nutshell, I wanna get the orderid that createorder sends in it's response and pass it to onApprove function where I wanna use it to call another function that goes to my backend implementation of authorize)
回答1:
Please see this example -> https://developer.paypal.com/demo/checkout/#/pattern/client
As you're using node.js and typescript, shouldn't you be using the server API samples at: https://developer.paypal.com/docs/business/checkout/server-side-api-calls/#server-side-api-calls
(make one server route for 'Create Order', and one for 'Capture Order', both of which should return JSON)
Paired with the approval flow example at, rather: https://developer.paypal.com/demo/checkout/#/pattern/server
Anyway, to really answer your question, onApprove should be:
onApprove: function(data, actions) {
It will be called with the orderID in data
.
来源:https://stackoverflow.com/questions/66039009/how-do-i-extract-orderid-from-this-object-response-pass-it-to-another-function