import {z} from 'zod' export type Store = {transaction?: ({payment_method_token: string, sub_merchant_key?: string | undefined, currency_code?: string | undefined, gateway_specific_fields?: Record | undefined, billing_address?: ({name?: string | undefined, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, phone_number?: string | undefined}) | undefined, tlid?: string | undefined, customer_agreement_id?: string | undefined}) | undefined}; export type CreateApiGatewaysGatewayTokenStoreArgs = {gateway_token: string, body: Store}; export type CreateApiGatewaysGatewayTokenStoreOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const gatewaySpecificFields = z.array(z.string()); export const thirdPartyToken = z.object({token: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), gateway_type: z.string().optional(), storage_state: z.string().optional(), metadata: z.object({}).optional(), third_party_token: z.string().optional(), payment_method_type: z.string().optional()}); export const storeResponse = z.object({transaction: z.object({created_at: z.string().optional(), currency_code: z.string().optional(), updated_at: z.string().optional(), succeeded: z.boolean().optional(), token: z.string().optional(), state: z.string().optional(), gateway_specific_fields: gatewaySpecificFields.optional(), gateway_specific_response_fields: z.object({}).optional(), transaction_type: z.string().optional(), third_party_token: z.string().optional(), gateway_transaction_id: z.string().optional(), gateway_latency_ms: z.string().optional(), message_key: z.string().optional(), message: z.string().optional(), gateway_token: z.string().optional(), gateway_type: z.string().optional(), payment_method: thirdPartyToken.optional(), basis_payment_method: z.object({}).optional(), response: z.object({}).optional(), sub_merchant_key: z.string().optional(), billing_address: z.object({}).optional(), tlid: z.string().optional(), customer_agreement_id: z.string().optional()}).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 store transaction * * POST /gateways/{gateway_token}/store */ export const createApiGatewaysGatewayTokenStore = async (args: CreateApiGatewaysGatewayTokenStoreArgs, options?: CreateApiGatewaysGatewayTokenStoreOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateways/${encodeURIComponent(String(args.gateway_token))}/store${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(storeResponse, await res.json()) };