import {z} from 'zod' export type PhoneNumber = {country_code?: string | undefined, number?: string | undefined}; export type CustomerSummary = {registration_date?: string | undefined, first_transaction_date?: string | undefined, last_payment_date?: string | undefined, total_order_count?: number | undefined, last_payment_amount?: number | undefined, is_premium_customer?: boolean | undefined, is_returning_customer?: boolean | undefined, lifetime_value?: number | undefined}; export type CustomerRequest = {id?: string | undefined, email?: string | undefined, name?: string | undefined, tax_number?: string | undefined, phone?: PhoneNumber | undefined, summary?: CustomerSummary | undefined}; export type LocalBillingDescriptor = {name: string, character_set: 'kanji' | 'katakana'}; export type BillingDescriptor = {name: string, city: string, reference?: string | undefined, local_descriptors?: (Array | null) | 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 TrackingInfo = {return_shipping_company?: string | undefined, return_tracking_number?: string | undefined, return_tracking_uri?: string | undefined, shipping_company?: string | undefined, tracking_number?: string | undefined, tracking_uri?: string | 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 AmountAllocations = Array<{id: string, amount: number, reference?: string | undefined, commission?: ({amount?: number | undefined, percentage?: number | undefined}) | undefined}>; export type CaptureRequestProcessingSettings = {order_id?: string | undefined, otp_value?: string | undefined, tax_amount?: number | undefined, surcharge_amount?: number | undefined, discount_amount?: number | undefined, duty_amount?: number | undefined, shipping_amount?: number | undefined, shipping_tax_amount?: number | undefined, purchase_country?: string | undefined, foreign_retailer_amount?: number | undefined}; export type CaptureRequest = {amount?: number | undefined, capture_type?: ('NonFinal' | 'Final') | undefined, reference?: string | undefined, customer?: CustomerRequest | undefined, description?: string | undefined, billing_descriptor?: BillingDescriptor | undefined, shipping?: ({address?: Address | undefined, phone?: PhoneNumber | undefined, from_address_zip?: string | undefined, tracking_info?: Array | undefined}) | undefined, items?: Items | undefined, amount_allocations?: AmountAllocations | undefined, processing?: CaptureRequestProcessingSettings | undefined, metadata?: Record | undefined}; export type CreateApiPaymentsIdCapturesArgs = {'Cko-Idempotency-Key'?: string | undefined, id: string, body: CaptureRequest}; export type CreateApiPaymentsIdCapturesOptions = {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 createApiPaymentsIdCapturesResponse = 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 }; /** * Capture a payment * * POST /payments/{id}/captures * * Also accepts auth: ApiSecretKey */ export const createApiPaymentsIdCaptures = async (args: CreateApiPaymentsIdCapturesArgs, options: CreateApiPaymentsIdCapturesOptions) => { 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))}/captures${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(createApiPaymentsIdCapturesResponse, await res.json()) };