PayPal JavaScript API传递运送地址

4

我需要在一个在线商店中使用PayPal API。用户在商店界面上设置他的送货信息(姓名和地址),然后点击“使用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沙盒界面仍显示(德国)默认的邮寄地址:paypalinterface 此外,在PayPal界面中后续不应更改该地址。如有建议,请告知我错误之处。
3个回答

8

感谢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

你确实做错了。

对于一个创建订单请求体,运输信息应该放在purchase_units -> shipping中的文档化键名对象结构中。你没有使用正确的键名。

其次,一旦你正确提供了运输地址,在PayPal批准流程中仍然可以更改它。如果你想强制使用你提供的运输地址,你需要将application_context -> shipping_preference变量设置为SET_PROVIDED_ADDRES,如上面相同的链接所述。请注意,application_context在purchase_units数组之外。如果你这样做以强制使用特定地址,而所提供的地址不被PayPal接受,交易将失败。

确保你在每个级别使用文档化的键名,而不是那里放置的对象的名称(例如order_*和*_portable不正确)。错误命名的变量将被忽略。


推荐的PayPal结账流程是从买家的帐户中获取地址,并允许他们在PayPal内更改此地址。您可以在交易获批准之后并在捕获之前获取所选送货地址。

非常感谢!那完全解决了我的问题。我使用了错误的对象键名。为了完整起见,我将添加一个答案,其中包含完整的可工作的PayPal按钮脚本,以供其他遇到此问题的人参考。 - Merlin0216
我可以用同样的方式发送计费信息吗? - Azahar Alam

0
如果要发送到SDK js地址,您需要先在“purchase_units”中添加“shipping”对象,然后在与“purchase_units”相同级别的位置添加“application_context”。
链接: https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_context 用于application_context https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit 用于purchase_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',
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接