afrimomo-sdk
Version:
A unified SDK for African payment providers
104 lines • 4.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentProviderAdapter = void 0;
const httpClient_1 = require("../../utils/httpClient");
const logger_1 = require("../../utils/logger");
const serviceWrapper_1 = require("../../utils/serviceWrapper");
class PaymentProviderAdapter {
network;
config;
constructor(config) {
this.config = config;
const httpConfig = {
baseUrl: config.baseUrl,
serviceName: config.name,
defaultHeaders: config.defaultHeaders,
};
const authStrategy = {
type: config.authType,
token: config.authToken,
username: config.username,
password: config.password,
};
this.network = new httpClient_1.HttpClient(httpConfig, authStrategy);
}
async createPayment(request) {
try {
const requestData = this.config.requestTransformer
? this.config.requestTransformer(request)
: request;
logger_1.logger.info(`${this.config.name}: Creating payment`, {
amount: request.amount,
currency: request.currency,
});
const response = await this.network.post(this.config.endpoints.createPayment, requestData, `creating payment for ${request.amount} ${request.currency}`);
return this.config.responseTransformer
? this.config.responseTransformer(response)
: response;
}
catch (error) {
logger_1.logger.error(`${this.config.name}: Failed to create payment`, error);
if ((0, serviceWrapper_1.isServiceError)(error)) {
return { success: false, message: error.errorMessage };
}
return {
success: false,
message: error instanceof Error ? error.message : "Unknown error occurred",
};
}
}
async getTransaction(transactionId) {
try {
logger_1.logger.info(`${this.config.name}: Getting transaction`, {
transactionId,
});
const endpoint = this.config.endpoints.getTransaction.replace("{id}", transactionId);
const response = await this.network.get(endpoint, `getting transaction ${transactionId}`);
return this.config.responseTransformer
? this.config.responseTransformer(response)
: response;
}
catch (error) {
logger_1.logger.error(`${this.config.name}: Failed to get transaction`, error);
return null;
}
}
async getBalance() {
if (!this.config.endpoints.getBalance) {
logger_1.logger.warn(`${this.config.name}: Balance checking not supported`);
return null;
}
try {
logger_1.logger.info(`${this.config.name}: Getting balance`);
const response = await this.network.get(this.config.endpoints.getBalance, "getting wallet balance");
return this.config.responseTransformer
? this.config.responseTransformer(response)
: response;
}
catch (error) {
logger_1.logger.error(`${this.config.name}: Failed to get balance`, error);
return null;
}
}
async request(method, endpoint, data) {
try {
logger_1.logger.info(`${this.config.name}: Making custom ${method.toUpperCase()} request to ${endpoint}`);
switch (method) {
case "get":
return await this.network.get(endpoint, `custom ${method} request`);
case "post":
return await this.network.post(endpoint, data || {}, `custom ${method} request`);
case "put":
return await this.network.put(endpoint, data || {}, `custom ${method} request`);
case "delete":
return await this.network.delete(endpoint, `custom ${method} request`);
}
}
catch (error) {
logger_1.logger.error(`${this.config.name}: Custom request failed`, error);
return null;
}
}
}
exports.PaymentProviderAdapter = PaymentProviderAdapter;
//# sourceMappingURL=paymentProvider.js.map