import {z} from 'zod' export type IntaSendXBSTKPushInit = {amount: unknown, phone_number: string, currency: 'UGX' | 'TZS', api_ref?: (string | null) | undefined, wallet_id?: (string | null) | undefined, mobile_tarrif?: ('BUSINESS-PAYS' | 'CUSTOMER-PAYS' | '' | unknown | null) | undefined}; export type CreateApiApiV1PaymentIntasendXbPushArgs = {body: IntaSendXBSTKPushInit}; export type CreateApiApiV1PaymentIntasendXbPushOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoicesSerStateEnum = z.enum(["PENDING", "PROCESSING", "FAILED", "CANCELED", "PARTIAL", "COMPLETE", "RETRY"]); export const provider906Enum = z.enum(["M-PESA", "PESALINK", "CARD-PAYMENT", "GOOGLE-PAY", "APPLE-PAY", "BITCOIN", "BANK-ACH", "COOP_B2B"]); export const invoicesSer = z.object({invoice_id: z.string(), state: invoicesSerStateEnum.optional(), provider: provider906Enum, charges: z.string().optional(), net_amount: z.string(), currency: z.string(), value: z.string(), account: z.string().max(140).nullable().optional(), api_ref: z.string().max(140).nullable().optional(), clearing_status: z.string(), mpesa_reference: z.string(), host: z.string().max(200).nullable().optional(), card_info: z.string(), retry_count: z.number().int().gte(-2147483648).lte(2147483647).optional(), failed_reason: z.string().nullable().optional(), failed_code: z.string().max(45).nullable().optional(), failed_code_link: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const paymentCustomersSer = z.object({customer_id: z.string(), phone_number: z.string().max(140).nullable().optional(), email: z.string().max(254).nullable().optional(), first_name: z.string().max(45).nullable().optional(), last_name: z.string().max(45).nullable().optional(), country: z.string().max(2).nullable().optional(), zipcode: z.string().max(10).nullable().optional(), provider: z.union([z.enum(["M-PESA", "PESALINK", "CARD-PAYMENT", "GOOGLE-PAY", "APPLE-PAY", "BITCOIN", "BANK-ACH", "COOP_B2B"]), z.literal(""), z.unknown()]).nullable().optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const currencyEnum = z.enum(["KES", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const mobileTarrifEnum = z.enum(["BUSINESS-PAYS", "CUSTOMER-PAYS"]); export const cardTarrifEnum = z.enum(["BUSINESS-PAYS", "CUSTOMER-PAYS"]); export const paymentLinksSer = z.object({id: z.string().optional(), title: z.string().max(140), is_active: z.boolean().optional(), redirect_url: z.string().max(400).nullable().optional(), amount: z.number().int().gte(0).optional(), usage_limit: z.number().int().gte(0).optional(), qrcode_file: z.string().nullable(), url: z.string(), currency: currencyEnum.optional(), mobile_tarrif: mobileTarrifEnum.optional(), card_tarrif: cardTarrifEnum.optional(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const paymentSessionsSer = z.object({id: z.string().optional(), invoice: invoicesSer, customer: paymentCustomersSer, payment_link: paymentLinksSer, customer_comment: z.string().nullable().optional(), refundable: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); 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 }; /** * Send IntaSend-XB STK Push * * POST /api/v1/payment/intasend-xb-push/ */ export const createApiApiV1PaymentIntasendXbPush = async (args: CreateApiApiV1PaymentIntasendXbPushArgs, options?: CreateApiApiV1PaymentIntasendXbPushOptions) => { 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/intasend-xb-push/${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(paymentSessionsSer, await res.json()) };