export type Country = string; export type Bank = {code?: string | undefined, bicfi?: string | undefined, name?: string | undefined, country?: Country | undefined}; export type AccountDetails = {account_number: string, branch?: string | undefined, owner_name?: string | undefined, account_format?: ('iban' | 'clabe' | 'cci' | 'pix' | 'bre_b' | 'alias' | 'cbu' | 'cvu') | undefined, tax_id_type?: ('rfc' | 'cnpj' | 'cpf' | 'ruc' | 'curp' | 'le' | 'dni' | 'lm' | 'pas' | 'ce' | 'nit' | 'cc' | 'pa' | 'ti' | 'nuip' | 'ppt' | 'pep' | 'cuil' | 'cuit' | 'other') | undefined, tax_id?: string | undefined, bank?: Bank | undefined}; export type VoucherDataEvent = {key_tracing?: string | undefined, url?: string | undefined}; export type PayloadError = {code?: string | undefined, message?: string | undefined}; export type Payload = {amount: string, concept: string, currency: string, id?: string | undefined, transaction_id?: string | undefined, external_id?: string | undefined, origin?: AccountDetails | undefined, destination?: AccountDetails | undefined, voucher_data?: VoucherDataEvent | undefined, error?: PayloadError | undefined}; export type Event = {event_type: 'payin.rejected' | 'payin.settled' | 'payout.initiated' | 'payout.settled' | 'payout.failed' | 'payout.cancelled', event_id: string, timestamp: string, payload: Payload}; export type WebhookPayload = {verify_token: string, events: Array}; export type CreateApiMerchantUrlWebhookArgs = {body: WebhookPayload}; export type CreateApiMerchantUrlWebhookOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * Send event webhook * * POST /merchant_url/webhook */ export const createApiMerchantUrlWebhook = async (args: CreateApiMerchantUrlWebhookArgs, options: CreateApiMerchantUrlWebhookOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/merchant_url/webhook${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 undefined };