forked from morfsys/nodejs-paypal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (96 loc) · 2.73 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
104
105
106
107
const express = require("express");
const bodyParser = require("body-parser");
const engines = require("consolidate");
const paypal = require("paypal-rest-sdk");
const app = express();
app.engine("ejs", engines.ejs);
app.set("views", "./views");
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
paypal.configure({
mode: "sandbox", //sandbox or live
client_id:
"AarUi7DfcZaVwTyuHOE2qMPGP1Gy65T8KNHicseSgSXB-gm_2upRM74fU-MKmslNaHKqNOUsxMrNv9I-",
client_secret:
"EI0ea3hVzYFwuts33i0RUVhxF48woSUSg7lwNbkImLrHpcEYpUmJPRXdf4CXn4kFacsRoZ-62cn9Xe6h"
});
app.get("/", (req, res) => {
res.render("index");
});
app.get("/paypal", (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: "item",
sku: "item",
price: "1.00",
currency: "USD",
quantity: 1
}
]
},
amount: {
currency: "USD",
total: "1.00"
},
description: "This is the payment description."
}
]
};
paypal.payment.create(create_payment_json, function(error, payment) {
if (error) {
throw error;
} else {
console.log("Create Payment Response");
console.log(payment);
res.redirect(payment.links[1].href);
}
});
});
app.get("/success", (req, res) => {
// res.send("Success");
var PayerID = req.query.PayerID;
var paymentId = req.query.paymentId;
var execute_payment_json = {
payer_id: PayerID,
transactions: [
{
amount: {
currency: "USD",
total: "1.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");
});
app.listen(3000, () => {
console.log("Server is running");
});