【问题标题】:How to get user info in PayPal REST API如何在 PayPal REST API 中获取用户信息
【发布时间】:2017-12-30 06:53:21
【问题描述】:

我正在从 NVP/SOAP PayPal API 集成迁移到更新的 REST API。

我的旧网站代码使用“快速结帐”流程。

简而言之:

  • 您会看到“购物车”摘要页面,其中包含要购买的产品列表;

  • 您点击“Pay with PayPal”按钮(无需填写用户数据表);

  • 网站发起支付,用户被重定向到PayPal;

  • 用户登录并确认发起的支付;

  • 用户被重定向到网站,该网站调用 PayPal 将用户个人信息保存在交易记录中(例如姓名、地址……)

  • 付款最终执行;

我在这里查看了集成模式:https://developer.paypal.com/demo/checkout/#/pattern/server

...但不知道如何使用新 API 实现完全相同的工作流程。

我的意思是,在 onAuthorize 回调方法中,我确实有用户授权付款的 paymentID 和 payerID,等待执行.. 但没有他的个人数据,以便我的订单保存.

是否有某种调用来检索该数据?还是将其包含在响应中?

【问题讨论】:

    标签: java rest api paypal paypal-rest-sdk


    【解决方案1】:

    首先,请让我知道您希望我深入了解细节。如果您喜欢工作流的一般概述、伪代码或代码示例,我会尽力提供这些。现在,我将使用参考文献进行总体概述。

    查看 Paypal 参考文档中的Identity API call。这是一个返回“userinfo”对象的GET请求,该对象又包含用户配置文件属性,例如姓名、地址等。

    但是,我怀疑您可能希望使用 Invoice API,以防您的买家指定的详细信息与他们的个人资料不同。在这种情况下,您应该实现 GET /v1/invoicing/invoices/invoice_id 请求,它将您的发票 ID 作为参数并返回用户指定的地址,名字等等。它还包含执行该请求所需构建的请求模板。

    编辑:我已经对其进行了更多研究,并且我认为我找到了更好的解决方案。您可以通过调用 return actions.payment.get().then(function(data) 并指定您想要的数据,从您的 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
                    });
                });
            });
        }
    

    这样您就可以自动获得所需的信息,而无需创建额外(和不必要的)大量请求。

    【讨论】:

    • 感谢您的回复。概述对我有好处。无论如何,我仍然不了解 Identity API。我的意思是,我应该打电话给 /v1/identity/openidconnect/userinfo .... 没有 ID?将返回哪些用户信息?听起来您查询当前登录的用户,但我在服务器上下文中,而不是客户端上下文中。给定付款人 ID/交易 ID,是否有某种方法可以要求提供身份数据?
    • 我已经更新了我的答案,我想我找到了更好的使用方法。无论如何,关于您评论中的问题:/v1/identity/openidconnect/userinfo 将 openID 作为参数,您可以通过OAuth 获得。这里有两个链接详细说明了如何在 Paypal 中设置和维护 OAuth,这应该会有所帮助:12
    • 我最终选择了另一种解决方案。我正在寻找“显示付款详细信息”API:developer.paypal.com/docs/integration/direct/express-checkout/… 我没有注意到响应中的“payerInfo”对象,这正是我想要的。现在我可以在执行付款之前获取用户信息,没关系。您的回答对于实现我的客户端代码也非常有用。谢谢。
    猜你喜欢
    • 2015-10-11
    • 2013-12-08
    • 2011-06-12
    • 2016-09-24
    • 2020-03-22
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多