forked from nodejsera/paypal-integration-using-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (74 loc) · 1.9 KB
/
server.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
var express = require('express');
var path = require('path');
var app = express();
var paypal = require('paypal-rest-sdk');
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': '',
'client_secret': ''
});
// set public directory to serve static files
app.use('/', express.static(path.join(__dirname, 'public')));
// redirect to store
app.get('/' , (req , res) => {
res.redirect('/index.html');
})
// start payment process
app.get('/buy' , ( req , res ) => {
var payment = {
"intent": "authorize",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://127.0.0.1:3000/success",
"cancel_url": "http://127.0.0.1:3000/err"
},
"transactions": [{
"amount": {
"total": 39.00,
"currency": "USD"
},
"description": " a book on mean stack "
}]
}
createPay( payment )
.then( ( transaction ) => {
var id = transaction.id;
var links = transaction.links;
var counter = links.length;
while( counter -- ) {
if ( links[counter].method == 'REDIRECT') {
return res.redirect( links[counter].href )
}
}
})
.catch( ( err ) => {
console.log( err );
res.redirect('/err');
});
});
app.get('/success' , (req ,res ) => {
console.log(req.query);
res.redirect('/success.html');
})
app.get('/err' , (req , res) => {
console.log(req.query);
res.redirect('/err.html');
})
app.listen( 3000 , () => {
console.log(' app listening on 3000 ');
})
// helper functions
var createPay = ( payment ) => {
return new Promise( ( resolve , reject ) => {
paypal.payment.create( payment , function( err , payment ) {
if ( err ) {
reject(err);
}
else {
resolve(payment);
}
});
});
}