ohehr_payment
Version:
Payment integration package for multiple payment providers
78 lines • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkKhanGateWayTransaction = exports.getKhanGateWayAccessToken = exports.getKhanGateWayConfig = void 0;
/**
* Generates a configuration object for Khan Gateway
* @param config - An object containing endpoint, consumerKey, secretKey, and bankAccount
* @returns KhanGatewayConfig object
*/
const getKhanGateWayConfig = ({ endpoint, consumerKey, secretKey, bankAccount, }) => {
return { endpoint, consumerKey, secretKey, bankAccount };
};
exports.getKhanGateWayConfig = getKhanGateWayConfig;
/**
* Fetches an access token from the Khan Gateway
* @param config - Configuration object for Khan Gateway
* @returns A Promise resolving to KhanGatewayAccessTokenResponse
* @throws Will throw an error if the request fails
*/
const getKhanGateWayAccessToken = async (config) => {
const token = `${config.consumerKey}:${config.secretKey}`;
const encodedToken = Buffer.from(token).toString("base64");
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${encodedToken}`,
};
try {
const response = await fetch(`${config.endpoint}/auth/token?grant_type=client_credentials`, {
method: "POST",
headers,
});
if (!response.ok) {
throw new Error(`Failed to fetch access token: ${response.statusText}`);
}
return (await response.json());
}
catch (error) {
console.error("Error fetching access token:", error);
throw error;
}
};
exports.getKhanGateWayAccessToken = getKhanGateWayAccessToken;
/**
* Fetches transaction data from the Khan Gateway
* @param from - Optional start date for transaction history
* @param to - Optional end date for transaction history
* @param tokens - Access token response from the gateway
* @param config - Configuration object for Khan Gateway
* @returns A Promise resolving to KhanGatewayTransactionResponse or undefined
* @throws Will throw an error if the request fails
*/
const checkKhanGateWayTransaction = async (from, to, tokens, config) => {
try {
if (!tokens.access_token) {
throw new Error("Access token is missing");
}
let url = `${config.endpoint}/statements/${config.bankAccount}`;
if (from)
url += `?from=${from.toISOString()}`;
if (to)
url += from ? `&to=${to.toISOString()}` : `?to=${to.toISOString()}`;
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${tokens.access_token}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch transactions: ${response.statusText}`);
}
return (await response.json());
}
catch (error) {
console.error("Error fetching transactions:", error);
throw error;
}
};
exports.checkKhanGateWayTransaction = checkKhanGateWayTransaction;
//# sourceMappingURL=utils.js.map