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 EdubankTransactionDto = {reference_id: string, transaction_limit_date: string, transaction_description?: string | undefined, sandbox: boolean, is_pinpad_transaction?: boolean | undefined, allows_pix: boolean, allows_credit_card: boolean, allows_bank_slip: boolean, transaction_items?: Array | undefined, transaction_details: EdubankTransactionDetails, split_rules?: Array | undefined, notification_url: string, buyer: EdubankBuyerDto}; export type CreateApiV1TransactionsArgs = {body: EdubankTransactionDto}; export type CreateApiV1TransactionsOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const edubankTransactionCreationResponse = z.object({transaction_id: z.string(), amount_to_pay_today: z.number().optional(), expires_in: z.string().optional(), status_code: z.number()}); 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 * * POST /v1/transactions * * Also accepts auth: authorization, authorization */ export const createApiV1Transactions = async (args: CreateApiV1TransactionsArgs, options: CreateApiV1TransactionsOptions) => { 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${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(edubankTransactionCreationResponse, await res.json()) };