首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么添加项目会使Paypal Checkout返回错误?

为什么添加项目会使Paypal Checkout返回错误?
EN

Stack Overflow用户
提问于 2019-12-18 09:18:10
回答 1查看 617关注 0票数 1

我正在尝试使用Javascript SDK创建一个PayPal订单。如果PayPal没有返回一个非描述性的400错误,我无法添加项目。

此标记可以很好地工作:

代码语言:javascript
复制
return actions.order.create({
  purchase_units: [{
    description: "Stuff",
    amount: {
      value: '57.49',
      currency_code: "CAD",
    },
  }],
  application_context: {
    brand_name: "MyBusiness",
    shipping_preference: 'NO_SHIPPING'
  }
});

这个标记,我在其中添加了金额细目和项目,但没有:

代码语言:javascript
复制
return actions.order.create({
  purchase_units: [{
    description: "Stuff",
    amount: {
      value: '57.49',
      currency_code: "CAD",
      breakdown: {
        item_total: '57.49',
      }
    },
    items: [{
      unit_amount: '57.49',
      quantity: '1',
      name: "item 1",
    }],
  }],
  application_context: {
    brand_name: "MyBusiness",
    shipping_preference: 'NO_SHIPPING'
  }
});

我遵循以下文档:

https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit_request

https://developer.paypal.com/docs/api/orders/v2/#definition-item

我猜我添加分解的方式不起作用。但该规范暗示这是数量->分解。

EN

回答 1

Stack Overflow用户

发布于 2019-12-18 10:13:40

完整的工作示例...保存为HTML文件,或仅复制purchase_units数组

代码语言:javascript
复制
<!DOCTYPE html>
<!-- example from https://developer.paypal.com/demo/checkout/#/pattern/client -->

<head>
    <!-- Add meta tags for mobile and IE -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>

<body>
    <!-- Set up a container element for the button -->
    <div id="paypal-button-container"></div>

    <!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=CAD"></script>

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

            // Set up the transaction
            createOrder: function(data, actions) {
                return actions.order.create({


// based on example array from https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
  "purchase_units": [{
      "description": "Stuff",
      "amount": {
        "value": "57.49",
        "currency_code": "CAD",
        "breakdown": {
          "item_total": {
            "currency_code": "CAD",
            "value": "57.49"
          },
        }
      },
      "items": [
        {
          "unit_amount": {
            "currency_code": "CAD",
            "value": "57.49"
          },
          "quantity": "1",
          "name": "item 1",
        },
      ],
    }
  ]

  ,
  application_context: {
    brand_name: "MyBusiness",
    shipping_preference: 'NO_SHIPPING'
  }


                }/*end of parameters to actions.order.create*/);
            },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            }


        }).render('#paypal-button-container');
    </script>
</body>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59384260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档