import {z} from 'zod' export type AmountAllocations = Array<{id: string, amount: number, reference?: string | undefined, commission?: ({amount?: number | undefined, percentage?: number | undefined}) | undefined}>; export type Items = Array<{type?: ('digital' | 'discount' | 'physical') | undefined, sub_type?: ('blockchain' | 'cbdc' | 'cryptocurrency' | 'nft' | 'stablecoin') | undefined, name?: string | undefined, quantity?: number | undefined, unit_price?: number | undefined, reference?: string | undefined, commodity_code?: string | undefined, unit_of_measure?: string | undefined, total_amount?: number | undefined, tax_rate?: number | undefined, tax_amount?: number | undefined, tax_exempt?: boolean | undefined, discount_amount?: number | undefined, wxpay_goods_id?: string | undefined, url?: string | undefined, image_url?: string | undefined, service_ends_on?: string | undefined, purchase_country?: string | undefined, foreign_retailer_amount?: number | undefined}>; export type Address = {address_line1?: string | undefined, address_line2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined}; export type PhoneNumber = {country_code?: string | undefined, number?: string | undefined}; export type AccountHolderIdentification = {type?: ('passport' | 'driving_licence' | 'national_id' | 'company_registration' | 'tax_id') | undefined, number?: string | undefined, issuing_country?: string | undefined}; export type RefundDestinationAccountHolder = {type?: ('individual' | 'corporate' | 'government') | undefined, first_name: string, last_name: string, company_name?: string | undefined, tax_id?: string | undefined, date_of_birth?: string | undefined, country_of_birth?: string | undefined, residential_status?: ('resident' | 'non_resident') | undefined, billing_address?: Address | undefined, phone?: PhoneNumber | undefined, identification?: AccountHolderIdentification | undefined, email?: string | undefined}; export type BankDetails = {name?: string | undefined, branch?: string | undefined, address?: Address | undefined}; export type RefundBankAccountDestination = {account_type?: ('savings' | 'current' | 'cash') | undefined, account_number: string, bank_code: string, branch_code?: string | undefined, iban?: string | undefined, bban?: string | undefined, swift_bic?: string | undefined, country: string, account_holder: RefundDestinationAccountHolder, bank?: BankDetails | undefined}; export type RefundRequest = {amount?: number | undefined, reference?: string | undefined, amount_allocations?: AmountAllocations | undefined, capture_action_id?: string | undefined, items?: Items | undefined, destination?: RefundBankAccountDestination | undefined, metadata?: Record | undefined}; export type CreateApiPaymentsIdRefundsArgs = {'Cko-Idempotency-Key'?: string | undefined, id: string, body: RefundRequest}; export type CreateApiPaymentsIdRefundsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const actionId = z.string().min(30).max(30); export const link = z.object({href: z.string()}); export const createApiPaymentsIdRefundsResponse = z.object({action_id: actionId, _links: z.object({payment: link}).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 }; /** * Refund a payment * * POST /payments/{id}/refunds * * Also accepts auth: ApiSecretKey */ export const createApiPaymentsIdRefunds = async (args: CreateApiPaymentsIdRefundsArgs, options: CreateApiPaymentsIdRefundsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Cko-Idempotency-Key'] != null) headers.set("Cko-Idempotency-Key", String(args['Cko-Idempotency-Key'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.id))}/refunds${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(createApiPaymentsIdRefundsResponse, await res.json()) };