import {z} from 'zod' export type OriginalMessageHeader = {message_id: string, creation_date: string, sender: {fin_id_schema: string, fin_id: string}, receiver: {fin_id_schema: string, fin_id: string}, options?: {[key: string]: unknown} | undefined}; export type CurrencyCode = string; export type CurrencyAmount = string; export type ReferenceNumber = string; export type TrackingKey = string; export type ExecutionMode = 'default' | 'immediate' | 'deferred'; export type PaymentPurpose = 'default' | 'salary_payment' | 'supplier_payment' | 'dividend' | 'confirming' | 'tax_payment' | 'pe_cts' | 'bonus_payment' | 'compensation_payment' | 'holiday_payment' | 'overtime_payment' | 'cash_management_transfer' | 'card_bulk_clearing' | 'credit_card_payment' | 'trade_settlement_payment' | 'debit_card_payment' | 'deliver_against_payment' | 'epayment' | 'fee_collection_and_interest' | 'fee_collection' | 'person_to_person_payment' | 'government_payment' | 'hedging' | 'irrevocable_credit_card_payment' | 'irrevocable_debit_card_payment' | 'intra_company_payment' | 'interest' | 'lockbox_transactions' | 'loan' | 'commercial' | 'consumer' | 'other_payment' | 'pension_payment' | 'represented' | 'reimbursement_received_credit_transfer' | 'receive_against_payment' | 'securities' | 'social_security_benefit' | 'trade' | 'treasury_payment' | 'value_added_tax_payment' | 'with_holding' | 'cash_management_sweep_account' | 'cash_management_top_account' | 'cash_management_zero_balance_account' | 'crossborder_mipayments' | 'foreign_currency_domestic_transfer' | 'cash_in_pre_credit' | 'cash_out_notes_coins' | 'carrier_guarded_wholesale_valuables' | 'saving' | 'profit_sharing_payment'; export type PoConnection = 'default' | 'api' | 'h2h'; export type PaymentRail = 'default' | 'swift' | 'swift_gpi' | 'swift_go' | 'cl_tef' | 'cl_tef_masiva' | 'cl_batch_credito' | 'cl_batch_debito' | 'cl_lbtr' | 'co_ach_batch' | 'co_ach_transfiya' | 'co_cenit_batch' | 'co_redeban' | 'co_breb' | 'co_pse' | 'co_lbtr' | 'mx_spei' | 'mx_spei_b' | 'mx_spid' | 'mx_batch_domiciliation' | 'mx_batch_payroll' | 'mx_batch_referenced' | 'mx_tef' | 'pe_cce_batch' | 'pe_cce_inmediate' | 'pe_visa_direct' | 'pe_lbtr' | 'cashier_back'; export type AccountType = string; export type PayoutDebtor = {name: string, identification: {id_schema: string, id: string}, financial_institution?: {fin_id_schema: string, fin_id: string} | undefined, account: string, account_type: AccountType, email: string}; export type PayoutCreditor = {name: string, identification: {id_schema: string, id: string}, financial_institution?: {fin_id_schema: string, fin_id: string} | undefined, account: string, account_type: AccountType, email: string, address?: ({street_name?: string | undefined, town_name?: string | undefined, country_code?: string | undefined, building_number?: string | undefined, country_subdivision?: string | undefined, postal_code?: string | undefined, address_line?: string | undefined}) | undefined}; export type OriginalPayoutTransaction = {transaction_type: string, transaction_id: string, currency: CurrencyCode, amount: CurrencyAmount, description?: string | undefined, reference_number?: ReferenceNumber | undefined, tracking_key?: TrackingKey | undefined, execution_mode?: ExecutionMode | undefined, payment_purpose_category?: PaymentPurpose | undefined, po_connection?: PoConnection | undefined, payment_rail?: PaymentRail | undefined, execution_date?: string | undefined, debtor: PayoutDebtor, creditor: PayoutCreditor, ttl?: string | undefined}; export type OriginalPayoutMessage = {document: {header: OriginalMessageHeader, transactions: Array}}; export type CreateApiMessagesPayoutsArgs = {body: OriginalPayoutMessage}; export type CreateApiMessagesPayoutsOptions = {baseUrl?: string | undefined, ShinkansenJWSSignature?: string | undefined, ShinkansenApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionIdsMapping = z.object({transactions: z.array(z.object({transaction_id: z.string().min(1), shinkansen_transaction_id: z.string().min(1)}))}); 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 }; /** * Sends a payout message * * POST /messages/payouts */ export const createApiMessagesPayouts = async (args: CreateApiMessagesPayoutsArgs, options?: CreateApiMessagesPayoutsOptions) => { const {baseUrl = "https://api.shinkansen.finance/v1", fetch: fetchFn = fetch, ShinkansenJWSSignature, ShinkansenApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ShinkansenJWSSignature !== undefined) headers.set("Shinkansen-JWS-Signature", ShinkansenJWSSignature) if (ShinkansenApiKey !== undefined) headers.set("Shinkansen-Api-Key", ShinkansenApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/messages/payouts${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(transactionIdsMapping, await res.json()) };