在 .net 中使用 Paypal REST api

XamDev

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

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

同样在OnApprove块中,我没有得到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 吗?

对此的任何帮助表示赞赏!

刺耳的 j

你的代码看起来很完美(几乎)。你只需要记住这里变量的范围。由于“data”变量仅限于“then”块,您将需要创建一个新变量来保存“data.id”的值并在 onApprove 块中使用它。我在下面的代码中添加了一个名为“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/#

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章