繁体   English   中英

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

[英]Paypal Checkout Integration - onShippingChange dont work here with new SDK

我遵循了教程https://developer.paypal.com/docs/checkout/integrate/,并且还想根据运输国家/地区更改运输成本,如此处所示https://developer.paypal.com/docs /结帐/集成的功能/航运回调/

我的“ 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()时,总是收到来自贝宝的消息:“卖方不在该国家/地区发货,请使用其他地址...”(但我使用德国地址)。

更改以下行:

data.shipping_address.country

至:

data.shipping_address.country_code

最好的问候,托马斯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM