【问题标题】:Add list of items to PayPal Standard button for payment将项目列表添加到 PayPal 标准按钮以进行付款
【发布时间】:2021-09-06 11:50:25
【问题描述】:

感觉应该可以使用 Paypal 网站上生成的标准按钮创建包含多个项目的订单。显然我遗漏了一些东西,并希望得到任何帮助或信息。以下是我的页面源代码和按下支付按钮时收到的错误消息。

    <div id="paypal-button-container"></div>
    <!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=MY_SANDBOX_CLIENT_ID&enable-funding=venmo&currency=USD&debug..."></script>
    <script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({
    style: {
    color: 'blue',
    shape: 'rect',
    label: 'pay',
    },
    
    // Sets up the transaction when a payment button is clicked
    createOrder: function(data, actions) {
    return actions.order.create({
  "intent": "CAPTURE",
  "payer": {
    "name": {
      "given_name": "John",
      "surname": "Doe"
    },
    "address": {
      "address_line_1": "123 nowhere lane",
      "address_line_2": "Apt 2",
      "admin_area_2": "Sacramento",
      "admin_area_1": "CA",
      "postal_code": "93423",
      "country_code": "US"
    },
    "email_address": "sb_test_email_address@business.example.com",
    "phone": {
      "phone_type": "MOBILE",
      "phone_number": {
        "national_number": "12312342343"
      }
    }
  },
  "purchase_units": [
    {
      "description": "My Purchases",
      "amount": {
        "currency_code": "USD",
        "value": "12.00",
        "breakdown": {
          "item_total": {
            "currency_code": "USD",
            "value": "12.00"
          },
          "shipping": {
            "currency_code": "USD",
            "value": "0"
          },
          "tax_total": {
            "currency_code": "USD",
            "value": "0"
          }
        }
      },
      "items": [
        {
          "name": "Item 1",
          "unit_amount": {
            "currency_code": "USD",
            "value": "6.00"
          },
          "quantity": "1"
        },
        {
          "name": "Item 2",
          "unit_amount": {
            "currency_code": "USD",
            "value": "6.00"
          },
          "quantity": "1"
        }
      ]
    }
  ]
});
    },
    
    // Finalize the transaction after payer approval
    onApprove: function(data, actions) {
    return actions.order.capture().then(function(orderData) {
    window.location.href = "Thanks.html";
    });
    },
    
    onCancel: function(data) {
    //payment cancelled
    alert("Payment Cancelled");
    },
    
    onError: function(err) {
    alert("Error Message: " + err);
    }
    }).render('#paypal-button-container');
    </script>
    </div>

The error message I get is shown in this image. Any help or guidance would be greatly appreciated.

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/TD8kw.png

【问题讨论】:

    标签: paypal


    【解决方案1】:

    在您的问题中重新格式化 Orders create request 有效负载以确保易读性并仅测试有效负载 at the demo site 后,它可以正常工作(下面的屏幕截图)。

    您还可以在the current documentation.中的“添加和修改代码”的第 6 步中找到参考示例

    【讨论】:

    • 感谢您提供清晰的解释和文档链接。还花时间整理代码并用经过测试的示例进行响应。我现在可以使用它并将您的回复标记为解决方案。非常感谢!干杯
    • 在文档中,他们指出可以从这样的表单输入框中获取 Total 值。 "value": document.getElementById("txtTotal").value, 这对我很有用,如果可能的话,我想以类似的方式包含项目。 "items": [ document.getElementById("txtItems").value ] 似乎不起作用。再次非常感谢任何指导。
    • 你需要构建一个完整的 items 数组,而不仅仅是在数组中放一个松散的值。 “添加和修改代码”的第 6 步(上面链接)有一个完整的项目数组的示例。特别是你需要给一个对象一个名称键、值键和数量。
    • 另外,在包含 items 数组时,不要忘记在数量中也添加细分对象。这是必需的
    • 感谢您的指导。我将深入研究您建议的第 6 步示例。
    猜你喜欢
    • 2014-03-01
    • 2015-01-19
    • 2019-10-23
    • 2017-01-21
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 2018-10-27
    • 2014-09-17
    相关资源
    最近更新 更多