Search code examples
javarestapipaypalpaypal-rest-sdk

How to get user info in PayPal REST API


I'm moving from NVP/SOAP PayPal API integration to newer REST APIs.

My old website code used the 'Express Checkout' flow.

In a nutshell:

  • you see the 'shopping cart' summary page, containing the list of products to purchase;

  • you click on 'Pay with PayPal' button (no need to fill out the user data form);

  • the website initiate a payment, the user is redirecte to PayPal;

  • the user logins and does confirm the initiated payment;

  • the user is redirected to the website, which calls PayPal for user personal information to save in the transaction record (e.g. name, address, ...);

  • the payment is finally executed;

I checked out the integration patterns here: https://developer.paypal.com/demo/checkout/#/pattern/server

...but can't figure out how to implement the exact same workflow with the new APIs.

I mean, inside the onAuthorize callback method I do have paymentID and payerID of the user-authorized payment, waiting for execution.. but don't have his personal data, for my order to save.

Is there some sort of call to retrieve that data? Or include it in the response?


Solution

  • First of all, please let me know how deep into details do you want me to go. Should you like a general overview of the workflow, a pseudocode, or a code example, I'll do my best to provide those. For now, I'll stick to a general overview with references.

    Check out the Identity API call in the Paypal reference document. It is a GET request that returns a "userinfo" object, which in turn contains user profile attributes such as name, address etc.

    However, I suspect you would want to use the Invoice API instead, in case if your buyer specifies details different from their profile. In this case, you should implement a GET /v1/invoicing/invoices/invoice_id request, which will take your invoice ID as a parameter and return the user-specified address, name and so on. It also contains templates of the requests you need to build in order to execute that.

    EDIT: I have looked into it some more, and I think I've found a better solution. You can return any transaction details from your Paypal button callback by calling return actions.payment.get().then(function(data) and specifying the data you want. I'm providing the example I've found below:

    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
                    });
                });
            });
        }
    

    This way you get the info you need automatically without creating additional (and needless) bulk of requests.