-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
87 lines (75 loc) · 2.28 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
const express = require('express');
const ejs = require('ejs');
const paypal =require('paypal-rest-sdk');
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'your client id',
'client_secret': 'your client secret'
});
const app= express();
app.set('view engine', 'ejs');
app.get('/', (req, res) => res.render('index'));
app.post('/pay', (req,res) => {
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Global Club pass",
"sku": "001",
"price": "16.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "16.00"
},
"description": "Global club Entry pass"
}]
};
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;
var execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": "USD",
"total": "16.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.send('Successfully Completed transaction');
}
});
} );
app.get('/cancel', (req,res) => res.send('Cancelled'));
app.listen(3000, () => console.log('Server started'));