@spreeloop/mtn_momo
Version:
A mtn momo api integration package
44 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccessToken = void 0;
const constants_1 = require("../../utils/constants");
const https_1 = require("../../utils/https");
const constants_2 = require("../utils/constants");
/**
* Gets the access token for the API.
*
* @param {MtnMomoPaymentParams} paymentConfig - The payment configuration.
* @param {string} endPoint - The end point route.
* @return {Promise<GetAccessTokenResponse>} The access token response.
*/
const getAccessToken = async (paymentConfig, endPoint) => {
var _a, _b, _c, _d;
const logger = paymentConfig.logger;
const encodeToBase64UserAndApikey = (0, https_1.encodeToBase64)(paymentConfig.apiUserKey, paymentConfig.apiKey);
const authorization = `${constants_1.ConstantRequestField.basic} ${encodeToBase64UserAndApikey}`;
const header = {
[constants_2.RequestKey.keyAuthorization]: authorization,
[constants_2.RequestKey.keySubscriptionKey]: paymentConfig.ocpApimSubscriptionKey,
};
logger.info(`Generates the access token with data: \n- headers: ${JSON.stringify(header)}\n- route: ${endPoint}`);
const response = await (0, https_1.postRequest)({
headers: header,
route: endPoint,
logger: logger,
});
const accessToken = (_b = (_a = response.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.access_token;
if (response.error || !accessToken) {
logger.warn(`Request failed to generate the access token. Error retrieved: ${JSON.stringify(response.error)}`);
return {
raw: response.error,
error: constants_2.ApiErrorType.failedToGenerateAccessToken,
};
}
logger.info(`Access token successfully generated with data ${JSON.stringify((_c = response.response) === null || _c === void 0 ? void 0 : _c.data)}`);
return {
data: accessToken,
raw: (_d = response.response) === null || _d === void 0 ? void 0 : _d.data,
};
};
exports.getAccessToken = getAccessToken;
//# sourceMappingURL=get_access_token.js.map