贝宝表达示例代码在PATCH上返回错误400

时间:2019-03-28 15:25:22

标签: javascript express paypal patch

我正在使用Paypal教程代码创建一个简单的Paypal Express按钮,直到我添加onshippingchange代码(在他们的教程中也是如此)之后,所有这些方法似乎都可以正常工作-当它尝试修补订单以增加运输费用时,Paypal返回错误400。

如果没有尝试打补丁,付款完成等代码,代码就会起作用。已经联系了贝宝(Paypal)支持,但他们花了很多时间来做出回应,到目前为止,不幸的是没有使用

<script>
      const baseOrderAmount = '15.00';
const addFloats = (...floats) => floats.reduce((v, t) => parseFloat(t) + parseFloat(v), 0).toFixed(2);

  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
     onApprove: function(data, actions) {
      // Capture the funds from the transaction
      return actions.order.capture().then(function(details) {
        // Show a success message to your buyer
        alert('Transaction completed by ' + details.payer.name.given_name);
      });
    }


onShippingChange: function(data, actions) {


        // Patch the shipping amount
        const shippingAmount =  '20.00';
        return actions.order.patch([
            {
                op: 'replace',
                path: '/purchase_units/@reference_id==default/amount',
                value: {
                    currency_code: 'USD',
                    value: addFloats(baseOrderAmount, shippingAmount),
                    breakdown: {
                        item_total: {
                            currency_code: 'USD',
                            value: baseOrderAmount
                        },
                        shipping: {
                            currency_code: 'USD',
                            value: shippingAmount
                        }
                    }
                }
            }
        ]);
    }


  }).render('#paypal-button-container');

我希望贝宝(Paypal)订单会更新为15.00种商品加20.00运费,但是贝宝(Paypal)窗口会返回“发生错误的错误”,并且控制台显示错误400

1 个答案:

答案 0 :(得分:0)

贝宝(Paypal)网站上的示例代码当前似乎有错误。

参考:https://developer.paypal.com/docs/checkout/integration-features/shipping-callback/

当前,贝宝(Paypal)在其示例中提供了以下代码:

path: '/purchase_units/@reference_id=='default'/amount'

您的代码(就像您起初删除引号一样):

path: '/purchase_units/@reference_id==default/amount',

但是,这似乎是所需的正确代码:

path: "/purchase_units/@reference_id=='default'/amount"