/** * Incoming webhooks and the links in sent messages on v2 (1.51.16). These aren't JSON API calls, * so they're plain Express routes, mounted by app.js beside the v2 router: * * - `webhooksRouter` at /api/v2/webhooks: Yoco, PayFast and WAWP (WhatsApp) post here. New * provider registrations use these URLs; the v1 paths (/api/webhooks/…, /api/whatsapp/webhook) * are still answered by the same handlers, because providers may not follow a redirect on POST. * - `linksRouter` at /api/v2: the one-click unsubscribe link (RFC 8058) from email headers. * - `legacyLinkRedirects` at /api: links in messages already sent (calendar files, opening a * one-click unsubscribe link) answer 308 with their v2 address. A mail app's one-click POST to * the v1 address is still handled there, like a webhook. */ import express, { Router, type RequestHandler } from 'express'; /* eslint-disable @typescript-eslint/no-explicit-any -- the handlers are JavaScript */ const { handleYocoWebhook } = require('../controllers/webhookController') as { handleYocoWebhook: RequestHandler }; const { handleProviderWebhook } = require('../payments/webhook') as { handleProviderWebhook(id: string): RequestHandler }; const { handleWebhook: handleWhatsappWebhook } = require('../controllers/whatsappController') as { handleWebhook: RequestHandler }; const privacy = require('../controllers/privacyController') as { oneClickRedirect: RequestHandler; oneClickUnsubscribe: RequestHandler }; /** Paths whose request body is read raw (signatures are checked over the exact bytes). */ export const RAW_BODY_PREFIXES = ['/api/webhooks', '/api/v2/webhooks']; /** WAWP sends JSON (and ?secret=…); on the webhook paths its body arrives raw. */ export const whatsappWebhook: RequestHandler[] = [(req: any, _res, next) => { try { req.body = req.rawBody ? JSON.parse(req.rawBody) : {}; } catch { req.body = {}; } next(); }, handleWhatsappWebhook]; export const webhooksRouter = Router(); webhooksRouter.post('/yoco', handleYocoWebhook); webhooksRouter.post('/payfast', handleProviderWebhook('payfast')); webhooksRouter.post('/whatsapp', ...whatsappWebhook); export const linksRouter = Router(); linksRouter.get('/unsubscribe/:token/one-click', privacy.oneClickRedirect); linksRouter.post('/unsubscribe/:token/one-click', express.urlencoded({ extended: false }), privacy.oneClickUnsubscribe); /** v1 links in messages already sent → their v2 address (308 keeps the method). */ const REDIRECTS: { method: string; v1: RegExp; v2: (m: RegExpMatchArray) => string }[] = [ { method: 'GET', v1: /^\/api\/events\/([^/]+)\/ics$/, v2: m => `/api/v2/events/${m[1]}/calendar.ics` }, { method: 'GET', v1: /^\/api\/privacy\/unsubscribe\/([^/]+)\/one-click$/, v2: m => `/api/v2/unsubscribe/${m[1]}/one-click` }, ]; export const legacyLinkRedirects: RequestHandler = (req, res, next) => { const [path, query] = req.originalUrl.split('?'); for (const r of REDIRECTS) { const m = req.method === r.method ? path.match(r.v1) : null; if (m) return res.redirect(308, r.v2(m) + (query ? `?${query}` : '')); } next(); };