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

标签 paypal paypal-rest-sdk

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

这个标记工作正常:

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

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

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

我猜我添加分割的方法不起作用。但规范暗示它是数量 -> 分割。

最佳答案

完整的工作示例...保存到 HTML 文件,或者只是复制 purchase_units 数组

<!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>

关于paypal - 为什么添加项目会使 Paypal Checkout 返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384260/

相关文章:

ruby-on-rails - 用户的未定义方法 'suspend_paypal'

api - 需要用于并行支付的 PayPal NVP api 请求代码示例代码

Paypal 沙盒。 10609 交易ID无效

JavaScript - Paypal API,如何更改送货地址?

php - 计费计划的多次付款定义

paypal - 尝试从智能按钮创建订阅时出错

paypal - 付款完成后,就称为销售?

node.js - Node 应用程序中与 KillBill 的 Paypal 集成问题

php - Paypal 付款的奇怪问题

forms - Paypal 折扣 - 它是如何运作的?