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

标签 asp.net-core paypal paypal-sandbox fetch-api

我正在尝试使用我的 .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/

相关文章:

paypal - 如何在activemerchant gem中允许不同于美元的货币?

php - 以编程方式创建 magento 订单中的 Paypal 发布错误

c# - 如何从 ASP.NET Core 应用程序访问 Azure Active Directory 的其他数据?

c# - Kestrel 上的身份验证失败,但 IIS Express 上没有

java - Paypal 沙箱响应中未返回收据编号

paypal - 沙盒加载有很多错误 net::ERR_CONNECTION_CLOSED

asp.net - 快速结帐继续加载并且没有返回给商家

尝试在沙盒中使用信用卡付款时出现 PayPal 错误

asp.net-core - 使用 dotnet pack 包含所有依赖项

asp.net-core - 单独程序集中的 EditorTemplate 模板