Skip to content

Instantly share code, notes, and snippets.

@juanchehin
Created December 5, 2022 23:16
Show Gist options
  • Save juanchehin/c7ce01ea778b20032aa6ffec0ad757f9 to your computer and use it in GitHub Desktop.
Save juanchehin/c7ce01ea778b20032aa6ffec0ad757f9 to your computer and use it in GitHub Desktop.
public async webhook(req: Request, res: Response) {
if (req.query.type === 'payment') {
const paymentId = req.query['data.id'];
console.log("paymentId: ",paymentId)
var options = {
'method': 'GET',
'hostname': 'api.mercadopago.com',
'path': '/v1/payments/'+ paymentId,
'headers': {
'Authorization': 'Bearer ' + process.env.MP_ACCESS_TOKEN_TEST
},
'maxRedirects': 20
};
var request = https.request(options, function (res: any) {
var body = '';
res.on('data', function(d: any) {
body += d;
});
return res.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);
if(parsed.status == 404)
{
return;
}
const idOrden = parsed.external_reference;
pool.query(`call bsp_dame_pedido_id('${idOrden}')`, function(err: any, result: any) {
if(err){
return;
}
var estadoPedidoBD = result[0][0].EstadoPedido;
if(estadoPedidoBD == 'C')
{
return;
}
var montoTotalBD = result[0][0].MontoTotal;
if (Number(montoTotalBD) === Number(parsed.transaction_amount)) {
if (parsed.status === 'approved') {
pool.query(`call bsp_confirmar_pedido('${idOrden}','${paymentId}')`, async function(err: any, result: any, fields: any){
if(err){
return;
}
})
}
}
})
});
});
res.status(200).json({ text: ''});
request.end();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment