import {z} from 'zod' export type GetApiV1MarketplacePlansIdArgs = {'integration-key': string, 'x-token': string, id: string}; export type GetApiV1MarketplacePlansIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const creditFees = z.object({"1x": z.number().optional(), "2x": z.number().optional(), "3x": z.number().optional(), "4x": z.number().optional(), "5x": z.number().optional(), "6x": z.number().optional(), "7x": z.number().optional(), "8x": z.number().optional(), "9x": z.number().optional(), "10x": z.number().optional(), "11x": z.number().optional(), "12x": z.number().optional(), "13x": z.number().optional(), "14x": z.number().optional(), "15x": z.number().optional(), "16x": z.number().optional(), "17x": z.number().optional(), "18x": z.number().optional(), "19x": z.number().optional(), "20x": z.number().optional(), "21x": z.number().optional(), "22x": z.number().optional(), "23x": z.number().optional(), "24x": z.number().optional()}); export const planFlagCalculation = z.object({pix: z.number(), debit: z.number(), credit: creditFees}); export const planFlagResponseWithFees = z.object({id: z.number(), name: z.string(), active: z.boolean(), standard: planFlagCalculation, markup: planFlagCalculation, fees: planFlagCalculation}); export const planMarketplaceResponse = z.object({id: z.number(), name: z.string(), active: z.boolean(), gateway_id: z.number(), description: z.string().nullable(), type: z.enum(["COMMERCIAL", "STANDARD"]), modality: z.enum(["ONLINE", "PHYSICAL"]), allow_anticipation: z.boolean(), created_at: z.string(), updated_at: z.string(), flags: z.array(planFlagResponseWithFees)}); 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 }; /** * Exibir plano * * GET /v1/marketplace/plans/{id} */ export const getApiV1MarketplacePlansId = async (args: GetApiV1MarketplacePlansIdArgs, options?: GetApiV1MarketplacePlansIdOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['integration-key'] != null) headers.set("integration-key", String(args['integration-key'])) if (args['x-token'] != null) headers.set("x-token", String(args['x-token'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/plans/${encodeURIComponent(String(args.id))}${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(planMarketplaceResponse, await res.json()) };