UNPKG

flutterwave-universal

Version:

Universal TypeScript SDK for Flutterwave payment services, compatible with Node.js, Bun, Deno, Cloudflare Workers and Edge Functions

2 lines 6.95 kB
"use strict";var R=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var F=(r,e)=>{for(var n in e)R(r,n,{get:e[n],enumerable:!0})},M=(r,e,n,p)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of E(e))!_.call(r,s)&&s!==n&&R(r,s,{get:()=>e[s],enumerable:!(p=B(e,s))||p.enumerable});return r};var S=r=>M(R({},"__esModule",{value:!0}),r);var V={};F(V,{Flutterwave:()=>f,FlutterwaveError:()=>o,default:()=>x});module.exports=S(V);var o=class r extends Error{constructor(e,n,p,s){super(e),this.name="FlutterwaveError",this.status=n,this.code=p,this.data=s,Object.setPrototypeOf(this,r.prototype)}};var $="v3",T=`https://api.flutterwave.com/${$}`;var l=class{constructor(e){if(!e.secretKey)throw new Error("Flutterwave secret key is required.");this.secretKey=e.secretKey,this.baseUrl=T,this.timeout=e.timeout||6e4}async request(e,n,p){let s=`${this.baseUrl}/${n}`,b={Authorization:`Bearer ${this.secretKey}`,"Content-Type":"application/json",Accept:"application/json"},P={method:e,headers:b};if(p){if(e==="PUT"||e==="POST")P.body=JSON.stringify(p);else if(e==="GET"||e==="DELETE"){let t=new URLSearchParams(Object.entries(p).reduce((i,[a,c])=>(c!=null&&(i[a]=String(c)),i),{})).toString();t&&(s=`${s}?${t}`)}}let w=new AbortController,k=setTimeout(()=>w.abort(),this.timeout);P.signal=w.signal;try{let t=await fetch(s,P);if(clearTimeout(k),!t.ok){let a;try{a=await t.json()}catch{a=await t.text()}let c=typeof a=="string"?a||`API request failed with status ${t.status}`:a.message||`API request failed with status ${t.status}`,A=typeof a!="string"?a.code:void 0,v=typeof a!="string"?a.data:void 0;throw new o(c,String(t.status),A,v)}if(t.status===204)return{};let i=await t.json();if(i.status&&i.status==="error"){let a=i;throw new o(a.message,"API Error",a.code,a.data)}return i.data!==void 0?i.data:i}catch(t){throw clearTimeout(k),t instanceof o?t:t instanceof Error&&t.name==="AbortError"?new o(`Request timed out after ${this.timeout}ms`,"TimeoutError"):new o(t instanceof Error?t.message:"An unexpected network error occurred","NetworkError")}}get(e,n){return this.request("GET",e,n)}post(e,n){return this.request("POST",e,n)}put(e,n){return this.request("PUT",e,n)}delete(e,n){return this.request("DELETE",e,n)}};var u=class{constructor(e){this.apiClient=e}async list(e){return this.apiClient.get(`banks/${e}`)}async branches(e){return this.apiClient.get(`banks/${e}/branches`)}};var y=class{constructor(e){this.client=e}async card(e){return this.client.post("charges?type=card",e)}async bankTransfer(e){return this.client.post("charges?type=bank_transfer",e)}async ach(e){return this.client.post("charges?type=ach_payment",e)}async bank_uk_eu(e){return this.client.post("charges?type=debit_uk_account",e)}async bank_ng(e){return this.client.post("charges?type=mono",e)}async mpesa(e){return this.client.post("charges?type=mpesa",e)}async ghanaMobile(e){return this.client.post("charges?type=mobile_money_ghana",e)}async rwandaMobile(e){return this.client.post("charges?type=mobile_money_rwanda",e)}async ugandaMobile(e){return this.client.post("charges?type=mobile_money_uganda",e)}async francophoneMobile(e){return this.client.post("charges?type=mobile_money_franco",e)}async tanzaniaMobile(e){return this.client.post("charges?type=mobile_money_tanzania",e)}async zambiaMobile(e){return this.client.post("harges?type=mobile_money_zambia",e)}async voucher(e){return this.client.post("charges?type=voucher_payment",e)}async applePay(e){return this.client.post("charges?type=applepay",e)}async googlePay(e){return this.client.post("charges?type=googlepay",e)}async enaira(e){return this.client.post("charges?type=enaira",e)}async ussd(e){return this.client.post("charges?type=ussd",e)}async nqr(e){return this.client.post("charges?type=qr",e)}async opay(e){return this.client.post("charges?type=opay",e)}async capitec(e){return this.client.post("charges?type=capitec",e)}async fawryPay(e){return this.client.post("charges?type=fawry_pay",e)}async validate(e){return this.client.post("validate-charge",e)}};var h=class{constructor(e){this.client=e}async generate(e){return this.client.post("otps",e)}async validate(e){return this.client.post(`otps/${crypto.randomUUID()}/validate`,e)}};var g=class{constructor(e){this.apiClient=e}async initiate(e){return this.apiClient.post("payments",e)}};var m=class{constructor(e){this.apiClient=e}async create(e){return this.apiClient.post("transfers",e)}async retry(e){return this.apiClient.post(`transfers/${e.id}/retries`,e)}async bulkTransfer(e){return this.apiClient.post("bulk-transfers",e)}async queryFee(e){return this.apiClient.get(`transfers/fee?amount=${e.amount}&currency=${e.currency}&type=${e.type}`)}async list(e){return this.apiClient.get(`transfers?${new URLSearchParams(e).toString()}`)}async get(e){return this.apiClient.get(`transfers/${e.id}`)}async getRetry(e){return this.apiClient.get(`transfers/${e.id}/retries`)}async getBulkTransfer(e){return this.apiClient.get(`transfers?${new URLSearchParams(e).toString()}`)}async queryTransferRates(e){return this.apiClient.get(`transfers/rates?${new URLSearchParams(e).toString()}`)}async queryBulkRates(e){return this.apiClient.post("bulk-rates",e)}async verifyBulkRates(e){return this.apiClient.get(`bulk-rates?id=${e.id}`)}};var C=class{constructor(e){this.apiClient=e}async create(e){return this.apiClient.post("beneficiaries",e)}async list(e){return this.apiClient.get(`beneficiaries?page=${e.page}`)}async get(e){return this.apiClient.get(`beneficiaries/${e.id}`)}async delete(e){return this.apiClient.delete(`beneficiaries/${e.id}`)}};var d=class{constructor(e){this.apiClient=e}async create(e){return this.apiClient.post("virtual-account-numbers",e)}async createBulk(e){return this.apiClient.post("bulk-virtual-account-numbers",e)}async get(e){return this.apiClient.get(`virtual-account-numbers/${e.account_number}`)}async getBulk(e){return this.apiClient.get(`bulk-virtual-account-numbers/${e.batch_id}`)}async updateBVN(e){return this.apiClient.put(`virtual-account-numbers/${e.order_ref}`,{bvn:e.bvn})}async delete(e){return this.apiClient.delete(`virtual-account-numbers/${e.order_ref}`,{status:e.status})}};var f=class{constructor(e){if(!e||!e.secretKey)throw new Error("Flutterwave configuration with secretKey is required.");this.apiClient=new l(e),this.banks=new u(this.apiClient),this.payments=new g(this.apiClient),this.otp=new h(this.apiClient),this.charges=new y(this.apiClient),this.transfer=new m(this.apiClient),this.beneficiary=new C(this.apiClient),this.virtualAccount=new d(this.apiClient)}async ping(){try{return await this.banks.list("NG"),{status:"success",message:"Flutterwave API connection successful."}}catch(e){return{status:"error",message:`Flutterwave API connection failed: ${e instanceof Error?e.message:"Unknown error"}`}}}},x=f;0&&(module.exports={Flutterwave,FlutterwaveError}); //# sourceMappingURL=index.js.map