import {z} from 'zod' export type Partner = number; export type BusinessUnitId = number; export type Identifier = string; export type CollectionOrderType = '0 - DynamicPixQrCodeDueDate' | '1 - Boleto' | '2 - BoletoPixQrCode' | '3 - DynamicPixQRCode'; export type PrincipalValue = number; export type RebateValue = number; export type InterestValue = number; export type FineValue = number; export type FinePercent = number; export type InterestPercent = number; export type FineDate = string; export type DueDate = string; export type ExpirationDate = string; export type InterestDate = string; export type Payer = {TaxNumber: string, Bank?: string | undefined, BankAccount?: string | undefined, BankAccountDigit?: string | undefined, BankBranch?: string | undefined}; export type CustomerAccountInfo = {PixKey: string, TaxNumber: string, Bank: string, BankBranch: string, BankAccount: string, BankAccountDigit: string}; export type Customer = {Name: string, CustomerAccountInfo: CustomerAccountInfo}; export type MinPaymentValue = number; export type MaxPaymentValue = number; export type DivergentPaymentType = '1 - AnyValue' | '2 - MinAndMaxValue' | '3 - NotAllowed' | '4 - OnlyMinValue'; export type OperationVars = Array<{Key?: string | undefined, Value?: string | undefined}>; export type CollectionOrderProductType = '0 - Ammount' | '1 - Percentage'; export type ReceiverContactInfo = {Mail?: string | undefined, Phone?: string | undefined}; export type ReceiverAccountInfo = {PixKey?: string | undefined, TaxNumber: string, Bank?: string | undefined, BankBranch?: string | undefined, BankAccount?: string | undefined, BankAccountDigit?: string | undefined}; export type Receiver = Array<{SplitValue: number, Name: string, Nickname?: string | undefined, BirthDate?: string | undefined, LegalName?: string | undefined, TradingName?: string | undefined, ConstitutionDate?: string | undefined, ReceiverContactInfo: ReceiverContactInfo, ReceiverAccountInfo: ReceiverAccountInfo}>; export type Receivers = {CollectionOrderProductType: CollectionOrderProductType, Receiver: Receiver}; export type Discount = {Type?: number | undefined, DiscountRange?: (Array<{DiscountDate?: string | undefined, DiscountValue?: number | undefined}>) | undefined}; export type Rate = {Sent?: boolean | undefined, Value?: number | undefined}; export type CreateApiGenerateCollectionOrderArgs = {body: {Method: string, PartnerId: Partner, BusinessUnitId: BusinessUnitId, Identifier: Identifier, CollectionOrderType?: CollectionOrderType | undefined, PrincipalValue: PrincipalValue, RebateValue?: RebateValue | undefined, InterestValue?: InterestValue | undefined, FineValue?: FineValue | undefined, FinePercent: FinePercent, InterestPercent: InterestPercent, FineDate?: FineDate | undefined, DueDate?: DueDate | undefined, ExpirationDate: ExpirationDate, InterestDate?: InterestDate | undefined, Payer: Payer, Customer: Customer, MinPaymentValue?: MinPaymentValue | undefined, MaxPaymentValue?: MaxPaymentValue | undefined, DivergentPaymentType?: DivergentPaymentType | undefined, OperationVars?: OperationVars | undefined, InstructionsToPayer?: string | undefined, NeedSplitConfirmation?: boolean | undefined, Receivers?: Receivers | undefined, Discount?: Discount | undefined, Rate?: Rate | undefined}}; export type CreateApiGenerateCollectionOrderOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiGenerateCollectionOrderResponse = z.object({Success: z.string().optional(), DocumentNumber: z.number().int().optional(), Message: z.string().optional(), ErrorCode: z.number().int().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 }; /** * Generate Collection Order * * POST /GenerateCollectionOrder */ export const createApiGenerateCollectionOrder = async (args: CreateApiGenerateCollectionOrderArgs, options?: CreateApiGenerateCollectionOrderOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/GenerateCollectionOrder${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(createApiGenerateCollectionOrderResponse, await res.json()) };