import {z} from 'zod' export type Entity = {id?: string | undefined, updatedDate?: string | undefined, createdDate?: string | undefined}; export type AuditLog = {changeType?: string | undefined, changeDate?: string | undefined, objectType?: ('ACCOUNT' | 'ACCOUNT_EMAIL' | 'BLOCKING_STATES' | 'BUNDLE' | 'CUSTOM_FIELD' | 'INVOICE' | 'PAYMENT' | 'TRANSACTION' | 'INVOICE_ITEM' | 'INVOICE_PAYMENT' | 'SUBSCRIPTION' | 'SUBSCRIPTION_EVENT' | 'SERVICE_BROADCAST' | 'PAYMENT_ATTEMPT' | 'PAYMENT_METHOD' | 'TAG' | 'TAG_DEFINITION' | 'TENANT' | 'TENANT_KVS') | undefined, objectId?: string | undefined, changedBy?: string | undefined, reasonCode?: string | undefined, comments?: string | undefined, userToken?: string | undefined, history?: Entity | undefined}; export type Account = {accountId?: string | undefined, name?: string | undefined, firstNameLength?: number | undefined, externalKey?: string | undefined, email?: string | undefined, billCycleDayLocal?: number | undefined, currency?: ('AED' | 'AFN' | 'ALL' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BRL' | 'BSD' | 'BTN' | 'BWP' | 'BYR' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CUC' | 'CUP' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'FJD' | 'FKP' | 'GBP' | 'GEL' | 'GGP' | 'GHS' | 'GIP' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'IMP' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JEP' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LBP' | 'LKR' | 'LRD' | 'LSL' | 'LTL' | 'LVL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRO' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLL' | 'SOS' | 'SPL' | 'SRD' | 'STD' | 'SVC' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TMT' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TVD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VEF' | 'VND' | 'VUV' | 'WST' | 'XAF' | 'XCD' | 'XDR' | 'XOF' | 'XPF' | 'YER' | 'ZAR' | 'ZMW' | 'ZWD' | 'BTC') | undefined, parentAccountId?: string | undefined, isPaymentDelegatedToParent?: boolean | undefined, paymentMethodId?: string | undefined, referenceTime?: string | undefined, timeZone?: string | undefined, address1?: string | undefined, address2?: string | undefined, postalCode?: string | undefined, company?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, locale?: string | undefined, phone?: string | undefined, notes?: string | undefined, isMigrated?: boolean | undefined, accountBalance?: number | undefined, accountCBA?: number | undefined, auditLogs?: Array | undefined}; export type PluginProperty = {key?: string | undefined, value?: string | undefined, isUpdatable?: boolean | undefined}; export type PaymentMethodPluginDetail = {externalPaymentMethodId?: string | undefined, isDefaultPaymentMethod?: boolean | undefined, properties?: Array | undefined}; export type PaymentMethod = {paymentMethodId?: string | undefined, externalKey?: string | undefined, accountId?: string | undefined, isDefault?: boolean | undefined, pluginName?: string | undefined, pluginInfo?: PaymentMethodPluginDetail | undefined, auditLogs?: Array | undefined}; export type HostedPaymentPageFields = {formFields?: Array | undefined}; export type ComboHostedPaymentPage = {account?: Account | undefined, paymentMethod?: PaymentMethod | undefined, hostedPaymentPageFields?: HostedPaymentPageFields | undefined, paymentMethodPluginProperties?: Array | undefined, auditLogs?: Array | undefined}; export type CreateApi10KbPaymentGatewaysHostedFormArgs = {controlPluginName?: Array | undefined, pluginProperty?: Array | undefined, 'X-Killbill-CreatedBy': string, 'X-Killbill-Reason'?: string | undefined, 'X-Killbill-Comment'?: string | undefined, body: ComboHostedPaymentPage}; export type CreateApi10KbPaymentGatewaysHostedFormOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const entity = z.object({id: z.string().optional(), updatedDate: z.string().optional(), createdDate: z.string().optional()}); export const auditLog = z.object({changeType: z.string().optional(), changeDate: z.string().optional(), objectType: z.enum(["ACCOUNT", "ACCOUNT_EMAIL", "BLOCKING_STATES", "BUNDLE", "CUSTOM_FIELD", "INVOICE", "PAYMENT", "TRANSACTION", "INVOICE_ITEM", "INVOICE_PAYMENT", "SUBSCRIPTION", "SUBSCRIPTION_EVENT", "SERVICE_BROADCAST", "PAYMENT_ATTEMPT", "PAYMENT_METHOD", "TAG", "TAG_DEFINITION", "TENANT", "TENANT_KVS"]).optional(), objectId: z.string().optional(), changedBy: z.string().optional(), reasonCode: z.string().optional(), comments: z.string().optional(), userToken: z.string().optional(), history: entity.optional()}); export const hostedPaymentPageFormDescriptor = z.object({kbAccountId: z.string().optional(), formMethod: z.string().optional(), formUrl: z.string().optional(), formFields: z.record(z.string(), z.object({})).optional(), properties: z.record(z.string(), z.object({})).optional(), auditLogs: z.array(auditLog).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 }; /** * Combo API to generate form data to redirect the customer to the gateway * * POST /1.0/kb/paymentGateways/hosted/form * * Also accepts auth: Killbill_Api_Key, Killbill_Api_Secret */ export const createApi10KbPaymentGatewaysHostedForm = async (args: CreateApi10KbPaymentGatewaysHostedFormArgs, options: CreateApi10KbPaymentGatewaysHostedFormOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.controlPluginName ?? []) query.append("controlPluginName", String(item)) for (const item of args.pluginProperty ?? []) query.append("pluginProperty", String(item)) if (args['X-Killbill-CreatedBy'] != null) headers.set("X-Killbill-CreatedBy", String(args['X-Killbill-CreatedBy'])) if (args['X-Killbill-Reason'] != null) headers.set("X-Killbill-Reason", String(args['X-Killbill-Reason'])) if (args['X-Killbill-Comment'] != null) headers.set("X-Killbill-Comment", String(args['X-Killbill-Comment'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/kb/paymentGateways/hosted/form${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(hostedPaymentPageFormDescriptor, await res.json()) };