gpt4 book ai didi

asp.net-core - 在 .net 中使用 Paypal REST api

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:45 25 4
gpt4 key购买 nike

我正在尝试使用我的 .net 应用程序实现 PayPal REST API。为此,我正在使用沙盒帐户。通过引用下面的演示/文档编写的代码将首先创建订单,然后为同一订单付款。

但是,我的问题是我无法获取订单 ID。虽然我从下面的代码进入 res.json() 。我需要获取订单 ID,将其设置为某个变量并在后续请求中使用它。下面的代码是我从演示链接中获得的,并根据我的要求进行了一些更改。

同样在 OnApprove block 中,我没有获得 data.id

 <div id="paypal-button-container">    </div>

<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({

// Set up the transaction
createOrder: function (data, actions) {
return fetch('https://api.sandbox.paypal.com/v2/checkout/orders', {
method: 'post',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer <My Access Token>'
},
body: JSON.stringify({
"intent": "CAPTURE",
"purchase_units": [
{
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
]
})
}).then(function (res) {
return res.json();
}).then(function (data) {
return data.id;
});
},

// Finalize the transaction
onApprove: function (data, actions) {

return fetch('https://api.sandbox.paypal.com/v2/checkout/orders/' + data.id + '/capture/', {
method: 'post',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer <My Access Token>'
},
}).then(function (res) {
return res.json();
}).then(function (details) {
console.log(details);
// Show a success message to the buyer
alert('Transaction completed');
});
}

}).render('#paypal-button-container');

</script>

另外,我可以从 PayPal 按钮执行我自己的 API 吗?

在此感谢任何帮助!

最佳答案

您的代码看起来很完美(几乎)。您只需要记住此处变量的范围即可。由于“data”变量仅限于“then” block ,因此您需要创建一个新变量来保存“data.id”的值并在 onApprove block 中使用它。我在下面的代码中添加了一个名为“orderID”的新变量,这似乎有效。

<script>
var orderID; //Declared a variable
// Render the PayPal button into #paypal-button-container
paypal.Buttons({

// Set up the transaction
createOrder: function (data, actions) {
return fetch('https://api.sandbox.paypal.com/v2/checkout/orders', {
method: 'post',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer <My Access Token>'
},
body: JSON.stringify({
"intent": "CAPTURE",
"purchase_units": [
{
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
]
})
}).then(function (res) {
return res.json();
}).then(function (data) {
orderID = data.id; //storing the id in our variable
return data.id;
});
},

// Finalize the transaction
onApprove: function (data, actions) {
//using the id stored in our variable
return fetch('https://api.sandbox.paypal.com/v2/checkout/orders/' + orderID + '/capture/', {
method: 'post',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer <My Access Token>'
},
}).then(function (res) {
return res.json();
}).then(function (details) {
console.log(details);
// Show a success message to the buyer
alert('Transaction completed');
});
}

}).render('#paypal-button-container');

</script>

您正在执行的实现非常适用于涉及服务器端组件的情况,并且对 PayPal 服务器的 API 调用是通过服务器完成的。如果您的实现不需要服务器端,那么我强烈建议您遵循智能支付按钮实现 - https://developer.paypal.com/docs/checkout/integrate/#

关于asp.net-core - 在 .net 中使用 Paypal REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56362621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com