ohehr_payment
Version:
Payment integration package for multiple payment providers
174 lines • 5.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.qPayCheckPayment = exports.qPayDeleteInvoice = exports.qPayCreateInvoice = exports.qPayGetAccessToken = exports.qPayGetConfig = exports.fetcher = void 0;
const axios_1 = __importDefault(require("axios"));
/**
* Fetcher utility using Axios
* @param options - Fetcher options
* @returns Response data of generic type T
*/
const fetcher = async ({ url, options, }) => {
try {
const axiosConfig = {
url,
method: options?.method,
headers: options?.headers
? Object.fromEntries(options.headers instanceof Headers
? options.headers.entries()
: Object.entries(options.headers))
: undefined,
data: options?.body,
};
const response = await (0, axios_1.default)(axiosConfig);
return { res: response.data };
}
catch (error) {
console.log('YMAR ERROR VE FETCHER', error);
if (axios_1.default.isAxiosError(error) && error.response) {
throw new Error(`Axios Error: ${error.response.data}`);
}
throw new Error('Axios fetcher Custom Error');
}
};
exports.fetcher = fetcher;
/**
* Generates a configuration object for QPay
* @param config - QPay configuration
* @returns IQPayConfig object
*/
const qPayGetConfig = ({ username, password, invoiceCode, url, callbackUrl, }) => {
return { username, password, invoiceCode, url, callbackUrl };
};
exports.qPayGetConfig = qPayGetConfig;
/**
* Fetches an access token from QPay
* @param config - QPay configuration
* @returns Access token string
*/
const qPayGetAccessToken = async (config) => {
const port = '/v2/auth/token';
const { res } = await (0, exports.fetcher)({
url: `${config.url}${port}`,
options: {
method: 'POST',
headers: {
Authorization: 'Basic ' +
Buffer.from(`${config.username}:${config.password}`).toString('base64'),
},
body: '',
redirect: 'follow',
},
});
if (!res.access_token) {
throw new Error('QPay Token Error');
}
return res.access_token;
};
exports.qPayGetAccessToken = qPayGetAccessToken;
/**
* Creates an invoice in QPay
* @param params - Invoice creation parameters
* @returns Invoice creation response
*/
const qPayCreateInvoice = async (params) => {
const { data, token, config } = params;
const body = JSON.stringify({
invoice_code: data.qPayV2InvoiceCode,
sender_invoice_no: data.senderInvoiceNo,
amount: data.amount,
callback_url: data.callbackUrl,
sender_branch_code: data.senderBranchCode,
sender_staff_code: data.senderStaffCode,
invoice_receiver_code: data.invoiceReceiverCode,
invoice_description: data.invoiceDescription,
});
const path = '/v2/invoice';
const { res } = await (0, exports.fetcher)({
url: `${config.url}${path}`,
options: {
method: 'POST',
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json',
},
body,
redirect: 'follow',
},
});
if (res.error) {
throw new Error('Invoice creation failed');
}
return {
invoiceId: res.invoice_id,
qrText: res.qr_text,
qPayShortUrl: res.qPay_shortUrl,
urls: res.urls,
};
};
exports.qPayCreateInvoice = qPayCreateInvoice;
/**
* Deletes an invoice by ID
* @param params - Invoice deletion parameters
* @returns Deletion result
*/
const qPayDeleteInvoice = async (params) => {
const { invoiceId, token, config } = params;
const port = `/v2/invoice/${invoiceId}`;
const { res } = await (0, exports.fetcher)({
url: `${config.url}${port}`,
options: {
method: 'DELETE',
headers: { Authorization: 'Bearer ' + token },
redirect: 'follow',
},
});
return res;
};
exports.qPayDeleteInvoice = qPayDeleteInvoice;
/**
* Checks payment status for an invoice
* @param params - Payment check parameters
* @returns Payment check response
*/
const qPayCheckPayment = async (params) => {
const { invoiceId, token, config } = params;
const path = '/v2/payment/check';
const body = JSON.stringify({
object_type: 'INVOICE',
object_id: invoiceId,
offset: { page_number: 1, page_limit: 100 },
});
const { res } = await (0, exports.fetcher)({
url: `${config.url}${path}`,
options: {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
body,
redirect: 'follow',
},
});
if (res.error) {
return undefined;
}
return {
count: Number(res.count ?? 0),
paidAmount: Number(res.paid_amount ?? 0),
rows: res.rows?.map((row) => ({
paymentId: row.payment_id,
paymentStatus: row.payment_status,
paymentAmount: Number(row.payment_amount ?? 0),
paymentFee: Number(row.trx_fee ?? 0),
paymentWallet: row.payment_wallet,
paymentType: row.payment_type,
paymentDate: row.payment_date ? new Date(row.payment_date) : undefined,
})),
};
};
exports.qPayCheckPayment = qPayCheckPayment;
//# sourceMappingURL=utils.js.map