"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPayClazz = exports.registerPayClazz = exports.getAccountEntity = void 0; const tslib_1 = require("tslib"); const assert_1 = tslib_1.__importDefault(require("assert")); const Offline_1 = tslib_1.__importDefault(require("./Offline")); const Account_1 = tslib_1.__importDefault(require("./Account")); const WechatPay_1 = tslib_1.__importDefault(require("./WechatPay")); const abstractChecker_1 = require("../../checkers/abstractChecker"); const PayChannelDict = {}; const PayClazzEntityDict = { 'account': { accountEntity: '', clazzConstructor: async () => new Account_1.default(), }, 'offlineAccount': { accountEntity: 'offlineAccount', clazzConstructor: async (entityId, context) => { const [offlineAccount] = await context.select('offlineAccount', { data: { id: 1, type: 1, channel: 1, name: 1, qrCode: 1, taxLossRatio: 1, refundCompensateRatio: 1, refundGapDays: 1, allowDeposit: 1, allowPay: 1, systemId: 1, price: 1, enabled: 1, allowWithdrawTransfer: 1, withdrawTransferLossRatio: 1, }, filter: { id: entityId, }, }, { dontCollect: true }); (0, assert_1.default)(offlineAccount.enabled); return new Offline_1.default(offlineAccount); } }, 'wpProduct': { accountEntity: 'wpAccount', clazzConstructor: async (entityId, context) => { const [wpProduct] = await context.select('wpProduct', { data: { id: 1, type: 1, taxLossRatio: 1, refundCompensateRatio: 1, refundGapDays: 1, needReceiving: 1, enabled: 1, application: { id: 1, config: 1, type: 1, }, wpAccount: { id: 1, mchId: 1, taxLossRatio: 1, refundGapDays: 1, refundCompensateRatio: 1, publicKeyFilePath: 1, privateKeyFilePath: 1, apiV3Key: 1, wechatPay: { id: 1, payNotifyUrl: 1, refundNotifyUrl: 1, }, enabled: 1, }, }, filter: { id: entityId, } }, { dontCollect: true }); const { application } = wpProduct; const { type, config } = application; (0, assert_1.default)(wpProduct.enabled && wpProduct.wpAccount?.enabled); switch (type) { case 'web': { const { wechatPay } = config; (0, assert_1.default)(wechatPay); const appId = wechatPay.appId; return new WechatPay_1.default(wpProduct, appId); } case 'wechatMp': { const { appId } = config; return new WechatPay_1.default(wpProduct, appId); } case 'wechatPublic': { const { appId } = config; return new WechatPay_1.default(wpProduct, appId); } default: { (0, assert_1.default)(false, '暂时不支持'); } } } }, }; function getAccountEntity(entity) { return PayClazzEntityDict[entity].accountEntity; } exports.getAccountEntity = getAccountEntity; // 这里用一个flag来表达先后顺序,如果有registerPayClazz,框架应保证register在get之前 // 目前因为没有registerPayClazz,所以没测,可能不对。by Xc 20240608 let MODULE_USED = false; function registerPayClazz(entity, def, schema) { (0, assert_1.default)(!MODULE_USED); PayClazzEntityDict[entity] = { accountEntity: def.accountEntity, clazzConstructor: def.clazzConstructor, }; // 检查此entity是否符合注册要求 // entity应该是AbstractPayProduct的子类,accountEntity应该是AbstractPayAccount的子类 const { attributes: payAttrs } = schema.pay; const { attributes: accountAttrs } = schema[def.accountEntity]; const { attributes: productAttrs } = schema[entity]; (0, assert_1.default)(payAttrs.entity.ref?.includes(entity)); (0, assert_1.default)(accountAttrs.price && (accountAttrs.price.type === 'decimal' || accountAttrs.price.type === 'money')); (0, assert_1.default)(accountAttrs.systemId && accountAttrs.systemId.type === 'ref' && accountAttrs.systemId.ref === 'system'); (0, assert_1.default)(accountAttrs.allowWithdrawTransfer.type === 'boolean'); (0, assert_1.default)(accountAttrs.withdrawTransferLossRatio.type === 'decimal'); (0, assert_1.default)(productAttrs.applicationId.type === 'ref' && productAttrs.applicationId.ref === 'application'); (0, assert_1.default)(productAttrs.enabled.type === 'boolean'); const attrDict = { taxLossRatio: 'decimal', refundGapDays: 'int', refundCompensateRatio: 'int', needReceiving: 'boolean', }; for (const attr in attrDict) { (0, assert_1.default)(accountAttrs[attr].type === attrDict[attr]); (0, assert_1.default)(productAttrs[attr].type === attrDict[attr]); } (0, abstractChecker_1.registerAccountEntity)(def.accountEntity); } exports.registerPayClazz = registerPayClazz; async function getPayClazz(entity, entityId, context) { if (!MODULE_USED) { MODULE_USED = true; } const key = entity === 'account' ? entity : `${entity}.${entityId}`; if (PayChannelDict.hasOwnProperty(key)) { return PayChannelDict[key]; } const PayClazz = await PayClazzEntityDict[entity].clazzConstructor(entityId, context); PayChannelDict[key] = PayClazz; return PayClazz; } exports.getPayClazz = getPayClazz;