import {z} from 'zod' export type CreateApiEventsMerchantIdEventIdRetryArgs = {merchant_id: string, event_id: string}; export type CreateApiEventsMerchantIdEventIdRetryOptions = {baseUrl: string, adminApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eventType = z.enum(["payment_succeeded", "payment_failed", "payment_processing", "payment_cancelled", "payment_cancelled_post_capture", "payment_authorized", "payment_partially_authorized", "payment_captured", "payment_expired", "action_required", "refund_succeeded", "refund_failed", "dispute_opened", "dispute_expired", "dispute_accepted", "dispute_cancelled", "dispute_challenged", "dispute_won", "dispute_lost", "mandate_active", "mandate_revoked", "payout_success", "payout_failed", "payout_initiated", "payout_processing", "payout_cancelled", "payout_expired", "payout_reversed", "invoice_paid", "surcharge_payment_succeeded", "surcharge_refund_succeeded"]); export const eventClass = z.enum(["payments", "refunds", "disputes", "mandates", "payouts", "subscriptions"]); export const outgoingWebhookRequestContent = z.object({body: z.string(), headers: z.array(z.array(z.string()))}); export const outgoingWebhookResponseContent = z.object({body: z.string().nullable().optional(), headers: z.array(z.array(z.string())).nullable().optional(), status_code: z.number().int().gte(0).nullable().optional(), error_message: z.string().nullable().optional()}); export const webhookDeliveryAttempt = z.enum(["initial_attempt", "automatic_retry", "manual_retry"]); export const eventRetrieveResponse = z.object({event_id: z.string().max(64), merchant_id: z.string().max(64), profile_id: z.string().max(64), object_id: z.string().max(64), event_type: eventType, event_class: eventClass, is_delivery_successful: z.boolean().nullable().optional(), initial_attempt_id: z.string().max(64), processor_merchant_id: z.string().max(64).nullable().optional(), created: z.string(), request: outgoingWebhookRequestContent, response: outgoingWebhookResponseContent, delivery_attempt: webhookDeliveryAttempt.nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Events - Manual Retry * * POST /events/{merchant_id}/{event_id}/retry */ export const createApiEventsMerchantIdEventIdRetry = async (args: CreateApiEventsMerchantIdEventIdRetryArgs, options: CreateApiEventsMerchantIdEventIdRetryOptions) => { const {baseUrl, fetch: fetchFn = fetch, adminApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (adminApiKey !== undefined) headers.set("api-key", adminApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/events/${encodeURIComponent(String(args.merchant_id))}/${encodeURIComponent(String(args.event_id))}/retry${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(eventRetrieveResponse, await res.json()) };