繁体   English   中英

PayPal 智能按钮 INVALID_RESOURCE_ID

[英]PayPal Smart Button INVALID_RESOURCE_ID

我正在尝试通过在服务器端创建订单,使用SmartButtons在我的网站上集成 PayPal 付款。

在遵循文档中的步骤后,例如通过初始化按钮然后通过设置服务器端代码来创建 orderID,当我尝试付款时,我在客户端收到以下错误:

Error: INVALID_RESOURCE_ID↵    at https://www.sandbox.paypal.com/smart/buttons?style.label=paypal&style.layout=horizontal&style.color=gold&style.shape=rect&style.tagline=false&style.height=52&components.0=buttons&locale.country...

在客户端收到的 orderID 是正确的,因为它与在 paypal 仪表板上创建的相同。在阅读了一些其他类似的问题后,有人说要捕获付款onApprove但即使在设置 API 以捕获付款请求之后只是没有进入 api 来捕获付款。

我的按钮代码如下所示:

var PAYPAL_SCRIPT = 'https://www.paypal.com/sdk/js?client-id=XXXXXX&currency=EUR';
var script = document.createElement('script');
script.setAttribute('src', PAYPAL_SCRIPT);
script.onload = function(){
    paypal.Buttons({
        style: {
            shape: 'rect',
            color: 'gold',
            layout: 'horizontal',
            label: 'paypal',
            tagline: false,
            height: 52
        },
        createOrder: function() {
            return fetch('https://localhost:44350/payment/paypal/order/create/', {
              method: 'post',
              headers: {
                'content-type': 'application/json'
              }
            }).then(function(res) {
              return res.json();
            }).then(function(data) {
              return data.headers[2].Value[0]; // Use the same key name for order ID on the client and server
            });
        },
        onApprove: function(data, actions) {
          return fetch('https://localhost:44350/payment/paypal/' + data.headers[2].Value[0] + '/capture/', {
            method: 'post'
        }).then(function(res) {
            return res.json();
        }).then(function(details) {
            // Show a success message to the buyer
            alert('Transaction completed by ' + details.payer.name.given_name + '!');
        });
        }
    }).render('#paypal-button-container');
}
document.head.appendChild(script);

那么我的 api 控制器如下:

    [HttpPost("paypal/order/create/")]
    public Task<HttpResponse> PayPal()
    {
        
        return PaypalHelper.CreateOrder("XXXXX", "XXXXXX", true);
    }

    [HttpPost("paypal/{orderID}/capture/")]
    public Task<HttpResponse> PayPal(string orderID)
    {

        return PaypalHelper.CaptureOrder(orderID, "XXXXX", "XXXXX", true);
    }

创建订单的方法如下所示:

 public async static Task<HttpResponse> CreateOrder(string publickey, string privatekey, bool debug = false)
        {
            var request = new OrdersCreateRequest();
            request.Prefer("return=representation");
            request.RequestBody(BuildRequestBody());
            //3. Call PayPal to set up a transaction
            var response = await PayPalClient.client(publickey, privatekey).Execute(request);

            if (debug)
            {
                var result = response.Result<Order>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                foreach (LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
                Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
            }

            return response;
        }

        private static OrderRequest BuildRequestBody()
        {
            var order = new OrderRequest()
            {
                CheckoutPaymentIntent = "CAPTURE",
                PurchaseUnits = new List<PurchaseUnitRequest>()
                {
                    new PurchaseUnitRequest()
                    {
                        AmountWithBreakdown = new AmountWithBreakdown()
                        {
                            CurrencyCode = "EUR",
                            Value = "100.00"
                        }
                    }
                },
                ApplicationContext = new ApplicationContext()
                {
                    ReturnUrl = "https://www.example.it/vmenu?paypal=success",
                    CancelUrl = "https://www.example.com?paypal=error"
                }
            };

            return order;
        }

createOrder fetch 中的数据如下:

{headers: Array(3), statusCode: 201}headers: Array(3)0: {Key: "Cache-Control", Value: Array(1)}1: {Key: "Date", Value: Array(1)}2: Key: "Paypal-Debug-Id"Value: ["97fcfafaae8ef"]__proto__: Objectlength: 3__proto__: Array(0)statusCode: 201__proto__: Object
menu_test.js:33

而通过获取data.headers[2].Value[0]结果是 97fcfafaae8ef

您传递的 ID 错误。 PayPal-Debug-Id 不是 orderID。

此外,orderID 不会在 header 中返回,它们在正文中作为“id”返回。

您可以在此处查看示例响应: https://developer.paypal.com/docs/api/orders/v2/#orders_create

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM