import {z} from 'zod' export type CaptureRequest = {amount: number}; export type CreateApiV1TransactionsIdCaptureArgs = {id: string, 'Idempotency-Key'?: string | undefined, 'User-Agent': string, body: CaptureRequest}; export type CreateApiV1TransactionsIdCaptureOptions = {baseUrl: string, EcommerceTransactionPaymentsPublicOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentsGatewayServiceCaptureIssuerResult = z.object({approval_code: z.string().optional(), response_code: z.string().optional()}); export const captureResponse = z.object({transaction_id: z.string(), card_acceptor_id: z.string(), status: z.enum(["SUCCESS", "FAILURE", "PENDING"]), status_reason: z.enum(["ACCOUNT_INVALID", "CANNOT_VALIDATE_AUTHENTICATION", "CARD_COUNT_EXCEEDED", "CARD_DECLINED", "CARD_EXPIRED", "CARD_LIMIT_EXCEEDED", "CARD_NOT_ACTIVE", "DISABLED_ACCOUNT", "DO_NOT_HONOR", "ECOM_NEGATIVE_CARD_AUTHENTICATION_VERIFICATION", "EXCEEDS_AMOUNT_LIMIT", "FORMAT_ERROR", "INCORRECT_CARD_AUTHENTICATION", "INSUFFICIENT_FUNDS", "INVALID_CARD_NUMBER", "INVALID_TRANSACTION", "ISSUER_DECLINED", "LOST_OR_STOLEN", "MERCHANT_CONFIGURATION", "PIN_LIMIT_EXCEEDED", "PROCESSOR_UNAVAILABLE", "RESTRICTED_CARD", "SCA_PIN_REQUIRED", "SCA_REQUIRED", "SUSPECTED_FRAUD", "TEMPORARY_ISSUE_RETRY", "TIMEOUT", "TRANSACTION_NOT_ALLOWED", "UNABLE_TO_ROUTE_TRANSACTION", "UNSUPPORTED_ISSUER", "NEGATIVE_BALANCE_LIMIT", "OTHER"]).optional(), issuer_result: paymentsGatewayServiceCaptureIssuerResult.optional(), processed_at: z.string().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 }; /** * Capture a pre-authorised transaction * * POST /v1/transactions/{id}/capture */ export const createApiV1TransactionsIdCapture = async (args: CreateApiV1TransactionsIdCaptureArgs, options: CreateApiV1TransactionsIdCaptureOptions) => { const {baseUrl, fetch: fetchFn = fetch, EcommerceTransactionPaymentsPublicOauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (EcommerceTransactionPaymentsPublicOauth !== undefined) headers.set('authorization', `Bearer ${EcommerceTransactionPaymentsPublicOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transactions/${encodeURIComponent(String(args.id))}/capture${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(captureResponse, await res.json()) };