-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (84 loc) · 2.27 KB
/
app.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
const express = require('express');
const app = express();
const paypal = require('paypal-rest-sdk');
const port = 3030;
paypal.configure({
'mode': 'sandbox',
'client_id': 'ASsfpwMvxaDpxysS-T_IrFQVx049T-UACrSh2ztukqgQfcSaKPUzzIXXszrehbYU6pDRjZJl3T9qjZSO',
'client_secret': 'EGurON82iCfa0D-Sb21N31UDfA4BU_Y8wiBu9bJitkSjtbHzjtkRn1x7g2Q5UtBBAD0eaglQAUoYClUQ'
});
// Set our template engine of choice
app.set("view engine", "ejs");
app.use(express.static("public"));
// Basic Routes
app.get("/", (req, res) => {
res.render('index');
});
app.post("/pay", (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
// you will need to replace localhost with your server url
"return_url": "http://localhost/success",
"cancel_url": "http://localhost/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Donation",
"sku": "001",
"price": "2.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "2.00"
},
"description": "Supporting open source programming."
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0; i < payment.links.length; i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
});
app.get('/success', (req, res) => {
const payerID = req.query.PayerID;
const paymentID = req.query.paymentId;
const execute_payment_json = {
"payer_id": payerID,
"transactions": [{
"amount": {
"currency": "USD",
"total": "2.00"
}
}]
};
paypal.payment.execute(paymentID, execute_payment_json, function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
console.log("Get Payment Response");
console.log(JSON.stringify(payment));
res.render('success');
}
});
});
app.get('/cancel', (req, res) => res.render('cancel'));
// Web Server
app.listen(3030, () => {
console.log(`Listening at port ${port}`);
});