import FlutterwaveInitError from './utils/FlutterwaveInitError' ; import ResponseParser from './utils/ResponseParser' ; import { STANDARD_URL } from './configs' ; export type Currency = 'AUD' | 'BIF' | 'CDF' | 'CAD' | 'CVE' | 'EUR' | 'GBP' | 'GHS' | 'GMD' | 'GNF' | 'KES' | 'LRD' | 'MWK' | 'MZN' | 'NGN' | 'RWF' | 'SLL' | 'STD' | 'TZS' | 'UGX' | 'USD' | 'XAF' | 'XOF' | 'ZAR' | 'ZMK' | 'ZMW' | 'ZWD' ; export interface FlutterwaveInitSubAccount { id : string ; transaction_split_ratio ? : number ; transaction_charge_type ? : string ; transaction_charge ? : number ; } export interface FlutterwaveInitOptionsBase { amount : number ; currency ? : Currency ; integrity_hash ? : string ; payment_options ? : string ; payment_plan ? : number ; redirect_url : string ; subaccounts ? : Array < FlutterwaveInitSubAccount > ; } interface FlutterwavePaymentMeta { [ k ] : any ; } export interface FlutterwaveInitCustomer { email : string ; phonenumber ? : string ; name ? : string ; } export interface FlutterwaveInitCustomizations { title ? : string ; logo ? : string ; description ? : string ; } export type FlutterwaveInitOptions = FlutterwaveInitOptionsBase & { authorization : string ; tx_ref : string ; customer : FlutterwaveInitCustomer ; meta ? : FlutterwavePaymentMeta | null ; customizations ? : FlutterwaveInitCustomizations ; } ; export interface FieldError { field : string ; message : string ; } export interface ResponseData { status ? : 'success' | 'error' ; message : string ; error_id ? : string ; errors ? : Array < FieldError > ; code ? : string ; data ? : { link : string ; } ; } interface FetchOptions { method : 'POST' ; body : string ; headers : Headers ; signal ? : AbortSignal ; } param options param abortController return export default async function FlutterwaveInit ( options , abortController ? ) { try { const { authorization , ... body } = options ; const headers = new Headers ; headers . append ( 'Content-Type' , 'application/json' ) ; headers . append ( 'Authorization' , `Bearer ${ authorization }` ) ; const fetchOptions = { method : 'POST' , body : JSON . stringify ( body ) , headers : headers } ; if ( abortController ) { fetchOptions . signal = abortController . signal ; } ; const response = await fetch ( STANDARD_URL , fetchOptions ) ; const responseData = await response . json ( ) ; return Promise . resolve ( await ResponseParser ( responseData ) ) ; } catch ( e ) { const error = e instanceof FlutterwaveInitError ? e : new FlutterwaveInitError ( { message : e . message , code : e . name . toUpperCase ( ) } ) ; return Promise . reject ( error ) ; } }