-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaypal.ts
154 lines (140 loc) · 4.64 KB
/
paypal.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import http, { IncomingMessage as HttpIncomingMessage, ServerResponse } from 'http';
interface IncomingMessage extends HttpIncomingMessage {
body?: any;
}
import { URL } from 'url';
import querystring from 'querystring';
import { paypalSdk } from '../../src/lib/sdk';
const sdk = paypalSdk({
endpoint: 'https://api.sandbox.paypal.com',
clientId: process.env.VITE_PAYPAL_CLIENT_ID || '',
clientSecret: process.env.VITE_PAYPAL_CLIENT_SECRET || '',
});
const routes = {
'POST /notify/paypal': async (req, res) => {
// log notification parameters
const params = req.body;
console.log(params);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('success');
},
'GET /return/paypal': async (req, res) => {
// log notification parameters
const url = new URL(req.url, 'http://localhost:3000');
const params = Object.fromEntries(url.searchParams);
console.log(params);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(JSON.stringify(params));
},
'GET /create-order': async (req, res) => {
try {
const response = await sdk.createOrder({
intent: 'CAPTURE',
purchase_units: [{
amount: {
currency_code: 'USD',
value: '100.00'
}
}]
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(response));
} catch (error) {
console.log(error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(error.message);
}
},
'GET /get-order': async (req, res) => {
try {
const response = await sdk.getOrder({ order_id: 'mockOrderId' });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(response));
} catch (error) {
console.log(error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(error.message);
}
},
'GET /capture-order': async (req, res) => {
try {
const response = await sdk.captureOrder({ order_id: 'mockOrderId' });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(response));
} catch (error) {
console.log(error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(error.message);
}
},
'GET /refund-order': async (req, res) => {
try {
const response = await sdk.refundOrder({ capture_id: 'mockCaptureId' });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(response));
} catch (error) {
console.log(error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(error.message);
}
}
};
// Handle incoming requests
function handleRequest(req: IncomingMessage, res: ServerResponse): void {
if (req.method === 'POST') {
let body = '';
// Listen for data
req.on('data', (chunk) => {
body += chunk.toString(); // Convert Buffer to string
});
// Listen for end of data
req.on('end', async () => {
try {
// Parse data based on Content-Type
const contentType = req.headers['content-type'];
let parsedData;
if (contentType === 'application/json') {
parsedData = JSON.parse(body);
} else if (contentType === 'application/x-www-form-urlencoded') {
parsedData = querystring.parse(body);
} else {
parsedData = body; // Return raw string for other types
}
// Attach parsed data to req.body
req.body = parsedData;
// Route the request
for (const [key, value] of Object.entries(routes)) {
const [method, path] = key.split(' ');
if (req.method === method && req.url?.startsWith(path)) {
return value(req, res);
}
}
// Handle not found
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
} catch (error) {
// Handle parsing error
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid data format' }));
}
});
} else {
// Route the request
for (const [key, value] of Object.entries(routes)) {
const [method, path] = key.split(' ');
if (req.method === method && req.url?.startsWith(path)) {
value(req, res);
return;
}
}
// Handle not found
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
}
// Create and start the server
const PORT: number = Number(process.env.PORT) || 3000;
const server = http.createServer(handleRequest);
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});