Skip to content

Commit 4ce5e89

Browse files
authored
chore(standard-integration): update fetch calls to match public docs (#23)
1 parent 55a452b commit 4ce5e89

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

standard-integration/public/index.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
.Buttons({
1414
// Sets up the transaction when a payment button is clicked
1515
createOrder: function () {
16-
return fetch("/api/orders", {
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
1922
// like product skus and quantities
2023
body: JSON.stringify({
2124
cart: [
2225
{
23-
sku: "<YOUR_PRODUCT_STOCK_KEEPING_UNIT>",
24-
quantity: "<YOUR_PRODUCT_QUANTITY>",
26+
sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
27+
quantity: "YOUR_PRODUCT_QUANTITY",
2528
},
2629
],
2730
}),
@@ -31,8 +34,14 @@
3134
},
3235
// Finalize the transaction after payer approval
3336
onApprove: function (data) {
34-
return fetch(`/api/orders/${data.orderID}/capture`, {
37+
return fetch("/my-server/capture-paypal-order", {
3538
method: "post",
39+
headers: {
40+
"Content-Type": "application/json",
41+
},
42+
body: JSON.stringify({
43+
orderID: data.orderID,
44+
}),
3645
})
3746
.then((response) => response.json())
3847
.then((orderData) => {
@@ -42,8 +51,7 @@
4251
orderData,
4352
JSON.stringify(orderData, null, 2)
4453
);
45-
var transaction =
46-
orderData.purchase_units[0].payments.captures[0];
54+
const transaction = orderData.purchase_units[0].payments.captures[0];
4755
alert(
4856
"Transaction " +
4957
transaction.status +

standard-integration/server.js

Lines changed: 6 additions & 3 deletions
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)