11/* eslint-disable import/prefer-default-export */
22import '@cloudcommerce/firebase/lib/init' ;
3- import logger from 'firebase-functions/logger' ;
43import axios from 'axios' ;
54import api from '@cloudcommerce/api' ;
65import { getFirestore } from 'firebase-admin/firestore' ;
76import * as functions from 'firebase-functions/v1' ;
8- import config from '@cloudcommerce/firebase/lib/config' ;
7+ import config , { logger } from '@cloudcommerce/firebase/lib/config' ;
98import { parsePaymentStatus } from './mp-create-transaction' ;
109
1110const { httpsFunctionOptions } = config . get ( ) ;
@@ -15,10 +14,9 @@ export const mercadopago = {
1514 . region ( httpsFunctionOptions . region )
1615 . runWith ( httpsFunctionOptions )
1716 . https . onRequest ( async ( req , res ) => {
18- const { method } = req ;
19- if ( method === 'POST' ) {
20- const { body } = req ;
21- logger . log ( '>> Webhook MP #' , JSON . stringify ( body ) , ' <<' ) ;
17+ const { method, body : notification } = req ;
18+ if ( method === 'POST' && notification ) {
19+ logger . info ( 'Webhook body' , { notification } ) ;
2220 try {
2321 const app = ( await api . get (
2422 `applications?app_id=${ config . get ( ) . apps . mercadoPago . appId } &fields=hidden_data` ,
@@ -33,11 +31,11 @@ export const mercadopago = {
3331 return ;
3432 }
3533
36- const notification = req . body ;
37- if ( notification . type !== 'payment' || ! notification . data || ! notification . data . id ) {
38- res . status ( 404 ) . send ( 'SKIP' ) ;
34+ if ( notification . type !== 'payment' || ! notification . data ?. id ) {
35+ res . sendStatus ( 400 ) ;
36+ return ;
3937 }
40- logger . log ( '> MP Notification for Payment #' , notification . data . id ) ;
38+ logger . info ( ` MP Notification for Payment #${ notification . data . id } ` ) ;
4139
4240 const docRef = getFirestore ( ) . collection ( 'mercadopagoPayments' )
4341 . doc ( String ( notification . data . id ) ) ;
@@ -50,18 +48,17 @@ export const mercadopago = {
5048 const order = ( await api . get (
5149 `orders/${ orderId } ` ,
5250 ) ) . data ;
53- logger . log ( '>order ' , JSON . stringify ( order ) , '<' ) ;
5451 if ( order && order . transactions ) {
5552 const payment = ( await axios . get (
5653 `https://api.mercadopago.com/v1/payments/${ notification . data . id } ` ,
5754 {
5855 headers : {
59- Authorization : `Bearer ${ process . env . MERCADOPAGO_TOKEN } ` ,
56+ ' Authorization' : `Bearer ${ process . env . MERCADOPAGO_TOKEN } ` ,
6057 'Content-Type' : 'application/json' ,
6158 } ,
6259 } ,
6360 ) ) . data ;
64- logger . log ( '>payment ' , JSON . stringify ( payment ) , ' <' ) ;
61+ logger . info ( `Payment for ${ order . _id } ` , { payment } ) ;
6562 const methodPayment = payment . payment_method_id ;
6663
6764 const transaction = order . transactions . find ( ( { intermediator } ) => {
@@ -104,15 +101,15 @@ export const mercadopago = {
104101 }
105102 res . status ( 200 ) . send ( 'SUCCESS' ) ;
106103 } else {
107- logger . log ( '> Transaction not found #' , notification . data . id ) ;
104+ logger . info ( ` Transaction not found ${ notification . data . id } ` ) ;
108105 res . sendStatus ( 404 ) ;
109106 }
110107 } else {
111- logger . log ( '> Order Not Found #' , orderId ) ;
108+ logger . info ( ` Order Not Found ${ orderId } ` ) ;
112109 res . sendStatus ( 404 ) ;
113110 }
114111 } else {
115- logger . log ( '> Payment not found in Firestore #' , notification . data . id ) ;
112+ logger . info ( ` Payment not found in Firestore ${ notification . data . id } ` ) ;
116113 res . sendStatus ( 404 ) ;
117114 }
118115 } )
0 commit comments