Skip to content

Commit

Permalink
Merge pull request #1 from Juanii95/feature/mp-integration
Browse files Browse the repository at this point in the history
add mp button modal
  • Loading branch information
Juan Ignacio Veltri authored Nov 20, 2020
2 parents e9f2195 + 65ec571 commit 830846e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
31 changes: 30 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ var express = require('express');
var exphbs = require('express-handlebars');
var port = process.env.PORT || 3000
var bodyParser = require('body-parser');
const mercadopago = require ('mercadopago');


mercadopago.configure({
access_token: 'ACCESS_TOKEN'
});

var app = express();

Expand All @@ -23,7 +29,30 @@ app.get('/', function (req, res) {


app.post("/detail", (req, res) => {
res.render('detail');
let preference = {
items: [
{
title: req.body.description,
unit_price: Number(req.body.transactionAmount),
quantity: Number(req.body.quantity),
}
]
};

mercadopago.preferences.create(preference)
.then(function(response){
res.render('detail', {
post: {
description: preference.items[0].title,
quantity: preference.items[0].quantity,
transactionAmount: preference.items[0].unit_price * preference.items[0].quantity,
id: response.body.id,
init_point: response.body.init_point,
}
});
}).catch(function(error){
console.log(error);
});
});

app.listen(port);
Expand Down
11 changes: 7 additions & 4 deletions views/detail.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
<h2 class="title">Summary</h3>
<div class="item">

<span class="price" id="summary-price"> {{!-- --}}</span>
<p class="item-name">Book x <span id="summary-quantity"> {{!-- --}}</span></p>
<span class="price" id="summary-price"> {{post.description}}</span>
<p class="item-name">Book x <span id="summary-quantity"> {{post.quantity}}</span></p>
</div>
<div class="total">Total<span class="price" id="summary-total"> {{!-- --}}</span></div>
<div class="total">Total<span class="price" id="summary-total"> {{post.transactionAmount}}</span></div>
</div>
<div class="payment-details">
<div class="form-group col-sm-12">
<br>
{{!-- checkout --}}
<script
src="https://www.mercadopago.com.ar/integrations/v1/web-payment-checkout.js"
data-preference-id={{post.id}}>
</script>
<br>
<a href="/" id="go-back">
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 10 10" class="chevron-left">
Expand Down

0 comments on commit 830846e

Please sign in to comment.