如何在 PayPal REST 中获取用户信息 API
How to get user info in PayPal REST API
我正在从 NVP/SOAP PayPal API 集成转移到较新的 REST APIs。
我的旧网站代码使用了 'Express Checkout' 流程。
简而言之:
您会看到 'shopping cart' 摘要页面,其中包含要购买的产品列表;
您点击了'Pay with PayPal'按钮(无需填写用户数据表);
网站发起支付,用户被重定向到PayPal;
用户登录并确认发起的支付;
用户被重定向到该网站,该网站调用PayPal获取用户个人信息以保存在交易记录中(例如姓名,地址,...);
付款终于执行了;
我在这里检查了集成模式:https://developer.paypal.com/demo/checkout/#/pattern/server
...但无法弄清楚如何使用新的 API 实现完全相同的工作流程。
我的意思是,在onAuthorize回调方法里面我有用户授权支付的paymentID和payerID,等待执行..但是没有他的个人数据,保存我的订单。
是否有某种调用来检索该数据?或者将其包含在响应中?
首先,请告诉我您希望我深入到什么程度。如果您想要工作流程的总体概述、伪代码或代码示例,我会尽力提供这些内容。现在,我将坚持使用参考文献进行总体概述。
查看 Paypal 参考文档中的 Identity API call。这是一个 GET 请求 return 一个 "userinfo" 对象,该对象又包含用户配置文件属性,例如姓名、地址等。
但是,我怀疑您会想改用 Invoice API,以防您的买家指定的详细信息与其个人资料不同。在这种情况下,您应该实施 GET /v1/invoicing/invoices/invoice_id 请求,该请求会将您的发票 ID 作为参数,并且 return 用户指定的地址、名称等。它还包含您需要构建以执行该请求的模板。
编辑:我进一步研究了它,我认为我找到了更好的解决方案。您可以通过调用 return actions.payment.get().then(function(data)
并指定您想要的数据来 return 来自 Paypal 按钮回调的任何交易详细信息。我提供的示例如下:
onAuthorize: function(data, actions) {
// Get the payment details
return actions.payment.get().then(function(data) {
// Display the payment details and a confirmation button. You may want to save the queried data to your server, make sure to do this here
var shipping = data.payer.payer_info.shipping_address;
document.querySelector('#recipient').innerText = shipping.recipient_name;
document.querySelector('#line1').innerText = shipping.line1;
document.querySelector('#city').innerText = shipping.city;
document.querySelector('#state').innerText = shipping.state;
document.querySelector('#zip').innerText = shipping.postal_code;
document.querySelector('#country').innerText = shipping.country_code;
document.querySelector('#paypal-button-container').style.display = 'none';
document.querySelector('#confirm').style.display = 'block';
// Listen for click on confirm button
document.querySelector('#confirmButton').addEventListener('click', function() {
// Button click event code
// Execute the payment
return actions.payment.execute().then(function() {
// payment executed code
// display confirmation, thank you notes and whatnot
});
});
});
}
通过这种方式,您可以自动获得所需的信息,而无需创建额外的(和不必要的)大量请求。
我正在从 NVP/SOAP PayPal API 集成转移到较新的 REST APIs。
我的旧网站代码使用了 'Express Checkout' 流程。
简而言之:
您会看到 'shopping cart' 摘要页面,其中包含要购买的产品列表;
您点击了'Pay with PayPal'按钮(无需填写用户数据表);
网站发起支付,用户被重定向到PayPal;
用户登录并确认发起的支付;
用户被重定向到该网站,该网站调用PayPal获取用户个人信息以保存在交易记录中(例如姓名,地址,...);
付款终于执行了;
我在这里检查了集成模式:https://developer.paypal.com/demo/checkout/#/pattern/server
...但无法弄清楚如何使用新的 API 实现完全相同的工作流程。
我的意思是,在onAuthorize回调方法里面我有用户授权支付的paymentID和payerID,等待执行..但是没有他的个人数据,保存我的订单。
是否有某种调用来检索该数据?或者将其包含在响应中?
首先,请告诉我您希望我深入到什么程度。如果您想要工作流程的总体概述、伪代码或代码示例,我会尽力提供这些内容。现在,我将坚持使用参考文献进行总体概述。
查看 Paypal 参考文档中的 Identity API call。这是一个 GET 请求 return 一个 "userinfo" 对象,该对象又包含用户配置文件属性,例如姓名、地址等。
但是,我怀疑您会想改用 Invoice API,以防您的买家指定的详细信息与其个人资料不同。在这种情况下,您应该实施 GET /v1/invoicing/invoices/invoice_id 请求,该请求会将您的发票 ID 作为参数,并且 return 用户指定的地址、名称等。它还包含您需要构建以执行该请求的模板。
编辑:我进一步研究了它,我认为我找到了更好的解决方案。您可以通过调用 return actions.payment.get().then(function(data)
并指定您想要的数据来 return 来自 Paypal 按钮回调的任何交易详细信息。我提供的示例如下:
onAuthorize: function(data, actions) {
// Get the payment details
return actions.payment.get().then(function(data) {
// Display the payment details and a confirmation button. You may want to save the queried data to your server, make sure to do this here
var shipping = data.payer.payer_info.shipping_address;
document.querySelector('#recipient').innerText = shipping.recipient_name;
document.querySelector('#line1').innerText = shipping.line1;
document.querySelector('#city').innerText = shipping.city;
document.querySelector('#state').innerText = shipping.state;
document.querySelector('#zip').innerText = shipping.postal_code;
document.querySelector('#country').innerText = shipping.country_code;
document.querySelector('#paypal-button-container').style.display = 'none';
document.querySelector('#confirm').style.display = 'block';
// Listen for click on confirm button
document.querySelector('#confirmButton').addEventListener('click', function() {
// Button click event code
// Execute the payment
return actions.payment.execute().then(function() {
// payment executed code
// display confirmation, thank you notes and whatnot
});
});
});
}
通过这种方式,您可以自动获得所需的信息,而无需创建额外的(和不必要的)大量请求。