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

77 lines
2.7 KiB
JavaScript

import { Feature } from "oak-frontend-base";
import { getWpProductTypeFromEnv } from "../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];
}
export default class Pay extends 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 = 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');
}
}