【问题标题】:PayPal JavaScript API pass shipping addressPayPal JavaScript API 通行证收货地址
【发布时间】:2021-11-25 21:20:23
【问题描述】:

我需要为在线商店使用 PayPal API。用户在商店界面中设置他的送货信息(姓名和地址),然后点击“Pay with PayPal”按钮。现在我需要将该信息传递给 PayPal API,以便 PayPal 结帐界面中的“送货地址”字段显示正确的地址。我按照 API 文档 (https://developer.paypal.com/docs/api/orders/v2/#definition-shipping_detail.address_portable) 中所示的方式实现了它,但它似乎不起作用,因为 PayPal 结帐界面中的送货地址和名称仍然是 John Doe 的默认沙箱地址。这是我实现它的方法,但它一定是错误的,因为它不起作用:

createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '34'
          },

          shipping_detail: {
            name: {
              full_name: 'Hans Muller'
            },
            address_portable: {
              address_line_1: 'Rohrd. 16',
              address_line_2: 'Rohrdorfer Street',
              admin_area_2: 'Stephanskirchen',
              admin_area_1: 'Bayern',
              postal_code: '83071',
              country_code: 'DE',
            },
          },
        }]
      });
    },

    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        console.log('Success');
      });
    },

    onError: function (err) {
      console.error(err);
    }

但是 PayPal 沙盒界面仍然显示(德语)默认收货地址:

此外,以后应该无法在 PayPal 界面中更改该地址。任何关于我在这里做错的建议将不胜感激。

【问题讨论】:

    标签: javascript paypal paypal-sandbox


    【解决方案1】:

    你确实做错了。

    对于Orders create request body,运输信息在 purchase_units -> shipping 中。您没有使用记录在案的键名对象结构。

    其次,一旦您正确提供了送货地址,它仍然可以在 PayPal 审批流程中进行更改。如果您想强制提供您提供的送货地址,您需要将 application_context -> shipping_preference 变量设置为SET_PROVIDED_ADDRES,如上面同一链接中所述。请注意,application_context 超出了 purchase_units 数组。如果您这样做是为了强制使用特定地址并且提供的地址不被 PayPal 接受,则交易将失败。

    确保在每个级别都使用记录在案的键名,而不是去那里的对象的名称(例如 order_* 和 *_portable 不正确)。错误命名的变量将被忽略。


    建议的 PayPal Checkout 流程是从买家的帐户中获取地址,并允许他们在 PayPal 中更改此地址。您可以在交易被批准后获取所选的收货地址,然后再进行抓包。

    【讨论】:

    • 非常感谢!这完全解决了我的问题。我使用了错误的对象键名。为了完整起见,我将为遇到此问题的其他所有人添加一个完整的工作 PayPal 按钮脚本的答案。
    【解决方案2】:

    感谢 Preston PHX(已接受的答案),我能够解决问题。这是所有懒人的完整 PayPal 按钮渲染调用:

    paypal.Buttons({
        style: {
          size: 'small',
          shape: 'rect',
          color: 'blue',
          layout: 'vertical',
          label: 'paypal',
          height: 40
        },
    
        createOrder: function(data, actions) {
          return actions.order.create({
            application_context: {
              brand_name: 'myBrand',
              locale: 'us-US',
              shipping_preference: 'SET_PROVIDED_ADDRESS',
            },
    
            purchase_units: [{
              amount: {
                value: '34'
              },
    
              shipping: {
                name: {
                  full_name: 'Hans Muller'
                },
                address: {
                  address_line_1: 'MyStreet 12',
                  admin_area_2: 'New York',
                  postal_code: '10001',
                  country_code: 'US',
                }
              }
            }]
          });
        },
    
        onApprove: function(data, actions) {
          return actions.order.capture().then(function(details) {
            console.log('Success');
          });
        },
    
        onError: function (err) {
          console.error(err);
        }
      }).render('#paypal-button-container');
    

    【讨论】:

      【解决方案3】:

      如果发送到SDK js地址,需要先在'purchase_units'中添加'application_context',然后在'purchase_units'同级添加'shipping'对象。

      链接:

      https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_contextapplication_context

      https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unitpurchase_units 中的送货信息

      代码:

      purchase_units: [{
        items: itemList,
        amount: { data },
        shipping: {
         name: {
          full_name: address.name+' '+address.surname
         },
         type: 'SHIPPING',
         address: {
          address_line_1: address.address,
          country_code: address.country.iso_code,
          postal_code: address.zip_code,
          admin_area_2: address.city,
          admin_area_1: address.country.general_name
        }
       },
      }],
      application_context: {
       shipping_preference: 'SET_PROVIDED_ADDRESS',
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-15
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 2021-12-19
        • 2013-10-30
        • 2015-10-05
        • 2015-02-17
        • 1970-01-01
        相关资源
        最近更新 更多