diff --git a/src/app.ts b/src/app.ts index bf79e19..e60c699 100644 --- a/src/app.ts +++ b/src/app.ts @@ -48,6 +48,9 @@ app.use('/api', createUnitRouter) // Adicione '/api' como prefixo para as rotas app.use('/proposal', proposalRoutes) // Adicione '/api' como prefixo para as rotas de autenticação app.use('/auth', validateToken) // Adicione '/api' como prefixo para as rotas de autenticação app.use('/all', publicRouter) // Adicione '/api' como prefixo para as rotas de autenticação +// nrok teste webHook +// app.user('/api',) + // Importações necessárias import { PrismaClient } from '@prisma/client' diff --git a/src/controllers/paymentWebHookController.ts b/src/controllers/hook.payment.controller.ts similarity index 88% rename from src/controllers/paymentWebHookController.ts rename to src/controllers/hook.payment.controller.ts index 8fb30da..d218df1 100644 --- a/src/controllers/paymentWebHookController.ts +++ b/src/controllers/hook.payment.controller.ts @@ -23,8 +23,9 @@ const verifyWebhookSignature = (req: any): boolean => { } const signatureHeader = req.headers['x-signature'] as string; - if (!signatureHeader) { - console.error('Cabeçalho x-signature ausente.'); + const xRequestId = req.headers['x-request-id'] as string; + if (!signatureHeader || !xRequestId) { + console.error('Cabeçalhos x-signature ou x-request-id ausentes.'); return false; } @@ -37,14 +38,12 @@ const verifyWebhookSignature = (req: any): boolean => { } const [, timestamp, receivedHash] = match; - - console.log('🚀 ~ verifyWebhookSignature ~ timestamp:', timestamp); - console.log('🚀 ~ verifyWebhookSignature ~ receivedHash:', receivedHash); - const rawBody = req.rawBody || JSON.stringify(req.body); + + const manifest = `id:${req.query['data.id']};request-id:${xRequestId};ts:${timestamp};${rawBody}`; const calculatedHash = crypto .createHmac('sha256', secret) - .update(`ts=${timestamp}${rawBody}`) + .update(manifest) .digest('hex'); console.log('🚀 ~ verifyWebhookSignature ~ calculatedHash:', calculatedHash); @@ -61,6 +60,10 @@ const verifyWebhookSignature = (req: any): boolean => { const getPaymentDetails = async (paymentId: string) => { try { const accessToken = process.env.MERCADO_PAGO_ACCESS_TOKEN; + if (!accessToken) { + throw new Error('Access token não configurado.'); + } + const response = await axios.get( `https://api.mercadopago.com/v1/payments/${paymentId}`, { @@ -112,6 +115,9 @@ const processNotification = async (notification: any) => { if (type === 'payment') { const paymentDetails = await getPaymentDetails(id); await updatePaymentStatus(paymentDetails); + } else if (type === 'merchant_order') { + console.log('Notificação de merchant order recebida:', notification); + // Processar a lógica de merchant_order aqui, se necessário } else { console.log(`Tipo de notificação não suportado: ${type}`); } diff --git a/src/routes/paymentRoutes.ts b/src/routes/paymentRoutes.ts index 90a597b..fd6642e 100644 --- a/src/routes/paymentRoutes.ts +++ b/src/routes/paymentRoutes.ts @@ -1,6 +1,6 @@ import express from 'express' import { createPayment } from '../controllers/paymentController' -import { webHook } from '@/controllers/paymentWebHookController'; +import { webHook } from '@/controllers/hook.payment.controller'; const router = express.Router() diff --git a/src/webhook/v1.ts b/src/webhook/v1.ts new file mode 100644 index 0000000..89dfb34 --- /dev/null +++ b/src/webhook/v1.ts @@ -0,0 +1,10 @@ +import express from 'express' + + +const router = express.Router() + +router.get('/', (req, res, next) => { + console.log("Get v1 REQ.body WebHook") + console.log(req.body) + res.send("Get Ok") +}) \ No newline at end of file