import {z} from 'zod' export type GetApiV1TransactionsTransactionIdPixPaymentIdSseArgs = {payment_id: string, transaction_id: string}; export type GetApiV1TransactionsTransactionIdPixPaymentIdSseOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const edubankPixNotification = z.object({status: z.enum(["PAID", "CREATED", "CANCELED", "REFUNDED", "VOIDED", "SETTLED", "OVERDUE", "EXPIRED", "ACTIVE", "CHARGED_BACK", "DISPUTED"]), payment_id: z.string(), reference_id: z.string(), payment_type: z.string(), payment_amount: z.number(), payment_confirmed: z.boolean(), payment_canceled: z.boolean(), payment_received_at: z.string(), expected_on: z.string(), fees: z.string(), pix_qrcode: z.string(), pix_expiration: z.string(), pix_authorization_code: z.string()}); export const edubankSendEvent = z.object({event_id: z.string(), create_at: z.string(), seller_id: z.string(), transaction_id: z.string().optional(), subscription_id: z.string().optional(), payment_method: edubankPixNotification}); export const edubankPixSse = z.object({status_code: z.number(), status: z.string(), message: z.string(), payment_event: edubankSendEvent.nullable()}); export const edubankMessageEventPixSse = z.object({id: z.string(), data: edubankPixSse}); 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 }; /** * Wait for Pix payment * * GET /v1/transactions/{transaction_id}/pix/{payment_id}/sse * * Also accepts auth: authorization */ export const getApiV1TransactionsTransactionIdPixPaymentIdSse = async (args: GetApiV1TransactionsTransactionIdPixPaymentIdSseArgs, options: GetApiV1TransactionsTransactionIdPixPaymentIdSseOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transactions/${encodeURIComponent(String(args.transaction_id))}/pix/${encodeURIComponent(String(args.payment_id))}/sse${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(edubankMessageEventPixSse, await res.json()) };