import {z} from 'zod' export type GetApiV1MarketplaceCheckoutIdArgs = {establishment_id: string, 'integration-key': string, 'x-token': string, id: string}; export type GetApiV1MarketplaceCheckoutIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentOrderResponse = z.object({discounts: z.object({}).optional(), payment_type_allowed: z.object({}).optional()}); export const themeResponse = z.object({main: z.string().optional(), secondary: z.string().optional(), logo_url: z.string().optional()}); export const checkoutOrderGetOneResponse = z.object({_id: z.string(), title: z.string(), checkout_token: z.string(), amount: z.number(), status: z.string(), reference_id: z.string().optional(), created_at: z.string(), updated_at: z.string(), interest: z.enum(["STORE", "CLIENT"]).optional(), multiple_payments: z.boolean(), current_use: z.number(), brand_plan: z.string().optional(), payment_order: paymentOrderResponse.optional(), theme: themeResponse.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 }; /** * Exibir smart checkout * * GET /v1/marketplace/checkout/{id} */ export const getApiV1MarketplaceCheckoutId = async (args: GetApiV1MarketplaceCheckoutIdArgs, options?: GetApiV1MarketplaceCheckoutIdOptions) => { 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.establishment_id != null) headers.set("establishment_id", String(args.establishment_id)) 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/checkout/${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(checkoutOrderGetOneResponse, await res.json()) };