import {z} from 'zod' export type ClientAddressDto = {city: string, state: string, zip_code: string, street: string, complement?: string | undefined, number: string, neighborhood: string}; export type ClientDto = {first_name: string, last_name?: string | undefined, email: string, document: string, phone?: string | undefined, address?: ClientAddressDto | undefined}; export type DiscountDto = {method: string, value: number, type: 'PERCENTAGE' | 'CURRENCY'}; export type PaymentOrderDto = {discounts?: Array | undefined, max_number_installments?: number | undefined, pix_expiration_interval?: number | undefined, payment_type_allowed?: (Array<'CREDIT' | 'PIX' | 'GOOGLEPAY' | 'APPLEPAY' | 'BANK_SLIP'>) | undefined}; export type ThemeDto = {main?: string | undefined, secondary?: string | undefined, logo_url?: string | undefined, favicon_url?: string | undefined, wallet_icon_url?: string | undefined, font_family?: ('roboto' | 'poppins' | 'sf-pro-text') | undefined, card_border_radius?: number | undefined, button_border_radius?: number | undefined}; export type BilletDiscountItem = {limit_date: string, value: number}; export type BilletDiscount = {type: 'VALOR_DATA_FIXA', items: Array}; export type BilletFine = {percentage: string, quantity_days: number}; export type TransactionBilletDto = {due_date: string, issue_date: string, document_kind?: string | undefined, days_until_expiration?: number | undefined, iof_percentage?: string | undefined, messages?: Array | undefined, discount?: BilletDiscount | undefined, fine?: BilletFine | undefined, interest_percentage?: string | undefined}; export type CreateCheckoutOrderDto = {client?: ClientDto | undefined, title: string, payment_order?: PaymentOrderDto | undefined, theme?: ThemeDto | undefined, duration?: number | undefined, reference_id?: (string | number) | undefined, multiple_payments?: boolean | undefined, interest?: ('STORE' | 'CLIENT') | undefined, brand_plan?: ('MASTERCARD' | 'VISA' | 'ELO' | 'OTHERS') | undefined, amount: number, billet?: TransactionBilletDto | undefined}; export type CreateApiV1MarketplaceCheckoutArgs = {establishment_id: string, 'integration-key': string, 'x-token': string, body: CreateCheckoutOrderDto}; export type CreateApiV1MarketplaceCheckoutOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const checkoutOrderResponse = 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()}); 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 }; /** * Criar smart checkout * * POST /v1/marketplace/checkout */ export const createApiV1MarketplaceCheckout = async (args: CreateApiV1MarketplaceCheckoutArgs, options?: CreateApiV1MarketplaceCheckoutOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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${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(checkoutOrderResponse, await res.json()) };