Paypal Checkout集成-onShippingChange不适用于新的SDK

时间:2019-02-26 16:49:59

标签: javascript paypal

我遵循了教程 https://developer.paypal.com/docs/checkout/integrate/ 并且还想根据运输国家/地区更改运输费用,如下所示 https://developer.paypal.com/docs/checkout/integration-features/shipping-callback/

我的“ Paypal-Button”代码如下:

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXX&currency=EUR">
</script>
</head>
<body>

<h2>Checkout</h2>

<div id="paypal-button-container"></div>
<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) {
      // Set up the transaction
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: baseOrderAmount
          }
        }]
      });
    },

    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);
        // Call your server to save the transaction
        return fetch('/paypal-transaction-complete.php', {
          method: 'post',
          body: JSON.stringify({
            orderID: data.orderID
          })
        });
      });
    },

    onShippingChange: function(data, actions) {

        // Reject all others
        if ((data.shipping_address.country !== 'DE') || (data.shipping_address.country !== 'AT') || (data.shipping_address.country !== 'CH')) {
            return actions.reject();
        }

        // Patch the shipping amount
        var shippingAmount = 10.0;
        if (data.shipping_address.country == 'AT') {
            shippingAmount = 20.0;
        }
        if (data.shipping_address.country == 'CH') {
            shippingAmount = 30.0;
        }

        return actions.order.patch([
        {
            op: 'replace',
            path: "/purchase_units/@reference_id=='default'/amount",
            value: {
                currency_code: 'EUR',
                value: addFloats(baseOrderAmount, shippingAmount),
                breakdown: {
                    item_total: {
                        currency_code: 'EUR',
                        value: baseOrderAmount
                    },
                    shipping: {
                        currency_code: 'EUR',
                        value: shippingAmount
                    }
                }
            }
        }]);
    }
  }).render('#paypal-button-container');
</script>
</body>
</html>

其他所有方法都可以正常工作,但是当我包含onShippingChange()时,总是会收到来自贝宝的消息:“卖方不在该国家/地区发货,请使用不同的地址...”(但是我使用的是德国地址)。

1 个答案:

答案 0 :(得分:0)

更改以下行:

data.shipping_address.country

收件人:

data.shipping_address.country_code
最好的问候, 托马斯