@yengapay/nodejs-sdk
Version:
Official Node.js SDK for YengaPay - Accept mobile money payments and send payouts in West Africa
3 lines (2 loc) • 5.06 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class i extends Error{constructor(t,o,e){super(t),this.statusCode=o,this.code=e,this.name="YengaPayError",Error.captureStackTrace(this,this.constructor)}}class d extends i{constructor(t="Authentication failed"){super(t,401,"AUTHENTICATION_ERROR"),this.name="AuthenticationError"}}class l extends i{constructor(t){super(t,400,"VALIDATION_ERROR"),this.name="ValidationError"}}class w extends i{constructor(t){super(t,404,"NOT_FOUND"),this.name="NotFoundError"}}class E extends i{constructor(t,o=400){super(t,o,"PAYMENT_ERROR"),this.name="PaymentError"}}class g extends i{constructor(t="Network request failed"){super(t,0,"NETWORK_ERROR"),this.name="NetworkError"}}class y{constructor(t){this.config=t}async get(t){return this.request("GET",t)}async post(t,o){return this.request("POST",t,o)}async put(t,o){return this.request("PUT",t,o)}async delete(t){return this.request("DELETE",t)}async request(t,o,e){const c=`${this.config.baseURL}${o}`,f={"Content-Type":"application/json","x-api-key":this.config.apiKey},p={method:t,headers:f};e&&(p.body=JSON.stringify(e));const n=this.config.environment==="development";n&&(console.log(`
[YengaPay SDK Request]`),console.log("Method:",t),console.log("URL:",c),e&&console.log("Body:",JSON.stringify(e,null,2)));try{const s=await fetch(c,p);if(!s.ok){const a=await s.json().catch(()=>({})),u=a.message||a.error||s.statusText;throw n&&(console.log("[YengaPay SDK Response] ERROR"),console.log("Status:",s.status),console.log("Error:",a)),s.status===401||s.status===403?new d(u):new i(u,s.status)}const h=await s.json();return n&&(console.log("[YengaPay SDK Response] SUCCESS"),console.log("Status:",s.status),console.log("Body:",JSON.stringify(h,null,2)),console.log("")),h}catch(s){throw s instanceof i?s:new g(s instanceof Error?s.message:"Unknown error occurred")}}}class m{constructor(t){this.config=t,this.http=new y(t)}async init(t){const o=`/groups/${this.config.groupId}/projects/${this.config.projectId}/direct-payment/init`,e=await this.http.post(o,t);return{...e,expiresAt:new Date(e.expiresAt)}}async pay(t){const o=`/groups/${this.config.groupId}/projects/${this.config.projectId}/direct-payment/pay`;return await this.http.post(o,t)}async sendOtp(t){const o=`/groups/${this.config.groupId}/projects/${this.config.projectId}/direct-payment/send-otp`;return await this.http.post(o,t)}async getStatus(t){const o=`/groups/${this.config.groupId}/projects/${this.config.projectId}/direct-payment/status/${t}`,e=await this.http.get(o);return{...e,createdAt:e.createdAt?new Date(e.createdAt):void 0,completedAt:e.completedAt?new Date(e.completedAt):void 0}}}class A{constructor(t){this.config=t,this.http=new y(t)}async create(t){const o=`/groups/${this.config.groupId}/project/${this.config.projectId}/payout`,e=await this.http.post(o,t);return{...e,createdAt:new Date(e.createdAt)}}async getStatus(t){const o=`/groups/${this.config.groupId}/project/${this.config.projectId}/payout/${t}`,e=await this.http.get(o);return{...e,createdAt:new Date(e.createdAt),processedAt:e.processedAt?new Date(e.processedAt):void 0}}async retry(t){const o=`/groups/${this.config.groupId}/project/${this.config.projectId}/payout/${t}/retry`,e=await this.http.post(o);return{...e,createdAt:new Date(e.createdAt),processedAt:e.processedAt?new Date(e.processedAt):void 0}}}class P{constructor(){this.config={environment:"production"},this.config.baseURL="https://api.yengapay.com/api"}withApiBaseURL(t){return this.config.baseURL=t,this._directPayment=void 0,this._payout=void 0,this}withEnvironment(t){switch(this.config.environment=t,t){case"production":this.config.baseURL="https://api.yengapay.com/api/v1";break;case"staging":this.config.baseURL="https://api.staging.yengapay.com/api/v1";break;case"development":this.config.baseURL="http://localhost:3332/api/v1";break}return this._directPayment=void 0,this._payout=void 0,this}withApiKey(t){return this.config.apiKey=t,this._directPayment=void 0,this._payout=void 0,this}withGroupId(t){return this.config.groupId=t,this._directPayment=void 0,this._payout=void 0,this}withProjectId(t){return this.config.projectId=t,this._directPayment=void 0,this._payout=void 0,this}get directPayment(){return this.validateConfig(),this._directPayment||(this._directPayment=new m(this.config)),this._directPayment}get payout(){return this.validateConfig(),this._payout||(this._payout=new A(this.config)),this._payout}validateConfig(){if(!this.config.apiKey)throw new Error("API key is required. Use .withApiKey() to set it.");if(!this.config.groupId)throw new Error("Group ID is required. Use .withGroupId() to set it.");if(!this.config.projectId)throw new Error("Project ID is required. Use .withProjectId() to set it.");if(!this.config.baseURL)throw new Error("Base URL is required. Use .withApiBaseURL() or .withEnvironment() to set it.")}}exports.AuthenticationError=d;exports.NetworkError=g;exports.NotFoundError=w;exports.PaymentError=E;exports.ValidationError=l;exports.YengaPayClient=P;exports.YengaPayError=i;