import {z} from 'zod' export type GetApiApiV1PaymentMethodsLangArgs = {lang: 'pl' | 'en', amount?: number | undefined, currency?: string | undefined}; export type GetApiApiV1PaymentMethodsLangOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const availabilityHours = z.object({mondayToFriday: z.string().optional(), saturday: z.string().optional(), sunday: z.string().optional()}); export const paymentMethodsResponseMethod = z.object({name: z.string().optional(), id: z.number().int().optional(), group: z.enum(["FastTransfers", "Blik", "eTransfer", "Credit Card", "Installments", "Wallet", "Foreign", "PrePayment", "TraditionalTransfer", "Another", "SplitPayment"]).optional(), subgroup: z.enum(["FastTransfers", "Blik", "eTransfer", "Credit Card", "Installments", "Wallet", "Foreign", "PrePayment", "TraditionalTransfer", "Another", "SplitPayment"]).optional(), status: z.boolean().optional(), imgUrl: z.string().optional(), mobileImgUrl: z.string().optional(), mobile: z.boolean().optional(), availabilityHours: availabilityHours.optional()}); export const getApiApiV1PaymentMethodsLangResponse = z.object({data: z.array(paymentMethodsResponseMethod).optional(), agreements: z.array(z.unknown()).optional(), responseCode: 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 }; /** * Payment Methods * * GET /api/v1/payment/methods/{lang} */ export const getApiApiV1PaymentMethodsLang = async (args: GetApiApiV1PaymentMethodsLangArgs, options?: GetApiApiV1PaymentMethodsLangOptions) => { const {baseUrl = "https://sandbox.przelewy24.pl", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.amount != null) query.append("amount", String(args.amount)) if (args.currency != null) query.append("currency", String(args.currency)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment/methods/${encodeURIComponent(String(args.lang))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1PaymentMethodsLangResponse, await res.json()) };