Integrate PayPal Checkout

SDKCurrentStandardPartner

Last updated: Mar 20th, 5:06pm

How it works

Integrate standard Checkout to show the PayPal payment buttons. To extend your integration, see Customize your payers' 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

Know before you code

Required
You need the following to use this integration:

Get access tokenOnboard sellers

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.

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/server.js",
    6 "type": "module",
    7 "scripts":
    8 {
    9 "test": "echo \"Error: no test specified\" && exit 1",
    10 "start": "nodemon server/server.js",
    11 "format": "npx prettier --write **/*.{js,md}",
    12 "format:check": "npx prettier --check **/*.{js,md}",
    13 "lint": "npx eslint server/*.js --env=node && npx eslint client/*.js --env=browser"
    14 },
    15 "license": "Apache-2.0",
    16 "dependencies":
    17 {
    18 "dotenv": "^16.3.1",
    19 "express": "^4.18.2",
    20 "node-fetch": "^3.3.2"
    21 },
    22 "devDependencies":
    23 {
    24 "nodemon": "^3.0.1"
    25 }
    26}

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

    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.
      3. You can also customize the way you capture money. For example, you can use Authorize and Capture which places a hold on buyer’s money until the buyer is authorized. See the pages under Customize for more information.

      Back-end code

      This sample shows how to generate a PayPal-Auth-Assertion header, add it to a node.js file, and set up a /server/server.js file to integrate with standard payments.

      1. Generate a PayPal-Auth-Assertion header

      Pass the PayPal-Auth-Assertion header with standard Content-Type, Authorization, and PayPal-Request-ID headers. Copy and modify the following code to generate the PayPal-Auth-Assertion header.

        1const clientId = "CLIENT-ID";
        2const sellerPayerId = "SELLER-PAYER-ID"
        3const jwt = getAuthAssertionValue(clientId, sellerPayerId);
        4console.log(jwt);
        5
        6function getAuthAssertionValue(clientId, sellerPayerId) {
        7 const header = {
        8 "alg": "none"
        9 };
        10 const encodedHeader = base64url(header);
        11 const payload = {
        12 "iss": clientId,
        13 "payer_id": sellerPayerId
        14 };
        15 const encodedPayload = base64url(payload);
        16 return `${encodedHeader}.${encodedPayload}.`;
        17}
        18
        19function base64url(json) {
        20 return btoa(JSON.stringify(json))
        21 .replace(/=+$/, '')
        22 .replace(/\+/g, '-')
        23 .replace(/\//g, '_');
        24}

        The preceding example contains period characters ( . ), which are required.

        Modify the code:

        • Replace CLIENT-ID with the client ID of the platform or marketplace from the PayPal developer dashboard.
        • Replace SELLER-PAYER-ID with the payer ID or the email of the receiving seller's PayPal account.

        Example functions to generate the PayPal-Auth-Assertion header:

          1const clientID = "Acuy17p2LcOf9RMv8SUVBb3wic3FPEP2NHFFqfSCBRFrNFdmbC1JQ0w8HIKRxW3RDy2R8QTL93eptFYl";
          2const merchantIDOrEmail = "identity_seller@paypal.com";
          3const auth1 = Buffer.from('\{"alg":"none"\}').toString(\"base64\");
          4const auth2 = Buffer.from(\`\{\"iss\":$\{clientID\},\"payer_id\":$\{merchantIDOrEmail\}\}\`).toString(\"base64\");
          5const authAssertionHeader = \`$\{auth1\}.$\{auth2\}.\`

          2. Set up your back end

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

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

            Understand the server.js code sample

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

            In the back-end code, you’ll need to modify as follows:

            • Change BN-CODE to your PayPal Partner Attribution ID.
            • Change ACCESS-TOKEN to your access token.
            • Change PAYPAL-AUTH-ASSERTION to the Paypal-auth-assertion header you generated in the Generate a PayPal-Auth-Assertion header section.

            Declare imports

            This section of code imports the dotenv dependency, the express module, the node fetch module and the path module.

              1import express from "express";
              2import fetch from "node-fetch";
              3import "dotenv/config";
              4import path from "path";

              Set up port and server

              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
                  4// parse post params sent in body in json format
                  5app.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{
                    7 try
                    8 {
                    9 if (!PAYPAL_CLIENT_ID || !PAYPAL_CLIENT_SECRET)
                    10 {
                    11 throw new Error("MISSING_API_CREDENTIALS");
                    12 }
                    13 const auth = Buffer.from(
                    14 PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET,
                    15 ).toString("base64");
                    16 const response = await fetch(`${base}/v1/oauth2/token`,
                    17 {
                    18 method: "POST",
                    19 body: "grant_type=client_credentials",
                    20 headers:
                    21 {
                    22 Authorization: `Basic ${auth}`,
                    23 },
                    24 });
                    25
                    26 const data = await response.json();
                    27 return data.access_token;
                    28 }
                    29 catch (error)
                    30 {
                    31 console.error("Failed to generate Access Token:", error);
                    32 }
                    33};
                    • Lines 10-12 combine 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 21-22 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.

                    If you process payments that require Strong Customer Authentication, you need to provide additional context with payment indicators.

                      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{
                      7 // use the cart information passed from the front-end to calculate the purchase unit details
                      8 console.log(
                      9 "shopping cart information passed from the frontend createOrder() callback:",
                      10 cart,
                      11 );
                      12
                      13 const accessToken = await generateAccessToken();
                      14 const url = `${base}/v2/checkout/orders`;
                      15 const payload = {
                      16 intent: "CAPTURE",
                      17 purchase_units: [
                      18 {
                      19 amount:
                      20 {
                      21 currency_code: "USD",
                      22 value: "100.00",
                      23 },
                      24 }, ],
                      25 };
                      26
                      27 const response = await fetch(url,
                      28 {
                      29 headers:
                      30 {
                      31 "Content-Type": "application/json",
                      32 Authorization: `Bearer ${accessToken}`,
                      33 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
                      34 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
                      35 // "PayPal-Mock-Response": '{"mock_application_codes": "MISSING_REQUIRED_PARAMETER"}'
                      36 // "PayPal-Mock-Response": '{"mock_application_codes": "PERMISSION_DENIED"}'
                      37 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
                      38 “PayPal - Partner - Attribution - Id”:BN - CODE,
                      39 “PayPal - Auth - Assertion”:PAYPAL - AUTH - ASSERTION,
                      40 },
                      41 method: "POST",
                      42 body: JSON.stringify(payload),
                      43 });
                      44
                      45 return handleResponse(response);
                      46};
                      • 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.

                      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 {
                          7 const accessToken = await generateAccessToken();
                          8 const url = `${base}/v2/checkout/orders/${orderID}/capture`;
                          9
                          10 const response = await fetch(url,
                          11 {
                          12 method: "POST",
                          13 headers:
                          14 {
                          15 "Content-Type": "application/json",
                          16 Authorization: `Bearer ${accessToken}`,
                          17 // Uncomment one of these to force an error for negative testing (in sandbox mode only). Documentation:
                          18 // https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/
                          19 // "PayPal-Mock-Response": '{"mock_application_codes": "INSTRUMENT_DECLINED"}'
                          20 // "PayPal-Mock-Response": '{"mock_application_codes": "TRANSACTION_REFUSED"}'
                          21 // "PayPal-Mock-Response": '{"mock_application_codes": "INTERNAL_SERVER_ERROR"}'
                          22 “PayPal - Partner - Attribution - Id”:BN - CODE,
                          23 “PayPal - Auth - Assertion”:PAYPAL - AUTH - ASSERTION,
                          24 },
                          25 });
                          • Line 5 establishes a listener to capture the accessToken from the generateAccessToken() function later in the API call.
                          • Line 8 declares the URL of the Capture API endpoint using the Order ID generated from the Create Order endpoint.
                          • Lines 10-16 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 19-21 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 {
                            6 "paypal":
                            7 {
                            8 "name":
                            9 {
                            10 "given_name": "Firstname",
                            11 "surname": "Lastname"
                            12 },
                            13 "email_address": "payer@example.com",
                            14 "account_id": "QYR5Z8XDVJNXQ",
                            15 },
                            16 },
                            17 "purchase_units": [
                            18 {
                            19 "reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",
                            20 "shipping":
                            21 {
                            22 "address":
                            23 {
                            24 "address_line_1": "123 Main St.",
                            25 "admin_area_2": "Anytown",
                            26 "admin_area_1": "CA",
                            27 "postal_code": "12345",
                            28 "country_code": "US",
                            29 },
                            30 },
                            31 "payments":
                            32 {
                            33 "captures": [
                            34 {
                            35 "id": "3C679366HH908993F",
                            36 "status": "COMPLETED",
                            37 "amount":
                            38 {
                            39 "currency_code": "USD",
                            40 "value": "100.00",
                            41 },
                            42 "seller_protection":
                            43 {
                            44 "status": "ELIGIBLE",
                            45 "dispute_categories": [
                            46 "ITEM_NOT_RECEIVED",
                            47 "UNAUTHORIZED_TRANSACTION",
                            48 ],
                            49 },
                            50 "final_capture": true,
                            51 "disbursement_mode": "INSTANT",
                            52 "seller_receivable_breakdown":
                            53 {
                            54 "gross_amount":
                            55 {
                            56 "currency_code": "USD",
                            57 "value": "100.00",
                            58 },
                            59 "paypal_fee":
                            60 {
                            61 "currency_code": "USD",
                            62 "value": "3.00",
                            63 },
                            64 "net_amount":
                            65 {
                            66 "currency_code": "USD",
                            67 "value": "97.00",
                            68 },
                            69 },
                            70 "create_time": "2018-04-01T21:20:49Z",
                            71 "update_time": "2018-04-01T21:20:49Z",
                            72 "links": [
                            73 {
                            74 "href": "https://api-m.paypal.com/v2/payments/captures/3C679366HH908993F",
                            75 "rel": "self",
                            76 "method": "GET",
                            77 },
                            78 {
                            79 "href": "https://api-m.paypal.com/v2/payments/captures/3C679366HH908993F/refund",
                            80 "rel": "refund",
                            81 "method": "POST",
                            82 }, ],
                            83 }, ],
                            84 },
                            85 }, ],
                            86 "payer":
                            87 {
                            88 "name":
                            89 {
                            90 "given_name": "Firstname",
                            91 "surname": "Lastname",
                            92 },
                            93 "email_address": "payer@example.com",
                            94 "payer_id": "QYR5Z8XDVJNXQ",
                            95 },
                            96 "links": [
                            97 {
                            98 "href": "https://api-m.paypal.com/v2/checkout/orders/5O190127TN364715T",
                            99 "rel": "self",
                            100 "method": "GET",
                            101 }, ],
                            102}
                            • Line 2 shows the ID for this orderData object.
                            • Lines 4-16 pass details about the payment source.
                            • Lines 17-85 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 19 passes the reference_id that identifies the purchase unit in this payment response.
                            • Lines 22-29 pass details about the shipping address.
                            • Line 31 declares the payments object that passes the payment details for this capture request.
                            • Line 37 declares the captures object that passes details about the captured payments for this request.
                            • Lines 33-83 pass the payment capture details, such as the capture identifier id, amount, disbursement_mode, and net_amount.
                            • Lines 72-82 pass the HATEOAS details of the capture response. See the REST API Response Reference for more details about HATEOAS.
                            • Lines 86-95 pass details about the payer.
                            • Lines 96-101 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
                              14app.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
                              26app.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. Errors 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={CLIENT_ID}&merchant-id={SELLER_PAYER_ID}&components=buttons" data-partner-attribution-id="{PARTNER_BN_CODE}"></script>
                              12 <script src="app.js"></script>
                              13 </body>
                              14</html>

                              checkout.html

                              This section explains 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={CLIENT_ID}&merchant-id={SELLER_PAYER_ID}&components=buttons" data-partner-attribution-id="{PARTNER_BN_CODE}"></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.

                                app.js

                                This section explains 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”);

                                  Create order for PayPal Button

                                  This code sample defines the createOrder() function.

                                    1async createOrder() {
                                    2try {
                                    3const response = await fetch("/api/orders", {
                                    4method: "POST",
                                    5headers: {
                                    6"Content-Type": "application/json",
                                    7 "Authorization”: “Bearer ACCESS-TOKEN",
                                    8 “PayPal-Partner-Attribution-Id”:BN-CODE
                                    9 “PayPal-Auth-Assertion”:PAYPAL-AUTH-ASSERTION
                                    10},
                                    11// use the "body" param to optionally pass additional order information
                                    12// like product ids and quantities
                                    13body: JSON.stringify({
                                    14cart: [
                                    15{
                                    16id: "YOUR_PRODUCT_ID",
                                    17quantity: "YOUR_PRODUCT_QUANTITY",
                                    18},
                                    19],
                                    20}),
                                    21});
                                    22
                                    23

 const orderData = await response.json();
                                    24
                                    25

 if (orderData.id) {
                                    26return orderData.id;
                                    27} else {
                                    28const errorDetail = orderData?.details?.[0];
                                    29const errorMessage = errorDetail
                                    30? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})`
                                    31: JSON.stringify(orderData);
                                    32
                                    33

 throw new Error(errorMessage);
                                    34}
                                    35} catch (error) {
                                    36console.error(error);
                                    37resultMessage(`Could not initiate PayPal Checkout...<br><br>${error}`);
                                    38}
                                    39},
                                    • Line 3 creates the order by calling the api/orders endpoint
                                    • Lines 14-21 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) {
                                      2 try {
                                      3 const response = await fetch(/api/orders / $ {
                                      4 data.orderID
                                      5 }
                                      6 /capture, {
                                      7 method:POST,
                                      8 headers: {
                                      9 “Content - Type”: “application / json”,
                                      10 "Authorization”: “Bearer ACCESS-TOKEN",
                                      11 “PayPal - Partner - Attribution - Id”:BN - CODE”“ PayPal - Auth - Assertion”:PAYPAL - AUTH - ASSERTION
                                      12 },
                                      13 }
                                      14 );
                                      15 const orderData = await response.json();
                                      16 // Three cases to handle:
                                      17 // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                                      18 // (2) Other non-recoverable errors -> Show a failure message
                                      19 // (3) Successful transaction -> Show confirmation or thank you message
                                      20
                                      21 const errorDetail = orderData?.details?.[0];
                                      22
                                      23 if (errorDetail?.issue === "INSTRUMENT_DECLINED") {
                                      24 // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                                      25 // recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/
                                      26 return actions.restart();
                                      27 }
                                      28 else if (errorDetail) {
                                      29 // (2) Other non-recoverable errors -> Show a failure message
                                      30 throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
                                      31 }
                                      32 else if (!orderData.purchase_units) {
                                      33 throw new Error(JSON.stringify(orderData));
                                      34 }
                                      35 else {
                                      36 // (3) Successful transaction -> Show confirmation or thank you message
                                      37 // Or go to another URL: actions.redirect('thank_you.html');
                                      38 const transaction =
                                      39 orderData?.purchase_units?.[0]?.payments?.captures?.[0] ||
                                      40 orderData?.purchase_units?.[0]?.payments?.authorizations?.[0];
                                      41 resultMessage(
                                      42 `Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`,
                                      43 );
                                      44 console.log(
                                      45 "Capture result",
                                      46 orderData,
                                      47 JSON.stringify(orderData, null, 2),
                                      48 );
                                      49 }
                                      50}
                                      51catch (error) {
                                      52 console.error(error);
                                      53 resultMessage(
                                      54 `Sorry, your transaction could not be processed...<br><br>${error}`,
                                      55 );
                                      56}
                                      57},
                                      58})
                                      • Line 3 creates the order by calling the api/orders/capture endpoint
                                      • Line 11 sets up a listener for responses from the api/orders/capture call.
                                      • Lines 17-43 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:

                                          Test the following use cases before going live:


                                          Test a purchase as a 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.