Integrate PayPal Checkout

SDKCurrentStandardDirect merchant

Last updated: Apr 5th, 6:14pm

How it works

Integrate standard Checkout to show the PayPal payment buttons. To extend your integration, see Customize your buyers' experience.

After you integrate standard Checkout, you can also offer the following options with some additional configuration:

  • Pay Later
  • Venmo
  • PayPal Credit

This integration guide follows the code in this GitHub sample.

How it works

Get up and running in GitHub Codespaces

GitHub Codespaces are cloud-based development environments where you can code and test your PayPal integrations. Learn more.

Know before you code

Required
You need a developer account to get sandbox credentials

PayPal uses the following REST API credentials, which you can get from the developer dashboard:

  • Client ID: Authenticates your account with PayPal and identifies an app in your sandbox.
  • Client secret: Authorizes an app in your sandbox. Keep this secret safe and don't share it.
DashboardRead the guide

Required
You'll need both PayPal and third-party tools

  • JavaScript SDK: Adds PayPal-supported payment methods.
  • Orders REST API: Create, update, retrieve, authorize, and capture orders.
  • npm: Registry used to install third-party libraries.
  • You can use Postman to explore and test PayPal APIs.
1

Set up your environment

Complete the following steps to set up your development environment.

1. Set up npm

This sample integration uses Node.js. You'll need to install npm to run the sample application. For more info, visit npm's documentation.

2. Install third-party libraries

You'll need to install the following third-party libraries to set up your integration. This sample command installs all libraries at the same time:

        npm install dotenv express node-fetch
    

Third-party libraries Description
dotenv Separates your configuration and code by loading environment variables from a .env file into process.env.
express This lean Node.js web application framework supports web and mobile applications.
node-fetch This function helps you make API requests, similar to window.fetch.

3. Verify Package.json

A package.json file lists the packages and version numbers your app needs. You can share your package.json file with other developers so they can use the same settings as you.

