Skip to content

Commit 6d0d42f

Browse files
authored
Merge branch 'main' into dotenv-enhancements
2 parents 9310c9e + 4ce5e89 commit 6d0d42f

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

advanced-integration/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
2. Run `npm install`
77
3. Run `npm start`
88
4. Open http://localhost:8888
9+
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)

advanced-integration/public/app.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
paypal
22
.Buttons({
33
// Sets up the transaction when a payment button is clicked
4-
createOrder: function (data, actions) {
4+
createOrder: function () {
55
return fetch("/api/orders", {
66
method: "post",
77
// use the "body" param to optionally pass additional order information
8-
// like product ids or amount
8+
// like product skus and quantities
9+
body: JSON.stringify({
10+
cart: [
11+
{
12+
sku: "<YOUR_PRODUCT_STOCK_KEEPING_UNIT>",
13+
quantity: "<YOUR_PRODUCT_QUANTITY>",
14+
},
15+
],
16+
}),
917
})
1018
.then((response) => response.json())
1119
.then((order) => order.id);
1220
},
1321
// Finalize the transaction after payer approval
14-
onApprove: function (data, actions) {
22+
onApprove: function (data) {
1523
return fetch(`/api/orders/${data.orderID}/capture`, {
1624
method: "post",
1725
})
@@ -47,8 +55,16 @@ if (paypal.HostedFields.isEligible()) {
4755
createOrder: () => {
4856
return fetch("/api/orders", {
4957
method: "post",
50-
// use the "body" param to optionally pass additional order information like
51-
// product ids or amount.
58+
// use the "body" param to optionally pass additional order information
59+
// like product skus and quantities
60+
body: JSON.stringify({
61+
cart: [
62+
{
63+
sku: "<YOUR_PRODUCT_STOCK_KEEPING_UNIT>",
64+
quantity: "<YOUR_PRODUCT_QUANTITY>",
65+
},
66+
],
67+
}),
5268
})
5369
.then((res) => res.json())
5470
.then((orderData) => {

standard-integration/README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ This folder contains example code for a standard PayPal integration using both t
44

55
## Instructions
66

7-
1. Replace `test` in `public/index.html` with your app's client-id
8-
2. Rename `.env.example` to `.env` and update `CLIENT_ID` and `APP_SECRET`.
9-
3. Run `npm install`
10-
4. Run `npm start`
11-
5. Open http://localhost:8888
7+
1. [Create an application](https://developer.paypal.com/dashboard/applications/sandbox/create)
8+
3. ARename `.env.example` to `.env` and update `CLIENT_ID` and `APP_SECRET`.
9+
2. Replace `test` in `public/index.html` with your app's client-id
10+
4. Run `npm install`
11+
5. Run `npm start`
12+
6. Open http://localhost:8888
13+
7. Click "PayPal" and log in with one of your [Sandbox test accounts](https://developer.paypal.com/dashboard/accounts).

standard-integration/public/index.html

+23-7
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,36 @@
1212
paypal
1313
.Buttons({
1414
// Sets up the transaction when a payment button is clicked
15-
createOrder: function (data, actions) {
16-
return fetch("/api/orders", {
15+
createOrder: function () {
16+
return fetch("/my-server/create-paypal-order", {
1717
method: "post",
18+
headers: {
19+
"Content-Type": "application/json",
20+
},
1821
// use the "body" param to optionally pass additional order information
19-
// like product ids or amount
22+
// like product skus and quantities
23+
body: JSON.stringify({
24+
cart: [
25+
{
26+
sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
27+
quantity: "YOUR_PRODUCT_QUANTITY",
28+
},
29+
],
30+
}),
2031
})
2132
.then((response) => response.json())
2233
.then((order) => order.id);
2334
},
2435
// Finalize the transaction after payer approval
25-
onApprove: function (data, actions) {
26-
return fetch(`/api/orders/${data.orderID}/capture`, {
36+
onApprove: function (data) {
37+
return fetch("/my-server/capture-paypal-order", {
2738
method: "post",
39+
headers: {
40+
"Content-Type": "application/json",
41+
},
42+
body: JSON.stringify({
43+
orderID: data.orderID,
44+
}),
2845
})
2946
.then((response) => response.json())
3047
.then((orderData) => {
@@ -34,8 +51,7 @@
3451
orderData,
3552
JSON.stringify(orderData, null, 2)
3653
);
37-
var transaction =
38-
orderData.purchase_units[0].payments.captures[0];
54+
const transaction = orderData.purchase_units[0].payments.captures[0];
3955
alert(
4056
"Transaction " +
4157
transaction.status +

standard-integration/server.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const app = express();
66

77
app.use(express.static("public"));
88

9-
app.post("/api/orders", async (req, res) => {
9+
// parse post params sent in body in json format
10+
app.use(express.json());
11+
12+
app.post("/my-server/create-paypal-order", async (req, res) => {
1013
try {
1114
const order = await paypal.createOrder();
1215
res.json(order);
@@ -15,8 +18,8 @@ app.post("/api/orders", async (req, res) => {
1518
}
1619
});
1720

18-
app.post("/api/orders/:orderID/capture", async (req, res) => {
19-
const { orderID } = req.params;
21+
app.post("/my-server/capture-paypal-order", async (req, res) => {
22+
const { orderID } = req.body;
2023
try {
2124
const captureData = await paypal.capturePayment(orderID);
2225
res.json(captureData);

0 commit comments

Comments
 (0)