-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
132 lines (125 loc) · 4.46 KB
/
index.js
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
import express from 'express';
import fetch from 'node-fetch';
import 'dotenv/config';
const app = express();
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
const port = process.env.PORT || 3000;
const environment = process.env.ENVIRONMENT || 'sandbox';
const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;
const endpoint_url = environment === 'sandbox' ? 'https://api-m.sandbox.paypal.com' : 'https://api-m.paypal.com';
/**
* Creates an order and returns it as a JSON response.
* @function
* @name createOrder
* @memberof module:routes
* @param {object} req - The HTTP request object.
* @param {object} req.body - The request body containing the order information.
* @param {string} req.body.intent - The intent of the order.
* @param {object} res - The HTTP response object.
* @returns {object} The created order as a JSON response.
* @throws {Error} If there is an error creating the order.
*/
app.post('/create_order', (req, res) => {
get_access_token()
.then(access_token => {
let order_data_json = {
'intent': req.body.intent.toUpperCase(),
'purchase_units': [{
'amount': {
'currency_code': 'USD',
'value': '100.00'
}
}]
};
const data = JSON.stringify(order_data_json)
fetch(endpoint_url + '/v2/checkout/orders', { //https://developer.paypal.com/docs/api/orders/v2/#orders_create
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`
},
body: data
})
.then(res => res.json())
.then(json => {
res.send(json);
}) //Send minimal data to client
})
.catch(err => {
console.log(err);
res.status(500).send(err)
})
});
/**
* Completes an order and returns it as a JSON response.
* @function
* @name completeOrder
* @memberof module:routes
* @param {object} req - The HTTP request object.
* @param {object} req.body - The request body containing the order ID and intent.
* @param {string} req.body.order_id - The ID of the order to complete.
* @param {string} req.body.intent - The intent of the order.
* @param {object} res - The HTTP response object.
* @returns {object} The completed order as a JSON response.
* @throws {Error} If there is an error completing the order.
*/
app.post('/complete_order', (req, res) => {
get_access_token()
.then(access_token => {
fetch(endpoint_url + '/v2/checkout/orders/' + req.body.order_id + '/' + req.body.intent, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`
}
})
.then(res => res.json())
.then(json => {
console.log(json);
res.send(json);
}) //Send minimal data to client
})
.catch(err => {
console.log(err);
res.status(500).send(err)
})
});
// Helper / Utility functions
//Servers the index.html file
app.get('/', (req, res) => {
res.sendFile(process.cwd() + '/index.html');
});
//Servers the style.css file
app.get('/style.css', (req, res) => {
res.sendFile(process.cwd() + '/style.css');
});
//Servers the script.js file
app.get('/script.js', (req, res) => {
res.sendFile(process.cwd() + '/script.js');
});
//PayPal Developer YouTube Video:
//How to Retrieve an API Access Token (Node.js)
//https://www.youtube.com/watch?v=HOkkbGSxmp4
function get_access_token() {
const auth = `${client_id}:${client_secret}`
const data = 'grant_type=client_credentials'
return fetch(endpoint_url + '/v1/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(auth).toString('base64')}`
},
body: data
})
.then(res => res.json())
.then(json => {
return json.access_token;
})
}
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`)
})