mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-01 09:24:46 +00:00
fix(coinpay): settle paid Hire Us checkouts from the signed webhook
The CoinPay webhook only logged. It now verifies X-CoinPay-Signature (t=,v1= over t.rawBody), reads the nested payload (data.payment_id, not the top-level event id), and records a confirmed/forwarded Hire Us payment once in hire_us_payments (payment_id primary key), mapped to the buyer via metadata.account_id/email. Unknown or pending payments are a 200 no-op. Hire Us checkout now sends property, account_id (CoinPay session) and email in metadata. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7171c2cdeb
commit
a5db866624
7 changed files with 292 additions and 16 deletions
92
apps/logicsrc-web/contract/coinpay-webhook.contract.test.ts
Normal file
92
apps/logicsrc-web/contract/coinpay-webhook.contract.test.ts
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
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>();
|
||||
return {
|
||||
rows,
|
||||
async insertIfAbsent(row: HireUsPaymentRow) {
|
||||
if (rows.has(row.payment_id)) return false;
|
||||
rows.set(row.payment_id, row);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
@ -199,6 +199,8 @@ describe("POST /api/hire-us/coinpay-checkout", () => {
|
|||
hours: 10,
|
||||
rate_usd_per_hour: 400,
|
||||
source: "logicsrc.com/hire-us",
|
||||
property: "logicsrc.com",
|
||||
email: "buyer@example.com",
|
||||
buyer_email: "buyer@example.com"
|
||||
}
|
||||
});
|
||||
|
|
@ -556,6 +558,7 @@ describe("POST /api/webhooks/coinpay", () => {
|
|||
const body = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(body).toEqual({ received: true, complete: true, payment_id: "pay_123" });
|
||||
// No Hire Us metadata: acknowledged as a handled no-op, nothing recorded.
|
||||
expect(body).toEqual({ received: true, handled: true, settled: false, reason: "unknown payment", payment_id: "pay_123" });
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue