import {z} from 'zod' export type Partner = number; export type BusinessUnit = number; export type PersonName = string; export type PhoneNumber = string; export type Mail = string; export type Nickname = string; export type PubliclyExposedPerson = boolean; export type MotherFullName = string; export type FatherFullName = string; export type Nationality = 'BRASILEIRO' | 'ESTRANGEIRO'; export type BirthCity = string; export type BirthStateNewAccount = '0 - AC' | '1 - AL' | '2 - AP' | '3 - AM' | '4 - BA' | '5 - CE' | '6 - DF' | '7 - ES' | '8 - GO' | '9 - MA' | '10 - MT' | '11 - MS' | '12 - MG' | '13 - PA' | '14 - PB' | '15 - PR' | '16 - PE' | '17 - PI' | '18 - RJ' | '19 - RN' | '20 - RS' | '21 - RO' | '22 - RR' | '23 - SC' | '24 - SP' | '25 - SE' | '26 - TO'; export type Gender = 'FEMININO' | 'MASCULINO'; export type MaritalStatus = 'NotMarried = 0' | 'Married = 1' | 'Divorced = 2' | 'Separate = 3' | 'Widower = 4' | 'Single = 5' | 'Other = 6'; export type SpouseName = string; export type Occupation = 'APOSENTADO_PENSIONISTA' | 'AUTONOMO' | 'EMPRESARIO' | 'PROFISSIONAL_LIBERAL' | 'ASSALARIADO' | 'FUNCIONARIO_PUBLICO' | 'DESEMPREGADO'; export type IdentityDocument = number; export type CheckPendingTransfers = boolean | null; export type Banks = string; export type AccountType = 'CONTA_CORRENTE_INDIVIDUAL' | 'CONTA_CORRENTE_CONJUNTA' | 'CONTA_POUPANCA_CONJUNTA' | 'CONTA_POUPANCA_INDIVIDUAL'; export type Bank = {bank: Banks, type: AccountType, agency: string, account: string, pixKey?: string | undefined}; export type BankBranch = string; export type BankAccount = string; export type BankAccountDigit = string; export type StateNewAccount = '0 - AC' | '1 - AL' | '2 - AP' | '3 - AM' | '4 - BA' | '5 - CE' | '6 - DF' | '7 - ES' | '8 - GO' | '9 - MA' | '10 - MT' | '11 - MS' | '12 - MG' | '13 - PA' | '14 - PB' | '15 - PR' | '16 - PE' | '17 - PI' | '18 - RJ' | '19 - RN' | '20 - RS' | '21 - RO' | '22 - RR' | '23 - SC' | '24 - SP' | '25 - SE' | '26 - TO'; export type AddressesNewAccount = Array<{AddressLine: string, AddressLine2: string, ZipCode: string, Neighborhood: string, CityCode?: number | undefined, CityName: string, State: StateNewAccount, AddressType: number, Country?: string | undefined, Complement: string, Reference: string}>; export type DocumentType = 'SELF' | 'IDENTITY_FRONT' | 'IDENTITY_BACK' | 'ADDRESS_PROOF' | 'INCOME_PROOF' | 'DECLARATION_IR' | 'CORPORATE_BILLING_RELATIONSHIP' | 'PROOF_OF_COMPANY_ADDRESS' | 'SOCIAL_CONTRACT' | 'PROOF_OF_CORPORATE_INCOME'; export type Documents = Array<{DocumentFile: string, DocumentFormat: number | null, DocumentName: string, DocumentType: DocumentType, Description: string, ExpirationDate?: (string | null) | undefined, DocumentKey?: string | undefined}>; export type Name = string; export type PersonRoleType = number; export type BusinessUnitId = number; export type UsageType = 'Balance = 0' | 'Overbalance = 1'; export type ConsumeType = 'WithLimit = 0' | 'NoLimit = 1'; export type IdentifierProduct = string; export type EmbossingName = string; export type EmbossingNameType = 'CardHolder = 0' | 'CardOwner = 1'; export type TaxNumber = string; export type FullName = string; export type Phone = string; export type CardOwner = {OwnerTaxNumber: TaxNumber, Bank?: Bank | undefined, BankBranch?: BankBranch | undefined, BankAccount?: BankAccount | undefined, BankAccountDigit?: BankAccountDigit | undefined, FullName: FullName, Phone?: Phone | undefined, Mail?: Mail | undefined}; export type SocialNumber = string; export type MotherName = string; export type BirthDate = string; export type CardHolder = {HolderTaxNumber: SocialNumber, FullName: FullName, MotherName: MotherName, BirthDate: BirthDate, Gender: Gender, Nationality: Nationality, MaritalStatus: MaritalStatus}; export type CardHolderContact = {Phone: Phone, Mail: Mail}; export type BillingAddress = {Line?: string | undefined, Number?: string | undefined, Complement?: string | undefined, Reference?: string | undefined, Neighborhood?: string | undefined, ZipCode?: string | undefined, City?: string | undefined, State?: string | undefined, Country?: string | undefined}; export type CardDeliveryAddress = {Line: string, Number: string, Complement: string, Reference?: string | undefined, Neighborhood: string, ZipCode: string, City: string, State: string, Country: string}; export type RequestCard = {Method: string, PartnerId: Partner, BusinessUnitId: BusinessUnitId, UsageType: UsageType, ConsumeType: ConsumeType, IdentifierProduct: IdentifierProduct, EmbossingName?: EmbossingName | undefined, EmbossingNameType?: EmbossingNameType | undefined, CardOwner: CardOwner, CardHolder: CardHolder, CardHolderContact: CardHolderContact, BillingAddress?: BillingAddress | undefined, CardDeliveryAddress: CardDeliveryAddress}; export type CreateApiNewAccountIndividualPersonArgs = {body: {Method: string, PartnerId: Partner, BusinessUnitId: BusinessUnit, PersonName: PersonName, PhoneNumber: PhoneNumber, TaxNumber: string, Mail: Mail, IsCompany?: (boolean | null) | undefined, Nickname?: Nickname | undefined, PubliclyExposedPerson: PubliclyExposedPerson, BirthDate: string, MotherFullName?: MotherFullName | undefined, FatherFullName?: FatherFullName | undefined, Nationality: Nationality, BirthCity?: BirthCity | undefined, BirthState?: BirthStateNewAccount | undefined, Gender?: Gender | undefined, MaritalStatus?: MaritalStatus | undefined, SpouseName?: SpouseName | undefined, Occupation?: Occupation | undefined, IdentityDocument?: IdentityDocument | undefined, CheckPendingTransfers?: CheckPendingTransfers | undefined, MonthlyIncome: string, Bank?: Bank | undefined, BankBranch?: BankBranch | undefined, BankAccount?: BankAccount | undefined, BankAccountDigit?: BankAccountDigit | undefined, Addresses?: AddressesNewAccount | undefined, Documents: Documents, Persons?: (Array<{Name: Name, TaxNumber: string, Mail: Mail, Occupation?: Occupation | undefined, PhoneNumber: string, PersonRoleType: PersonRoleType, MotherFullName?: MotherFullName | undefined, FatherFullName?: FatherFullName | undefined, Nationality: Nationality, BirthCity?: BirthCity | undefined, BirthState?: BirthStateNewAccount | undefined, Gender?: Gender | undefined, MaritalStatus?: MaritalStatus | undefined, SpouseName?: SpouseName | undefined, IdentityDocument?: IdentityDocument | undefined, BirthDate: string, PersonName?: PersonName | undefined, Nickname?: Nickname | undefined, PubliclyExposedPerson: PubliclyExposedPerson, PersonDocuments: Documents}>) | undefined, RequestCard?: RequestCard | undefined, WasSigned: boolean, SignatureDate: string}}; export type CreateApiNewAccountIndividualPersonOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const structureResponseNewAccount = z.object({Success: z.string().optional(), AccountKey: z.string().optional(), Code: z.string().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 }; /** * New Account for Individual * * POST / NewAccount (Individual Person) */ export const createApiNewAccountIndividualPerson = async (args: CreateApiNewAccountIndividualPersonArgs, options?: CreateApiNewAccountIndividualPersonOptions) => { 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(/\/+$/, '')}/ NewAccount (Individual Person)${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(structureResponseNewAccount, await res.json()) };