From c9881868ddd17649f958c69580e14a27c80fa353 Mon Sep 17 00:00:00 2001 From: justin Date: Mon, 4 May 2026 07:32:42 -0500 Subject: [PATCH] Add Telegram notification on every new paid order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sends to the monitoring bot immediately when payment is confirmed: - Customer name and email - Service/slug ordered - Total amount (includes all fees: service + formation + state + addons) - Payment method - Order number and type Fire-and-forget — never blocks the payment flow. Requires TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID env vars on API container. Co-Authored-By: Claude Opus 4.6 (1M context) --- api/src/routes/checkout.ts | 36 ++++++++++++++++++++++++++++++++++++ docker-compose.yml | 2 ++ 2 files changed, 38 insertions(+) diff --git a/api/src/routes/checkout.ts b/api/src/routes/checkout.ts index e4b4431..c5ae223 100644 --- a/api/src/routes/checkout.ts +++ b/api/src/routes/checkout.ts @@ -1183,6 +1183,42 @@ export async function handlePaymentComplete( const paymentMethod = (order.payment_method as string) || "card"; console.log(`[checkout] Payment confirmed: ${order_type} ${order_id} via ${paymentMethod}`); + // ── Telegram order notification ────────────────────────────────────── + try { + const botToken = process.env.TELEGRAM_BOT_TOKEN; + const chatId = process.env.TELEGRAM_CHAT_ID; + if (botToken && chatId) { + const customerName = (order.customer_name as string) || "Unknown"; + const customerEmail = (order.customer_email as string) || ""; + const serviceName = (order.service_slug as string) + || (order.service_name as string) + || order_type; + // Calculate total in dollars + const feeCents = Number(order.service_fee_cents || order.total_cents || 0); + const formationCents = Number(order.formation_fee_cents || 0); + const stateCents = Number(order.state_fee_cents || 0); + const addonCents = Number(order.addon_fee_cents || 0); + const pucCents = Number(order.puc_fee_cents || 0); + const totalCents = feeCents + formationCents + stateCents + addonCents + pucCents; + const totalDollars = (totalCents / 100).toFixed(2); + + const msg = `💰 NEW ORDER\n\n` + + `Customer: ${customerName}\n` + + `Email: ${customerEmail}\n` + + `Service: ${serviceName}\n` + + `Total: $${totalDollars}\n` + + `Payment: ${paymentMethod}\n` + + `Order: ${order_id}\n` + + `Type: ${order_type}`; + + fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ chat_id: chatId, text: msg }), + }).catch(() => {}); // fire and forget + } + } catch {} + // ── CDR traffic study paywall: issue grants on qualifying orders ────── // // When a customer pays for a 499-A filing service OR the standalone diff --git a/docker-compose.yml b/docker-compose.yml index d45f2ba..ebf44a7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,6 +48,8 @@ services: - LISTMONK_URL=http://listmonk:9000 - LISTMONK_USER=${LISTMONK_USER:-admin} - LISTMONK_PASSWORD=${LISTMONK_PASSWORD} + - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN} + - TELEGRAM_CHAT_ID=${TELEGRAM_CHAT_ID} depends_on: - api-postgres restart: unless-stopped