oak-pay-business/es/utils/payClazz/Account.js

80 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'assert';
import dayJs from 'dayjs';
import { generateNewIdAsync } from "oak-domain/lib/utils/uuid";
export default class Account {
getAccountEntity() {
return ['', ''];
}
async getAccountAmount(context) {
throw new Error('不应走到这里');
}
calcTransferTax(price) {
return [0, '', ''];
}
calcRefundTax() {
return [0, '', ''];
}
calcPayTax() {
return [0, '', ''];
}
getRefundableAt(successAt) {
return dayJs(successAt).add(1000, 'y').valueOf();
}
async refund(refund) {
return;
}
async getRefundState(refund) {
return ['refuding', undefined];
}
decodePayNotification(params, body) {
throw new Error("account类型的pay不需调用此接口");
}
decodeRefundNotification(params, body) {
throw new Error("account类型的pay不需调用此接口");
}
async prepay(pay, data, context) {
const { entity, entityId, price } = pay;
assert(entity === 'account' && entityId);
/**
* account类型的支付就是直接从account中扣除款项
* 但是注意最多只能把avail扣空。
* 如果一个pay没有完全支付等account中充值了会自动继续进行支付
*/
const [account] = await context.select('account', {
data: {
id: 1,
avail: 1,
},
filter: {
id: entityId,
}
}, { forUpdate: true });
const { avail } = account;
const paid = Math.min(price, avail);
if (paid > 0) {
data.accountOper$entity = [
{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
totalPlus: -paid,
availPlus: -paid,
type: 'consume',
accountId: account.id,
},
}
];
data.meta = {};
data.paid = paid;
}
}
getState(pay) {
throw new Error("account类型的pay不应该需要查询此状态");
}
async close(pay) {
// throw new Error("account类型的pay无法关闭");
return;
}
}