Server-side REST integration

Set up your client

  1. In the payment function:

  2. In the onAuthorize function:

    • Call your server with the data.paymentID and data.payerID values.
    • Execute the payment on your server-side.
  3. Optionally show a confirmation page

<div id="paypal-button"></div>

<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<script>
    var CREATE_PAYMENT_URL  = 'https://my-store.com/paypal/create-payment';
    var EXECUTE_PAYMENT_URL = 'https://my-store.com/paypal/execute-payment';

    paypal.Button.render({

        env: 'production', // Or 'sandbox'

        commit: true, // Show a 'Pay Now' button

        payment: function() {
            return paypal.request.post(CREATE_PAYMENT_URL).then(function(data) {
                return data.id;
            });
        },

        onAuthorize: function(data) {
            return paypal.request.post(EXECUTE_PAYMENT_URL, {
                paymentID: data.paymentID,
                payerID:   data.payerID
            }).then(function() {

                // The payment is complete!
                // You can now show a confirmation message to the customer
            });
        }

    }, '#paypal-button');
</script>

How the server integration works

This example describes how your server makes API calls to set up and execute the payment. For detailed server integration steps, see the REST server steps.

Set up the payment

  1. The payment method you created on the client makes a call to your server.

  2. Your server sends a create payment request to the Payments API.

    Note: You must pass redirect_urls in the create-payment request but PayPal does not automatically call these URLs. PayPal invokes your onAuthorize function when the buyer authorizes the payment. At this point you can choose to redirect the buyer.

  3. Your server reads the id from the JSON response:

        {
            "id": "PAY-0J356327TH335450NK56Y2PQ",
            "intent": "sale",
            "state": "created",
            ...
        }
    
  4. Your server sends a response to your client to pass back the payment ID:

    {
        "paymentID": "PAY-0J356327TH335450NK56Y2PQ"
    }
    
  5. To initiate the checkout flow, the payment method you created on the client passes the paymentID from the JSON response to the checkout.js script.

  6. The Express Checkout flow is launched in a lightbox on your page and gets the buyer's approval for payment.

To further customize the buyer's checkout experience, you can optionally create web experience profiles.

Execute the payment

When the buyer approves the payment, the checkout.js script calls your onAuthorize call back.

Note: You can show payment details on a confirmation page either before or after you execute the payment.

  1. Your client makes a call to your server in the onAuthorize call back.

    The required data.paymentID and data.payerID parameters are passed to your server to execute the payment.

  2. Your server sends an execute payment request to the Payments API and finalizes the payment.


Additional information

Try the full interactive demo

Next

Create a payment by calling the REST API