import {z} from 'zod' export type MiniAppPaymentMethodsRequest = {initData: string}; export type CreateApiMiniappPaymentsMethodsArgs = {body: MiniAppPaymentMethodsRequest}; export type CreateApiMiniappPaymentsMethodsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const miniAppPaymentIntegrationType = z.enum(["iframe", "redirect"]); export const miniAppPaymentOption = z.object({id: z.string(), icon: z.string().nullable().optional(), title: z.string().nullable().optional(), description: z.string().nullable().optional(), titleKey: z.string().nullable().optional(), descriptionKey: z.string().nullable().optional()}); export const miniAppPaymentIframeConfig = z.object({expected_origin: z.string()}); export const miniAppPaymentMethod = z.object({id: z.string(), name: z.string().nullable().optional(), icon: z.string().nullable().optional(), requires_amount: z.boolean().optional(), currency: z.string().optional(), min_amount_kopeks: z.number().int().nullable().optional(), max_amount_kopeks: z.number().int().nullable().optional(), amount_step_kopeks: z.number().int().nullable().optional(), integration_type: miniAppPaymentIntegrationType, options: z.array(miniAppPaymentOption).optional(), iframe_config: miniAppPaymentIframeConfig.nullable().optional()}); export const miniAppPaymentMethodsResponse = z.object({methods: z.array(miniAppPaymentMethod).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 }; /** * Get Payment Methods * * POST /miniapp/payments/methods */ export const createApiMiniappPaymentsMethods = async (args: CreateApiMiniappPaymentsMethodsArgs, options: CreateApiMiniappPaymentsMethodsOptions) => { const {baseUrl, 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(/\/+$/, '')}/miniapp/payments/methods${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(miniAppPaymentMethodsResponse, await res.json()) };