import {z} from 'zod' export type EdubankTransactionItems = {item: string, amount: string, reference?: string | undefined, note?: string | undefined}; export type EdubankLateFee = {mode: string, amount: string}; export type EdubankInterest = {mode: string, amount: string}; export type EdubankDiscount = {mode: string, date_1: string, amount_1: number, date_2?: string | undefined, amount_2?: number | undefined, date_3?: string | undefined, amount_3?: number | undefined}; export type EdubankTransactionDetails = {amount: number, due_date: string, payment_limit_date: string, body_instructions?: Array | undefined, late_fee?: EdubankLateFee | undefined, interest?: EdubankInterest | undefined, discount?: EdubankDiscount | undefined}; export type EdubankSplit = {recipient: string, amount?: number | undefined, percentage: string, processing_fee: string}; export type EdubankBuyersAddress = {city: string, additional_information?: string | undefined, number: string, state_code: string, street: string, neighborhood: string, zip: string, country_code: 'BR'}; export type EdubankBuyerDto = {first_name: string, last_name: string, email?: string | undefined, taxpayer_id: string, birthdate?: string | undefined, phone_number?: string | undefined, address: EdubankBuyersAddress}; export type EdubankTransactionOneStepDto = {reference_id: string, transaction_limit_date: string, transaction_description?: string | undefined, sandbox: boolean, is_pinpad_transaction?: boolean | undefined, transaction_items?: Array | undefined, transaction_details: EdubankTransactionDetails, split_rules?: Array | undefined, notification_url: string, buyer: EdubankBuyerDto, bank_slip_link?: ('PDF' | 'HTML') | undefined}; export type CreateApiV1TransactionsBankSlipArgs = {body: EdubankTransactionOneStepDto}; export type CreateApiV1TransactionsBankSlipOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const edubankTransactionSuccessResponseBankSlip = z.object({status_code: z.number(), transaction_id: z.string(), transaction_type: z.string(), status: z.string(), payment_id: z.string(), bank_slip_link: z.string().optional(), bank_slip_barcode: z.string().optional(), bank_slip_digitable_line: z.string().optional(), bank_slip_document_number: z.string().optional(), pix_qrcode: z.string().optional(), pix_base64: z.string().optional(), pix_expiration: 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 }; /** * Create a transaction and generate bank slip for payment * * POST /v1/transactions/bank-slip * * Also accepts auth: authorization, authorization */ export const createApiV1TransactionsBankSlip = async (args: CreateApiV1TransactionsBankSlipArgs, options: CreateApiV1TransactionsBankSlipOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transactions/bank-slip${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(edubankTransactionSuccessResponseBankSlip, await res.json()) };