diff --git a/advanced-integration/README.md b/advanced-integration/README.md index 923a5234..a444deb8 100644 --- a/advanced-integration/README.md +++ b/advanced-integration/README.md @@ -1,9 +1,14 @@ -# Advanced Integration Example +# Advanced Checkout Integration Example -This folder contains example code for an Advanced PayPal integration using both the JS SDK and Node.js to complete transactions with the PayPal REST API. +This folder contains example code for an advanced Checkout PayPal integration using both the JavaScript SDK and Node.js to complete transactions with the PayPal REST API. + +* `v2` contains sample code for the current advanced Checkout integration. This includes guidance on using Hosted Card Fields. +* `v1` contains sample code for the legacy advanced Checkout integration. Use `v2` for new integrations. ## Instructions +These instructions apply to the sample code for both `v2` and `v1`: + 1. Rename `.env.example` to `.env` and update `PAYPAL_CLIENT_ID` and `PAYPAL_CLIENT_SECRET`. 2. Run `npm install` 3. Run `npm start` diff --git a/advanced-integration/package.json b/advanced-integration/package.json deleted file mode 100644 index d17aa000..00000000 --- a/advanced-integration/package.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "paypal-advanced-integration", - "description": "Sample Node.js web app to integrate PayPal Advanced Checkout for online payments", - "version": "1.0.0", - "main": "server/server.js", - "type": "module", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "nodemon server.js", - "format": "npx prettier --write **/*.{js,md}", - "format:check": "npx prettier --check **/*.{js,md}", - "lint": "npx eslint server.js paypal-api.js --env=node && npx eslint public/*.js --env=browser" - }, - "license": "Apache-2.0", - "dependencies": { - "dotenv": "^16.3.1", - "ejs": "^3.1.9", - "express": "^4.18.2", - "node-fetch": "^3.3.2" - }, - "devDependencies": { - "nodemon": "^3.0.1" - } -} diff --git a/advanced-integration/paypal-api.js b/advanced-integration/paypal-api.js deleted file mode 100644 index 6e6c8aaf..00000000 --- a/advanced-integration/paypal-api.js +++ /dev/null @@ -1,100 +0,0 @@ -import fetch from "node-fetch"; - -// set some important variables -const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET } = process.env; -const base = "https://api-m.sandbox.paypal.com"; - -/** - * Create an order - * @see https://developer.paypal.com/docs/api/orders/v2/#orders_create - */ -export async function createOrder() { - const purchaseAmount = "100.00"; // TODO: pull prices from a database - const accessToken = await generateAccessToken(); - const url = `${base}/v2/checkout/orders`; - const response = await fetch(url, { - method: "post", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${accessToken}`, - }, - body: JSON.stringify({ - intent: "CAPTURE", - purchase_units: [ - { - amount: { - currency_code: "USD", - value: purchaseAmount, - }, - }, - ], - }), - }); - - return handleResponse(response); -} - -/** - * Capture payment for an order - * @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture - */ -export async function capturePayment(orderId) { - const accessToken = await generateAccessToken(); - const url = `${base}/v2/checkout/orders/${orderId}/capture`; - const response = await fetch(url, { - method: "post", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${accessToken}`, - }, - }); - - return handleResponse(response); -} - -/** - * Generate an OAuth 2.0 access token - * @see https://developer.paypal.com/api/rest/authentication/ - */ -export async function generateAccessToken() { - const auth = Buffer.from( - PAYPAL_CLIENT_ID + ":" + PAYPAL_CLIENT_SECRET, - ).toString("base64"); - const response = await fetch(`${base}/v1/oauth2/token`, { - method: "post", - body: "grant_type=client_credentials", - headers: { - Authorization: `Basic ${auth}`, - }, - }); - const jsonData = await handleResponse(response); - return jsonData.access_token; -} - -/** - * Generate a client token - * @see https://developer.paypal.com/docs/checkout/advanced/integrate/#link-sampleclienttokenrequest - */ -export async function generateClientToken() { - const accessToken = await generateAccessToken(); - const response = await fetch(`${base}/v1/identity/generate-token`, { - method: "post", - headers: { - Authorization: `Bearer ${accessToken}`, - "Accept-Language": "en_US", - "Content-Type": "application/json", - }, - }); - console.log("response", response.status); - const jsonData = await handleResponse(response); - return jsonData.client_token; -} - -async function handleResponse(response) { - if (response.status === 200 || response.status === 201) { - return response.json(); - } - - const errorMessage = await response.text(); - throw new Error(errorMessage); -} diff --git a/advanced-integration/server.js b/advanced-integration/server.js deleted file mode 100644 index 73076fcb..00000000 --- a/advanced-integration/server.js +++ /dev/null @@ -1,44 +0,0 @@ -import "dotenv/config"; -import express from "express"; -import * as paypal from "./paypal-api.js"; -const { PORT = 8888 } = process.env; - -const app = express(); -app.set("view engine", "ejs"); -app.use(express.static("public")); - -// render checkout page with client id & unique client token -app.get("/", async (req, res) => { - const clientId = process.env.PAYPAL_CLIENT_ID; - try { - const clientToken = await paypal.generateClientToken(); - res.render("checkout", { clientId, clientToken }); - } catch (err) { - res.status(500).send(err.message); - } -}); - -// create order -app.post("/api/orders", async (req, res) => { - try { - const order = await paypal.createOrder(); - res.json(order); - } catch (err) { - res.status(500).send(err.message); - } -}); - -// capture payment -app.post("/api/orders/:orderID/capture", async (req, res) => { - const { orderID } = req.params; - try { - const captureData = await paypal.capturePayment(orderID); - res.json(captureData); - } catch (err) { - res.status(500).send(err.message); - } -}); - -app.listen(PORT, () => { - console.log(`Server listening at http://localhost:${PORT}/`); -}); diff --git a/advanced-integration/.env.example b/advanced-integration/v1/.env.example similarity index 100% rename from advanced-integration/.env.example rename to advanced-integration/v1/.env.example diff --git a/advanced-integration/v1/README.md b/advanced-integration/v1/README.md index 923a5234..4e5b1442 100644 --- a/advanced-integration/v1/README.md +++ b/advanced-integration/v1/README.md @@ -1,11 +1,14 @@ # Advanced Integration Example -This folder contains example code for an Advanced PayPal integration using both the JS SDK and Node.js to complete transactions with the PayPal REST API. +This folder contains example code for [version 1](https://developer.paypal.com/docs/checkout/advanced/integrate/sdk/v1) of an advanced Checkout PayPal integration using the JavaScript SDK and Node.js to complete transactions with the PayPal REST API. + +> **Note:** Version 1 is a legacy integration. Use [version 2](https://developer.paypal.com/docs/checkout/advanced/integrate/) for new integrations. ## Instructions -1. Rename `.env.example` to `.env` and update `PAYPAL_CLIENT_ID` and `PAYPAL_CLIENT_SECRET`. -2. Run `npm install` -3. Run `npm start` -4. Open http://localhost:8888 -5. Enter the credit card number provided from one of your [sandbox accounts](https://developer.paypal.com/dashboard/accounts) or [generate a new credit card](https://developer.paypal.com/dashboard/creditCardGenerator) +1. [Create an application](https://developer.paypal.com/dashboard/applications/sandbox/create). +2. Rename `.env.example` to `.env` and update `PAYPAL_CLIENT_ID` and `PAYPAL_CLIENT_SECRET`. +3. Run `npm install`. +4. Run `npm start`. +5. Open http://localhost:8888. +6. Enter the credit card number provided from one of your [sandbox accounts](https://developer.paypal.com/dashboard/accounts) or [generate a new credit card](https://developer.paypal.com/dashboard/creditCardGenerator). diff --git a/advanced-integration/beta/.env.example b/advanced-integration/v2/.env.example similarity index 100% rename from advanced-integration/beta/.env.example rename to advanced-integration/v2/.env.example diff --git a/advanced-integration/beta/README.md b/advanced-integration/v2/README.md similarity index 64% rename from advanced-integration/beta/README.md rename to advanced-integration/v2/README.md index 2f6e5140..db269ce4 100644 --- a/advanced-integration/beta/README.md +++ b/advanced-integration/v2/README.md @@ -1,6 +1,8 @@ # Advanced Integration Example -This folder contains example code for an Advanced PayPal integration using both the JS SDK and Node.js to complete transactions with the PayPal REST API. +This folder contains example code for [version 2](https://developer.paypal.com/docs/checkout/advanced/integrate/) of an advanced Checkout PayPal integration using both the JavaScript SDK and Node.js to complete transactions with the PayPal REST API. + +Version 2 is the current advanced Checkout integration, and includes Hosted Card Fields. ## Instructions diff --git a/advanced-integration/beta/client/checkout.html b/advanced-integration/v2/client/checkout.html similarity index 100% rename from advanced-integration/beta/client/checkout.html rename to advanced-integration/v2/client/checkout.html diff --git a/advanced-integration/beta/client/checkout.js b/advanced-integration/v2/client/checkout.js similarity index 100% rename from advanced-integration/beta/client/checkout.js rename to advanced-integration/v2/client/checkout.js diff --git a/advanced-integration/beta/package.json b/advanced-integration/v2/package.json similarity index 100% rename from advanced-integration/beta/package.json rename to advanced-integration/v2/package.json diff --git a/advanced-integration/beta/server/server.js b/advanced-integration/v2/server/server.js similarity index 100% rename from advanced-integration/beta/server/server.js rename to advanced-integration/v2/server/server.js