import {z} from 'zod' export type PaymentRequestInput = {merchant_id?: string | undefined, customer: {name: string, email: string | '' | null, phone_number?: string | undefined}, billingAddress: {address_line1: string, address_line2?: string | undefined, apartment?: string | undefined, city: string, state_province?: string | undefined, country: string, postal_code: string, company_name?: string | undefined}, amount: number, currency: string, payment_reference?: string | undefined, order_id?: string | undefined, locale: string, platform: string, device_type?: string | undefined, external_payment_request_id?: string | undefined, test_transaction?: boolean | undefined, group?: string | undefined, gid?: string | undefined, selected_bank_id?: string | undefined, session_id?: string | undefined, success_url_redirect?: string | undefined, fail_url_redirect?: string | undefined, simulation?: ({straddle?: ({customer_outcome?: ('standard' | 'verified' | 'rejected' | 'review') | undefined, paykey_outcome?: ('standard' | 'active' | 'rejected') | undefined, charge_outcome?: ('standard' | 'paid' | 'on_hold_daily_limit' | 'cancelled_for_fraud_risk' | 'cancelled_for_balance_check' | 'failed_insufficient_funds' | 'reversed_insufficient_funds' | 'failed_customer_dispute' | 'reversed_customer_dispute' | 'failed_closed_bank_account' | 'reversed_closed_bank_account') | undefined, balance_check?: ('required' | 'enabled' | 'disabled') | undefined}) | undefined, flinks?: ({outcome?: ('happy' | 'async' | 'no_us_account' | 'partial_data' | 'webhook_only' | 'rate_limit' | 'unreachable') | undefined, account?: ({total: number, existing?: Array | undefined}) | undefined, mfa?: ('skip' | 'correct' | 'wrong') | undefined}) | undefined}) | undefined} | {[key: string]: unknown}; export type CreateApiApiV1PaymentRequestsArgs = {body: PaymentRequestInput}; export type CreateApiApiV1PaymentRequestsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentRequestResponse = z.object({success: z.boolean(), data: z.object({payment_link: z.string()}).and(z.record(z.string(), z.unknown()))}); 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 }; /** * Create a new payment request * * POST /api/v1/payment-requests */ export const createApiApiV1PaymentRequests = async (args: CreateApiApiV1PaymentRequestsArgs, options?: CreateApiApiV1PaymentRequestsOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment-requests${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(paymentRequestResponse, await res.json()) };