import {z} from 'zod' export type CurrencyEnum = 'KES' | 'GHS' | 'NGN' | 'UGX' | 'TZS' | 'XAF' | 'XOF'; export type CardTarrifEnum = 'BUSINESS-PAYS' | 'CUSTOMER-PAYS'; export type AutosendRemiderEnum = 0 | 1 | 2 | 3 | 7; export type PayRequestsCreateSer = {amount: unknown, first_name: string, last_name: string, reason: string, currency: CurrencyEnum, email: string, card_tarrif?: CardTarrifEnum | undefined, autosend_remider: AutosendRemiderEnum}; export type CreateApiApiV1PaymentRequestsArgs = {body: PayRequestsCreateSer}; export type CreateApiApiV1PaymentRequestsOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyEnum = z.enum(["KES", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const cardTarrifEnum = z.enum(["BUSINESS-PAYS", "CUSTOMER-PAYS"]); export const autosendRemiderEnum = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(7)]); export const payRequestsCreateSer = z.object({amount: z.string(), first_name: z.string(), last_name: z.string(), reason: z.string(), currency: currencyEnum, email: z.string(), card_tarrif: cardTarrifEnum.optional(), autosend_remider: autosendRemiderEnum}); 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 Payment requests * * POST /api/v1/payment-requests/ */ export const createApiApiV1PaymentRequests = async (args: CreateApiApiV1PaymentRequestsArgs, options?: CreateApiApiV1PaymentRequestsOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) 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(payRequestsCreateSer, await res.json()) };