Criar somente a automação backend em Node.js para integrar Hotmart → Webhook → Banco → Fluxos de e-mail e status, para uso dentro da minha plataforma já existente Zeyra (não criar frontend, sistema, telas nem painel).
848f927 verified Download src/services/webhookService.js from fernandosliborioo/hotmartflow-integrator: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/spaces/fernandosliborioo/hotmartflow-integrator/resolve/main/src/services/webhookService.js
- Command line
-
hf download hf://spaces/fernandosliborioo/hotmartflow-integrator/src/services/webhookService.js
-
curl -L -o webhookService.js https://huggingface.co/spaces/fernandosliborioo/hotmartflow-integrator/resolve/main/src/services/webhookService.js
3.79 kB
| const bcrypt = require('bcryptjs'); | |
| const { PrismaClient } = require('@prisma/client'); | |
| const emailService = require('./emailService'); | |
| const prisma = new PrismaClient(); | |
| class WebhookService { | |
| async processHotmartEvent(payload) { | |
| const event = payload.event; | |
| const data = payload.data; | |
| console.log(`Processing Hotmart event: ${event}`); | |
| switch (event) { | |
| case 'approved': | |
| case 'completed': | |
| await this.handleApprovedCompleted(data); | |
| break; | |
| case 'canceled': | |
| case 'chargeback': | |
| case 'delayed': | |
| case 'subscription_canceled': | |
| await this.handleBlockedEvent(data, event); | |
| break; | |
| case 'refunded': | |
| await this.handleRefunded(data); | |
| break; | |
| default: | |
| console.log(`Unhandled event type: ${event}`); | |
| } | |
| } | |
| async handleApprovedCompleted(data) { | |
| const email = data.buyer?.email || data.email; | |
| const name = data.buyer?.name || data.name; | |
| const hotmartId = data.subscription?.code || data.purchase?.transaction; | |
| const plan = data.product?.name || data.plan; | |
| const purchaseDate = new Date(data.purchase?.approved_date || data.event_date); | |
| if (!email) { | |
| throw new Error('Email is required for approved/completed event'); | |
| } | |
| // Generate temporary password | |
| const tempPassword = this.generateTempPassword(); | |
| const hashedPassword = await bcrypt.hash(tempPassword, 12); | |
| let user = await prisma.user.findUnique({ | |
| where: { email: email.toLowerCase() } | |
| }); | |
| if (user) { | |
| // Update existing user | |
| user = await prisma.user.update({ | |
| where: { email: email.toLowerCase() }, | |
| data: { | |
| hotmartId, | |
| plan, | |
| purchaseDate, | |
| status: 'active' | |
| } | |
| }); | |
| } else { | |
| // Create new user | |
| user = await prisma.user.create({ | |
| data: { | |
| email: email.toLowerCase(), | |
| name, | |
| password: hashedPassword, | |
| hotmartId, | |
| plan, | |
| purchaseDate, | |
| status: 'active' | |
| } | |
| }); | |
| // Send welcome email with temporary password | |
| await emailService.sendWelcomeEmail(email, tempPassword, name); | |
| } | |
| } | |
| async handleBlockedEvent(data, event) { | |
| const email = data.buyer?.email || data.email; | |
| const hotmartId = data.subscription?.code || data.purchase?.transaction; | |
| if (!email) { | |
| throw new Error('Email is required for blocked event'); | |
| } | |
| const user = await prisma.user.findUnique({ | |
| where: { email: email.toLowerCase() } | |
| }); | |
| if (user) { | |
| await prisma.user.update({ | |
| where: { email: email.toLowerCase() }, | |
| data: { status: 'blocked' } | |
| }); | |
| // Send blocked email | |
| await emailService.sendBlockedEmail(email, event, user.name); | |
| } | |
| } | |
| async handleRefunded(data) { | |
| const email = data.buyer?.email || data.email; | |
| const hotmartId = data.subscription?.code || data.purchase?.transaction; | |
| if (!email) { | |
| throw new Error('Email is required for refunded event'); | |
| } | |
| const user = await prisma.user.findUnique({ | |
| where: { email: email.toLowerCase() } | |
| }); | |
| if (user) { | |
| await prisma.user.update({ | |
| where: { email: email.toLowerCase() }, | |
| data: { status: 'refunded' } | |
| }); | |
| // Send refunded email | |
| await emailService.sendRefundedEmail(email, user.name); | |
| } | |
| } | |
| generateTempPassword() { | |
| const length = 10; | |
| const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
| let password = ''; | |
| for (let i = 0; i < length; i++) { | |
| password += charset.charAt(Math.floor(Math.random() * charset.length)); | |
| } | |
| return password; | |
| } | |
| } | |
| module.exports = new WebhookService(); |