import {z} from 'zod' export type Phone = {calling_code?: string | undefined, number?: string | undefined}; export type CustomerDetails = {name: string, email: string, country: string, phone?: Phone | undefined}; export type Address = {line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, postal_code?: string | undefined}; export type ShippingDetails = {name?: string | undefined, address?: Address | undefined, phone?: Phone | undefined, label?: string | undefined}; export type BillingDetails = {name?: string | undefined, address?: Address | undefined, phone?: Phone | undefined, label?: string | undefined}; export type MetadataInput = unknown; export type UpdateApiV3PayinIdArgs = {id: string, body: {customer_details?: CustomerDetails | undefined, customer?: string | undefined, success_url?: string | undefined, cancel_url?: string | undefined, shipping_details?: ShippingDetails | undefined, billing_details?: BillingDetails | undefined, transaction_documents?: unknown | undefined, metadata?: MetadataInput | undefined, reference_id?: string | undefined, statement_descriptor?: string | undefined, payment_method_details?: unknown | undefined}}; export type UpdateApiV3PayinIdOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional()}); export const phone = z.object({calling_code: z.string().optional(), number: z.string().optional()}); export const billingDetails = z.object({name: z.string().optional(), address: address.optional(), phone: phone.optional(), label: z.string().optional()}); export const customerDetails = z.object({name: z.string(), email: z.string(), country: z.string(), phone: phone.optional()}); export const metadata = z.record(z.string(), z.string()).nullable(); export const shippingDetails = z.object({name: z.string().optional(), address: address.optional(), phone: phone.optional(), label: z.string().optional()}); export const payinData = z.object({amount: z.number().int().optional(), amount_paid: z.number().int().optional(), billing_details: billingDetails.optional(), cancel_url: z.string().optional(), cancelled_at: z.string().optional(), client_token: z.string().optional(), confirm: z.boolean().optional(), created_at: z.string().optional(), customer: z.string().optional(), customer_details: customerDetails.optional(), form: z.string().optional(), holding_currency: z.string().optional(), id: z.string().optional(), invoice_currency: z.string().optional(), items: z.array(z.unknown()).optional(), latest_payment_attempt: z.string().optional(), latest_payment_attempt_data: z.object({}).optional(), metadata: metadata.optional(), object: z.string().optional(), paid_in_excess: z.boolean().optional(), partially_paid: z.boolean().optional(), payment_attempts: z.array(z.unknown()).optional(), payment_method_details: z.object({}).optional(), reference_id: z.string().optional(), shipping_details: shippingDetails.optional(), statement_descriptor: z.string().optional(), status: z.string().optional(), status_description: z.string().optional(), success_url: z.string().optional(), transaction_data: z.array(z.unknown()).optional(), transaction_description: z.string().optional(), transaction_documents: z.array(z.unknown()).optional(), webhook_url: z.string().optional(), on_behalf_of: z.string().optional()}); export const updateApiV3PayinIdResponse = z.object({status: z.string().optional(), message: z.string().optional(), data: payinData.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 }; /** * Updating a payin * * PUT /v3/payin/{id} */ export const updateApiV3PayinId = async (args: UpdateApiV3PayinIdArgs, options?: UpdateApiV3PayinIdOptions) => { const {baseUrl = "https://service-sandbox.tazapay.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/payin/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV3PayinIdResponse, await res.json()) };