import {z} from 'zod' export type CardId = string; export type ApplicationSelection = 'credit' | 'debit'; export type Card = {cardId: CardId, applicationSelection?: ApplicationSelection | undefined}; export type Currency = string; export type Money = {amount?: number | undefined, currency?: Currency | undefined}; export type RetailTemplateRequest = {card: Card, transactionData: {transactionAmount: Money, settlementAmount?: Money | undefined}}; export type CreateApiV1AuthorizationTemplatesRequestRetailOfflinePinArgs = {auditUser: string, body: RetailTemplateRequest}; export type CreateApiV1AuthorizationTemplatesRequestRetailOfflinePinOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const cardId = z.string().max(39); export const applicationSelection = z.enum(["credit", "debit"]); export const card = z.object({cardId: cardId, applicationSelection: applicationSelection.optional()}); export const currency = z.string(); export const money = z.object({amount: z.number().optional(), currency: currency.optional()}); export const transactionType = z.enum(["RETAIL", "CASH", "ATM", "CREDIT", "UNIQUE", "P2P_CREDIT", "P2P_DEBIT", "BALANCE_INQUIRY", "CASH_DISBURSEMENT"]); export const cardEntryMode = z.enum(["MANUAL_ENTRY", "MAGNETIC_STRIPE_READ", "CHIP_READ", "CONTACTLESS", "ELECTRONIC_COMMERCE"]); export const transactionData = z.object({transactionAmount: money, settlementAmount: money.optional(), transactionType: transactionType, cardEntryMode: cardEntryMode}); export const threeDsData = z.object({authenticationValue: z.string().optional(), eci: z.string().optional(), dsTransactionId: z.string().optional(), protocolVersion: z.enum(["2.1.0", "2.2.0"]).optional()}); export const merchantId = z.string().max(15); export const subMerchantId = z.string().max(15); export const merchantName = z.string().max(25); export const merchantCity = z.string().max(13); export const country = z.string(); export const merchantCategoryCode = z.string().max(4); export const acquirerId = z.string().max(11); export const terminalId = z.string().max(8); export const partialApprovalCapable = z.boolean(); export const merchantData = z.object({merchantId: merchantId, subMerchantId: subMerchantId.optional(), merchantName: merchantName, merchantCity: merchantCity, merchantCountry: country, merchantCategoryCode: merchantCategoryCode, acquirerId: acquirerId, acquirerCountry: country, terminalId: terminalId, partialApprovalCapable: partialApprovalCapable}); export const authRequest = z.object({card: card, transactionData: transactionData, threeDsData: threeDsData.optional(), merchantData: merchantData}); 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 }; /** * Generate retail offline pin auth request template * * POST /v1/authorization-templates/request/retail-offline-pin */ export const createApiV1AuthorizationTemplatesRequestRetailOfflinePin = async (args: CreateApiV1AuthorizationTemplatesRequestRetailOfflinePinArgs, options: CreateApiV1AuthorizationTemplatesRequestRetailOfflinePinOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/authorization-templates/request/retail-offline-pin${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(authRequest, await res.json()) };