forked from hdlopez/mp-ecommerce-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (116 loc) · 3.5 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var express = require('express');
var exphbs = require('express-handlebars');
var mercadopago = require ('mercadopago');
var bodyParser = require('body-parser');
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
mercadopago.configure({
access_token: 'APP_USR-6317427424180639-042414-47e969706991d3a442922b0702a0da44-469485398',
'x-integrator-id': 'dev_24c65fb163bf11ea96500242ac130004',
});
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.get('/', function (req, res) {
res.render('home');
});
app.get('/pending', function (req, res) {
console.log('pending');
res.render('pending');
});
app.get('/failure', function (req, res) {
console.log('failure');
res.render('failure');
});
app.post('/webhook', function (req, res) {
console.log(JSON.stringify({query: req.query}));
console.log(JSON.stringify({body: req.body}));
res.status(200).json({});
});
app.get('/success', function (req, res) {
console.log('success');
console.log(JSON.stringify({query: req.query}));
console.log(JSON.stringify({body: req.body}));
res.render('success', {
...req.query,
});
});
function getUrl(path) {
return process.env.BASE_URL + path;
}
app.get('/detail', function (req, res) {
// Crea un objeto de preferencia
let preference = {
items: [
{
id: 1234,
title: req.query.title,
description: 'Dispositivo móvil de Tienda e-commerce',
currency_id: "ARS",
quantity: parseInt(req.query.unit),
unit_price: parseFloat(req.query.price),
picture_url: getUrl(req.query.img.substring(2)),
},
],
payer: {
name: "Lalo",
surname: "Landa",
email: "[email protected]",
phone: {
area_code: "11",
number: 22223333
},
address: {
street_name: "False",
street_number: 123,
zip_code: "1111"
},
},
back_urls: {
success: getUrl("success"),
failure: getUrl("failure"),
pending: getUrl("pending"),
},
auto_return: "approved",
payment_methods: {
excluded_payment_methods: [
{
id: "amex"
}
],
excluded_payment_types: [
{
id: "atm",
},
],
installments: 6,
},
auto_return: 'approved',
"notification_url": getUrl("webhook"),
"external_reference": "[email protected]",
};
console.log(JSON.stringify({
preference: preference
}));
mercadopago.preferences.create(preference)
.then(function(response) {
console.log(JSON.stringify({
response_preference: response.body,
}));
res.render('detail', {
...req.query,
init_point: response.body.init_point,
});
}).catch(function(error){
console.log(error);
res.status(500);
res.render('error', { error: error });
});
});
app.use(express.static('assets'));
app.use('/assets', express.static(__dirname + '/assets'));
var port = process.env.PORT || 3000;
app.listen(port);
console.log(`Listening on port ${port}`);