afrimomo-sdk
Version:
A unified SDK for African payment providers
105 lines • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AfromomoSDK = void 0;
const paychangu_1 = require("@afrimomo-sdk/services/paychangu");
const pawapay_1 = require("@afrimomo-sdk/services/pawapay");
const env_1 = require("./config/env");
class AfromomoSDK {
config;
static instance;
_paychangu;
_pawapay;
envConfig;
constructor(config = {}) {
this.config = config;
if (config.env) {
(0, env_1.loadEnvFile)(config.env);
}
else {
(0, env_1.loadEnvFile)();
}
this.envConfig = (0, env_1.loadEnvConfig)();
this.initializeServices();
}
static initialize(config = {}) {
if (!AfromomoSDK.instance) {
AfromomoSDK.instance = new AfromomoSDK(config);
const services = AfromomoSDK.instance.getConfiguredServices();
if (services.length === 0) {
console.warn("⚠️ No payment services were configured");
console.log("Available services:");
console.log("- PayChangu (requires PAYCHANGU_SECRET_KEY)");
console.log("- PawaPay (requires PAWAPAY_JWT)");
}
else {
console.log("✓ Initialized Afromomo SDK with services:");
for (const service of services) {
console.log(`- ${service.charAt(0).toUpperCase() + service.slice(1)}`);
}
}
}
return AfromomoSDK.instance;
}
static getInstance() {
if (!AfromomoSDK.instance) {
throw new Error("SDK not initialized. Call AfromomoSDK.initialize() first");
}
return AfromomoSDK.instance;
}
get paychangu() {
if (!this._paychangu) {
throw new Error("PayChangu service is not configured. Please provide PayChangu credentials in the SDK config or environment variables.");
}
return this._paychangu;
}
get pawapay() {
if (!this._pawapay) {
throw new Error("PawaPay service is not configured. Please provide PawaPay credentials in the SDK config or environment variables.");
}
return this._pawapay;
}
isServiceConfigured(service) {
switch (service) {
case "paychangu":
return !!this._paychangu;
case "pawapay":
return !!this._pawapay;
default:
return false;
}
}
getConfiguredServices() {
const services = [];
if (this._paychangu)
services.push("paychangu");
if (this._pawapay)
services.push("pawapay");
return services;
}
initializeServices() {
this.initializeFromEnv();
this.initializeFromConfig();
}
initializeFromEnv() {
if (!this.envConfig)
return;
const paychanguValidation = (0, env_1.validatePSPConfig)(this.envConfig, "paychangu");
if (paychanguValidation.isValid) {
this._paychangu = new paychangu_1.PayChangu(this.envConfig.PAYCHANGU_SECRET_KEY);
}
const pawapayValidation = (0, env_1.validatePSPConfig)(this.envConfig, "pawapay");
if (pawapayValidation.isValid) {
this._pawapay = new pawapay_1.PawaPay(this.envConfig.PAWAPAY_JWT);
}
}
initializeFromConfig() {
if (this.config.paychangu?.secretKey) {
this._paychangu = new paychangu_1.PayChangu(this.config.paychangu.secretKey);
}
if (this.config.pawapay?.jwt) {
this._pawapay = new pawapay_1.PawaPay(this.config.pawapay.jwt);
}
}
}
exports.AfromomoSDK = AfromomoSDK;
//# sourceMappingURL=sdk.js.map