import {z} from 'zod' export type ItemObject = {id?: string | undefined, name?: string | undefined, reference?: string | undefined, price?: number | undefined, quantity?: number | undefined, discount?: number | undefined, taxRate?: number | undefined, totalPrice?: number | undefined}; export type CustomerObject = {id?: string | undefined, name?: string | undefined, phone?: string | undefined, email?: string | undefined, address?: string | undefined, city?: string | undefined, postalCode?: string | undefined, country: string, locale?: string | undefined, createdAt?: string | undefined, updatedAt?: string | undefined}; export type BrowserInfo = {browserAcceptHeader?: string | undefined, browserJavaEnabled?: boolean | undefined, browserLanguage?: string | undefined, browserTZ?: string | undefined, browserUserAgent?: string | undefined, browserColorDepth?: string | undefined, browserScreenHeight?: string | undefined, browserScreenWidth?: string | undefined, browserJavascriptEnabled?: boolean | undefined, browserIP?: string | undefined, challengeWindowSize?: string | undefined}; export type CardObject = {holderName?: string | undefined, number?: string | undefined, expMonth?: number | undefined, expYear?: number | undefined, cvv?: number | undefined, browserInfo?: BrowserInfo | undefined}; export type TokenizedCardObject = {cardId?: string | undefined, cvv?: number | undefined}; export type PaymentObjectRequest = {amount?: number | undefined, currency?: string | undefined, orderDetails?: Array | undefined, paymentReference?: string | undefined, deviceId?: string | undefined, customerId?: string | undefined, customerObject?: CustomerObject | undefined, country?: string | undefined, cardObject?: CardObject | undefined, tokenizedCardObject?: TokenizedCardObject | undefined, authorization?: boolean | undefined, merchantReference?: string | undefined, successRedirectUrl?: string | undefined, errorRedirectUrl?: string | undefined, allowUpdateCustomer?: boolean | undefined} | {invoiceId?: string | undefined, country?: string | undefined, cardObject?: CardObject | undefined, tokenizedCardObject?: TokenizedCardObject | undefined, authorization?: boolean | undefined, merchantReference?: string | undefined, successRedirectUrl?: string | undefined, errorRedirectUrl?: string | undefined, allowUpdateCustomer?: boolean | undefined} | {paymentLinkId?: string | undefined, country?: string | undefined, cardObject?: CardObject | undefined, tokenizedCardObject?: TokenizedCardObject | undefined, authorization?: boolean | undefined, merchantReference?: string | undefined, successRedirectUrl?: string | undefined, errorRedirectUrl?: string | undefined, allowUpdateCustomer?: boolean | undefined} | {orderId?: string | undefined, country?: string | undefined, cardObject?: CardObject | undefined, tokenizedCardObject?: TokenizedCardObject | undefined, authorization?: boolean | undefined, merchantReference?: string | undefined, successRedirectUrl?: string | undefined, errorRedirectUrl?: string | undefined, allowUpdateCustomer?: boolean | undefined}; export type CreateApiChargesArgs = {payment_type?: ('wave_money' | 'orange_money' | 'maxit' | 'mtn_money' | 'free_money' | 'moov' | 'mobicash' | 'togocell' | 'bictorys' | 'card') | undefined, 'X-Amzn-Trace-Id'?: string | undefined, body: PaymentObjectRequest}; export type CreateApiChargesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const confirmationObjectType = z.enum(["MobilePaymentObject", "CardPaymentObject"]); export const enum3DSState = z.enum(["AWAIT_3DS", "PURCHASED", "CAPTURED", "AUTHORIZED", "FAILED", "DECLINED"]); export const aCS3DSObject = z.object({acsUrl: z.string().optional(), acsPaReq: z.string().optional(), acsMd: z.string().optional(), acsTerm: z.string().optional()}); export const aCS3DS2Challenge = z.object({acsURL: z.string().optional(), creq: z.string().optional()}); export const confirmationLinkObject = z.union([z.object({transactionId: z.string().optional(), redirectUrl: z.string().optional(), merchantReference: z.string().optional(), type: confirmationObjectType.optional(), link: z.string().optional(), qrCode: z.string().optional(), message: z.string().optional()}), z.object({transactionId: z.string().optional(), redirectUrl: z.string().optional(), merchantReference: z.string().optional(), type: confirmationObjectType.optional(), state: enum3DSState.optional(), "3ds": aCS3DSObject.optional(), "3ds2": aCS3DS2Challenge.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 }; /** * Initiate a payment for a merchant * * POST /charges */ export const createApiCharges = async (args: CreateApiChargesArgs, options?: CreateApiChargesOptions) => { const {baseUrl = "https://api.test.bictorys.com/pay/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.payment_type != null) query.append("payment_type", String(args.payment_type)) if (args['X-Amzn-Trace-Id'] != null) headers.set("X-Amzn-Trace-Id", String(args['X-Amzn-Trace-Id'])) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/charges${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(confirmationLinkObject, await res.json()) };