Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Brasileiro authored and Fabio Brasileiro committed Nov 16, 2024
1 parent 050d99c commit 3563acb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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}`,
{
Expand Down Expand Up @@ -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}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/paymentRoutes.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down
10 changes: 10 additions & 0 deletions src/webhook/v1.ts
Original file line number Diff line number Diff line change
@@ -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")
})

0 comments on commit 3563acb

Please sign in to comment.