96 lines
2.9 KiB
JavaScript
96 lines
2.9 KiB
JavaScript
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;
|
||
// }
|
||
//允许将account扣成负数
|
||
data.accountOper$entity = [
|
||
{
|
||
id: await generateNewIdAsync(),
|
||
action: 'create',
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
totalPlus: -price,
|
||
availPlus: -price,
|
||
type: 'consume',
|
||
accountId: account.id,
|
||
},
|
||
}
|
||
];
|
||
data.meta = {};
|
||
data.paid = price;
|
||
}
|
||
getState(pay) {
|
||
throw new Error("account类型的pay不应该需要查询此状态");
|
||
}
|
||
async close(pay) {
|
||
// throw new Error("account类型的pay无法关闭");
|
||
return;
|
||
}
|
||
}
|