import {z} from 'zod' export type CreateApiV102FloaCreateArgs = {body: {payment: {identifier?: string | undefined, amount?: {value: number, currency: string} | undefined, lang: 'PT' | 'ES' | 'FR', backUrl: string, successUrl: string, failUrl: string, reportDelayInDays?: string | undefined}, installmentCode: 'BC3XFPT', items: Array<{name: string, price: number, quantity: number, category: string, subCategory: string}>, customer?: ({civility?: (''Mr'' | ''Ms'' | ''Mrs'') | undefined, firstName?: string | undefined, lastName?: string | undefined, secondLastName?: string | undefined, birthDate?: string | undefined, birthDepartment?: string | undefined, email?: string | undefined, phoneNumber?: string | undefined, countryCodePhone?: string | undefined, vatNumber?: string | undefined, billingAddress?: ({address?: string | undefined, zipCode?: string | undefined, city?: string | undefined, countryCode?: ('PT' | 'ES' | 'FR') | undefined}) | undefined, ''?: string | undefined}) | undefined, shippingAddress?: ({shippingMethod?: ('STD' | 'EXP' | 'TRK' | 'REG' | 'COL' | 'CHR' | 'REL' | 'TNT' | 'UPS' | 'TRP' | 'MAG' | 'LCK' | 'VIR' | 'RIM') | undefined, address?: string | undefined, zipCode?: string | undefined, city?: string | undefined, countryCode?: ('PT' | 'ES' | 'FR') | undefined}) | undefined}}; export type CreateApiV102FloaCreateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV102FloaCreateResponse = z.object({transactionStatus: z.string().optional(), transactionID: z.string().optional(), reference: z.string().optional(), redirectUrl: z.string().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 }; /** * Floa - Payment * * POST /v1.02/floa/create */ export const createApiV102FloaCreate = async (args: CreateApiV102FloaCreateArgs, options?: CreateApiV102FloaCreateOptions) => { const {baseUrl = "https://sandbox.eupago.pt/api/auth", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1.02/floa/create${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(createApiV102FloaCreateResponse, await res.json()) };