afrimomo-sdk
Version:
A unified SDK for African payment providers
160 lines • 6.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AfromomoSDK = void 0;
const services_1 = require("./services");
const services_2 = require("./services");
const services_3 = require("./services");
const services_4 = require("./services");
const env_1 = require("./config/env");
class AfromomoSDK {
config;
static instance;
_paychangu;
_pawapay;
_onekhusa;
envConfig;
_providers = {};
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();
const providers = Object.keys(AfromomoSDK.instance._providers);
if (services.length === 0 && providers.length === 0) {
console.warn("⚠️ No payment services were configured");
console.log("Available built-in services:");
console.log("- PayChangu (requires PAYCHANGU_SECRET_KEY)");
console.log("- PawaPay (requires PAWAPAY_JWT)");
console.log("- OneKhusa (requires ONEKHUSA_API_KEY, ONEKHUSA_API_SECRET, ONEKHUSA_ORGANISATION_ID)");
console.log("- Custom providers (configure via providers option)");
}
else {
console.log("✓ Initialized Afromomo SDK with services:");
for (const service of services) {
console.log(`- ${service.charAt(0).toUpperCase() + service.slice(1)}`);
}
if (providers.length > 0) {
console.log("✓ Custom payment providers:");
for (const provider of providers) {
console.log(`- ${provider}`);
}
}
}
}
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;
}
get onekhusa() {
if (!this._onekhusa) {
throw new Error("OneKhusa service is not configured. Please provide OneKhusa credentials in the SDK config or environment variables.");
}
return this._onekhusa;
}
getProvider(providerName) {
if (!this._providers[providerName]) {
throw new Error(`Provider '${providerName}' is not configured. Please add it to the providers configuration.`);
}
return this._providers[providerName];
}
addProvider(name, config) {
const providerConfig = { ...config, name };
const provider = new services_4.PaymentProviderAdapter(providerConfig);
this._providers[name] = provider;
return provider;
}
isServiceConfigured(service) {
switch (service) {
case "paychangu":
return !!this._paychangu;
case "pawapay":
return !!this._pawapay;
case "onekhusa":
return !!this._onekhusa;
default:
return !!this._providers[service];
}
}
getConfiguredServices() {
const services = [];
if (this._paychangu)
services.push("paychangu");
if (this._pawapay)
services.push("pawapay");
if (this._onekhusa)
services.push("onekhusa");
return services;
}
getCustomProviders() {
return Object.keys(this._providers);
}
initializeServices() {
this.initializeFromEnv();
this.initializeFromConfig();
this.initializeCustomProviders();
}
initializeFromEnv() {
if (!this.envConfig)
return;
const paychanguValidation = (0, env_1.validatePSPConfig)(this.envConfig, "paychangu");
if (paychanguValidation.isValid) {
this._paychangu = new services_1.PayChangu(this.envConfig.PAYCHANGU_SECRET_KEY);
}
const pawapayValidation = (0, env_1.validatePSPConfig)(this.envConfig, "pawapay");
if (pawapayValidation.isValid) {
this._pawapay = new services_2.PawaPay(this.envConfig.PAWAPAY_JWT);
}
const onekhusaValidation = (0, env_1.validatePSPConfig)(this.envConfig, "onekhusa");
if (onekhusaValidation.isValid) {
this._onekhusa = new services_3.OneKhusa(this.envConfig.ONEKHUSA_API_KEY, this.envConfig.ONEKHUSA_API_SECRET, this.envConfig.ONEKHUSA_ORGANISATION_ID, this.envConfig.ONEKHUSA_ENVIRONMENT);
}
}
initializeFromConfig() {
if (this.config.paychangu?.secretKey) {
this._paychangu = new services_1.PayChangu(this.config.paychangu.secretKey, this.config.paychangu.environment, this.config.paychangu.sandboxUrl, this.config.paychangu.productionUrl);
}
if (this.config.pawapay?.jwt) {
this._pawapay = new services_2.PawaPay(this.config.pawapay.jwt, this.config.pawapay.environment, this.config.pawapay.sandboxUrl, this.config.pawapay.productionUrl);
}
if (this.config.onekhusa?.apiKey &&
this.config.onekhusa?.apiSecret &&
this.config.onekhusa?.organisationId) {
this._onekhusa = new services_3.OneKhusa(this.config.onekhusa.apiKey, this.config.onekhusa.apiSecret, this.config.onekhusa.organisationId, this.config.onekhusa.environment, this.config.onekhusa.sandboxUrl, this.config.onekhusa.productionUrl);
}
}
initializeCustomProviders() {
if (this.config.providers) {
for (const [name, config] of Object.entries(this.config.providers)) {
this.addProvider(name, config);
}
}
}
}
exports.AfromomoSDK = AfromomoSDK;
//# sourceMappingURL=sdk.js.map