@pisell/pisellos 2.1.4 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/index.js +2 -1
- package/dist/effects/index.d.ts +4 -3
- package/dist/effects/index.js +15 -6
- package/dist/modules/AccountList/index.d.ts +7 -1
- package/dist/modules/AccountList/index.js +81 -14
- package/dist/modules/AccountList/types.d.ts +28 -0
- package/dist/modules/AccountList/types.js +8 -0
- package/dist/modules/Cart/index.js +1 -1
- package/dist/modules/Customer/constants.d.ts +7 -0
- package/dist/modules/Customer/constants.js +12 -0
- package/dist/modules/Customer/index.d.ts +122 -0
- package/dist/modules/Customer/index.js +697 -0
- package/dist/modules/Customer/types.d.ts +146 -0
- package/dist/modules/Customer/types.js +41 -0
- package/dist/modules/Discount/index.d.ts +1 -0
- package/dist/modules/Order/index.d.ts +25 -1
- package/dist/modules/Order/index.js +224 -2
- package/dist/modules/Order/types.d.ts +45 -0
- package/dist/modules/Order/types.js +8 -0
- package/dist/modules/Payment/cash.d.ts +19 -0
- package/dist/modules/Payment/cash.js +97 -0
- package/dist/modules/Payment/cashRecommendationAlgorithm.d.ts +22 -0
- package/dist/modules/Payment/cashRecommendationAlgorithm.js +423 -0
- package/dist/modules/Payment/eftpos.d.ts +11 -0
- package/dist/modules/Payment/eftpos.js +80 -0
- package/dist/modules/Payment/index.d.ts +184 -9
- package/dist/modules/Payment/index.js +2012 -108
- package/dist/modules/Payment/mx51.d.ts +0 -0
- package/dist/modules/Payment/mx51.js +0 -0
- package/dist/modules/Payment/types.d.ts +750 -30
- package/dist/modules/Payment/types.js +236 -12
- package/dist/modules/Payment/utils.d.ts +17 -0
- package/dist/modules/Payment/utils.js +62 -0
- package/dist/modules/Payment/walletpass.d.ts +99 -0
- package/dist/modules/Payment/walletpass.js +701 -0
- package/dist/modules/Product/index.d.ts +1 -1
- package/dist/modules/ProductList/index.d.ts +12 -1
- package/dist/modules/ProductList/index.js +27 -3
- package/dist/modules/Rules/index.d.ts +2 -0
- package/dist/modules/Rules/index.js +39 -17
- package/dist/modules/Rules/types.d.ts +1 -0
- package/dist/modules/Schedule/index.d.ts +9 -0
- package/dist/modules/Schedule/index.js +68 -0
- package/dist/modules/index.d.ts +1 -0
- package/dist/modules/index.js +1 -0
- package/dist/plugins/app-types/app/app.d.ts +83 -0
- package/dist/plugins/app-types/app/const.d.ts +4 -0
- package/dist/plugins/app-types/app/index.d.ts +14 -0
- package/dist/plugins/app-types/applicationManager/application.d.ts +50 -0
- package/dist/plugins/app-types/applicationManager/index.d.ts +13 -0
- package/dist/plugins/app-types/config.d.ts +3 -0
- package/dist/plugins/app-types/cookie/index.d.ts +13 -0
- package/dist/plugins/app-types/data/index.d.ts +8 -0
- package/dist/plugins/app-types/history/config.d.ts +24 -0
- package/dist/plugins/app-types/history/index.d.ts +20 -0
- package/dist/plugins/app-types/history/type.d.ts +2 -0
- package/dist/plugins/app-types/hooks/index.d.ts +12 -0
- package/dist/plugins/app-types/hooks/useDelayedValue/index.d.ts +2 -0
- package/dist/plugins/app-types/hooks/useDispatch/index.d.ts +2 -0
- package/dist/plugins/app-types/hooks/useLowCode/index.d.ts +13 -0
- package/dist/plugins/app-types/hooks/useStore/index.d.ts +6 -0
- package/dist/plugins/app-types/index.d.ts +6 -0
- package/dist/plugins/app-types/indexDB/index.d.ts +178 -0
- package/dist/plugins/app-types/locales/en.d.ts +3 -0
- package/dist/plugins/app-types/locales/index.d.ts +37 -0
- package/dist/plugins/app-types/locales/original.d.ts +3 -0
- package/dist/plugins/app-types/locales/type.d.ts +19 -0
- package/dist/plugins/app-types/locales/zh-CN.d.ts +3 -0
- package/dist/plugins/app-types/locales/zh-HK.d.ts +3 -0
- package/dist/plugins/app-types/logger/feishu.d.ts +11 -0
- package/dist/plugins/app-types/logger/index.d.ts +122 -0
- package/dist/plugins/app-types/menuManager/hooks.d.ts +17 -0
- package/dist/plugins/app-types/menuManager/index.d.ts +28 -0
- package/dist/plugins/app-types/models/global.d.ts +32 -0
- package/dist/plugins/app-types/models/index.d.ts +45 -0
- package/dist/plugins/app-types/models/type.d.ts +2 -0
- package/dist/plugins/app-types/package.json +15 -0
- package/dist/plugins/app-types/plugin/index.d.ts +0 -0
- package/dist/plugins/app-types/pubsub/example.d.ts +5 -0
- package/dist/plugins/app-types/pubsub/index.d.ts +63 -0
- package/dist/plugins/app-types/request/cache.d.ts +46 -0
- package/dist/plugins/app-types/request/cancelToken.d.ts +38 -0
- package/dist/plugins/app-types/request/config.d.ts +3 -0
- package/dist/plugins/app-types/request/constants.d.ts +2 -0
- package/dist/plugins/app-types/request/index.d.ts +24 -0
- package/dist/plugins/app-types/request/pisell2Request.d.ts +6 -0
- package/dist/plugins/app-types/request/type.d.ts +41 -0
- package/dist/plugins/app-types/request/utils.d.ts +46 -0
- package/dist/plugins/app-types/routes/config.d.ts +7 -0
- package/dist/plugins/app-types/routes/index.d.ts +28 -0
- package/dist/plugins/app-types/socket/components/SocketMonitorPage.d.ts +6 -0
- package/dist/plugins/app-types/socket/components/index.d.ts +2 -0
- package/dist/plugins/app-types/socket/constants.d.ts +33 -0
- package/dist/plugins/app-types/socket/events.d.ts +31 -0
- package/dist/plugins/app-types/socket/heartbeat.d.ts +66 -0
- package/dist/plugins/app-types/socket/index.d.ts +61 -0
- package/dist/plugins/app-types/socket/monitor.d.ts +169 -0
- package/dist/plugins/app-types/socket/reconnect.d.ts +61 -0
- package/dist/plugins/app-types/socket/socket.d.ts +129 -0
- package/dist/plugins/app-types/socket/types.d.ts +85 -0
- package/dist/plugins/app-types/storage/index.d.ts +17 -0
- package/dist/plugins/app-types/tasks/index.d.ts +77 -0
- package/dist/plugins/app-types/tasks/type.d.ts +62 -0
- package/dist/plugins/app-types/tasks/useTasks.d.ts +5 -0
- package/dist/plugins/app-types/type.d.ts +2 -0
- package/dist/plugins/app-types/variables/VariablesProvider.d.ts +7 -0
- package/dist/plugins/app-types/variables/config.d.ts +3 -0
- package/dist/plugins/app-types/variables/index.d.ts +6 -0
- package/dist/plugins/app-types/variables/type.d.ts +2 -0
- package/dist/plugins/app-types/website/index.d.ts +6 -0
- package/dist/plugins/app.d.ts +8 -0
- package/dist/plugins/app.js +1 -0
- package/dist/plugins/window.d.ts +1 -0
- package/dist/solution/BookingByStep/index.d.ts +1 -1
- package/dist/solution/BookingByStep/index.js +1 -1
- package/dist/solution/BookingTicket/index.d.ts +180 -0
- package/dist/solution/BookingTicket/index.js +689 -0
- package/dist/solution/BookingTicket/types.d.ts +68 -0
- package/dist/solution/BookingTicket/types.js +43 -0
- package/dist/solution/BookingTicket/utils/scan/cloudSearch.d.ts +22 -0
- package/dist/solution/BookingTicket/utils/scan/cloudSearch.js +159 -0
- package/dist/solution/BookingTicket/utils/scan/handleScan.d.ts +22 -0
- package/dist/solution/BookingTicket/utils/scan/handleScan.js +189 -0
- package/dist/solution/BookingTicket/utils/scan/index.d.ts +81 -0
- package/dist/solution/BookingTicket/utils/scan/index.js +285 -0
- package/dist/solution/BookingTicket/utils/scan/scanCache.d.ts +78 -0
- package/dist/solution/BookingTicket/utils/scan/scanCache.js +305 -0
- package/dist/solution/Checkout/appointmentDemo.json +1 -0
- package/dist/solution/Checkout/index.d.ts +509 -0
- package/dist/solution/Checkout/index.js +4874 -0
- package/dist/solution/Checkout/types.d.ts +923 -0
- package/dist/solution/Checkout/types.js +148 -0
- package/dist/solution/Checkout/utils/index.d.ts +117 -0
- package/dist/solution/Checkout/utils/index.js +549 -0
- package/dist/solution/ShopDiscount/index.d.ts +4 -9
- package/dist/solution/ShopDiscount/index.js +106 -54
- package/dist/solution/ShopDiscount/types.d.ts +10 -1
- package/dist/solution/index.d.ts +2 -0
- package/dist/solution/index.js +3 -1
- package/dist/types/index.d.ts +3 -1
- package/dist/utils/task.d.ts +40 -0
- package/dist/utils/task.js +171 -0
- package/dist/utils/watch.d.ts +102 -0
- package/dist/utils/watch.js +294 -0
- package/lib/core/index.js +1 -1
- package/lib/effects/index.d.ts +4 -3
- package/lib/effects/index.js +4 -1
- package/lib/modules/AccountList/index.d.ts +7 -1
- package/lib/modules/AccountList/index.js +27 -0
- package/lib/modules/AccountList/types.d.ts +28 -0
- package/lib/modules/Cart/index.js +1 -1
- package/lib/modules/Customer/constants.d.ts +7 -0
- package/lib/modules/Customer/constants.js +39 -0
- package/lib/modules/Customer/index.d.ts +122 -0
- package/lib/modules/Customer/index.js +440 -0
- package/lib/modules/Customer/types.d.ts +146 -0
- package/lib/modules/Customer/types.js +37 -0
- package/lib/modules/Discount/index.d.ts +1 -0
- package/lib/modules/Order/index.d.ts +25 -1
- package/lib/modules/Order/index.js +182 -0
- package/lib/modules/Order/types.d.ts +45 -0
- package/lib/modules/Payment/cash.d.ts +19 -0
- package/lib/modules/Payment/cash.js +64 -0
- package/lib/modules/Payment/cashRecommendationAlgorithm.d.ts +22 -0
- package/lib/modules/Payment/cashRecommendationAlgorithm.js +342 -0
- package/lib/modules/Payment/eftpos.d.ts +11 -0
- package/lib/modules/Payment/eftpos.js +51 -0
- package/lib/modules/Payment/index.d.ts +184 -9
- package/lib/modules/Payment/index.js +1044 -50
- package/lib/modules/Payment/mx51.d.ts +0 -0
- package/lib/modules/Payment/mx51.js +0 -0
- package/lib/modules/Payment/types.d.ts +750 -30
- package/lib/modules/Payment/types.js +79 -6
- package/lib/modules/Payment/utils.d.ts +17 -0
- package/lib/modules/Payment/utils.js +67 -0
- package/lib/modules/Payment/walletpass.d.ts +99 -0
- package/lib/modules/Payment/walletpass.js +468 -0
- package/lib/modules/Product/index.d.ts +1 -1
- package/lib/modules/ProductList/index.d.ts +12 -1
- package/lib/modules/ProductList/index.js +29 -2
- package/lib/modules/Rules/index.d.ts +2 -0
- package/lib/modules/Rules/index.js +32 -15
- package/lib/modules/Rules/types.d.ts +1 -0
- package/lib/modules/Schedule/index.d.ts +9 -0
- package/lib/modules/Schedule/index.js +51 -0
- package/lib/modules/index.d.ts +1 -0
- package/lib/modules/index.js +2 -0
- package/lib/plugins/app-types/app/app.d.ts +83 -0
- package/lib/plugins/app-types/app/const.d.ts +4 -0
- package/lib/plugins/app-types/app/index.d.ts +14 -0
- package/lib/plugins/app-types/applicationManager/application.d.ts +50 -0
- package/lib/plugins/app-types/applicationManager/index.d.ts +13 -0
- package/lib/plugins/app-types/config.d.ts +3 -0
- package/lib/plugins/app-types/cookie/index.d.ts +13 -0
- package/lib/plugins/app-types/data/index.d.ts +8 -0
- package/lib/plugins/app-types/history/config.d.ts +24 -0
- package/lib/plugins/app-types/history/index.d.ts +20 -0
- package/lib/plugins/app-types/history/type.d.ts +2 -0
- package/lib/plugins/app-types/hooks/index.d.ts +12 -0
- package/lib/plugins/app-types/hooks/useDelayedValue/index.d.ts +2 -0
- package/lib/plugins/app-types/hooks/useDispatch/index.d.ts +2 -0
- package/lib/plugins/app-types/hooks/useLowCode/index.d.ts +13 -0
- package/lib/plugins/app-types/hooks/useStore/index.d.ts +6 -0
- package/lib/plugins/app-types/index.d.ts +6 -0
- package/lib/plugins/app-types/indexDB/index.d.ts +178 -0
- package/lib/plugins/app-types/locales/en.d.ts +3 -0
- package/lib/plugins/app-types/locales/index.d.ts +37 -0
- package/lib/plugins/app-types/locales/original.d.ts +3 -0
- package/lib/plugins/app-types/locales/type.d.ts +19 -0
- package/lib/plugins/app-types/locales/zh-CN.d.ts +3 -0
- package/lib/plugins/app-types/locales/zh-HK.d.ts +3 -0
- package/lib/plugins/app-types/logger/feishu.d.ts +11 -0
- package/lib/plugins/app-types/logger/index.d.ts +122 -0
- package/lib/plugins/app-types/menuManager/hooks.d.ts +17 -0
- package/lib/plugins/app-types/menuManager/index.d.ts +28 -0
- package/lib/plugins/app-types/models/global.d.ts +32 -0
- package/lib/plugins/app-types/models/index.d.ts +45 -0
- package/lib/plugins/app-types/models/type.d.ts +2 -0
- package/lib/plugins/app-types/package.json +15 -0
- package/lib/plugins/app-types/plugin/index.d.ts +0 -0
- package/lib/plugins/app-types/pubsub/example.d.ts +5 -0
- package/lib/plugins/app-types/pubsub/index.d.ts +63 -0
- package/lib/plugins/app-types/request/cache.d.ts +46 -0
- package/lib/plugins/app-types/request/cancelToken.d.ts +38 -0
- package/lib/plugins/app-types/request/config.d.ts +3 -0
- package/lib/plugins/app-types/request/constants.d.ts +2 -0
- package/lib/plugins/app-types/request/index.d.ts +24 -0
- package/lib/plugins/app-types/request/pisell2Request.d.ts +6 -0
- package/lib/plugins/app-types/request/type.d.ts +41 -0
- package/lib/plugins/app-types/request/utils.d.ts +46 -0
- package/lib/plugins/app-types/routes/config.d.ts +7 -0
- package/lib/plugins/app-types/routes/index.d.ts +28 -0
- package/lib/plugins/app-types/socket/components/SocketMonitorPage.d.ts +6 -0
- package/lib/plugins/app-types/socket/components/index.d.ts +2 -0
- package/lib/plugins/app-types/socket/constants.d.ts +33 -0
- package/lib/plugins/app-types/socket/events.d.ts +31 -0
- package/lib/plugins/app-types/socket/heartbeat.d.ts +66 -0
- package/lib/plugins/app-types/socket/index.d.ts +61 -0
- package/lib/plugins/app-types/socket/monitor.d.ts +169 -0
- package/lib/plugins/app-types/socket/reconnect.d.ts +61 -0
- package/lib/plugins/app-types/socket/socket.d.ts +129 -0
- package/lib/plugins/app-types/socket/types.d.ts +85 -0
- package/lib/plugins/app-types/storage/index.d.ts +17 -0
- package/lib/plugins/app-types/tasks/index.d.ts +77 -0
- package/lib/plugins/app-types/tasks/type.d.ts +62 -0
- package/lib/plugins/app-types/tasks/useTasks.d.ts +5 -0
- package/lib/plugins/app-types/type.d.ts +2 -0
- package/lib/plugins/app-types/variables/VariablesProvider.d.ts +7 -0
- package/lib/plugins/app-types/variables/config.d.ts +3 -0
- package/lib/plugins/app-types/variables/index.d.ts +6 -0
- package/lib/plugins/app-types/variables/type.d.ts +2 -0
- package/lib/plugins/app-types/website/index.d.ts +6 -0
- package/lib/plugins/app.d.ts +8 -0
- package/lib/plugins/app.js +17 -0
- package/lib/plugins/window.d.ts +1 -0
- package/lib/solution/BookingByStep/index.d.ts +1 -1
- package/lib/solution/BookingByStep/index.js +1 -2
- package/lib/solution/BookingTicket/index.d.ts +180 -0
- package/lib/solution/BookingTicket/index.js +420 -0
- package/lib/solution/BookingTicket/types.d.ts +68 -0
- package/lib/solution/BookingTicket/types.js +72 -0
- package/lib/solution/BookingTicket/utils/scan/cloudSearch.d.ts +22 -0
- package/lib/solution/BookingTicket/utils/scan/cloudSearch.js +117 -0
- package/lib/solution/BookingTicket/utils/scan/handleScan.d.ts +22 -0
- package/lib/solution/BookingTicket/utils/scan/handleScan.js +132 -0
- package/lib/solution/BookingTicket/utils/scan/index.d.ts +81 -0
- package/lib/solution/BookingTicket/utils/scan/index.js +210 -0
- package/lib/solution/BookingTicket/utils/scan/scanCache.d.ts +78 -0
- package/lib/solution/BookingTicket/utils/scan/scanCache.js +231 -0
- package/lib/solution/Checkout/appointmentDemo.json +1 -0
- package/lib/solution/Checkout/index.d.ts +509 -0
- package/lib/solution/Checkout/index.js +3002 -0
- package/lib/solution/Checkout/types.d.ts +923 -0
- package/lib/solution/Checkout/types.js +87 -0
- package/lib/solution/Checkout/utils/index.d.ts +117 -0
- package/lib/solution/Checkout/utils/index.js +403 -0
- package/lib/solution/ShopDiscount/index.d.ts +4 -9
- package/lib/solution/ShopDiscount/index.js +41 -13
- package/lib/solution/ShopDiscount/types.d.ts +10 -1
- package/lib/solution/index.d.ts +2 -0
- package/lib/solution/index.js +5 -1
- package/lib/types/index.d.ts +3 -1
- package/lib/utils/task.d.ts +40 -0
- package/lib/utils/task.js +109 -0
- package/lib/utils/watch.d.ts +102 -0
- package/lib/utils/watch.js +217 -0
- package/package.json +4 -2
|
@@ -14,74 +14,1068 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
14
14
|
}
|
|
15
15
|
return to;
|
|
16
16
|
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
17
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
19
|
|
|
19
20
|
// src/modules/Payment/index.ts
|
|
20
21
|
var Payment_exports = {};
|
|
21
22
|
__export(Payment_exports, {
|
|
22
|
-
PaymentModule: () => PaymentModule
|
|
23
|
+
PaymentModule: () => PaymentModule,
|
|
24
|
+
generateRequestUniqueId: () => generateRequestUniqueId
|
|
23
25
|
});
|
|
24
26
|
module.exports = __toCommonJS(Payment_exports);
|
|
25
27
|
var import_BaseModule = require("../BaseModule");
|
|
26
28
|
var import_types = require("./types");
|
|
29
|
+
var import_utils = require("../Cart/utils");
|
|
30
|
+
var import_cash = require("./cash");
|
|
31
|
+
var import_eftpos = require("./eftpos");
|
|
32
|
+
var import_walletpass = require("./walletpass");
|
|
33
|
+
var import_decimal = require("decimal.js");
|
|
34
|
+
__reExport(Payment_exports, require("./types"), module.exports);
|
|
35
|
+
function formatAmount(amount) {
|
|
36
|
+
try {
|
|
37
|
+
const decimal = new import_decimal.Decimal(amount);
|
|
38
|
+
return decimal.toFixed(2);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.warn(`[PaymentModule] 金额格式化失败: ${amount},使用默认值 0.00`);
|
|
41
|
+
return "0.00";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function generateRequestUniqueId() {
|
|
45
|
+
const now = /* @__PURE__ */ new Date();
|
|
46
|
+
const year = now.getFullYear();
|
|
47
|
+
const month = String(now.getMonth() + 1).padStart(2, "0");
|
|
48
|
+
const day = String(now.getDate()).padStart(2, "0");
|
|
49
|
+
const hour = String(now.getHours()).padStart(2, "0");
|
|
50
|
+
const minute = String(now.getMinutes()).padStart(2, "0");
|
|
51
|
+
const second = String(now.getSeconds()).padStart(2, "0");
|
|
52
|
+
const millisecond = String(now.getMilliseconds()).padStart(3, "0");
|
|
53
|
+
const randomDigits = Math.floor(Math.random() * 9e3) + 1e3;
|
|
54
|
+
return `${year}${month}${day}${hour}${minute}${second}${millisecond}${randomDigits}`;
|
|
55
|
+
}
|
|
27
56
|
var PaymentModule = class extends import_BaseModule.BaseModule {
|
|
28
57
|
constructor(name, version) {
|
|
29
58
|
super(name, version);
|
|
30
|
-
this.defaultName = "
|
|
59
|
+
this.defaultName = "pay";
|
|
31
60
|
this.defaultVersion = "1.0.0";
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
61
|
+
// LoggerManager 实例
|
|
62
|
+
this.otherParams = {};
|
|
63
|
+
this.cash = new import_cash.CashPaymentImpl(this);
|
|
64
|
+
this.eftpos = new import_eftpos.EftposPaymentImpl(this);
|
|
65
|
+
this.wallet = new import_walletpass.WalletPassPaymentImpl(this);
|
|
36
66
|
}
|
|
37
67
|
async initialize(core, options) {
|
|
38
68
|
this.core = core;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
69
|
+
this.store = options.store;
|
|
70
|
+
this.otherParams = options.otherParams || {};
|
|
71
|
+
this.request = core.getPlugin("request");
|
|
72
|
+
const appPlugin = core.getPlugin("app");
|
|
73
|
+
if (!this.request) {
|
|
74
|
+
throw new Error("支付模块需要 request 插件支持");
|
|
75
|
+
}
|
|
76
|
+
if (!appPlugin) {
|
|
77
|
+
throw new Error("支付模块需要 app 插件支持");
|
|
78
|
+
}
|
|
79
|
+
this.app = appPlugin.getApp();
|
|
80
|
+
this.dbManager = this.app.dbManager;
|
|
81
|
+
this.logger = this.app.logger;
|
|
82
|
+
await this.ensurePaymentTables();
|
|
83
|
+
this.registerNetworkHandlers();
|
|
84
|
+
console.log("[PaymentModule] 初始化完成");
|
|
85
|
+
this.logInfo("PaymentModule initialized successfully");
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 记录信息日志
|
|
89
|
+
*/
|
|
90
|
+
logInfo(title, metadata) {
|
|
91
|
+
var _a, _b;
|
|
92
|
+
if ((_a = this.otherParams) == null ? void 0 : _a.fatherModule) {
|
|
93
|
+
const fatherModule = this.core.getModule((_b = this.otherParams) == null ? void 0 : _b.fatherModule);
|
|
94
|
+
if (fatherModule) {
|
|
95
|
+
fatherModule.logInfo(`${title}`, metadata);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (this.logger) {
|
|
100
|
+
this.logger.addLog({
|
|
101
|
+
type: "info",
|
|
102
|
+
title: `[PaymentModule] ${title}`,
|
|
103
|
+
metadata: metadata || {}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 记录警告日志
|
|
109
|
+
*/
|
|
110
|
+
logWarning(title, metadata) {
|
|
111
|
+
if (this.logger) {
|
|
112
|
+
this.logger.addLog({
|
|
113
|
+
type: "warning",
|
|
114
|
+
title: `[PaymentModule] ${title}`,
|
|
115
|
+
metadata: metadata || {}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 记录错误日志
|
|
121
|
+
*/
|
|
122
|
+
logError(title, error, metadata) {
|
|
123
|
+
if (this.logger) {
|
|
124
|
+
this.logger.addLog({
|
|
125
|
+
type: "error",
|
|
126
|
+
title: `[PaymentModule] ${title}`,
|
|
127
|
+
metadata: {
|
|
128
|
+
error: error ? {
|
|
129
|
+
message: error.message,
|
|
130
|
+
stack: error.stack,
|
|
131
|
+
code: error.code,
|
|
132
|
+
...error
|
|
133
|
+
} : void 0,
|
|
134
|
+
...metadata || {}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 记录调试日志
|
|
141
|
+
*/
|
|
142
|
+
logDebug(title, metadata) {
|
|
143
|
+
if (this.logger) {
|
|
144
|
+
this.logger.addLog({
|
|
145
|
+
type: "debug",
|
|
146
|
+
title: `[PaymentModule] ${title}`,
|
|
147
|
+
metadata: metadata || {}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 网络恢复以后,尝试执行队列
|
|
153
|
+
*
|
|
154
|
+
*/
|
|
155
|
+
registerNetworkHandlers() {
|
|
156
|
+
const network = this.app.plugins.get("network");
|
|
157
|
+
this.logInfo("Registering network status listener");
|
|
158
|
+
network == null ? void 0 : network.addListener("networkStatusChange", async (status) => {
|
|
159
|
+
console.log("网络状态:", status.connected);
|
|
160
|
+
this.logInfo("Network status changed", {
|
|
161
|
+
connected: status.connected,
|
|
162
|
+
previousStatus: status.previousStatus
|
|
163
|
+
});
|
|
164
|
+
if (status.connected) {
|
|
165
|
+
this.logInfo("Network reconnected");
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* 获取支付方式列表
|
|
171
|
+
*/
|
|
172
|
+
async getPayMethodListAsync() {
|
|
173
|
+
this.logInfo("Starting getPayMethodListAsync");
|
|
174
|
+
try {
|
|
175
|
+
let cachedMethods = [];
|
|
176
|
+
try {
|
|
177
|
+
cachedMethods = await this.dbManager.getAll("pay_method");
|
|
178
|
+
} catch (dbError) {
|
|
179
|
+
console.warn("[PaymentModule] pay_method 表不存在,将从服务器获取数据");
|
|
180
|
+
}
|
|
181
|
+
const hasCache = cachedMethods.length > 0;
|
|
182
|
+
if (hasCache) {
|
|
183
|
+
this.refreshPaymentMethodsInBackground(cachedMethods);
|
|
184
|
+
return cachedMethods;
|
|
185
|
+
}
|
|
186
|
+
const response = await this.request.get("/pay/custom-payment/available", { channel: "pos" });
|
|
187
|
+
const payMethods = response.data || [];
|
|
188
|
+
try {
|
|
189
|
+
for (const method of payMethods) {
|
|
190
|
+
await this.dbManager.update("pay_method", method);
|
|
191
|
+
}
|
|
192
|
+
} catch (dbError) {
|
|
193
|
+
console.warn("[PaymentModule] 无法缓存支付方式,pay_method 表不存在");
|
|
194
|
+
}
|
|
195
|
+
await this.core.effects.emit(
|
|
196
|
+
import_types.PaymentHooks.OnPaymentMethodsLoaded,
|
|
197
|
+
payMethods
|
|
198
|
+
);
|
|
199
|
+
this.logInfo("getPayMethodListAsync completed successfully", {
|
|
200
|
+
methodCount: payMethods.length,
|
|
201
|
+
hasCache: cachedMethods.length > 0
|
|
202
|
+
});
|
|
203
|
+
return payMethods;
|
|
204
|
+
} catch (error) {
|
|
205
|
+
console.error("[PaymentModule] 获取支付方式列表失败", error);
|
|
206
|
+
this.logError("getPayMethodListAsync failed", error);
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* 后台刷新支付方式列表
|
|
212
|
+
*/
|
|
213
|
+
async refreshPaymentMethodsInBackground(cachedMethods) {
|
|
214
|
+
this.logInfo("Starting refreshPaymentMethodsInBackground", {
|
|
215
|
+
cachedMethodsCount: cachedMethods.length
|
|
216
|
+
});
|
|
217
|
+
try {
|
|
218
|
+
console.log("[PaymentModule] 后台刷新支付方式列表...");
|
|
219
|
+
const response = await this.request.get("/pay/custom-payment/available", { channel: "pos" });
|
|
220
|
+
const newPayMethods = response.data || [];
|
|
221
|
+
const hasChanges = this.hasPaymentMethodsChanged(
|
|
222
|
+
cachedMethods,
|
|
223
|
+
newPayMethods
|
|
224
|
+
);
|
|
225
|
+
if (hasChanges) {
|
|
226
|
+
console.log("[PaymentModule] 支付方式列表已更新");
|
|
227
|
+
this.logInfo("Payment methods updated in background", {
|
|
228
|
+
oldCount: cachedMethods.length,
|
|
229
|
+
newCount: newPayMethods.length
|
|
230
|
+
});
|
|
231
|
+
try {
|
|
232
|
+
for (const method of cachedMethods) {
|
|
233
|
+
await this.dbManager.delete("pay_method", method.id);
|
|
234
|
+
}
|
|
235
|
+
for (const method of newPayMethods) {
|
|
236
|
+
await this.dbManager.update("pay_method", method);
|
|
237
|
+
}
|
|
238
|
+
} catch (dbError) {
|
|
239
|
+
console.warn("[PaymentModule] 无法更新支付方式缓存", dbError);
|
|
240
|
+
}
|
|
241
|
+
const eventData = {
|
|
242
|
+
oldMethods: cachedMethods,
|
|
243
|
+
newMethods: newPayMethods
|
|
244
|
+
};
|
|
245
|
+
await this.core.effects.emit(
|
|
246
|
+
import_types.PaymentHooks.OnPaymentMethodsChanged,
|
|
247
|
+
eventData
|
|
248
|
+
);
|
|
249
|
+
} else {
|
|
250
|
+
console.log("[PaymentModule] 支付方式列表无变化");
|
|
251
|
+
}
|
|
252
|
+
} catch (error) {
|
|
253
|
+
console.error("[PaymentModule] 后台刷新支付方式失败", error);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 检查支付方式列表是否有变化
|
|
258
|
+
*/
|
|
259
|
+
hasPaymentMethodsChanged(oldMethods, newMethods) {
|
|
260
|
+
if (oldMethods.length !== newMethods.length) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
const oldMethodsMap = new Map(
|
|
264
|
+
oldMethods.map((method) => [method.id, method])
|
|
265
|
+
);
|
|
266
|
+
for (const newMethod of newMethods) {
|
|
267
|
+
const oldMethod = oldMethodsMap.get(newMethod.id);
|
|
268
|
+
if (!oldMethod) {
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
if (oldMethod.code !== newMethod.code || oldMethod.name !== newMethod.name || oldMethod.type !== newMethod.type || oldMethod.description !== newMethod.description || oldMethod.status !== newMethod.status || oldMethod.disable !== newMethod.disable || oldMethod.is_surcharge !== newMethod.is_surcharge || oldMethod.fixed !== newMethod.fixed || oldMethod.percentage !== newMethod.percentage || oldMethod.enabled !== newMethod.enabled || JSON.stringify(oldMethod.channel_application || []) !== JSON.stringify(newMethod.channel_application || []) || JSON.stringify(oldMethod.companies || []) !== JSON.stringify(newMethod.companies || []) || JSON.stringify(oldMethod.metadata || {}) !== JSON.stringify(newMethod.metadata || {})) {
|
|
272
|
+
console.log(`[PaymentModule] 支付方式 ${newMethod.id} (${newMethod.code}) 发生变化:`, {
|
|
273
|
+
id: newMethod.id,
|
|
274
|
+
changes: {
|
|
275
|
+
code: oldMethod.code !== newMethod.code ? { old: oldMethod.code, new: newMethod.code } : void 0,
|
|
276
|
+
name: oldMethod.name !== newMethod.name ? { old: oldMethod.name, new: newMethod.name } : void 0,
|
|
277
|
+
type: oldMethod.type !== newMethod.type ? { old: oldMethod.type, new: newMethod.type } : void 0,
|
|
278
|
+
description: oldMethod.description !== newMethod.description ? { old: oldMethod.description, new: newMethod.description } : void 0,
|
|
279
|
+
status: oldMethod.status !== newMethod.status ? { old: oldMethod.status, new: newMethod.status } : void 0,
|
|
280
|
+
disable: oldMethod.disable !== newMethod.disable ? { old: oldMethod.disable, new: newMethod.disable } : void 0,
|
|
281
|
+
is_surcharge: oldMethod.is_surcharge !== newMethod.is_surcharge ? { old: oldMethod.is_surcharge, new: newMethod.is_surcharge } : void 0,
|
|
282
|
+
fixed: oldMethod.fixed !== newMethod.fixed ? { old: oldMethod.fixed, new: newMethod.fixed } : void 0,
|
|
283
|
+
percentage: oldMethod.percentage !== newMethod.percentage ? { old: oldMethod.percentage, new: newMethod.percentage } : void 0,
|
|
284
|
+
enabled: oldMethod.enabled !== newMethod.enabled ? { old: oldMethod.enabled, new: newMethod.enabled } : void 0,
|
|
285
|
+
channel_application: JSON.stringify(oldMethod.channel_application || []) !== JSON.stringify(newMethod.channel_application || []) ? { old: oldMethod.channel_application, new: newMethod.channel_application } : void 0,
|
|
286
|
+
companies: JSON.stringify(oldMethod.companies || []) !== JSON.stringify(newMethod.companies || []) ? { old: oldMethod.companies, new: newMethod.companies } : void 0,
|
|
287
|
+
metadata: JSON.stringify(oldMethod.metadata || {}) !== JSON.stringify(newMethod.metadata || {}) ? { old: oldMethod.metadata, new: newMethod.metadata } : void 0
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 获取订单列表
|
|
297
|
+
*/
|
|
298
|
+
async getOrderListAsync() {
|
|
299
|
+
try {
|
|
300
|
+
return await this.dbManager.getAll("order");
|
|
301
|
+
} catch (error) {
|
|
302
|
+
console.error("[PaymentModule] 获取订单列表失败", error);
|
|
303
|
+
return [];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* 根据订单UUID获取支付订单(新方法)
|
|
308
|
+
*/
|
|
309
|
+
async getPaymentOrderByUuidAsync(orderUuid) {
|
|
310
|
+
try {
|
|
311
|
+
return await this.dbManager.get("order", orderUuid);
|
|
312
|
+
} catch (error) {
|
|
313
|
+
console.error("[PaymentModule] 获取支付订单失败", error);
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* 创建支付订单(新方法,专注支付数据)
|
|
319
|
+
*/
|
|
320
|
+
async createPaymentOrderAsync(params) {
|
|
321
|
+
this.logInfo("Starting createPaymentOrderAsync", {
|
|
322
|
+
orderId: params.order_id,
|
|
323
|
+
totalAmount: params.total_amount
|
|
324
|
+
});
|
|
325
|
+
try {
|
|
326
|
+
const existingOrders = await this.dbManager.getAll("order");
|
|
327
|
+
const existingOrder = existingOrders.find(
|
|
328
|
+
(order) => String(order.id) === String(params.order_id)
|
|
329
|
+
);
|
|
330
|
+
if (existingOrder) {
|
|
331
|
+
console.log(
|
|
332
|
+
`[PaymentModule] 发现重复订单 ID: ${params.order_id},更新现有支付订单`
|
|
333
|
+
);
|
|
334
|
+
const originalOrder = { ...existingOrder };
|
|
335
|
+
existingOrder.order_info = params.order_info;
|
|
336
|
+
existingOrder.total_amount = params.total_amount;
|
|
337
|
+
existingOrder.is_deposit = params.is_deposit || 0;
|
|
338
|
+
existingOrder.deposit_amount = params.deposit_amount || "0.00";
|
|
339
|
+
this.recalculateOrderAmount(existingOrder);
|
|
340
|
+
await this.dbManager.update("order", existingOrder);
|
|
341
|
+
await this.core.effects.emit(
|
|
342
|
+
import_types.PaymentHooks.OnOrderUpdated,
|
|
343
|
+
existingOrder
|
|
344
|
+
);
|
|
345
|
+
return existingOrder;
|
|
346
|
+
} else {
|
|
347
|
+
const newOrder = {
|
|
348
|
+
uuid: (0, import_utils.getUniqueId)("pay_order_"),
|
|
349
|
+
id: params.order_id,
|
|
350
|
+
order_id: params.order_id,
|
|
351
|
+
order_info: params.order_info,
|
|
352
|
+
payment_status: import_types.PaymentStatus.Processing,
|
|
353
|
+
payment: [],
|
|
354
|
+
adjust_offline_payments: [],
|
|
355
|
+
total_amount: params.total_amount,
|
|
356
|
+
expect_amount: params.total_amount,
|
|
357
|
+
tax_fee: "0.00",
|
|
358
|
+
is_deposit: params.is_deposit || 0,
|
|
359
|
+
deposit_amount: params.deposit_amount || "0.00"
|
|
360
|
+
};
|
|
361
|
+
await this.dbManager.add("order", newOrder);
|
|
362
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderAdded, newOrder);
|
|
363
|
+
this.logInfo("createPaymentOrderAsync completed - new payment order created", {
|
|
364
|
+
orderUuid: newOrder.uuid,
|
|
365
|
+
orderId: newOrder.id
|
|
366
|
+
});
|
|
367
|
+
return newOrder;
|
|
368
|
+
}
|
|
369
|
+
} catch (error) {
|
|
370
|
+
console.error("[PaymentModule] 创建支付订单失败", error);
|
|
371
|
+
this.logError("createPaymentOrderAsync failed", error, {
|
|
372
|
+
orderId: params.order_id
|
|
373
|
+
});
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* 删除支付订单(新方法)
|
|
379
|
+
*/
|
|
380
|
+
async deletePaymentOrderAsync(orderUuid) {
|
|
381
|
+
try {
|
|
382
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
383
|
+
if (order) {
|
|
384
|
+
await this.dbManager.delete("order", orderUuid);
|
|
385
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderDeleted, order);
|
|
386
|
+
console.log("[PaymentModule] 支付订单删除成功:", orderUuid);
|
|
387
|
+
}
|
|
388
|
+
} catch (error) {
|
|
389
|
+
console.error("[PaymentModule] 删除支付订单失败", error);
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* 更新订单
|
|
395
|
+
*/
|
|
396
|
+
async updateOrderAsync(orderUuid, params) {
|
|
397
|
+
try {
|
|
398
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
399
|
+
if (order) {
|
|
400
|
+
const updatedOrder = { ...order, ...params };
|
|
401
|
+
await this.dbManager.update("order", updatedOrder);
|
|
402
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderUpdated, updatedOrder);
|
|
403
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
404
|
+
action: "update",
|
|
405
|
+
order: updatedOrder,
|
|
406
|
+
originalOrder: order
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
} catch (error) {
|
|
410
|
+
console.error("[PaymentModule] 更新订单失败", error);
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* 基于UUID替换订单ID
|
|
416
|
+
*
|
|
417
|
+
* 此方法用于将本地虚拟订单ID替换为真实的订单ID。
|
|
418
|
+
* 当前端模拟下单流程完成后,后端返回真实订单ID时调用此方法。
|
|
419
|
+
*
|
|
420
|
+
* @param orderUuid 订单的UUID
|
|
421
|
+
* @param newOrderId 新的订单ID (来自后端)
|
|
422
|
+
* @returns 更新后的订单对象,如果订单不存在则返回null
|
|
423
|
+
*/
|
|
424
|
+
async replaceOrderIdByUuidAsync(orderUuid, newOrderId) {
|
|
425
|
+
this.logInfo("Starting replaceOrderIdByUuidAsync", {
|
|
426
|
+
orderUuid,
|
|
427
|
+
newOrderId
|
|
428
|
+
});
|
|
429
|
+
try {
|
|
430
|
+
const existingOrder = await this.dbManager.get("order", orderUuid);
|
|
431
|
+
if (!existingOrder) {
|
|
432
|
+
this.logWarning("Order not found for UUID replacement", { orderUuid });
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
436
|
+
const duplicateOrder = allOrders.find(
|
|
437
|
+
(order) => String(order.order_id) === String(newOrderId) && order.uuid !== orderUuid
|
|
438
|
+
);
|
|
439
|
+
if (duplicateOrder) {
|
|
440
|
+
this.logWarning("New order ID already exists", {
|
|
441
|
+
newOrderId,
|
|
442
|
+
existingOrderUuid: duplicateOrder.uuid
|
|
443
|
+
});
|
|
444
|
+
throw new Error(`订单ID ${newOrderId} 已存在`);
|
|
445
|
+
}
|
|
446
|
+
const originalOrderId = existingOrder.id;
|
|
447
|
+
const updatedOrder = {
|
|
448
|
+
...existingOrder,
|
|
449
|
+
id: newOrderId,
|
|
450
|
+
order_id: newOrderId,
|
|
451
|
+
// 🔧 修复:同时更新根级的 order_id 字段
|
|
452
|
+
order_info: {
|
|
453
|
+
...existingOrder.order_info,
|
|
454
|
+
order_id: newOrderId,
|
|
455
|
+
// 保留原始本地订单ID作为参考
|
|
456
|
+
original_local_order_id: originalOrderId,
|
|
457
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
await this.dbManager.update("order", updatedOrder);
|
|
461
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderUpdated, updatedOrder);
|
|
462
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
463
|
+
action: "order_id_replaced",
|
|
464
|
+
order: updatedOrder,
|
|
465
|
+
originalOrder: existingOrder,
|
|
466
|
+
metadata: {
|
|
467
|
+
originalOrderId,
|
|
468
|
+
newOrderId
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
this.logInfo("Order ID replacement completed successfully", {
|
|
472
|
+
orderUuid,
|
|
473
|
+
originalOrderId,
|
|
474
|
+
newOrderId
|
|
475
|
+
});
|
|
476
|
+
console.log(`[PaymentModule] 订单ID替换成功: ${originalOrderId} → ${newOrderId}`);
|
|
477
|
+
return updatedOrder;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
console.error("[PaymentModule] 替换订单ID失败", error);
|
|
480
|
+
this.logError("replaceOrderIdByUuidAsync failed", error, {
|
|
481
|
+
orderUuid,
|
|
482
|
+
newOrderId
|
|
483
|
+
});
|
|
484
|
+
throw error;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* 获取支付项(新方法)
|
|
489
|
+
*
|
|
490
|
+
* @param orderUuid 订单UUID
|
|
491
|
+
* @param includeVoided 是否包含已撤销的支付项,默认为false
|
|
492
|
+
* @returns 支付项数组
|
|
493
|
+
*/
|
|
494
|
+
async getPaymentItemsAsync(orderUuid, includeVoided = false) {
|
|
495
|
+
if (!orderUuid) {
|
|
496
|
+
throw new Error("orderUuid is required");
|
|
497
|
+
}
|
|
498
|
+
const order = await this.getPaymentOrderByUuidAsync(orderUuid);
|
|
499
|
+
const allPayments = (order == null ? void 0 : order.payment) || [];
|
|
500
|
+
if (includeVoided) {
|
|
501
|
+
return allPayments;
|
|
502
|
+
}
|
|
503
|
+
return allPayments.filter((payment) => payment.status !== "voided");
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* 获取所有支付项(包括已撤销的)
|
|
507
|
+
*
|
|
508
|
+
* @param orderUuid 订单UUID
|
|
509
|
+
* @returns 所有支付项数组(包括撤销的)
|
|
510
|
+
*/
|
|
511
|
+
async getAllPaymentItemsAsync(orderUuid) {
|
|
512
|
+
return this.getPaymentItemsAsync(orderUuid, true);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* 为某个订单添加支付项(新方法)
|
|
516
|
+
*/
|
|
517
|
+
async addPaymentItemAsync(orderUuid, paymentItem) {
|
|
518
|
+
var _a, _b, _c;
|
|
519
|
+
this.logInfo("Starting addPaymentItemAsync", {
|
|
520
|
+
orderUuid,
|
|
521
|
+
paymentAmount: paymentItem.amount,
|
|
522
|
+
paymentCode: paymentItem.code,
|
|
523
|
+
orderPaymentType: paymentItem.order_payment_type
|
|
524
|
+
});
|
|
525
|
+
try {
|
|
526
|
+
const order = await this.getPaymentOrderByUuidAsync(orderUuid);
|
|
527
|
+
if (!order) {
|
|
528
|
+
throw new Error(`Order not found: ${orderUuid}`);
|
|
529
|
+
}
|
|
530
|
+
const paymentUuid = (0, import_utils.getUniqueId)("payment_");
|
|
531
|
+
const newPaymentItem = {
|
|
532
|
+
uuid: paymentUuid,
|
|
533
|
+
id: paymentItem.id || 0,
|
|
534
|
+
name: paymentItem.name,
|
|
535
|
+
code: paymentItem.code,
|
|
536
|
+
type: paymentItem.type,
|
|
537
|
+
amount: formatAmount(paymentItem.amount),
|
|
538
|
+
voucher_id: paymentItem.voucher_id || "",
|
|
539
|
+
rounding_amount: paymentItem.rounding_amount || "0.00",
|
|
540
|
+
service_charge: paymentItem.service_charge,
|
|
541
|
+
status: "active",
|
|
542
|
+
// 新支付项默认为活跃状态
|
|
543
|
+
order_payment_type: paymentItem.order_payment_type || "normal",
|
|
544
|
+
// 默认为正常支付
|
|
545
|
+
metadata: {
|
|
546
|
+
...paymentItem.metadata,
|
|
547
|
+
// 保留传入的所有 metadata 字段
|
|
548
|
+
unique_payment_number: paymentUuid
|
|
549
|
+
// 设置唯一支付号为支付项的 uuid
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
order.payment.push(newPaymentItem);
|
|
553
|
+
this.recalculateOrderAmount(order);
|
|
554
|
+
await this.dbManager.update("order", order);
|
|
555
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentAdded, {
|
|
556
|
+
orderUuid,
|
|
557
|
+
payment: newPaymentItem
|
|
558
|
+
});
|
|
559
|
+
this.logInfo("addPaymentItemAsync completed successfully", {
|
|
560
|
+
orderUuid,
|
|
561
|
+
paymentUuid: newPaymentItem.uuid,
|
|
562
|
+
uniquePaymentNumber: (_a = newPaymentItem.metadata) == null ? void 0 : _a.unique_payment_number,
|
|
563
|
+
newExpectAmount: order.expect_amount,
|
|
564
|
+
paymentAmount: newPaymentItem.amount,
|
|
565
|
+
paymentCode: newPaymentItem.code,
|
|
566
|
+
orderPaymentType: newPaymentItem.order_payment_type,
|
|
567
|
+
metadataFields: Object.keys(newPaymentItem.metadata || {}),
|
|
568
|
+
// 现金支付找零信息
|
|
569
|
+
actualPaidAmount: (_b = newPaymentItem.metadata) == null ? void 0 : _b.actual_paid_amount,
|
|
570
|
+
changeGivenAmount: (_c = newPaymentItem.metadata) == null ? void 0 : _c.change_given_amount
|
|
571
|
+
});
|
|
572
|
+
} catch (error) {
|
|
573
|
+
console.error("[PaymentModule] 添加支付项失败", error);
|
|
574
|
+
this.logError("addPaymentItemAsync failed", error, {
|
|
575
|
+
orderUuid,
|
|
576
|
+
paymentItem
|
|
577
|
+
});
|
|
578
|
+
throw error;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* 删除一个支付项 - 标记删除而非物理删除
|
|
583
|
+
*/
|
|
584
|
+
async deletePaymentAsync(orderUuid, paymentUuid) {
|
|
585
|
+
this.logInfo("Starting deletePaymentAsync (mark as voided)", { orderUuid, paymentUuid });
|
|
586
|
+
try {
|
|
587
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
588
|
+
if (!order) {
|
|
589
|
+
throw new Error("订单不存在");
|
|
590
|
+
}
|
|
591
|
+
const paymentItem = order.payment.find(
|
|
592
|
+
(payment) => payment.uuid === paymentUuid
|
|
593
|
+
);
|
|
594
|
+
if (!paymentItem) {
|
|
595
|
+
throw new Error(`支付项不存在: ${paymentUuid}`);
|
|
596
|
+
}
|
|
597
|
+
if (paymentItem.status === "voided") {
|
|
598
|
+
console.warn(`[PaymentModule] 支付项 ${paymentUuid} 已经被删除,跳过操作`);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
paymentItem.status = "voided";
|
|
602
|
+
paymentItem.origin_amount = paymentItem.amount;
|
|
603
|
+
paymentItem.amount = "0.00";
|
|
604
|
+
console.log(`[PaymentModule] 支付项标记为删除:`, {
|
|
605
|
+
uuid: paymentItem.uuid,
|
|
606
|
+
originalAmount: paymentItem.origin_amount,
|
|
607
|
+
status: paymentItem.status
|
|
608
|
+
});
|
|
609
|
+
this.recalculateOrderAmount(order);
|
|
610
|
+
await this.dbManager.update("order", order);
|
|
611
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentDeleted, {
|
|
612
|
+
order,
|
|
613
|
+
paymentItem
|
|
614
|
+
});
|
|
615
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
616
|
+
action: "payment_delete",
|
|
617
|
+
order,
|
|
618
|
+
paymentItem
|
|
619
|
+
});
|
|
620
|
+
this.logInfo("deletePaymentAsync completed successfully (marked as voided)", {
|
|
621
|
+
orderUuid,
|
|
622
|
+
paymentUuid,
|
|
623
|
+
originalAmount: paymentItem.origin_amount,
|
|
624
|
+
newExpectAmount: order.expect_amount
|
|
625
|
+
});
|
|
626
|
+
} catch (error) {
|
|
627
|
+
console.error("[PaymentModule] 删除支付项失败", error);
|
|
628
|
+
this.logError("deletePaymentAsync failed", error, {
|
|
629
|
+
orderUuid,
|
|
630
|
+
paymentUuid
|
|
631
|
+
});
|
|
632
|
+
throw error;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* 批量更新代金券类支付项(覆盖更新)
|
|
637
|
+
*
|
|
638
|
+
* 删除所有现有的带 voucher_id 的支付项,然后添加新的代金券支付项
|
|
639
|
+
* 这是一个覆盖式更新,确保代金券支付项的一致性
|
|
640
|
+
*/
|
|
641
|
+
async updateVoucherPaymentItemsAsync(orderUuid, voucherPaymentItems) {
|
|
642
|
+
this.logInfo("Starting updateVoucherPaymentItemsAsync", {
|
|
643
|
+
orderUuid,
|
|
644
|
+
newVoucherCount: voucherPaymentItems.length,
|
|
645
|
+
voucherItems: voucherPaymentItems.map((item) => ({
|
|
646
|
+
code: item.code,
|
|
647
|
+
amount: item.amount,
|
|
648
|
+
voucher_id: item.voucher_id,
|
|
649
|
+
order_payment_type: item.order_payment_type
|
|
650
|
+
}))
|
|
651
|
+
});
|
|
652
|
+
try {
|
|
653
|
+
const order = await this.getPaymentOrderByUuidAsync(orderUuid);
|
|
654
|
+
if (!order) {
|
|
655
|
+
throw new Error(`订单不存在: ${orderUuid}`);
|
|
656
|
+
}
|
|
657
|
+
const existingVoucherItems = order.payment.filter(
|
|
658
|
+
(payment) => payment.voucher_id && payment.status !== "voided"
|
|
659
|
+
);
|
|
660
|
+
console.log("[PaymentModule] 发现现有代金券支付项:", {
|
|
661
|
+
orderUuid,
|
|
662
|
+
existingVoucherCount: existingVoucherItems.length,
|
|
663
|
+
existingItems: existingVoucherItems.map((item) => ({
|
|
664
|
+
uuid: item.uuid,
|
|
665
|
+
code: item.code,
|
|
666
|
+
amount: item.amount,
|
|
667
|
+
voucher_id: item.voucher_id
|
|
668
|
+
}))
|
|
669
|
+
});
|
|
670
|
+
for (const voucherItem of existingVoucherItems) {
|
|
671
|
+
console.log(`[PaymentModule] 删除现有代金券支付项: ${voucherItem.uuid}`);
|
|
672
|
+
await this.deletePaymentAsync(orderUuid, voucherItem.uuid);
|
|
673
|
+
}
|
|
674
|
+
console.log("[PaymentModule] 添加新的代金券支付项:", {
|
|
675
|
+
orderUuid,
|
|
676
|
+
newItemCount: voucherPaymentItems.length
|
|
677
|
+
});
|
|
678
|
+
for (const voucherItem of voucherPaymentItems) {
|
|
679
|
+
if (!voucherItem.voucher_id) {
|
|
680
|
+
throw new Error(`代金券支付项缺少 voucher_id: ${JSON.stringify(voucherItem)}`);
|
|
681
|
+
}
|
|
682
|
+
console.log(`[PaymentModule] 添加代金券支付项:`, {
|
|
683
|
+
code: voucherItem.code,
|
|
684
|
+
amount: voucherItem.amount,
|
|
685
|
+
voucher_id: voucherItem.voucher_id,
|
|
686
|
+
order_payment_type: voucherItem.order_payment_type
|
|
687
|
+
});
|
|
688
|
+
await this.addPaymentItemAsync(orderUuid, voucherItem);
|
|
689
|
+
}
|
|
690
|
+
const updatedOrder = await this.getPaymentOrderByUuidAsync(orderUuid);
|
|
691
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentAdded, {
|
|
692
|
+
orderUuid,
|
|
693
|
+
order: updatedOrder,
|
|
694
|
+
payment: null
|
|
695
|
+
// 批量操作不提供单个支付项
|
|
696
|
+
});
|
|
697
|
+
this.logInfo("updateVoucherPaymentItemsAsync completed successfully", {
|
|
698
|
+
orderUuid,
|
|
699
|
+
removedVoucherCount: existingVoucherItems.length,
|
|
700
|
+
addedVoucherCount: voucherPaymentItems.length,
|
|
701
|
+
finalExpectAmount: updatedOrder == null ? void 0 : updatedOrder.expect_amount
|
|
702
|
+
});
|
|
703
|
+
} catch (error) {
|
|
704
|
+
console.error("[PaymentModule] 批量更新代金券支付项失败:", error);
|
|
705
|
+
this.logError("updateVoucherPaymentItemsAsync failed", error, {
|
|
706
|
+
orderUuid,
|
|
707
|
+
voucherPaymentItems
|
|
708
|
+
});
|
|
709
|
+
throw error;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* 更新一个支付项
|
|
714
|
+
*/
|
|
715
|
+
async updatePaymentAsync(orderUuid, paymentUuid, params) {
|
|
716
|
+
try {
|
|
717
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
718
|
+
if (!order) {
|
|
719
|
+
throw new Error("订单不存在");
|
|
720
|
+
}
|
|
721
|
+
const paymentItem = order.payment.find(
|
|
722
|
+
(payment) => payment.uuid === paymentUuid
|
|
723
|
+
);
|
|
724
|
+
if (paymentItem) {
|
|
725
|
+
const formattedParams = { ...params };
|
|
726
|
+
if ("amount" in formattedParams && formattedParams.amount !== void 0) {
|
|
727
|
+
formattedParams.amount = formatAmount(formattedParams.amount);
|
|
728
|
+
}
|
|
729
|
+
Object.assign(paymentItem, formattedParams);
|
|
730
|
+
this.recalculateOrderAmount(order);
|
|
731
|
+
await this.dbManager.update("order", order);
|
|
732
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentUpdated, {
|
|
733
|
+
order,
|
|
734
|
+
paymentItem
|
|
735
|
+
});
|
|
736
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
737
|
+
action: "payment_update",
|
|
738
|
+
order,
|
|
739
|
+
paymentItem
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
} catch (error) {
|
|
743
|
+
console.error("[PaymentModule] 更新支付项失败", error);
|
|
744
|
+
throw error;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* 提交支付
|
|
749
|
+
*/
|
|
750
|
+
async submitPayAsync(orderUuid) {
|
|
751
|
+
this.logInfo("Starting submitPayAsync", { orderUuid });
|
|
752
|
+
try {
|
|
753
|
+
let orderToSubmit;
|
|
754
|
+
if (orderUuid) {
|
|
755
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
756
|
+
orderToSubmit = order ? [order] : [];
|
|
757
|
+
} else {
|
|
758
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
759
|
+
orderToSubmit = allOrders.filter(
|
|
760
|
+
(order) => order.payment_status === import_types.PaymentStatus.Processing || order.payment_status === import_types.PaymentStatus.PartiallyPaid
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
for (const order of orderToSubmit) {
|
|
764
|
+
await this.submitSingleOrderPayment(order);
|
|
765
|
+
}
|
|
766
|
+
const result = { status: "success" };
|
|
767
|
+
this.logInfo("submitPayAsync completed", {
|
|
768
|
+
result: result.status,
|
|
769
|
+
processedOrdersCount: orderToSubmit.length
|
|
770
|
+
});
|
|
771
|
+
return result;
|
|
772
|
+
} catch (error) {
|
|
773
|
+
console.error("[PaymentModule] 提交支付失败", error);
|
|
774
|
+
this.logError("submitPayAsync failed", error, { orderUuid });
|
|
775
|
+
return { status: "failed" };
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* 提交单个订单的支付(推送到任务队列)
|
|
780
|
+
*/
|
|
781
|
+
async submitSingleOrderPayment(order) {
|
|
782
|
+
try {
|
|
783
|
+
const paymentData = {
|
|
784
|
+
payments: order.payment.map((payment) => ({
|
|
785
|
+
uuid: payment.uuid,
|
|
786
|
+
amount: payment.amount,
|
|
787
|
+
code: payment.code,
|
|
788
|
+
id: payment.id,
|
|
789
|
+
name: payment.name,
|
|
790
|
+
type: payment.type,
|
|
791
|
+
voucher_id: payment.voucher_id,
|
|
792
|
+
metadata: payment.metadata,
|
|
793
|
+
rounding_amount: payment.rounding_amount,
|
|
794
|
+
service_charge: payment.service_charge
|
|
795
|
+
})),
|
|
796
|
+
payment_status: "pending"
|
|
797
|
+
};
|
|
798
|
+
const totalPaidAmount = paymentData.payments.reduce(
|
|
799
|
+
(sum, payment) => sum.plus(payment.amount),
|
|
800
|
+
new import_decimal.Decimal(0)
|
|
801
|
+
);
|
|
802
|
+
const orderTotalAmount = new import_decimal.Decimal(order.total_amount);
|
|
803
|
+
if (totalPaidAmount.gte(orderTotalAmount)) {
|
|
804
|
+
order.payment_status = import_types.PaymentStatus.Finished;
|
|
805
|
+
console.log(
|
|
806
|
+
`[PaymentModule] 订单 ${order.uuid} 支付完成,金额: ${totalPaidAmount.toString()}/${orderTotalAmount.toString()}`
|
|
807
|
+
);
|
|
808
|
+
} else {
|
|
809
|
+
order.payment_status = import_types.PaymentStatus.PartiallyPaid;
|
|
810
|
+
console.log(
|
|
811
|
+
`[PaymentModule] 订单 ${order.uuid} 部分支付,金额: ${totalPaidAmount.toString()}/${orderTotalAmount.toString()}`
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
paymentData.payment_status = order.payment_status === import_types.PaymentStatus.Finished ? "paid" : "partially_paid";
|
|
815
|
+
await this.dbManager.update("order", order);
|
|
816
|
+
if (paymentData.payments.length === 0) {
|
|
817
|
+
console.log(`[PaymentModule] 订单 ${order.uuid} 支付列表为空,跳过支付处理`);
|
|
818
|
+
this.logWarning("Empty payment list", {
|
|
819
|
+
orderUuid: order.uuid,
|
|
820
|
+
orderId: order.order_id
|
|
821
|
+
});
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentSubmitted, order);
|
|
825
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
826
|
+
action: "submit",
|
|
827
|
+
order
|
|
828
|
+
});
|
|
829
|
+
} catch (error) {
|
|
830
|
+
console.error(`[PaymentModule] 订单 ${order.uuid} 支付提交失败`, error);
|
|
831
|
+
throw error;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* 获取订单剩余待付金额
|
|
836
|
+
*/
|
|
837
|
+
async getRemainingOrderAmountAsync(orderUuid) {
|
|
838
|
+
const order = await this.getPaymentOrderByUuidAsync(orderUuid);
|
|
839
|
+
if (!order) {
|
|
840
|
+
throw new Error("订单不存在");
|
|
841
|
+
}
|
|
842
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* 在比如现金支付界面的地方,用户输入了一个金额,在下方显示剩余多少金额,通过此方法获取
|
|
846
|
+
*/
|
|
847
|
+
async getRemainingOrderAmountWithInputAsync(inputAmount, orderUuid) {
|
|
848
|
+
const order = await this.getPaymentOrderByUuidAsync(orderUuid);
|
|
849
|
+
if (!order) {
|
|
850
|
+
throw new Error("订单不存在");
|
|
851
|
+
}
|
|
852
|
+
if (inputAmount === null || inputAmount === void 0) {
|
|
853
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
854
|
+
}
|
|
855
|
+
if (typeof inputAmount === "string") {
|
|
856
|
+
if (inputAmount.trim() === "") {
|
|
857
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
if (typeof inputAmount === "number") {
|
|
861
|
+
if (isNaN(inputAmount) || !isFinite(inputAmount)) {
|
|
862
|
+
console.warn(
|
|
863
|
+
`[PaymentModule] 输入金额不是有效数字: ${inputAmount},返回原始待付金额`
|
|
864
|
+
);
|
|
865
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
try {
|
|
869
|
+
const inputDecimal = new import_decimal.Decimal(inputAmount);
|
|
870
|
+
return new import_decimal.Decimal(order.expect_amount).minus(inputDecimal).toNumber();
|
|
871
|
+
} catch (error) {
|
|
872
|
+
console.warn(
|
|
873
|
+
`[PaymentModule] 输入金额格式无效: ${inputAmount},返回原始待付金额`
|
|
874
|
+
);
|
|
875
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* 重新计算订单金额
|
|
880
|
+
*/
|
|
881
|
+
recalculateOrderAmount(order) {
|
|
882
|
+
const totalAmount = new import_decimal.Decimal(order.total_amount);
|
|
883
|
+
const paidAmount = order.payment.reduce(
|
|
884
|
+
(sum, payment) => {
|
|
885
|
+
try {
|
|
886
|
+
if (payment.status === "voided") {
|
|
887
|
+
return sum;
|
|
888
|
+
}
|
|
889
|
+
const paymentAmount = new import_decimal.Decimal(payment.amount || 0);
|
|
890
|
+
const roundingAmount = new import_decimal.Decimal(payment.rounding_amount || 0);
|
|
891
|
+
const effectiveAmount = paymentAmount.plus(roundingAmount.abs());
|
|
892
|
+
return sum.plus(effectiveAmount);
|
|
893
|
+
} catch (error) {
|
|
894
|
+
console.warn(`[PaymentModule] 无效的支付金额: amount=${payment.amount}, rounding_amount=${payment.rounding_amount},跳过计算`);
|
|
895
|
+
return sum;
|
|
896
|
+
}
|
|
897
|
+
},
|
|
898
|
+
new import_decimal.Decimal(0)
|
|
899
|
+
);
|
|
900
|
+
const remainingAmount = totalAmount.minus(paidAmount);
|
|
901
|
+
order.expect_amount = import_decimal.Decimal.max(0, remainingAmount).toFixed(2);
|
|
902
|
+
console.log(`[PaymentModule] 重新计算订单金额:`, {
|
|
903
|
+
orderUuid: order.uuid,
|
|
904
|
+
totalAmount: order.total_amount,
|
|
905
|
+
effectivePaidAmount: paidAmount.toFixed(2),
|
|
906
|
+
remainingAmount: order.expect_amount,
|
|
907
|
+
activePayments: order.payment.filter((p) => p.status !== "voided").length,
|
|
908
|
+
voidedPayments: order.payment.filter((p) => p.status === "voided").length,
|
|
909
|
+
paymentDetails: order.payment.filter((p) => p.status !== "voided").map((p) => ({
|
|
910
|
+
code: p.code,
|
|
911
|
+
amount: p.amount,
|
|
912
|
+
rounding_amount: p.rounding_amount || "0.00",
|
|
913
|
+
effective_amount: new import_decimal.Decimal(p.amount || 0).plus(new import_decimal.Decimal(p.rounding_amount || 0).abs()).toFixed(2)
|
|
914
|
+
})),
|
|
915
|
+
说明: "有效支付金额包含抹零计算(amount + |rounding_amount|)"
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* 获取现金支付方式
|
|
920
|
+
*/
|
|
921
|
+
async getCashPaymentMethod() {
|
|
922
|
+
try {
|
|
923
|
+
const payMethods = await this.dbManager.getAll("pay_method");
|
|
924
|
+
return payMethods.find(
|
|
925
|
+
(method) => method.code === import_types.PaymentMethodType.Cash
|
|
926
|
+
) || null;
|
|
927
|
+
} catch (error) {
|
|
928
|
+
console.error("[PaymentModule] 获取现金支付方式失败", error);
|
|
929
|
+
return null;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* 获取Eftpos支付方式
|
|
934
|
+
*/
|
|
935
|
+
async getEftposPaymentMethod() {
|
|
936
|
+
try {
|
|
937
|
+
const payMethods = await this.dbManager.getAll("pay_method");
|
|
938
|
+
return payMethods.find(
|
|
939
|
+
(method) => method.code === import_types.PaymentMethodType.Eftpos
|
|
940
|
+
) || null;
|
|
941
|
+
} catch (error) {
|
|
942
|
+
console.error("[PaymentModule] 获取Eftpos支付方式失败", error);
|
|
943
|
+
return null;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* 获取钱包支付方式
|
|
948
|
+
*/
|
|
949
|
+
async getWalletPaymentMethod() {
|
|
950
|
+
try {
|
|
951
|
+
const payMethods = await this.dbManager.getAll("pay_method");
|
|
952
|
+
return payMethods.find(
|
|
953
|
+
(method) => method.code === import_types.PaymentMethodType.Wallet
|
|
954
|
+
) || null;
|
|
955
|
+
} catch (error) {
|
|
956
|
+
console.error("[PaymentModule] 获取钱包支付方式失败", error);
|
|
957
|
+
return null;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* 确保支付模块所需的数据库表已创建
|
|
962
|
+
*/
|
|
963
|
+
async ensurePaymentTables() {
|
|
964
|
+
try {
|
|
965
|
+
await this.dbManager.getAll("pay_method");
|
|
966
|
+
} catch (error) {
|
|
967
|
+
console.warn(
|
|
968
|
+
"[PaymentModule] pay_method 表不存在,请在数据库配置中添加以下配置:"
|
|
969
|
+
);
|
|
970
|
+
console.warn('{ name: "pay_method", keyPath: "id" }');
|
|
971
|
+
}
|
|
972
|
+
try {
|
|
973
|
+
await this.dbManager.getAll("order");
|
|
974
|
+
} catch (error) {
|
|
975
|
+
console.warn(
|
|
976
|
+
"[PaymentModule] order 表不存在,请在数据库配置中添加以下配置:"
|
|
977
|
+
);
|
|
978
|
+
console.warn('{ name: "order", keyPath: "uuid" }');
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* 获取部分支付的订单
|
|
983
|
+
*/
|
|
984
|
+
async getPartiallyPaidOrdersAsync() {
|
|
985
|
+
try {
|
|
986
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
987
|
+
return allOrders.filter(
|
|
988
|
+
(order) => order.payment_status === import_types.PaymentStatus.PartiallyPaid
|
|
989
|
+
);
|
|
990
|
+
} catch (error) {
|
|
991
|
+
console.error("[PaymentModule] 获取部分支付订单失败", error);
|
|
992
|
+
return [];
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
// === 新的 API 方法实现 ===
|
|
996
|
+
/**
|
|
997
|
+
* 智能金额舍入
|
|
998
|
+
*
|
|
999
|
+
* 根据指定的舍入间隔和规则对金额进行舍入处理
|
|
1000
|
+
*
|
|
1001
|
+
* @param originalAmount 原始金额
|
|
1002
|
+
* @param interval 舍入间隔 (0.05, 0.1, 0.5, 1)
|
|
1003
|
+
* @param rule 舍入规则 (standard, standard_down, always_up, always_down)
|
|
1004
|
+
* @returns 舍入结果详情(包含原始金额、舍入后金额和舍入差额)
|
|
1005
|
+
*/
|
|
1006
|
+
async roundAmountAsync(originalAmount, interval, rule) {
|
|
1007
|
+
try {
|
|
1008
|
+
const amount = new import_decimal.Decimal(originalAmount);
|
|
1009
|
+
const roundingInterval = new import_decimal.Decimal(interval);
|
|
1010
|
+
if (roundingInterval.lte(0)) {
|
|
1011
|
+
throw new Error("舍入间隔必须大于 0");
|
|
1012
|
+
}
|
|
1013
|
+
const supportedIntervals = [0.05, 0.1, 0.5, 1];
|
|
1014
|
+
const intervalValue = roundingInterval.toNumber();
|
|
1015
|
+
if (!supportedIntervals.includes(intervalValue)) {
|
|
1016
|
+
console.warn(`[PaymentModule] 不支持的舍入间隔: ${intervalValue}, 支持的间隔: ${supportedIntervals.join(", ")}`);
|
|
1017
|
+
}
|
|
1018
|
+
const baseValue = amount.div(roundingInterval);
|
|
1019
|
+
console.log(`[PaymentModule] 舍入计算 - 原始金额: ${amount.toString()}, 间隔: ${intervalValue}, 基础值: ${baseValue.toString()}, 规则: ${rule}`);
|
|
1020
|
+
let roundedValue;
|
|
1021
|
+
switch (rule) {
|
|
1022
|
+
case import_types.RoundingRule.Standard:
|
|
1023
|
+
case "standard":
|
|
1024
|
+
roundedValue = this.standardRound(baseValue, true);
|
|
1025
|
+
break;
|
|
1026
|
+
case import_types.RoundingRule.StandardDown:
|
|
1027
|
+
case "standard_down":
|
|
1028
|
+
roundedValue = this.standardRound(baseValue, false);
|
|
1029
|
+
break;
|
|
1030
|
+
case import_types.RoundingRule.AlwaysUp:
|
|
1031
|
+
case "always_up":
|
|
1032
|
+
roundedValue = baseValue.ceil();
|
|
1033
|
+
break;
|
|
1034
|
+
case import_types.RoundingRule.AlwaysDown:
|
|
1035
|
+
case "always_down":
|
|
1036
|
+
roundedValue = baseValue.floor();
|
|
1037
|
+
break;
|
|
1038
|
+
default:
|
|
1039
|
+
throw new Error(`不支持的舍入规则: ${rule}`);
|
|
1040
|
+
}
|
|
1041
|
+
const finalAmount = roundedValue.mul(roundingInterval);
|
|
1042
|
+
const roundedAmountStr = finalAmount.toFixed(2);
|
|
1043
|
+
const originalAmountStr = amount.toFixed(2);
|
|
1044
|
+
const roundingDifference = finalAmount.sub(amount);
|
|
1045
|
+
const roundingDifferenceStr = roundingDifference.toFixed(2);
|
|
1046
|
+
console.log(`[PaymentModule] 舍入结果 - 原始金额: ${originalAmountStr}, 舍入后金额: ${roundedAmountStr}, 舍入差额: ${roundingDifferenceStr}`);
|
|
1047
|
+
return {
|
|
1048
|
+
originalAmount: originalAmountStr,
|
|
1049
|
+
roundedAmount: roundedAmountStr,
|
|
1050
|
+
roundingDifference: roundingDifferenceStr
|
|
1051
|
+
};
|
|
1052
|
+
} catch (error) {
|
|
1053
|
+
console.error("[PaymentModule] 金额舍入失败:", error);
|
|
1054
|
+
throw new Error(`金额舍入失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
/**
|
|
1058
|
+
* 标准舍入处理(处理中点情况)
|
|
1059
|
+
*
|
|
1060
|
+
* @param value 要舍入的值
|
|
1061
|
+
* @param midpointUp 中点是否向上舍入
|
|
1062
|
+
* @returns 舍入后的值
|
|
1063
|
+
*/
|
|
1064
|
+
standardRound(value, midpointUp) {
|
|
1065
|
+
const floorValue = value.floor();
|
|
1066
|
+
const fractionalPart = value.minus(floorValue);
|
|
1067
|
+
if (fractionalPart.eq(0.5)) {
|
|
1068
|
+
return midpointUp ? floorValue.plus(1) : floorValue;
|
|
1069
|
+
} else if (fractionalPart.gt(0.5)) {
|
|
1070
|
+
return floorValue.plus(1);
|
|
1071
|
+
} else {
|
|
1072
|
+
return floorValue;
|
|
1073
|
+
}
|
|
82
1074
|
}
|
|
83
1075
|
};
|
|
84
1076
|
// Annotate the CommonJS export names for ESM import in node:
|
|
85
1077
|
0 && (module.exports = {
|
|
86
|
-
PaymentModule
|
|
1078
|
+
PaymentModule,
|
|
1079
|
+
generateRequestUniqueId,
|
|
1080
|
+
...require("./types")
|
|
87
1081
|
});
|