import {z} from 'zod' export type Customer = {id?: (string | null) | undefined, name?: (string | null) | undefined, emailAddress?: (string | null) | undefined, phoneNumber?: (string | null) | undefined}; export type CaptureMode = 'Auto' | 'Manual'; export type PaymentMethod = string; export type Address = {address1?: (string | null) | undefined, address2?: (string | null) | undefined, address3?: (string | null) | undefined, address4?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, postcode?: (string | null) | undefined, countryCode: string, locality?: (string | null) | undefined, county?: (string | null) | undefined}; export type ShippingDetails = {name?: (string | null) | undefined, deliveryNotes?: (string | null) | undefined, address?: (Address | null) | undefined}; export type PaymentIntentConfigCustomerEmail = {collectionRequired?: boolean | undefined}; export type PaymentIntentConfigDetails = {showTotal?: boolean | undefined, showReference?: boolean | undefined}; export type PaymentIntentConfigBillingAddress = {collectionRequired?: boolean | undefined}; export type PaymentIntentConfigShippingDetails = {collectionRequired?: boolean | undefined}; export type PaymentConfigDetails = {customAmountAllowed?: boolean | undefined, tipsAllowed?: boolean | undefined}; export type PaymentIntentConfigRequest = {title?: (string | null) | undefined, redirectUrl?: (string | null) | undefined, cancelUrl?: (string | null) | undefined, customerEmail?: (PaymentIntentConfigCustomerEmail | null) | undefined, details?: (PaymentIntentConfigDetails | null) | undefined, billingAddress?: (PaymentIntentConfigBillingAddress | null) | undefined, shippingDetails?: (PaymentIntentConfigShippingDetails | null) | undefined, payment?: (PaymentConfigDetails | null) | undefined}; export type Money = {value: number, currencyCode: string}; export type MerchantInitiatedTransactionType = string; export type OrderDetails = {availabilityDate?: (string | null) | undefined, isUpdate?: (boolean | null) | undefined, id?: (string | null) | undefined, externalId?: (string | null) | undefined}; export type CreateSetupIntentRequest = {reference: string, description?: (string | null) | undefined, customer?: (Customer | null) | undefined, captureMode?: CaptureMode | undefined, paymentMethods?: (Array | null) | undefined, billingAddress?: (Address | null) | undefined, shippingDetails?: (ShippingDetails | null) | undefined, config?: (PaymentIntentConfigRequest | null) | undefined, metadata?: ({[key: string]: string} | null) | undefined, intendedAmount?: (Money | null) | undefined, merchantInitiatedTransactionType: MerchantInitiatedTransactionType, terms: string, orderDetails?: (OrderDetails | null) | undefined, expireAt?: (string | null) | undefined}; export type CreateApiSetupIntentsArgs = {version: string, body: CreateSetupIntentRequest}; export type CreateApiSetupIntentsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const setupIntentStatus = z.enum(["Created", "Authorized", "Canceled", "Completed", "Expired", "CardExpired", "Closed"]); export const paymentMethod = z.string(); export const customer = z.object({id: z.string().nullable().optional(), name: z.string().max(256).nullable().optional(), emailAddress: z.string().max(254).nullable().optional(), phoneNumber: z.string().max(50).nullable().optional()}); export const requestSecurity = z.object({userAgent: z.string().max(300).nullable().optional(), ipAddress: z.string().max(70).nullable().optional(), device: z.string().max(120).nullable().optional()}); export const address = z.object({address1: z.string().max(100).nullable().optional(), address2: z.string().max(100).nullable().optional(), address3: z.string().max(100).nullable().optional(), address4: z.string().max(100).nullable().optional(), city: z.string().max(100).nullable().optional(), state: z.string().max(100).nullable().optional(), postcode: z.string().max(50).nullable().optional(), countryCode: z.string().max(10), locality: z.string().max(100).nullable().optional(), county: z.string().max(100).nullable().optional()}); export const shippingDetails = z.object({name: z.string().max(100).nullable().optional(), deliveryNotes: z.string().max(1024).nullable().optional(), address: address.nullable().optional()}); export const cardScheme = z.string(); export const walletPaymentOption = z.string(); export const supportedPaymentMethods = z.object({cardSchemes: z.array(cardScheme).nullable().optional(), wallets: z.array(walletPaymentOption).nullable().optional()}); export const merchantConfig = z.object({supportedPaymentMethods: supportedPaymentMethods.nullable().optional()}); export const paymentIntentConfigCustomerEmail = z.object({collectionRequired: z.boolean().optional()}); export const paymentIntentConfigDetails = z.object({showTotal: z.boolean().optional(), showReference: z.boolean().optional()}); export const paymentIntentConfigBillingAddress = z.object({collectionRequired: z.boolean().optional()}); export const paymentIntentConfigShippingDetails = z.object({collectionRequired: z.boolean().optional()}); export const paymentConfigDetails = z.object({customAmountAllowed: z.boolean().optional(), tipsAllowed: z.boolean().optional()}); export const paymentIntentConfigResponse = z.object({title: z.string().max(100).nullable().optional(), redirectUrl: z.string().nullable().optional(), cancelUrl: z.string().nullable().optional(), customerEmail: paymentIntentConfigCustomerEmail.nullable().optional(), details: paymentIntentConfigDetails.nullable().optional(), billingAddress: paymentIntentConfigBillingAddress.nullable().optional(), shippingDetails: paymentIntentConfigShippingDetails.nullable().optional(), payment: paymentConfigDetails.nullable().optional()}); export const cardFundingType = z.string(); export const terminalSessionPaymentEntryMode = z.string(); export const terminalSessionPaymentVerificationMode = z.string(); export const card = z.object({cardNumber: z.string().nullable().optional(), cardName: z.string().nullable().optional(), expiryDate: z.string().nullable().optional(), cardType: z.string().nullable().optional(), cardFundingType: cardFundingType.optional(), last4PAN: z.string().nullable().optional(), entryMode: terminalSessionPaymentEntryMode.optional(), verificationMethod: terminalSessionPaymentVerificationMode.optional()}); export const paymentDetails = z.object({transactionId: z.string().nullable().optional(), transactionDateTime: z.string().nullable().optional(), message: z.string().nullable().optional(), paymentMethodId: z.string().nullable().optional(), authCode: z.string().nullable().optional(), card: card.nullable().optional(), avsResult: z.string().nullable().optional()}); export const money = z.object({value: z.number().int(), currencyCode: z.string().min(1).max(10)}); export const merchantInitiatedTransactionType = z.string(); export const paymentEventType = z.enum(["Created", "Authorized", "Captured", "Reversed", "Refunded", "Canceled", "AuthChallenged", "Declined", "ManualAuthReady", "Error", "Unknown"]); export const paymentStatusResponse = z.object({transactionId: z.string().nullable().optional(), transactionDateTime: z.string().nullable().optional(), eventType: paymentEventType.optional(), authCode: z.string().nullable().optional(), cardNumber: z.string().nullable().optional(), expiryDate: z.string().nullable().optional(), cardType: z.string().nullable().optional(), cardholderName: z.string().nullable().optional(), paymentMethodId: z.string().nullable().optional()}); export const setupIntentResponse = z.object({id: z.string().nullable().optional(), clientSessionSecret: z.string().nullable().optional(), clientSessionSecretExpirationDate: z.string().nullable().optional(), status: setupIntentStatus.optional(), paymentMethods: z.array(paymentMethod).nullable().optional(), customer: customer.nullable().optional(), requestSecurity: requestSecurity.nullable().optional(), billingAddress: address.nullable().optional(), shippingDetails: shippingDetails.nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), expireAt: z.string().nullable().optional(), reference: z.string().nullable().optional(), description: z.string().nullable().optional(), merchantConfig: merchantConfig.nullable().optional(), config: paymentIntentConfigResponse.nullable().optional(), metadata: z.record(z.string(), z.string()).nullable().optional(), paymentDetails: paymentDetails.nullable().optional(), intendedAmount: money.nullable().optional(), merchantInitiatedTransactionType: merchantInitiatedTransactionType.optional(), terms: z.string().nullable().optional(), paymentSource: z.string().nullable().optional(), setupEvents: z.array(paymentStatusResponse).nullable().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 }; /** * Create a setup intent * * POST /setup-intents */ export const createApiSetupIntents = async (args: CreateApiSetupIntentsArgs, options?: CreateApiSetupIntentsOptions) => { const {baseUrl = "https://api.dojo.tech", 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.version != null) headers.set("version", String(args.version)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/setup-intents${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(setupIntentResponse, await res.json()) };