import { createHmac } from "node:crypto"; import { describe, expect, it } from "vitest"; import { verifyCoinPayWebhook } from "@/lib/coinpay"; import { readCoinPayWebhook, settleHireUsWebhook, type HireUsPaymentRow } from "@/lib/coinpay-webhook"; const SECRET = "whsecret_logicsrcContractTestSecret"; // Same signing as coinpayportal signWebhookPayload: t=,v1=hmac(`${t}.${body}`). function sign(rawBody: string, ts = Math.floor(Date.now() / 1000)) { return `t=${ts},v1=${createHmac("sha256", SECRET).update(`${ts}.${rawBody}`).digest("hex")}`; } const PAYMENT_ID = "d084f959-c33c-4540-82b8-221181347639"; function realShaped(status = "confirmed", type = "payment.confirmed") { return { id: `evt_${PAYMENT_ID}_1775551263`, type, data: { payment_id: PAYMENT_ID, status, amount: 4000, amount_crypto: "4000.000000", amount_usd: 4000, currency: "USDC_POL", metadata: { product: "logicsrc-hire-us", billing: "metered_hours", unit: "agent_hour", hours: 10, rate_usd_per_hour: 400, source: "logicsrc.com/hire-us", property: "logicsrc.com", account_id: "coinpay-user-123", email: "buyer@example.com" } }, created_at: "2026-10-21T00:00:00.000Z", business_id: "biz_1" }; } function memoryStore() { const rows = new Map(); const paidSessions: [string, string][] = []; return { rows, paidSessions, async insertIfAbsent(row: HireUsPaymentRow) { if (rows.has(row.payment_id)) return false; rows.set(row.payment_id, row); return true; }, async markCheckoutPaid(sessionId: string, paymentId: string) { paidSessions.push([sessionId, paymentId]); } }; } const SID = "cs_Ju1h16BXSobdRs7XWg9MUIj0"; // A hosted checkout session payment: CoinPay adds the session fields to data. function sessionShaped(status = "confirmed", type = "payment.confirmed") { const base = realShaped(status, type); return { ...base, data: { ...base.data, checkout_session_id: SID, customer_email: "guest@example.com", account_id: "guest@example.com", property: "logicsrc.com", metadata: { ...base.data.metadata, account_id: "guest@example.com", email: "guest@example.com" } } }; } describe("CoinPay webhook (logicsrc Hire Us)", () => { it("verifies the X-CoinPay-Signature t=,v1= scheme over the raw body", () => { const raw = JSON.stringify(realShaped()); expect(verifyCoinPayWebhook(raw, sign(raw), SECRET)).toBe(true); expect(verifyCoinPayWebhook(raw + " ", sign(raw), SECRET)).toBe(false); expect(verifyCoinPayWebhook(raw, sign(raw, Math.floor(Date.now() / 1000) - 600), SECRET)).toBe(false); expect(verifyCoinPayWebhook(raw, createHmac("sha256", SECRET).update(raw).digest("hex"), SECRET)).toBe(false); }); it("reads the payment id from data, not the top-level event id", () => { const e = readCoinPayWebhook(realShaped()); expect(e.paymentId).toBe(PAYMENT_ID); expect(e.eventId).toMatch(/^evt_/); expect(e.metadata.account_id).toBe("coinpay-user-123"); }); it("settles a paid Hire Us payment once, mapped to the account", async () => { const store = memoryStore(); const payload = JSON.parse(JSON.stringify(realShaped())); const first = await settleHireUsWebhook(payload, store); expect(first).toMatchObject({ settled: true, duplicate: false, payment_id: PAYMENT_ID, account_id: "coinpay-user-123" }); const row = store.rows.get(PAYMENT_ID)!; expect(row).toMatchObject({ amount_usd: 4000, hours: 10, buyer_email: "buyer@example.com", property: "logicsrc.com" }); const again = await settleHireUsWebhook(realShaped("forwarded", "payment.forwarded"), store); expect(again).toMatchObject({ settled: true, duplicate: true }); expect(store.rows.size).toBe(1); }); it("acknowledges pending and unknown payments without recording", async () => { const store = memoryStore(); expect(await settleHireUsWebhook(realShaped("pending", "payment.pending"), store)).toMatchObject({ settled: false }); const foreign = realShaped(); foreign.data.metadata = {} as typeof foreign.data.metadata; expect(await settleHireUsWebhook(foreign, store)).toMatchObject({ settled: false, reason: "unknown payment" }); expect(store.rows.size).toBe(0); }); it("settles a checkout-session payment from the session fields, once", async () => { const store = memoryStore(); const first = await settleHireUsWebhook(sessionShaped(), store); expect(first).toMatchObject({ settled: true, duplicate: false, payment_id: PAYMENT_ID, checkout_session_id: SID, account_id: "guest@example.com", email: "guest@example.com" }); expect(store.rows.get(PAYMENT_ID)).toMatchObject({ checkout_session_id: SID, property: "logicsrc.com", hours: 10 }); expect(store.paidSessions).toEqual([[SID, PAYMENT_ID]]); const forwarded = await settleHireUsWebhook(sessionShaped("forwarded", "payment.forwarded"), store); const replay = await settleHireUsWebhook(sessionShaped(), store); expect(forwarded).toMatchObject({ settled: true, duplicate: true }); expect(replay).toMatchObject({ settled: true, duplicate: true }); expect(store.rows.size).toBe(1); }); it("does not record a pending session payment", async () => { const store = memoryStore(); expect(await settleHireUsWebhook(sessionShaped("pending", "payment.pending"), store)).toMatchObject({ settled: false }); expect(store.rows.size).toBe(0); expect(store.paidSessions).toEqual([]); }); });