feat(hire-us): CoinPay hosted checkout sessions for Hire Us (#254)
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>
This commit is contained in:
Anthony Ettinger 2026-10-21 06:22:17 -07:00 • committed by GitHub
parent 5192684de9
commit 580817b06f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 341 additions and 215 deletions

View file

@ -2,7 +2,8 @@
//
// CoinPay signs and sends a NESTED body (coinpayportal src/lib/webhooks/service.ts):
// { id: "evt_<paymentId>_<ts>", type: "payment.confirmed",
// data: { payment_id, status, amount, amount_usd, metadata: {...} },
// data: { payment_id, status, amount, amount_usd, checkout_session_id,
// customer_email, account_id, property, metadata: {...} },
// created_at, business_id }
// The top-level `id` is the EVENT id, not the payment id. Read `data` first and
// fall back to the flat shape only for the legacy test sender.
@ -13,6 +14,11 @@ export interface CoinPayWebhookEvent {
paymentId: string | null;
status: string;
amountUsd: number | null;
/** Hosted checkout session the payment belongs to (cs_...), when there is one. */
checkoutSessionId: string | null;
accountId: string | null;
customerEmail: string | null;
property: string | null;
metadata: Record<string, unknown>;
}
@ -43,13 +49,19 @@ export function readCoinPayWebhook(payload: Record<string, unknown>): CoinPayWeb
const type = asString(payload.type) ?? asString(payload.event);
let status = (asString(d.status) ?? "").toLowerCase();
if (!status && type && SETTLED_TYPES.has(type)) status = type.slice("payment.".length);
const metadata = asRecord(d.metadata) ?? asRecord(payload.metadata) ?? {};
const sid = asString(d.checkout_session_id) ?? asString(metadata.checkout_session_id);
return {
eventId: nested ? asString(payload.id) : null,
type,
paymentId,
status,
amountUsd: asNumber(d.amount_usd) ?? asNumber(d.amount),
metadata: asRecord(d.metadata) ?? asRecord(payload.metadata) ?? {}
checkoutSessionId: sid && /^cs_[A-Za-z0-9]{8,64}$/.test(sid) ? sid : null,
accountId: asString(d.account_id) ?? asString(metadata.account_id),
customerEmail: asString(d.customer_email) ?? asString(metadata.email) ?? asString(metadata.buyer_email),
property: asString(d.property) ?? asString(metadata.property),
metadata
};
}
@ -68,16 +80,27 @@ export interface HireUsPaymentRow {
account_id: string | null;
buyer_email: string | null;
property: string | null;
checkout_session_id: string | null;
metadata: Record<string, unknown>;
}
export interface HireUsPaymentStore {
/** Insert once; resolve true if this call created the row, false if it already existed. */
insertIfAbsent(row: HireUsPaymentRow): Promise<boolean>;
/** Mark the hosted checkout session paid (no-op if it is already). */
markCheckoutPaid?(sessionId: string, paymentId: string): Promise<void>;
}
export type SettleResult =
| { handled: true; settled: true; duplicate: boolean; payment_id: string; account_id: string | null; email: string | null }
| {
handled: true;
settled: true;
duplicate: boolean;
payment_id: string;
checkout_session_id: string | null;
account_id: string | null;
email: string | null;
}
| { handled: true; settled: false; reason: string; payment_id: string | null };
export async function settleHireUsWebhook(
@ -95,8 +118,10 @@ export async function settleHireUsWebhook(
return { handled: true, settled: false, reason: "unknown payment", payment_id: event.paymentId };
}
const accountId = asString(meta.account_id);
const email = asString(meta.email) ?? asString(meta.buyer_email);
// Session fields first (CoinPay copies them onto every session payment),
// then our own metadata for payments opened before checkout sessions.
const accountId = event.accountId;
const email = event.customerEmail;
const inserted = await store.insertIfAbsent({
payment_id: event.paymentId,
event_id: event.eventId,
@ -107,8 +132,20 @@ export async function settleHireUsWebhook(
rate_usd_per_hour: asNumber(meta.rate_usd_per_hour),
account_id: accountId,
buyer_email: email,
property: asString(meta.property),
property: event.property,
checkout_session_id: event.checkoutSessionId,
metadata: meta
});
return { handled: true, settled: true, duplicate: !inserted, payment_id: event.paymentId, account_id: accountId, email };
if (event.checkoutSessionId && store.markCheckoutPaid) {
await store.markCheckoutPaid(event.checkoutSessionId, event.paymentId);
}
return {
handled: true,
settled: true,
duplicate: !inserted,
payment_id: event.paymentId,
checkout_session_id: event.checkoutSessionId,
account_id: accountId,
email
};
}