首页 > 解决方案 > 如何向 actions.order.create paypal 集成添加更多属性

问题描述

我想在 paypal 的 actions.order.create 函数中添加更多属性。tax_total、运费、保险、客户购买的物品清单等属性。

我已经从他们的网站实现了代码,它工作正常,剩下的就是在代码中添加额外的细节。这是他们的文档: https ://developer.paypal.com/docs/api/orders/v2/#orders_create 。

<script>
    paypal.Buttons({
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        // value: '350.00',
                        breakdown: {
                            item_total: '300.00',
                             tax_total: '20.00',
                             shipping: '10.00',
                             handling: '10.00',
                             insurance: '10.00'
                         },
                     }
                 }]
             });
         },
         onApprove: function(data, actions) {
             // Capture the funds from the transaction
             return actions.order.capture().then(function(details) {
                 // Show a success message to your buyer
                 console.log(data, actions);
                 alert('Transaction completed by ' + details.payer.name.given_name);
                 // Call your server to save the transaction
                 return fetch('/paypal-transaction-complete', {
                     method: 'post',
                     headers: {
                         'content-type': 'application/json',
                         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                     },
                     body: JSON.stringify({
                         orderID: data.orderID
                     })
                 });
             });
         }
     }).render('#paypal-button-container');
</script>

上面的代码是我尝试过的,我希望分解属性将成为有效负载的一部分,如文档中所示,但我从控制台收到错误消息。

那么添加这些属性的正确方法是什么?提前致谢 :)

标签: javascriptlaravelpaypal-sandbox

解决方案


我在这里找到了答案:

https://github.com/paypal/paypal-checkout-components/issues/1098

该链接中的示例有一个对我不起作用的货币代码。下面是一个类似的例子:

var createOrder = function createOrder(data, actions) { 
    return actions.order.create({
      "purchase_units": [
        {
          "reference_id": 1234,
          "description": "Attempt n.1 for Quote ID 1234",
          "amount": {
            "currency_code": "USD",
            "value": 14.4,
            "breakdown": {
              "item_total": { "currency_code":"USD", "value":"12"},
              "shipping": { "currency_code":"USD", "value":"1"},
              "tax_total": { "currency_code":"USD", "value":"1.4"},
              "discount": { "currency_code":"USD", "value":"0"}
            }
          },
          "items": [
            {
              "name": "OnePlus 6 T-rex 12\" name for 14\"\" blabla \" more double quotes",
              "unit_amount": {
                "currency_code": "USD",
                "value": 12
              },
              "tax": {
                "currency_code": "USD",
                "value": 1.4
              },
              "quantity": 1,
              "sku": "OnePlus61",
              "category": "PHYSICAL_GOODS"
            }
          ],
          "shipping": {
            "address": {
              "address_line_1": "Some line 1",
              "address_line_2": "Some line 2",
              "admin_area_2": "Some city",
              "admin_area_1": "some state",
              "postal_code": "12345",
              "country_code": "GB"
            }
          }
        }
      ]
    });
  };

请注意,每个金额必须加起来等于总值(在本例中为 14.4)。另请注意,在此示例中,键 tax_total 和 tax 的值必须匹配。如果您的订单中有不止一件商品,则情况可能并非如此。


推荐阅读