The following code sample shows a package.json file for a PayPal integration. Compare this sample to the paste in your project:

    1{
    2 "name": "paypal-standard-integration",
    3 "description": "Sample Node.js web app to integrate PayPal Standard Checkout for online payments",
    4 "version": "1.0.0",
    5 "main": "server/YOUR-SERVER-NAME.js",
    6 "type": "module",
    7 "scripts": {
    8 "start": "node server/YOUR-SERVER-NAME.js",
    9 },
    10 "dependencies": {
    11 "dotenv": "^16.3.1",
    12 "express": "^4.18.2",
    13 "node-fetch": "^3.3.2"
    14 },
    15}

    Replace YOUR-SERVER-NAME.js in main with the name of your server file on lines 5 and 8.

    If you're having trouble with your app, reinstall your local library and package files using npm install.

    If you're getting the following node error, include "type": "module" in your package.json file. This line isn't automatically added when package.json is created.

    Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension (Use `node --trace-warnings ...` to show where the warning was created)

    See line 6 of the sample package.json file for an example.

    4. Set up .env

    A .env file is a line-delimited text file that sets your local working environment variables. Use this .env file to securely pass the client ID and client secret for your app.

    The following code shows an example .env file. Replace the PAYPAL-CLIENT-ID and PAYPAL-CLIENT-SECRET with values from your app:

      1PAYPAL_CLIENT_ID=YOUR_CLIENT_ID_GOES_HERE
      2PAYPAL_CLIENT_SECRET=YOUR_SECRET_GOES_HERE
      Note: View your client ID and client secret in the PayPal Developer Dashboard under Apps & Credentials.
      2

      Integrate back end

      This section explains how to set up your back end to integrate standard Checkout.

      Back-end process

      1. Your app creates an order on the back end by making a call to the Create Orders API endpoint.
      2. Your app moves the money when the payer confirms the order by making a call to to the Capture Payment for Order API endpoint on the back end.

      The following code sample uses the server.js file to set up the back end to integrate with standard payments.

        1import fetch from "node-fetch";
        2import "dotenv/config";
        3import path from "path";
        4
        5const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PORT = 8888 } = process.env;
        6const base = "https://api-m.sandbox.paypal.com";
        7const app = express();
        8
        9// host static files
        10app.use(express.static("client"));
        11
        12// parse post params sent in body in json format
        13app.use(express.json());
        14
        15/**
        16* Generate an OAuth 2.0 access token for authenticating with PayPal REST APIs.
        17* @see https://developer.paypal.com/api/rest/authentication/
        18*/
        19const generateAccessToken = async () => {
        20 try {
        21 if (!PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET) {
        22 throw new Error("MISSING_API_CREDENTIALS");
        23 }
        24 const auth = Buffer.from(
        25 PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET,
        26 ).toString("base64");
        27 const response = await fetch(`${base}/v1/oauth2/token`, {
        28 method: "POST",
        29 body: "grant_type=client_credentials",
        30 headers: {
        31 Authorization: `Basic ${auth}`,
        32 },
        33 });
        34
        35 const data = await response.json();
        36 return data.access_token;
        37 } catch (error) {
        38 console.error("Failed to generate Access Token:", error);
        39 }
        40};
        41
        42/**
        43* Create an order to start the transaction.
        44* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
        45*/
        46const createOrder = async (cart) => {
        47 // use the cart information passed from the front-end to calculate the purchase unit details
        48 console.log(
        49 "shopping cart information passed from the frontend createOrder() callback:",
        50 cart,
        51 );
        52
        53 const accessToken = await generateAccessToken();
        54 const url = `${base}/v2/checkout/orders`;
        55 const payload = {
        56 intent: "CAPTURE",
        57 purchase_units: [
        58 {
        59 amount: {
        60 currency_code: "USD",
        61 value: "100.00",
        62 },
        63 },
        64 ],
        65 };
        66
        67 const response = await fetch(url, {
        68 headers: {
        69 "Content-Type": "application/json",
        70 Authorization: `Bearer ${accessToken}`,
        71 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
        72 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
        73 // "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
        74 // "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
        75 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
        76 },
        77 method: "POST",
        78 body: JSON.stringify(payload),
        79 });
        80
        81 return handleResponse(response);
        82};
        83
        84/**
        85* Capture payment for the created order to complete the transaction.
        86* @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
        87*/
        88const captureOrder = async (orderID) => {
        89 const accessToken = await generateAccessToken();
        90 const url = `${base}/v2/checkout/orders/${orderID}/capture`;
        91
        92 const response = await fetch(url, {
        93 method: "POST",
        94 headers: {
        95 "Content-Type": "application/json",
        96 Authorization: `Bearer ${accessToken}`,
        97 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
        98 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
        99 // "PayPal-Mock-Response": '{"mock_application_codes": "INSTRUMENT_DECLINED"}'
        100 // "PayPal-Mock-Response": '{"mock_application_codes": "TRANSACTION_REFUSED"}'
        101 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
        102 },
        103 });
        104
        105 return handleResponse(response);
        106};
        107
        108async function handleResponse(response) {
        109 try {
        110 const jsonResponse = await response.json();
        111 return {
        112 jsonResponse,
        113 httpStatusCode: response.status,
        114 };
        115 } catch (err) {
        116 const errorMessage = await response.text();
        117 throw new Error(errorMessage);
        118 }
        119}
        120
        121app.post("/api/orders", async (req, res) => {
        122 try {
        123 // use the cart information passed from the front-end to calculate the order amount detals
        124 const { cart } = req.body;
        125 const { jsonResponse, httpStatusCode } = await createOrder(cart);
        126 res.status(httpStatusCode).json(jsonResponse);
        127 } catch (error) {
        128 console.error("Failed to create order:", error);
        129 res.status(500).json({ error: "Failed to create order." });
        130 }
        131});
        132
        133app.post("/api/orders/:orderID/capture", async (req, res) => {
        134 try {
        135 const { orderID } = req.params;
        136 const { jsonResponse, httpStatusCode } = await captureOrder(orderID);
        137 res.status(httpStatusCode).json(jsonResponse);
        138 } catch (error) {
        139 console.error("Failed to create order:", error);
        140 res.status(500).json({ error: "Failed to capture order." });
        141 }
        142});
        143
        144// serve index.html
        145app.get("/", (req, res) => {
        146 res.sendFile(path.resolve("./client/checkout.html"));
        147});
        148
        149app.listen(PORT, () => {
        150 console.log(`Node server listening at http://localhost:${PORT}/`);
        151});

        Understand the server.js code sample

        The following section explains key parts of the server.js code sample.

        Declare imports

        This section of code sets up the port to run your server and starts the express Node.js web application framework. It also retreives variables and sets the base sandbox URL.

          1const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET, PORT = 8888 } = process.env;
          2const base = "https://api-m.sandbox.paypal.com";
          3const app = express();
          • Line 1 uses the DotEnv library to import and declare the client ID and client secret from your .env file.
          • Line 2 declares the base URL for PayPal's sandbox API.

          Additional functions

          This section of the code defines a file directory for static files and calls the express.json function to parse JSON response bodies.

            1// host static files
            2app.use(express.static("client"));
            3// parse post params sent in body in json format
            4app.use(express.json());

            Generate access token

            You need an access token to authenticate all REST API requests. The following code sample makes a POST call to the /v1/oauth2/ token endpoint to create an access token.

              1/**
              2* Generate an OAuth 2.0 access token for authenticating with PayPal REST APIs.
              3* @see https://developer.paypal.com/api/rest/authentication/
              4*/
              5const generateAccessToken = async () => {
              6 try {
              7 if (!PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET) {
              8 throw new Error("MISSING_API_CREDENTIALS");
              9 }
              10 const auth = Buffer.from(
              11 PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET,
              12 ).toString("base64");
              13 const response = await fetch(`${base}/v1/oauth2/token`, {
              14 method: "POST",
              15 body: "grant_type=client_credentials",
              16 headers: {
              17 Authorization: `Basic ${auth}`,
              18 },
              19 });
              20 const data = await response.json();
              21 return data.access_token;
              22 } catch (error) {
              23 console.error("Failed to generate Access Token:", error);
              24 }
              25};
              • Line 10 combines the PAYPAL_CLIENT_ID and PAYPAL_CLIENT_SECRET as a key-value pair.
              • Lines 13-18 define a response that makes a POST call to the /v1/oauth2/token API endpoint to generate an access token.
              • Lines 20-21 establish a listener to capture the response data from the request and return the access_token.

              Create order

              Create an order to start a payment between a payer and a seller by making a POST request to /v2/checkout/orders.

                1/**
                2* Create an order to start the transaction.
                3* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
                4*/
                5const createOrder = async (cart) => {
                6 // use the cart information passed from the front-end to calculate the purchase unit details
                7 console.log(
                8 "shopping cart information passed from the frontend createOrder() callback:",
                9 cart,
                10 );
                11 const accessToken = await generateAccessToken();
                12 const url = `${base}/v2/checkout/orders`;
                13 const payload = {
                14 intent: "CAPTURE",
                15 purchase_units: [
                16 {
                17 amount: {
                18 currency_code: "USD",
                19 value: "100.00",
                20 },
                21 },
                22 ],
                23 };
                24 const response = await fetch(url, {
                25 headers: {
                26 "Content-Type": "application/json",
                27 Authorization: `Bearer ${accessToken}`,
                28 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
                29 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
                30 // "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
                31 // "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
                32 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
                33 },
                34 method: "POST",
                35 body: JSON.stringify(payload),
                36 });
                37 return handleResponse(response);
                38};
                • Line 5 calls the createOrder function and uses the cart information from the front-end to calculate the purchase units for the order.
                • Line 12 establishes a listener to capture the accessToken from the generateAccessToken() function later in the API call.
                • Lines 13-38 create an order by sending a POST request to the Orders v2 API, using the accessToken.

                See the Create order endpoint of the PayPal Orders v2 API for sample responses and other details.

                Processor response codes

                Payment processors return processor response codes when they receive a transaction request. For advanced card payments, the code displays in the authorization object under the response_code field.

                  1"processor_response": {
                  2 "avs_code": "Y",
                  3 "cvv_code": "S",
                  4 "payment_advice_code": "",
                  5 "response_code": "0000"
                  6}

                  If an external payment processor declines a transaction, PayPal returns a HTTP 201 CREATED status code and a status of DECLINED in the capture status.

                  See the Orders API response_code object to get the processor response code for the non-PayPal payment processor errors.

                  Capture payment

                  Capture an order to move money from the payer to the merchant by making a POST call to the /v2/checkout/orders/ORDER-ID/capture endpoint.

                    1/**
                    2 * Capture payment for the created order to complete the transaction.
                    3 * @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
                    4 */
                    5const captureOrder = async (orderID) => {
                    6 const accessToken = await generateAccessToken();
                    7 const url = `${base}/v2/checkout/orders/${orderID}/capture`;
                    8
                    9 const response = await fetch(url, {
                    10 method: "POST",
                    11 headers: {
                    12 "Content-Type": "application/json",
                    13 Authorization: `Bearer ${accessToken}`,
                    14 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
                    15 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
                    16 // "PayPal-Mock-Response": '{"mock_application_codes": "INSTRUMENT_DECLINED"}'
                    17 // "PayPal-Mock-Response": '{"mock_application_codes": "TRANSACTION_REFUSED"}'
                    18 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
                    19 },
                    20 });
                    • Line 5 establishes a listener to capture the accessToken from the generateAccessToken() function later in the API call.
                    • Line 7 declares the URL of the Capture API endpoint using the Order ID generated from the Create Order endpoint.
                    • Lines 9-13 define a response that makes a POST call to the /v2/checkout/orders/ORDER-ID/capture endpoint to capture the order, using the accessToken.
                    • Lines 16-18 include mock responses for negative testing in the sandbox.

                    Sample capture response

                    This code sample shows a response to a POST call to /v2/checkout/orders/ORDER-ID/capture. The response is the orderData retrieved in the create order section.

                      1{
                      2 "id": "5O190127TN364715T",
                      3 "status": "COMPLETED",
                      4 "payment_source": {
                      5 "paypal": {
                      6 "name": {
                      7 "given_name": "Firstname",
                      8 "surname": "Lastname"
                      9 },
                      10 "email_address": "payer@example.com",
                      11 "account_id": "QYR5Z8XDVJNXQ",
                      12 },
                      13 },
                      14 "purchase_units": [
                      15 {
                      16 "reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",
                      17 "shipping": {
                      18 "address": {
                      19 "address_line_1": "123 Main St.",
                      20 "admin_area_2": "Anytown",
                      21 "admin_area_1": "CA",
                      22 "postal_code": "12345",
                      23 "country_code": "US",
                      24 },
                      25 },
                      26 "payments": {
                      27 "captures": [
                      28 {
                      29 "id": "3C679366HH908993F",
                      30 "status": "COMPLETED",
                      31 "amount": {
                      32 "currency_code": "USD",
                      33 "value": "100.00",
                      34 },
                      35 "seller_protection": {
                      36 "status": "ELIGIBLE",
                      37 "dispute_categories": [
                      38 "ITEM_NOT_RECEIVED",
                      39 "UNAUTHORIZED_TRANSACTION",
                      40 ],
                      41 },
                      42 "final_capture": true,
                      43 "disbursement_mode": "INSTANT",
                      44 "seller_receivable_breakdown": {
                      45 "gross_amount": {
                      46 "currency_code": "USD",
                      47 "value": "100.00",
                      48 },
                      49 "paypal_fee": {
                      50 "currency_code": "USD",
                      51 "value": "3.00",
                      52 },
                      53 "net_amount": {
                      54 "currency_code": "USD",
                      55 "value": "97.00",
                      56 },
                      57 },
                      58 "create_time": "2018-04-01T21:20:49Z",
                      59 "update_time": "2018-04-01T21:20:49Z",
                      60 "links": [
                      61 {
                      62 "href": "https://api-m.paypal.com/v2/payments/captures/3C679366HH908993F",
                      63 "rel": "self",
                      64 "method": "GET",
                      65 },
                      66 {
                      67 "href": "https://api-m.paypal.com/v2/payments/captures/3C679366HH908993F/refund",
                      68 "rel": "refund",
                      69 "method": "POST",
                      70 },
                      71 ],
                      72 },
                      73 ],
                      74 },
                      75 },
                      76 ],
                      77 "payer": {
                      78 "name": {
                      79 "given_name": "Firstname",
                      80 "surname": "Lastname",
                      81 },
                      82 "email_address": "payer@example.com",
                      83 "payer_id": "QYR5Z8XDVJNXQ",
                      84 },
                      85 "links": [
                      86 {
                      87 "href": "https://api-m.paypal.com/v2/checkout/orders/5O190127TN364715T",
                      88 "rel": "self",
                      89 "method": "GET",
                      90 },
                      91 ],
                      92}
                      • Line 2 shows the ID for this orderData object.
                      • Lines 4-13 pass details about the payment source.
                      • Lines 14-76 pass the purchase_units in this transaction. Each purchase unit represents either a full or partial order and establishes a contract between a payer and a merchant.
                      • Line 16 passes the reference_id that identifies the purchase unit in this payment response.
                      • Lines 17-24 pass details about the shipping address.
                      • Line 26 declares the payments object that passes the payment details for this capture request.
                      • Line 27 declares the captures object that passes details about the captured payments for this request.
                      • Lines 28-57 pass the payment capture details, such as the capture identifier id, amount, disbursement_mode, and net_amount.
                      • Lines 60-71 pass the HATEOAS details of the capture response. See the REST API Response Reference for more details about HATEOAS.
                      • Lines 77-84 pass details about the payer.
                      • Lines 85-91 pass the HATEOAS details for the orders response.

                      Handle responses

                      The handleResponse function sets up a listener for API responses.

                        1async function handleResponse(response) {
                        2 try {
                        3 const jsonResponse = await response.json();
                        4 return {
                        5 jsonResponse,
                        6 httpStatusCode: response.status,
                        7 };
                        8 } catch (err) {
                        9 const errorMessage = await response.text();
                        10 throw new Error(errorMessage);
                        11 }
                        12 }
                        13
                        14 app.post("/api/orders", async (req, res) => {
                        15 try {
                        16 // use the cart information passed from the front-end to calculate the order amount detals
                        17 const { cart } = req.body;
                        18 const { jsonResponse, httpStatusCode } = await createOrder(cart);
                        19 res.status(httpStatusCode).json(jsonResponse);
                        20 } catch (error) {
                        21 console.error("Failed to create order:", error);
                        22 res.status(500).json({ error: "Failed to create order." });
                        23 }
                        24 });
                        25
                        26 app.post("/api/orders/:orderID/capture", async (req, res) => {
                        27 try {
                        28 const { orderID } = req.params;
                        29 const { jsonResponse, httpStatusCode } = await captureOrder(orderID);
                        30 res.status(httpStatusCode).json(jsonResponse);
                        31 } catch (error) {
                        32 console.error("Failed to create order:", error);
                        33 res.status(500).json({ error: "Failed to capture order." });
                        34 }
                        35 });
                        • Line 1 creates a function which returns a HTTP status code response. Error status codes send an error message.
                        • Line 14 makes a POST call to the api/orders/ endpoint and returns an HTTP status code response. Error status codes send an error message.
                        • Line 26 makes a POST call to the api/orders/:orderID/capture endpoint and returns an HTTP status code response for the particular order. Error status codes send an error message.
                        3

                        Integrate front end

                        Set up your front-end to integrate standard checkout payments.

                        Front-end process

                        1. Your app displays the PayPal checkout buttons.
                        2. Your app calls server endpoints to create the order and capture payment.

                        Front-end code

                        This example uses the checkout.html file to show how to set up the front end to integrate standard payments.

                        /client/checkout.html handles the client-side logic and defines how the PayPal front-end components connect with the back end. Use this file to set up the PayPal checkout using the JavaScript SDK and handle the payer's interactions with the PayPal checkout button.

                        1. /client/checkout.html
                        2. /client/app.js
                        1<html lang="en">
                        2 <head>
                        3 <meta charset="UTF-8">
                        4 <meta name="viewport" content="width=device-width, initial-scale=1.0">
                        5 <title>PayPal JS SDK Standard Integration</title>
                        6 </head>
                        7 <body>
                        8 <div id="paypal-button-container"></div>
                        9 <p id="result-message"></p>
                        10 <!-- Replace the "test" client-id value with your client-id -->
                        11 <script src="https://www.paypal.com/sdk/js?client-id=test&currency=USD"></script>
                        12 <script src="app.js"></script>
                        13 </body>
                        14</html>

                        Understand the front-end code

                        This section explains the front-end code samples, checkout.html and app.js.

                        Understand the checkout.html code sample

                        The following section explains key parts of the checkout.html code sample.

                        PayPal Buttons JavaScript

                        This code sample calls the PayPal JavaScript SDK.

                          1<!DOCTYPE html>
                          2<html lang="en">
                          3 <head>
                          4 <meta charset="UTF-8">
                          5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
                          6 <title>PayPal JS SDK Standard Integration</title>
                          7 </head>
                          8 <body>
                          9 <div id="paypal-button-container"></div>
                          10 <p id="result-message"></p>
                          11 <!-- Replace the "test" client-id value with your client-id -->
                          12 <script src="https://www.paypal.com/sdk/js?client-id=test&currency=USD"></script>
                          13 <script src="app.js"></script>
                          14 </body>
                          15</html>
                          • Line 9 displays the PayPal buttons.
                          • Line 10 displays the transaction results.
                          • Line 12 calls the PayPal SDK.
                          • line 13 runs the app.js script to start a checkout transaction.

                          Understand the app.js code sample

                          The following section explains key parts of the app.js code sample.

                          PayPal Buttons JavaScript

                          This code sample calls the JavaScript SDK that defines the PayPal buttons.

                            1window.paypal.Buttons({{}}).render("#paypal-button-container");
                            • Line 3 creates the order by calling the api/orders endpoint
                            • Lines 10-15 pass the SKU and quantity for the product in the cart. See the createOrder section of the JavaScript SDK reference guide for more information about creating orders. See the Orders V2 REST API documentation for additional customization options.

                            Create order for PayPal Button

                            This code sample defines the createOrder() function.

                              1async createOrder() {
                              2 try {
                              3 const response = await fetch("/api/orders", {
                              4 method: "POST",
                              5 headers: {
                              6 "Content-Type": "application/json",
                              7 },
                              8 // use the "body" param to optionally pass additional order information
                              9 // like product ids and quantities
                              10 body: JSON.stringify({
                              11 cart: [
                              12 {
                              13 id: "YOUR_PRODUCT_ID",
                              14 quantity: "YOUR_PRODUCT_QUANTITY",
                              15 },
                              16 ],
                              17 }),
                              18 });
                              19
                              20 const orderData = await response.json();
                              21
                              22 if (orderData.id) {
                              23 return orderData.id;
                              24 } else {
                              25 const errorDetail = orderData?.details?.[0];
                              26 const errorMessage = errorDetail
                              27 ? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})`
                              28 : JSON.stringify(orderData);
                              29
                              30 throw new Error(errorMessage);
                              31 }
                              32 } catch (error) {
                              33 console.error(error);
                              34 resultMessage(`Could not initiate PayPal Checkout...<br><br>${error}`);
                              35 }
                              36},
                              • Line 3 creates the order by calling the api/orders endpoint
                              • Lines 10-15 pass the SKU and quantity for the product in the cart. See the createOrder section of the JavaScript SDK reference guide for more information about creating orders. See the Orders REST API documentation for additional customization options.

                              Capture payment when approved

                              This code sample defines a POST call to /api/orders/orderID/capture. The orderID is passed from the SDK when you run the onCreate() call.

                                1async onApprove(data, actions) {
                                2try {
                                3 const response = await fetch(/api/orders / $ {
                                4 data.orderID
                                5 }
                                6 /capture, {
                                7 method: "POST", headers: {
                                8 "Content-Type": "application/json",
                                9 },
                                10 });
                                11const orderData = await response.json();
                                12// Three cases to handle:
                                13// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                                14// (2) Other non-recoverable errors -> Show a failure message
                                15// (3) Successful transaction -> Show confirmation or thank you message
                                16const errorDetail = orderData?.details?.[0];
                                17if(errorDetail?.issue === "INSTRUMENT_DECLINED") {
                                18 // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                                19 // recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/
                                20 return actions.restart();
                                21} else if(errorDetail) {
                                22 // (2) Other non-recoverable errors -> Show a failure message
                                23 throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
                                24} else if(!orderData.purchase_units) {
                                25 throw new Error(JSON.stringify(orderData));
                                26} else {
                                27 // (3) Successful transaction -> Show confirmation or thank you message
                                28 // Or go to another URL: actions.redirect('thank_you.html');
                                29 const transaction = orderData?.purchase_units?.[0]?.payments?.captures?.[0] || orderData?.purchase_units?.[0]?.payments?.authorizations?.[0];
                                30 resultMessage(`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`, );
                                31 console.log("Capture result", orderData, JSON.stringify(orderData, null, 2), );
                                32}
                                33} catch (error) {
                                34console.error(error);
                                35resultMessage(`Sorry, your transaction could not be processed...<br><br>${error}`, );
                                36}
                                37},
                                38})
                                • Line 3 captures the order by calling the api/orders/OrderID/capture endpoint
                                • Line 11 sets up a listener for responses from the api/orders/capture call.
                                • Lines 16-30 parse the order details from the response.
                                • Line 31 calls a response function to display the transaction results.

                                Display transaction results

                                This code sample defines an example resultMessage() function.

                                  1// Example function to show a result to the user. Your site's UI library can be used instead.function resultMessage(message) {const container = document.querySelector("#result-message");container.innerHTML = message;

                                  Render the PayPal buttons

                                  This code sample renders the PayPal buttons.

                                    1.render("#paypal-button-container");

                                    Run your app

                                    • Run npm start to run your server again.
                                    • Open your browser and navigate to localhost:8888
                                    • When your server is running, proceed to the next section to test your integration.

                                    Design guidance

                                    • Show the PayPal button on all pages that start the checkout process.
                                    • Give PayPal equal prominence and function for your payers alongside all other acceptance marks, including cards, split tender, buy online, and pickup in-store.
                                    • It should take no more than 2 steps for your payer to pay and complete their order when they return to your site.
                                    • Leave room on your checkout page for the Debit or Credit Card button to expand. If you place the payment buttons too low on the page, payers won't be able to access the drop-down credit card form fields.
                                    • If the Debit or Credit Card drop-down form isn't available when your payers check out, the PayPal guest checkout shows up in a pop-up window.
                                    Design guidance
                                    4

                                    Test integration

                                    Before going live, test your integration in the sandbox environment.

                                    Learn more about the following resources on card testing:

                                    Note: Use the credit card generator to generate test credit cards for sandbox testing.

                                    Test the following use cases before going live:


                                    Test a purchase as a PayPal payer

                                    1. Select the PayPal button on your checkout page.
                                    2. Log in using one of your personal sandbox accounts. This ensures the payments will be sent to the correct account. Make sure that you use the sandbox business account that corresponds to the REST app you are using.
                                    3. Note the purchase amount in the PayPal checkout window.
                                    4. Approve the purchase with the Pay Now button. The PayPal window closes and redirects you to your page, indicating that the transaction was completed.


                                    Confirm the money reached the business account

                                    1. Log in to the PayPal sandbox using the sandbox business account that received the payment. Remember that the SDK source now uses a sandbox client ID from one of your own REST apps, and not the default test ID.
                                    2. In Recent Activity, confirm that the sandbox business account received the money, subtracting any fees.
                                    3. Log out of the account.


                                    Test a purchase as a card payment

                                    1. Go to the checkout page for your integration.
                                    2. Generate a test card using the credit card generator.
                                    3. Enter the card details in the hosted field, including the name on the card, billing address, and 2-character country code. Then, submit the order.
                                    4. Confirm that the order was processed.
                                    5. Log in to your merchant sandbox account and navigate to the activity page to ensure the payment amount shows up in the account.
                                    5

                                    Go live

                                    Follow this checklist to take your application live:

                                    1. Log into the PayPal Developer Dashboard with your PayPal business account.
                                    2. Obtain your live credentials.
                                    3. Include the new credentials in your integration and update your PayPal endpoint.

                                    See Move your app to production for more details.