@spreeloop/mtn_momo
Version:
A mtn momo api integration package
74 lines • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPaymentStatus = void 0;
const constants_1 = require("../../utils/constants");
const https_1 = require("../../utils/https");
const constants_2 = require("../utils/constants");
/**
* Retrieves the payment status for an MTN MOMO transaction.
*
* @param mobileStatusVerificationParams - Parameters containing the reference ID for the payment request.
* @param paymentServiceConfig - Configuration parameters for the payment service, including API keys and environment settings.
* @param endPoint - The API endpoint to check the payment status.
* @param accessToken - Optional access token for authorization.
* @returns A promise that resolves to the payment status response, including status data or an error type.
*/
async function getPaymentStatus({ mobileStatusVerificationParams, paymentServiceConfig, endPoint, accessToken, }) {
var _a;
const logger = paymentServiceConfig.logger;
const ocpApimSubscription = paymentServiceConfig.ocpApimSubscriptionKey;
const xReferenceId = mobileStatusVerificationParams.referenceId;
const apiUser = paymentServiceConfig.apiUserKey;
const apiKey = paymentServiceConfig.apiKey;
const xTargetEnvironment = paymentServiceConfig.xTargetEnvironment;
logger.info(`[Mtn MOMO] Checks the Mtn MOMO Payment status with route: ${endPoint}`);
if (!(ocpApimSubscription &&
xReferenceId &&
xTargetEnvironment &&
apiKey &&
apiUser)) {
logger.warn("Status check failed. Invalid data provided.", JSON.stringify({
ocpApimSubscription,
xReferenceId,
xTargetEnvironment,
apiKey,
apiUser,
}));
return {
error: constants_2.ApiErrorType.invalidData,
raw: {
error: "Status check failed. Invalid data provided.",
status: 400,
},
};
}
const paymentHeader = {
[constants_2.RequestKey.keyAuthorization]: `${constants_1.ConstantRequestField.bearer} ${accessToken}`,
[constants_2.RequestKey.keySubscriptionKey]: ocpApimSubscription,
[constants_2.RequestKey.keyEnvironmentTarget]: xTargetEnvironment,
};
const response = await (0, https_1.getRequest)({
headers: paymentHeader,
route: endPoint,
logger: logger,
});
const responseData = response === null || response === void 0 ? void 0 : response.response;
const status = (_a = responseData === null || responseData === void 0 ? void 0 : responseData.data) === null || _a === void 0 ? void 0 : _a.status;
if (responseData && (0, https_1.isSuccessfulCodeResponse)(responseData.status) && status) {
logger.log(`[Mtn MOMO] Status verification successful with result ${JSON.stringify(responseData.data)}`);
return {
data: {
status: status,
reason: responseData.data.reason,
},
raw: responseData.data,
};
}
logger.warn(`[Mtn MOMO] Status verification failed with error data: ${JSON.stringify(response.error)}`);
return {
raw: response.error,
error: constants_2.ApiErrorType.failedToCheckPaymentStatus,
};
}
exports.getPaymentStatus = getPaymentStatus;
//# sourceMappingURL=get_payment_status.js.map