84 lines
2.5 KiB
JavaScript
84 lines
2.5 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
const tslib_1 = require("tslib");
|
||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
||
const uuid_1 = require("oak-domain/lib/utils/uuid");
|
||
class Account {
|
||
getAccountEntity() {
|
||
return ['', ''];
|
||
}
|
||
async getAccountAmount(context) {
|
||
throw new Error('不应走到这里');
|
||
}
|
||
calcTransferTax(price) {
|
||
return [0, '', ''];
|
||
}
|
||
calcRefundTax() {
|
||
return [0, '', ''];
|
||
}
|
||
calcPayTax() {
|
||
return [0, '', ''];
|
||
}
|
||
getRefundableAt(successAt) {
|
||
return (0, dayjs_1.default)(successAt).add(1000, 'y').valueOf();
|
||
}
|
||
async refund(refund) {
|
||
return;
|
||
}
|
||
async closeRefund(refund) {
|
||
return;
|
||
}
|
||
async getRefundState(refund) {
|
||
return ['refuding', undefined];
|
||
}
|
||
decodePayNotification(params, body) {
|
||
throw new Error("account类型的pay不需调用此接口");
|
||
}
|
||
async prepay(pay, data, context) {
|
||
const { entity, entityId, price } = pay;
|
||
(0, assert_1.default)(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 (0, uuid_1.generateNewIdAsync)(),
|
||
action: 'create',
|
||
data: {
|
||
id: await (0, uuid_1.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;
|
||
}
|
||
}
|
||
exports.default = Account;
|