mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-11-29 15:51:52 +00:00
Some checks failed
CI / build (push) Has been cancelled
Deploy to dev2 / deploy (push) Has been cancelled
test / test (push) Has been cancelled
POST /api/hire-us/coinpay-checkout now opens a CoinPay checkout session instead of a single-chain USDC_POL /api/payments/create. The buyer picks any coin the business has a wallet for, or PayPal. Each session sends customer_email (required), account_id (CoinPay OAuth sub when signed in, else the buyer email for guests), property logicsrc.com and the Hire Us metadata. The cs_ id is recorded in the new hire_us_checkouts table. The webhook records session payments from the session fields (checkout_session_id, account_id, customer_email, property), stores the session id on hire_us_payments, marks the checkout paid, and stays idempotent on payment_id across confirmed, forwarded and replays. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5.5 KiB
TypeScript
142 lines
5.5 KiB
TypeScript
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=<unix>,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<string, HireUsPaymentRow>();
|
|
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([]);
|
|
});
|
|
});
|