60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
import { PAY_CHANNEL_ACCOUNT_NAME } from '../../types/PayConfig';
|
||
import assert from 'assert';
|
||
import { generateNewIdAsync } from "oak-domain/lib/utils/uuid";
|
||
export default class Account {
|
||
async refund(refund) {
|
||
return;
|
||
}
|
||
async closeRefund(refund) {
|
||
return;
|
||
}
|
||
async getRefundState(refund) {
|
||
return ['refuding', undefined];
|
||
}
|
||
decodePayNotification(params, body) {
|
||
throw new Error("account类型的pay不需调用此接口");
|
||
}
|
||
channel = PAY_CHANNEL_ACCOUNT_NAME;
|
||
async prepay(pay, data, context) {
|
||
const { accountId, price } = pay;
|
||
assert(accountId);
|
||
/**
|
||
* account类型的支付就是直接从account中扣除款项
|
||
* 但是注意最多只能把avail扣空。
|
||
* 如果一个pay没有完全支付,等account中充值了会自动继续进行支付
|
||
*/
|
||
const [account] = await context.select('account', {
|
||
data: {
|
||
id: 1,
|
||
avail: 1,
|
||
},
|
||
filter: {
|
||
id: accountId,
|
||
}
|
||
}, { forUpdate: true });
|
||
const { avail } = account;
|
||
const paid = Math.min(price, avail);
|
||
data.accountOper$entity = [
|
||
{
|
||
id: await generateNewIdAsync(),
|
||
action: 'create',
|
||
data: {
|
||
id: await generateNewIdAsync(),
|
||
totalPlus: -paid,
|
||
availPlus: -paid,
|
||
type: 'consume',
|
||
accountId,
|
||
},
|
||
}
|
||
];
|
||
data.meta = {};
|
||
data.paid = paid;
|
||
}
|
||
getState(pay) {
|
||
throw new Error("account类型的pay不应该需要查询此状态");
|
||
}
|
||
close(pay) {
|
||
throw new Error("account类型的pay无法关闭");
|
||
}
|
||
}
|