forked from hdlopez/mp-ecommerce-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (102 loc) · 3.16 KB
/
app.js
File metadata and controls
113 lines (102 loc) · 3.16 KB
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
var express = require("express");
var exphbs = require("express-handlebars");
var id = "";
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
app.get("/", function (req, res) {
res.render("home", { id: id });
});
app.get("/failure", function (req, res) {
res.render("failure");
});
app.get("/success", function (req, res) {
console.log(req.query);
payment_method_id = req.query.payment_type;
external_reference = req.query.external_reference;
payment_id = req.query.collection_id;
res.render("success", { payment_method_id, external_reference, payment_id });
});
app.get("/pending", function (req, res) {
res.render("pending");
});
app.get("/detail", function (req, res) {
// SDK de Mercado Pago
const mercadopago = require("mercadopago");
// Agrega credenciales
mercadopago.configure({
integrator_id: "dev_24c65fb163bf11ea96500242ac130004",
access_token:
"APP_USR-1159009372558727-072921-8d0b9980c7494985a5abd19fbe921a3d-617633181",
});
// Crea un objeto de preferencia
let preference = {
items: [
{
id: "1234",
title: req.query.title,
description: "Dispositivo móvil de Tienda e-commerce",
picture_url:
"https://epaulf-mp-commerce-nodejs.herokuapp.com/" +
req.query.img.substring(1),
quantity: parseInt(req.query.unit),
unit_price: parseInt(req.query.price),
external_reference: "ericpaulflorese@gmail.com",
},
],
external_reference: "ericpaulflorese@gmail.com",
payer: {
name: "Lalo",
surname: "Landa",
email: "test_user_58295862@testuser.com",
phone: {
area_code: "52",
number: 5549737300,
},
address: {
street_name: "Insurgentes Sur",
street_number: 1602,
zip_code: "03940",
},
},
payment_methods: {
excluded_payment_methods: [{ id: "amex" }],
excluded_payment_types: [{ id: "atm" }],
installments: 6,
default_installments: 6,
},
back_urls: {
success: "https://epaulf-mp-commerce-nodejs.herokuapp.com/success",
failure: "https://epaulf-mp-commerce-nodejs.herokuapp.com/failure",
pending: "https://epaulf-mp-commerce-nodejs.herokuapp.com/pending",
},
notification_url: "https://epaulf-mp-commerce-nodejs.herokuapp.com/webhook",
auto_return: "approved",
};
mercadopago.preferences
.create(preference)
.then(function (response) {
// Este valor reemplazará el string "<%= global.id %>" en tu HTML
init_point = response.response.init_point;
id = response.body.id;
res.render("detail", {
query: req.query,
id: id,
init_point: init_point,
});
})
.catch(function (error) {
console.log(error);
});
});
app.post("/webhook", function (req, res) {
console.log("MENSAJE :", req.body);
console.log(req.query);
res.status(200).json("OK");
});
app.use(express.static("assets"));
app.use("/assets", express.static(__dirname + "/assets"));
app.listen(process.env.PORT || 3000);