import {z} from 'zod' export type DelegatedPaymentMethodCard = {type: 'card', card_number_type: 'fpan' | 'network_token', number: string, exp_month?: string | undefined, exp_year?: string | undefined, name?: string | undefined, cvc?: string | undefined, cryptogram?: string | undefined, eci_value?: string | undefined, checks_performed?: Array | undefined, iin?: string | undefined, display_card_funding_type?: ('credit' | 'debit' | 'prepaid') | undefined, display_wallet_type?: string | undefined, display_brand?: string | undefined, display_last4?: string | undefined, metadata: {[key: string]: string}}; export type DelegatedPaymentAllowance = {reason: 'one_time', max_amount: number, currency: string, merchant_id: string, checkout_session_id: string, expires_at: string}; export type DelegatedPaymentBillingAddress = {name: string, line_one: string, line_two?: string | undefined, city: string, state?: string | undefined, postal_code: string, country: string}; export type DelegatedPaymentRiskSignal = {type: string, score: number, action: string}; export type DelegatedPaymentRequest = {payment_method: DelegatedPaymentMethodCard, allowance: DelegatedPaymentAllowance, billing_address?: DelegatedPaymentBillingAddress | undefined, risk_signals: Array, metadata: {[key: string]: string}}; export type CreateApiAgenticCommerceDelegatePaymentArgs = {Signature: string, Timestamp: string, 'Cko-Idempotency-Key'?: string | undefined, 'API-Version'?: string | undefined, body: DelegatedPaymentRequest}; export type CreateApiAgenticCommerceDelegatePaymentOptions = {baseUrl: string, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const delegatedPaymentResponse = z.object({id: z.string(), created: z.string(), metadata: z.record(z.string(), z.string())}); 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 delegated payment token * * POST /agentic_commerce/delegate_payment */ export const createApiAgenticCommerceDelegatePayment = async (args: CreateApiAgenticCommerceDelegatePaymentArgs, options: CreateApiAgenticCommerceDelegatePaymentOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Signature != null) headers.set("Signature", String(args.Signature)) if (args.Timestamp != null) headers.set("Timestamp", String(args.Timestamp)) if (args['Cko-Idempotency-Key'] != null) headers.set("Cko-Idempotency-Key", String(args['Cko-Idempotency-Key'])) if (args['API-Version'] != null) headers.set("API-Version", String(args['API-Version'])) if (ApiSecretKey !== undefined) headers.set("Authorization", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/agentic_commerce/delegate_payment${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(delegatedPaymentResponse, await res.json()) };