import type { StorageAdapter, PaymentProviderAdapter, PayLogger, PayHooks, WebhookEvent } from "../interfaces.js"; import type { ID } from "../types.js"; import type { Operation } from "../schemas.js"; import type { OperationsDomain, SettleOptions } from "./operations.js"; /** Input for processing an inbound webhook from a payment provider. */ export interface ProcessWebhookInput { /** Registered provider name (must match an adapter in `providers`). */ provider: string; /** Raw request body as received from the provider (used for signature verification). */ rawBody: string | Buffer; /** Request headers (used for provider signature verification). */ headers: Record; /** Optional pre-verified event to avoid duplicate signature checks/parsing. */ event?: WebhookEvent; /** Optional settle options forwarded to operations.settle(). */ settleOptions?: Omit; /** * Extra metadata to merge into the operation upon settlement. * Forwarded to {@link SettleOptions.extraMetadata}. * Use this to attach provider-specific context (e.g. payer identity from a * webhook payload) that the provider adapter does not persist itself. */ extraMetadata?: Record; } /** Result of a successfully processed webhook. */ export interface ProcessWebhookResult { /** The operation that was settled or failed as a result of this webhook. */ operation: Operation; /** The verified event parsed from the webhook payload. */ event: WebhookEvent; } /** * Webhook processing domain — verifies and handles inbound provider callbacks, * automatically settling or failing the matching pending operation. */ export interface WebhooksDomain { /** * Verify and process a webhook from a payment provider. * * 1. Looks up the registered adapter for `input.provider`. * 2. Verifies the webhook signature (unless `input.event` is pre-supplied). * 3. Finds the matching pending operation by `externalId`. * 4. Calls {@link OperationsDomain.settle} or {@link OperationsDomain.fail} * based on the event status. * * @param input - Webhook request data. * @returns The settled or failed operation. * @throws {ProviderNotConfiguredError} if the provider is not registered. * @throws {WebhookVerificationError} if the signature verification fails. * @throws {OperationNotFoundError} if no operation matches the `externalId` in the event. * * @example * ```ts * // Express route handler * app.post("/webhooks/:provider", async (req, res) => { * const op = await pay.webhooks.handle({ * provider: req.params.provider, * rawBody: req.body, * headers: req.headers as Record, * }); * res.json({ operationId: op.id, status: op.status }); * }); * ``` */ handle(input: ProcessWebhookInput): Promise; /** * Verify and process a webhook, returning both the settled operation and the * parsed provider event. * * Identical to {@link handle} but also returns the parsed {@link WebhookEvent}, * useful when you need the provider event for downstream logic (e.g. logging, * custom metadata). * * @param input - Webhook request data. * @returns An object containing the operation and the parsed event. * @throws {ProviderNotConfiguredError} if the provider is not registered. * @throws {WebhookVerificationError} if the signature verification fails. * @throws {OperationNotFoundError} if no operation matches the event's `externalId`. */ handleWithEvent(input: ProcessWebhookInput): Promise; } export declare function createWebhooksDomain(tenantId: ID, storage: StorageAdapter, providers: Map, logger?: PayLogger, hooks?: PayHooks, operations?: Pick): WebhooksDomain; //# sourceMappingURL=webhooks.d.ts.map