CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 5s
Security Scan (SIAX Cloud) / security (push) Successful in 15s
CI (SIAX Cloud) / security (push) Successful in 16s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 7s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 21s
CI (SIAX Cloud) / security (pull_request) Successful in 25s
CI (SIAX Cloud) / quality (push) Successful in 1m17s
CI (SIAX Cloud) / integration (pull_request) Successful in 54s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m7s
115 lines
3.9 KiB
TypeScript
115 lines
3.9 KiB
TypeScript
import { SiaxHttp } from '../clients/http';
|
|
import type { SiaxRequestContext } from '../platform/siax-context';
|
|
import type { CreditCommand } from './credit-ledger';
|
|
|
|
/** This snapshot must come from an immutable server-owned order, never from a browser or webhook payload. */
|
|
export interface CreditPurchaseSnapshot {
|
|
tenantId: string;
|
|
purchaseId: string;
|
|
checkoutSessionId: string;
|
|
amountMinor: number;
|
|
currency: string;
|
|
credits: number;
|
|
}
|
|
|
|
interface Checkout {
|
|
id: string;
|
|
tenant_id: string;
|
|
payment_intent_id: string;
|
|
payable_type: string;
|
|
payable_ref: string;
|
|
amount_snapshot: number;
|
|
currency: string;
|
|
status: string;
|
|
provider?: string;
|
|
provider_reference?: string;
|
|
}
|
|
interface Intent {
|
|
id: string;
|
|
tenant_id: string;
|
|
payable_type: string;
|
|
payable_ref: string;
|
|
amount: number;
|
|
currency: string;
|
|
canonical_status: string;
|
|
captured_amount: number;
|
|
refunded_amount: number;
|
|
}
|
|
|
|
/**
|
|
* Re-read PAY0's canonical session and intent before deriving a grant command.
|
|
* This does not create an order, authenticate a caller, write to the ledger,
|
|
* or handle a later refund/chargeback. No customer-facing route uses it yet.
|
|
*/
|
|
export class Pay0CreditVerifier {
|
|
private readonly http: SiaxHttp;
|
|
constructor(baseUrl: string, serviceToken: string) {
|
|
if (!baseUrl.trim() || !serviceToken.trim())
|
|
throw new Error('PAY0 URL and service credential are required');
|
|
this.http = new SiaxHttp({ dependency: 'pay0', baseUrl, token: serviceToken });
|
|
}
|
|
|
|
async verify(snapshot: CreditPurchaseSnapshot, context: SiaxRequestContext): Promise<CreditCommand> {
|
|
if (
|
|
!snapshot.tenantId ||
|
|
!snapshot.purchaseId ||
|
|
!snapshot.checkoutSessionId ||
|
|
!Number.isSafeInteger(snapshot.amountMinor) ||
|
|
snapshot.amountMinor <= 0 ||
|
|
!Number.isSafeInteger(snapshot.credits) ||
|
|
snapshot.credits <= 0 ||
|
|
!/^[A-Z]{3}$/.test(snapshot.currency) ||
|
|
context.tenantId !== snapshot.tenantId
|
|
)
|
|
throw new Error('Invalid or mismatched credit purchase');
|
|
const checkoutResponse = await this.http.request({
|
|
method: 'GET',
|
|
path: `/v1/pay0/checkout-sessions/${encodeURIComponent(snapshot.checkoutSessionId)}`,
|
|
context,
|
|
});
|
|
const checkout = (checkoutResponse.json as { data?: Checkout } | undefined)?.data;
|
|
if (
|
|
!checkout ||
|
|
checkout.id !== snapshot.checkoutSessionId ||
|
|
checkout.tenant_id !== snapshot.tenantId ||
|
|
checkout.payable_type !== '0pt_credit_purchase' ||
|
|
checkout.payable_ref !== snapshot.purchaseId ||
|
|
checkout.amount_snapshot !== snapshot.amountMinor ||
|
|
checkout.currency !== snapshot.currency ||
|
|
checkout.status !== 'completed' ||
|
|
!checkout.payment_intent_id ||
|
|
!checkout.provider ||
|
|
!checkout.provider_reference
|
|
) {
|
|
throw new Error('PAY0 checkout does not match a completed credit purchase');
|
|
}
|
|
const intentResponse = await this.http.request({
|
|
method: 'GET',
|
|
path: `/v1/pay0/payment-intents/${encodeURIComponent(checkout.payment_intent_id)}`,
|
|
context,
|
|
});
|
|
const intent = (intentResponse.json as { data?: Intent } | undefined)?.data;
|
|
if (
|
|
!intent ||
|
|
intent.id !== checkout.payment_intent_id ||
|
|
intent.tenant_id !== snapshot.tenantId ||
|
|
intent.payable_type !== '0pt_credit_purchase' ||
|
|
intent.payable_ref !== snapshot.purchaseId ||
|
|
intent.amount !== snapshot.amountMinor ||
|
|
intent.currency !== snapshot.currency ||
|
|
intent.canonical_status !== 'captured' ||
|
|
intent.captured_amount !== snapshot.amountMinor ||
|
|
intent.refunded_amount !== 0
|
|
) {
|
|
throw new Error('PAY0 payment intent does not match an unrefunded captured purchase');
|
|
}
|
|
return {
|
|
tenantId: snapshot.tenantId,
|
|
idempotencyKey: `pay0:credit-purchase:${snapshot.purchaseId}`,
|
|
kind: 'verified_payment',
|
|
reference: `pay0:intent:${intent.id}`,
|
|
amount: snapshot.credits,
|
|
};
|
|
}
|
|
}
|