【问题标题】:Paypal Checkout Integration - onShippingChange dont work here with new SDKPaypal Checkout 集成 - onShippingChange 不适用于新 SDK
【发布时间】:2019-07-20 06:51:29
【问题描述】:

我按照教程 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() 时,我总是收到来自贝宝的消息,“卖家不在这个国家发货,使用不同的地址......”(但我使用德国地址)。

【问题讨论】:

    标签: javascript paypal


    【解决方案1】:

    更改以下行:

    data.shipping_address.country
    

    收件人:

    data.shipping_address.country_code
    

    最好的问候, 托马斯

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2021-05-31
      • 2012-08-27
      • 2016-05-04
      • 2021-02-19
      • 2014-02-27
      • 2015-03-25
      • 2016-02-25
      • 2012-10-10
      相关资源
      最近更新 更多