oak-pay-business/lib/features/Pay.js

80 lines
2.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const oak_frontend_base_1 = require("oak-frontend-base");
const wpProductFrontend_1 = require("../utils/wpProductFrontend");
function getDepositLoss(price, application) {
const { system } = application;
const { payConfig } = system;
const depositLoss = payConfig?.depositLoss;
if (depositLoss) {
const { ratio, highest, lowest } = depositLoss;
let loss = ratio && Math.round(price * ratio / 100) || 0;
if (highest && loss > highest) {
return [highest, 'common::deposit.lossReason.highest', { value: (highest / 100).toFixed(2) }];
}
if (lowest && loss < lowest) {
return [lowest, 'common::deposit.lossReason.lowest', { value: (lowest / 100).toFixed(2) }];
}
if (loss > 0) {
return [loss, 'common::deposit.lossReason.ratio', { value: ratio }];
}
}
return [0, '', undefined];
}
class Pay extends oak_frontend_base_1.Feature {
application;
locales;
constructor(application, locales) {
super();
this.application = application;
this.locales = locales;
}
getPayChannels(type, accountId) {
const application = this.application.getApplication();
const { wpProduct$application: wpProducts, system } = application;
const { offlineAccount$system: offlineAccounts } = system;
const availableWpProductTypes = (0, wpProductFrontend_1.getWpProductTypeFromEnv)();
const channels = [
...offlineAccounts.filter((ele) => {
if (type) {
if (type === 'deposit') {
return ele.allowDeposit;
}
else if (type === 'pay') {
return ele.allowPay;
}
}
return true;
}).map(ele => ({
entity: 'offlineAccount',
entityId: ele.id,
label: `offlineAccount:v.type.${ele.type}`,
})),
...wpProducts.filter((ele) => availableWpProductTypes.includes(ele.type)).map(ele => ({
entity: 'wpProduct',
entityId: ele.id,
label: 'payChannel::wpProduct',
}))
];
if (accountId && type === 'pay') {
channels.push({
entity: 'account',
entityId: accountId,
label: 'payChannel::account',
});
}
return channels;
}
getPayConfigs() {
throw new Error('method not support anymore');
}
calcDepositLoss(price, channel) {
const { entity, entityId } = channel;
return getDepositLoss(price, this.application.getApplication());
}
getDepositRatio(channel) {
throw new Error('method not implemented');
}
}
exports.default = Pay;