import {z} from 'zod' export type PaymentReversalReasonRos = {code: 'RB001' | 'RB002' | 'RB003' | 'RB004' | 'RB005' | 'RB008' | 'RB009' | 'RB010' | 'RB011' | 'RB012' | 'RB013' | 'RB017' | 'RB018' | 'RB023' | 'RB024' | 'RB026' | 'RB027' | 'RB028' | 'RB029' | 'RB030' | 'RB031' | 'RB032' | 'RB033' | 'RB034' | 'RB035' | 'RB036' | 'RB037' | 'AC01' | 'AC03' | 'AC04' | 'AC06' | 'AC08' | 'AC09' | 'AC11' | 'AC12' | 'AC13' | 'AC14' | 'AC15' | 'AG01' | 'AG02' | 'AG03' | 'AG05' | 'AG06' | 'AM02' | 'AM03' | 'AM04' | 'AM11' | 'AM13' | 'AM14' | 'AM15' | 'AM21' | 'AM22' | 'BE01' | 'BE04' | 'BE05' | 'BE06' | 'BE07' | 'BE08' | 'BE09' | 'BE10' | 'BE11' | 'BE12' | 'BE13' | 'BE14' | 'BE15' | 'BE16' | 'BE17' | 'BE18' | 'BE19' | 'BE20' | 'BE21' | 'BE22' | 'CERI' | 'CH11' | 'CH12' | 'CH13' | 'CH14' | 'CH21' | 'CNOR' | 'CUST' | 'DS24' | 'DU05' | 'DUPL' | 'ED06' | 'FF03' | 'FF04' | 'FF05' | 'FF06' | 'FF07' | 'FF08' | 'FF10' | 'MD06' | 'MD07' | 'MS02' | 'MS03' | 'NARR' | 'RC01' | 'RC02' | 'RC04' | 'RC05' | 'RC07' | 'RC08' | 'RC10' | 'RC12' | 'RF01' | 'RR01' | 'RR02' | 'RR03' | 'RR04' | 'RR05' | 'RR06' | 'RR07' | 'RR10' | 'SL03', description?: string | undefined}; export type OrchestrationPaymentReversalRequest = {sender_end_to_end_id: string, original_payment_id: string, reversal_reason: PaymentReversalReasonRos}; export type CreateApiV4OrchestrationPaymentReversalArgs = {body: OrchestrationPaymentReversalRequest}; export type CreateApiV4OrchestrationPaymentReversalOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationPaymentSubmitResponse = z.object({sender_end_to_end_id: 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 }; /** * Reverse orchestration payment * * POST /v4/orchestration/payment/reversal */ export const createApiV4OrchestrationPaymentReversal = async (args: CreateApiV4OrchestrationPaymentReversalArgs, options: CreateApiV4OrchestrationPaymentReversalOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/v4/orchestration/payment/reversal${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(orchestrationPaymentSubmitResponse, await res.json()) };