import {z} from 'zod' export type PaymentOptionsRequestPaymentSource = {paymentOption?: ({nickName?: string | undefined, isDefault?: string | undefined, sourceId?: string | undefined}) | undefined, shopper?: ({ipAddress?: string | undefined}) | undefined}; export type PaymentOptionsRequestLegacy = {paymentOption?: ({nickName?: string | undefined, isDefault?: string | undefined, creditCard?: ({expirationMonth?: number | undefined, expirationYear?: number | undefined, displayableNumber?: string | undefined, cardSecurityCode?: string | undefined, type?: string | undefined}) | undefined}) | undefined, shopper?: ({ipAddress?: string | undefined}) | undefined}; export type CreateApiV1ShoppersMePaymentOptionsArgs = {token?: string | undefined, expand?: string | undefined, fields?: string | undefined, body: PaymentOptionsRequestPaymentSource | PaymentOptionsRequestLegacy}; export type CreateApiV1ShoppersMePaymentOptionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentOptionsResponsePaymentSourceForPost = z.object({uri: z.string().optional(), id: z.number().optional(), nickName: z.string().optional(), isDefault: z.string().optional(), type: z.string().optional(), sourceId: z.string().optional(), sourceClientSecret: z.string().optional(), creditCard: z.object({expirationYear: z.string().optional(), lastFourDigits: z.string().optional(), clientSecret: z.string().optional(), expirationMonth: z.string().optional(), fundingSource: z.string().optional(), brand: z.string().optional(), reusable: 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 the shopper payment options * * POST /v1/shoppers/me/payment-options */ export const createApiV1ShoppersMePaymentOptions = async (args: CreateApiV1ShoppersMePaymentOptionsArgs, options?: CreateApiV1ShoppersMePaymentOptionsOptions) => { const {baseUrl = "https://api.digitalriver.com", 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.token != null) query.append("token", String(args.token)) if (args.expand != null) query.append("expand", String(args.expand)) if (args.fields != null) query.append("fields", String(args.fields)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/shoppers/me/payment-options${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(paymentOptionsResponsePaymentSourceForPost, await res.json()) };