-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (85 loc) · 2.48 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
const express = require("express"),
ejs =require("ejs"),
paypal = require("paypal-rest-sdk")
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'AVhLlZxbRBh_-qpC73IQrS3lqzTylRSsTwEbdi3N_4Sqojj5H7Am4d9J1q8iOun8VUZq3ev8PejhxuIE',
'client_secret': 'EN1tYC2c3_1LT_UfDcW6AgqpZSn-77noX8grap_uXaCWyNeEA0rBK8cuWwPEfzj0db6uaCV3-XlJDAPz'
});
const app = express()
app.set("view engine", "ejs")
//INDEX Route
app.get("/", (req,res)=>{
res.render("index")
})
//POST route
app.post("/pay", (req,res)=>{
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cencel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "visa card",
"sku": "0001",
"price": "110.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "110.00"
},
"description": "This is the payment description."
}]
};
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)
}
}
}
});
})
//SUCCES Route
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": "110.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("success")
}
});
})
//CANCEL ROute
app.get("/cancel", (req, res)=>{
res.send("canceled")
})
app.listen(3000, ()=>{
console.log("listening to port 3000")
})