-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (95 loc) · 2.67 KB
/
server.js
File metadata and controls
110 lines (95 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import express from 'express';
import fetch from 'node-fetch';
import cors from 'cors';
import 'dotenv/config';
import path from 'path';
const __dirname = path.resolve();
const { CLIENT_ID, APP_SECRET } = process.env;
const base = 'https://api-m.sandbox.paypal.com';
const app = express();
app.use(cors({ origin: '*' }));
const generateAccessToken = async () => {
try {
const auth = Buffer.from(CLIENT_ID + ':' + APP_SECRET).toString('base64');
const response = await fetch(`${base}/v1/oauth2/token`, {
method: 'post',
body: 'grant_type=client_credentials',
headers: {
Authorization: `Basic ${auth}`,
},
});
const data = await response.json();
return data.access_token;
} catch (error) {
console.error('Failed to generate Access Token:', error);
}
};
const createOrder = async () => {
const accessToken = await generateAccessToken();
const url = `${base}/v2/checkout/orders`;
const payload = {
intent: 'CAPTURE',
purchase_units: [
{
amount: {
currency_code: 'USD',
value: '0.02',
},
},
],
};
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
method: 'POST',
body: JSON.stringify(payload),
});
return handleResponse(response);
};
const capturePayment = async (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);
};
async function handleResponse(response) {
if (response.status === 200 || response.status === 201) {
return response.json();
}
const errorMessage = await response.text();
throw new Error(errorMessage);
}
app.post('/orders', async (req, res) => {
try {
const response = await createOrder();
res.json(response);
} catch (error) {
console.error('Failed to create order:', error);
res.status(500).json({ error: 'Failed to create order.' });
}
});
app.post('/orders/:orderID/capture', async (req, res) => {
try {
const { orderID } = req.params;
const response = await capturePayment(orderID);
res.json(response);
} catch (error) {
console.error('Failed to create order:', error);
res.status(500).json({ error: 'Failed to capture order.' });
}
});
//Serve index.html
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
app.listen(9597, () => {
console.log('listening on http://localhost:9597/');
});