首页 > 解决方案 > 使用 PayPal 智能按钮的动态运费

问题描述

我在我的网站上使用 PayPal 智能按钮。如何按国家/地区制定不同的运费?我根据PayPal教程在这里尝试了这个:

    <script>
    function initPayPalButton() {
  paypal.Buttons({
     
          style: {
          shape: 'pill',
          color: 'gold',
          layout: 'vertical',
          label: 'paypal',
          
        },

            createOrder: function(data, actions) {

                return actions.order.create({

                    purchase_units: [{
                      description:  document.getElementById("Color").innerText,
                        amount: {
                         
                          value:
                          {{ current_variant.price | divided_by: 100.00 }}
                          },

                    }]
                        
                });
            },

        onApprove: function(data, actions) {
          return actions.order.capture().then(function(details) {
            alert('Transaction completed by ' + details.payer.name.given_name + '! Thank You For Your 
            Purchase');
          });
        },

常量 baseOrderAmount = '15.00'; onShippingChange:函数(数据,操作){

        // Patch the shipping amount
        const shippingAmount = data.shipping_address.country_code === 'US' ? '5.00' : '0.00';
        return actions.order.patch([
            {
                op: 'replace',
                path: '/purchase_units/@reference_id==\'default\'/amount',
                value: {
                    currency_code: 'USD',
                    value: (parseFloat(baseOrderAmount) + parseFloat(shippingAmount)).toFixed(2),
                    breakdown: {
                        item_total: {
                            currency_code: 'USD',
                            value: baseOrderAmount
                        },
                        shipping: {
                            currency_code: 'USD',
                            value: shippingAmount
                        }
                    }
                }
            }
        ]);
    }
}).render('#paypal-button-container');

我试图只向美国运送 5 美元,因为这是我的供应商费用。我也尝试了一些 if/else 语句,但似乎没有任何效果!我究竟做错了什么?或者也许有人知道用这些 PayPal 按钮制作这个的其他方法?到目前为止没有发现任何有用的东西。顺便说一句 - 使用 Shopify Narrative Theme 物有所值。

标签: javascriptpaypalshopifypaypal-rest-sdk

解决方案


推荐阅读