@pisell/pisellos 0.0.192 → 0.0.194
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/modules/Cart/index.js +1 -1
- package/dist/modules/Payment/cash.d.ts +11 -0
- package/dist/modules/Payment/cash.js +78 -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 +175 -9
- package/dist/modules/Payment/index.js +1909 -102
- package/dist/modules/Payment/types.d.ts +351 -31
- package/dist/modules/Payment/types.js +116 -14
- package/dist/modules/Payment/wallet.d.ts +11 -0
- package/dist/modules/Payment/wallet.js +78 -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/solution/BookingByStep/index.js +1 -1
- package/dist/solution/BookingByStep/utils/resources.js +0 -1
- package/lib/modules/Cart/index.js +1 -1
- package/lib/modules/Payment/cash.d.ts +11 -0
- package/lib/modules/Payment/cash.js +51 -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 +175 -9
- package/lib/modules/Payment/index.js +962 -49
- package/lib/modules/Payment/types.d.ts +351 -31
- package/lib/modules/Payment/types.js +41 -6
- package/lib/modules/Payment/wallet.d.ts +11 -0
- package/lib/modules/Payment/wallet.js +51 -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/solution/BookingByStep/index.js +1 -2
- package/lib/solution/BookingByStep/utils/resources.js +0 -1
- package/package.json +3 -1
|
@@ -14,6 +14,7 @@ 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
|
|
@@ -24,64 +25,976 @@ __export(Payment_exports, {
|
|
|
24
25
|
module.exports = __toCommonJS(Payment_exports);
|
|
25
26
|
var import_BaseModule = require("../BaseModule");
|
|
26
27
|
var import_types = require("./types");
|
|
28
|
+
var import_utils = require("../Cart/utils");
|
|
29
|
+
var import_cash = require("./cash");
|
|
30
|
+
var import_eftpos = require("./eftpos");
|
|
31
|
+
var import_wallet = require("./wallet");
|
|
32
|
+
var import_decimal = require("decimal.js");
|
|
33
|
+
__reExport(Payment_exports, require("./types"), module.exports);
|
|
34
|
+
function formatAmount(amount) {
|
|
35
|
+
try {
|
|
36
|
+
const decimal = new import_decimal.Decimal(amount);
|
|
37
|
+
return decimal.toFixed(2);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.warn(`[PaymentModule] 金额格式化失败: ${amount},使用默认值 0.00`);
|
|
40
|
+
return "0.00";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
27
43
|
var PaymentModule = class extends import_BaseModule.BaseModule {
|
|
28
44
|
constructor(name, version) {
|
|
29
45
|
super(name, version);
|
|
30
|
-
this.defaultName = "
|
|
46
|
+
this.defaultName = "pay";
|
|
31
47
|
this.defaultVersion = "1.0.0";
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
48
|
+
this.cash = new import_cash.CashPaymentImpl(this);
|
|
49
|
+
this.eftpos = new import_eftpos.EftposPaymentImpl(this);
|
|
50
|
+
this.wallet = new import_wallet.WalletPaymentImpl(this);
|
|
36
51
|
}
|
|
37
52
|
async initialize(core, options) {
|
|
38
53
|
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
|
-
|
|
54
|
+
this.store = options.store;
|
|
55
|
+
this.request = core.getPlugin("request");
|
|
56
|
+
const appPlugin = core.getPlugin("app");
|
|
57
|
+
if (!this.request) {
|
|
58
|
+
throw new Error("支付模块需要 request 插件支持");
|
|
59
|
+
}
|
|
60
|
+
if (!appPlugin) {
|
|
61
|
+
throw new Error("支付模块需要 app 插件支持");
|
|
62
|
+
}
|
|
63
|
+
this.app = appPlugin.getApp();
|
|
64
|
+
this.dbManager = this.app.dbManager;
|
|
65
|
+
this.logger = this.app.logger;
|
|
66
|
+
await this.ensurePaymentTables();
|
|
67
|
+
this.registerTaskHandlers();
|
|
68
|
+
this.registerNetworkHandlers();
|
|
69
|
+
console.log("[PaymentModule] 初始化完成");
|
|
70
|
+
this.logInfo("PaymentModule initialized successfully");
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 记录信息日志
|
|
74
|
+
*/
|
|
75
|
+
logInfo(title, metadata) {
|
|
76
|
+
if (this.logger) {
|
|
77
|
+
this.logger.addLog({
|
|
78
|
+
type: "info",
|
|
79
|
+
title: `[PaymentModule] ${title}`,
|
|
80
|
+
metadata: metadata || {}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 记录警告日志
|
|
86
|
+
*/
|
|
87
|
+
logWarning(title, metadata) {
|
|
88
|
+
if (this.logger) {
|
|
89
|
+
this.logger.addLog({
|
|
90
|
+
type: "warning",
|
|
91
|
+
title: `[PaymentModule] ${title}`,
|
|
92
|
+
metadata: metadata || {}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 记录错误日志
|
|
98
|
+
*/
|
|
99
|
+
logError(title, error, metadata) {
|
|
100
|
+
if (this.logger) {
|
|
101
|
+
this.logger.addLog({
|
|
102
|
+
type: "error",
|
|
103
|
+
title: `[PaymentModule] ${title}`,
|
|
104
|
+
metadata: {
|
|
105
|
+
error: error ? {
|
|
106
|
+
message: error.message,
|
|
107
|
+
stack: error.stack,
|
|
108
|
+
code: error.code,
|
|
109
|
+
...error
|
|
110
|
+
} : void 0,
|
|
111
|
+
...metadata || {}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 记录调试日志
|
|
118
|
+
*/
|
|
119
|
+
logDebug(title, metadata) {
|
|
120
|
+
if (this.logger) {
|
|
121
|
+
this.logger.addLog({
|
|
122
|
+
type: "debug",
|
|
123
|
+
title: `[PaymentModule] ${title}`,
|
|
124
|
+
metadata: metadata || {}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 网络恢复以后,尝试执行队列
|
|
130
|
+
*
|
|
131
|
+
*/
|
|
132
|
+
registerNetworkHandlers() {
|
|
133
|
+
const network = this.app.usePlugin("network");
|
|
134
|
+
this.logInfo("Registering network status listener");
|
|
135
|
+
network == null ? void 0 : network.addListener("networkStatusChange", (status) => {
|
|
136
|
+
console.log("网络状态:", status.connected);
|
|
137
|
+
this.logInfo("Network status changed", {
|
|
138
|
+
connected: status.connected,
|
|
139
|
+
previousStatus: status.previousStatus
|
|
140
|
+
});
|
|
141
|
+
if (status.connected) {
|
|
142
|
+
this.logInfo("Network reconnected - triggering payment sync queue");
|
|
143
|
+
this.runPaymentSyncQueue();
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 获取支付方式列表
|
|
149
|
+
*/
|
|
150
|
+
async getPayMethodListAsync() {
|
|
151
|
+
this.logInfo("Starting getPayMethodListAsync");
|
|
152
|
+
try {
|
|
153
|
+
let cachedMethods = [];
|
|
154
|
+
try {
|
|
155
|
+
cachedMethods = await this.dbManager.getAll("pay_method");
|
|
156
|
+
} catch (dbError) {
|
|
157
|
+
console.warn("[PaymentModule] pay_method 表不存在,将从服务器获取数据");
|
|
158
|
+
}
|
|
159
|
+
const hasCache = cachedMethods.length > 0;
|
|
160
|
+
if (hasCache) {
|
|
161
|
+
this.refreshPaymentMethodsInBackground(cachedMethods);
|
|
162
|
+
return cachedMethods;
|
|
163
|
+
}
|
|
164
|
+
const response = await this.request.get("/pay/custom-payment/available");
|
|
165
|
+
const payMethods = response.data || [];
|
|
166
|
+
try {
|
|
167
|
+
for (const method of payMethods) {
|
|
168
|
+
await this.dbManager.update("pay_method", method);
|
|
169
|
+
}
|
|
170
|
+
} catch (dbError) {
|
|
171
|
+
console.warn("[PaymentModule] 无法缓存支付方式,pay_method 表不存在");
|
|
172
|
+
}
|
|
173
|
+
await this.core.effects.emit(
|
|
174
|
+
import_types.PaymentHooks.OnPaymentMethodsLoaded,
|
|
175
|
+
payMethods
|
|
176
|
+
);
|
|
177
|
+
this.logInfo("getPayMethodListAsync completed successfully", {
|
|
178
|
+
methodCount: payMethods.length,
|
|
179
|
+
hasCache: cachedMethods.length > 0
|
|
180
|
+
});
|
|
181
|
+
return payMethods;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error("[PaymentModule] 获取支付方式列表失败", error);
|
|
184
|
+
this.logError("getPayMethodListAsync failed", error);
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 后台刷新支付方式列表
|
|
190
|
+
*/
|
|
191
|
+
async refreshPaymentMethodsInBackground(cachedMethods) {
|
|
192
|
+
this.logInfo("Starting refreshPaymentMethodsInBackground", {
|
|
193
|
+
cachedMethodsCount: cachedMethods.length
|
|
194
|
+
});
|
|
195
|
+
try {
|
|
196
|
+
console.log("[PaymentModule] 后台刷新支付方式列表...");
|
|
197
|
+
const response = await this.request.get("/pay/custom-payment/available");
|
|
198
|
+
const newPayMethods = response.data || [];
|
|
199
|
+
const hasChanges = this.hasPaymentMethodsChanged(
|
|
200
|
+
cachedMethods,
|
|
201
|
+
newPayMethods
|
|
202
|
+
);
|
|
203
|
+
if (hasChanges) {
|
|
204
|
+
console.log("[PaymentModule] 支付方式列表已更新");
|
|
205
|
+
this.logInfo("Payment methods updated in background", {
|
|
206
|
+
oldCount: cachedMethods.length,
|
|
207
|
+
newCount: newPayMethods.length
|
|
208
|
+
});
|
|
209
|
+
try {
|
|
210
|
+
for (const method of cachedMethods) {
|
|
211
|
+
await this.dbManager.delete("pay_method", method.id);
|
|
212
|
+
}
|
|
213
|
+
for (const method of newPayMethods) {
|
|
214
|
+
await this.dbManager.update("pay_method", method);
|
|
215
|
+
}
|
|
216
|
+
} catch (dbError) {
|
|
217
|
+
console.warn("[PaymentModule] 无法更新支付方式缓存", dbError);
|
|
218
|
+
}
|
|
219
|
+
const eventData = {
|
|
220
|
+
oldMethods: cachedMethods,
|
|
221
|
+
newMethods: newPayMethods
|
|
222
|
+
};
|
|
223
|
+
await this.core.effects.emit(
|
|
224
|
+
import_types.PaymentHooks.OnPaymentMethodsChanged,
|
|
225
|
+
eventData
|
|
226
|
+
);
|
|
227
|
+
} else {
|
|
228
|
+
console.log("[PaymentModule] 支付方式列表无变化");
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.error("[PaymentModule] 后台刷新支付方式失败", error);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 检查支付方式列表是否有变化
|
|
236
|
+
*/
|
|
237
|
+
hasPaymentMethodsChanged(oldMethods, newMethods) {
|
|
238
|
+
if (oldMethods.length !== newMethods.length) {
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
const oldMethodsMap = new Map(
|
|
242
|
+
oldMethods.map((method) => [method.id, method])
|
|
243
|
+
);
|
|
244
|
+
for (const newMethod of newMethods) {
|
|
245
|
+
const oldMethod = oldMethodsMap.get(newMethod.id);
|
|
246
|
+
if (!oldMethod) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
if (oldMethod.code !== newMethod.code || oldMethod.name !== newMethod.name || oldMethod.type !== newMethod.type || oldMethod.enabled !== newMethod.enabled) {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* 获取订单列表
|
|
257
|
+
*/
|
|
258
|
+
async getOrderListAsync() {
|
|
259
|
+
try {
|
|
260
|
+
return await this.dbManager.getAll("order");
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.error("[PaymentModule] 获取订单列表失败", error);
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* 根据uuid获取指定订单
|
|
268
|
+
*/
|
|
269
|
+
async getOrderByUuidAsync(orderUuid) {
|
|
270
|
+
try {
|
|
271
|
+
return await this.dbManager.get("order", orderUuid);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
console.error("[PaymentModule] 获取订单失败", error);
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* 往交易组中添加订单
|
|
279
|
+
*/
|
|
280
|
+
async pushOrderAsync(params) {
|
|
281
|
+
this.logInfo("Starting pushOrderAsync", {
|
|
282
|
+
orderId: params.order_info.order_id,
|
|
283
|
+
totalAmount: params.order_info.total_amount
|
|
284
|
+
});
|
|
285
|
+
try {
|
|
286
|
+
const existingOrders = await this.dbManager.getAll("order");
|
|
287
|
+
const existingOrder = existingOrders.find(
|
|
288
|
+
(order) => order.id === params.order_info.order_id
|
|
289
|
+
);
|
|
290
|
+
if (existingOrder) {
|
|
291
|
+
console.log(
|
|
292
|
+
`[PaymentModule] 发现重复订单 ID: ${params.order_info.order_id},更新现有订单`
|
|
293
|
+
);
|
|
294
|
+
const originalOrder = { ...existingOrder };
|
|
295
|
+
existingOrder.order_info = params.order_info;
|
|
296
|
+
existingOrder.total_amount = params.order_info.total_amount;
|
|
297
|
+
this.recalculateOrderAmount(existingOrder);
|
|
298
|
+
await this.dbManager.update("order", existingOrder);
|
|
299
|
+
await this.core.effects.emit(
|
|
300
|
+
import_types.PaymentHooks.OnOrderUpdated,
|
|
301
|
+
existingOrder
|
|
302
|
+
);
|
|
303
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
304
|
+
action: "update",
|
|
305
|
+
order: existingOrder,
|
|
306
|
+
originalOrder
|
|
307
|
+
});
|
|
308
|
+
return existingOrder;
|
|
309
|
+
} else {
|
|
310
|
+
const newOrder = {
|
|
311
|
+
uuid: (0, import_utils.getUniqueId)("pay_order_"),
|
|
312
|
+
id: params.order_info.order_id,
|
|
313
|
+
order_info: params.order_info,
|
|
314
|
+
payment_status: import_types.PaymentStatus.Processing,
|
|
315
|
+
payment: [],
|
|
316
|
+
adjust_offline_payments: [],
|
|
317
|
+
total_amount: params.order_info.total_amount,
|
|
318
|
+
expect_amount: params.order_info.total_amount,
|
|
319
|
+
tax_fee: "0.00"
|
|
320
|
+
};
|
|
321
|
+
await this.dbManager.add("order", newOrder);
|
|
322
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderAdded, newOrder);
|
|
323
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
324
|
+
action: "add",
|
|
325
|
+
order: newOrder
|
|
326
|
+
});
|
|
327
|
+
this.logInfo("pushOrderAsync completed - new order created", {
|
|
328
|
+
orderUuid: newOrder.uuid,
|
|
329
|
+
orderId: newOrder.id
|
|
330
|
+
});
|
|
331
|
+
return newOrder;
|
|
332
|
+
}
|
|
333
|
+
} catch (error) {
|
|
334
|
+
console.error("[PaymentModule] 添加订单失败", error);
|
|
335
|
+
this.logError("pushOrderAsync failed", error, {
|
|
336
|
+
orderId: params.order_info.order_id
|
|
337
|
+
});
|
|
338
|
+
throw error;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* 删除订单
|
|
343
|
+
*/
|
|
344
|
+
async deleteOrderAsync(orderUuid) {
|
|
345
|
+
try {
|
|
346
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
347
|
+
if (order) {
|
|
348
|
+
await this.dbManager.delete("order", orderUuid);
|
|
349
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderDeleted, order);
|
|
350
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
351
|
+
action: "delete",
|
|
352
|
+
order
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
} catch (error) {
|
|
356
|
+
console.error("[PaymentModule] 删除订单失败", error);
|
|
357
|
+
throw error;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* 更新订单
|
|
362
|
+
*/
|
|
363
|
+
async updateOrderAsync(orderUuid, params) {
|
|
364
|
+
try {
|
|
365
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
366
|
+
if (order) {
|
|
367
|
+
const updatedOrder = { ...order, ...params };
|
|
368
|
+
await this.dbManager.update("order", updatedOrder);
|
|
369
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderUpdated, updatedOrder);
|
|
370
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
371
|
+
action: "update",
|
|
372
|
+
order: updatedOrder,
|
|
373
|
+
originalOrder: order
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
} catch (error) {
|
|
377
|
+
console.error("[PaymentModule] 更新订单失败", error);
|
|
378
|
+
throw error;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* 获取支付项
|
|
383
|
+
*/
|
|
384
|
+
async getPaymentAsync(orderUuid) {
|
|
385
|
+
if (!orderUuid) {
|
|
386
|
+
throw new Error("orderUuid is required");
|
|
387
|
+
}
|
|
388
|
+
const order = await this.getOrderByUuidAsync(orderUuid);
|
|
389
|
+
return (order == null ? void 0 : order.payment) || [];
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* 为某个订单设置一个支付项
|
|
393
|
+
*/
|
|
394
|
+
async pushPaymentAsync(orderUuid, paymentItem) {
|
|
395
|
+
this.logInfo("Starting pushPaymentAsync", {
|
|
396
|
+
orderUuid,
|
|
397
|
+
paymentAmount: paymentItem.amount,
|
|
398
|
+
paymentCode: paymentItem.code
|
|
399
|
+
});
|
|
400
|
+
try {
|
|
401
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
402
|
+
if (!order) {
|
|
403
|
+
throw new Error("订单不存在");
|
|
404
|
+
}
|
|
405
|
+
const newPaymentItem = {
|
|
406
|
+
...paymentItem,
|
|
407
|
+
amount: formatAmount(paymentItem.amount),
|
|
408
|
+
// 格式化金额为保留两位小数的字符串
|
|
409
|
+
uuid: (0, import_utils.getUniqueId)("pay_item_"),
|
|
410
|
+
isSynced: false
|
|
411
|
+
};
|
|
412
|
+
order.payment.push(newPaymentItem);
|
|
413
|
+
this.recalculateOrderAmount(order);
|
|
414
|
+
await this.dbManager.update("order", order);
|
|
415
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentAdded, {
|
|
416
|
+
order,
|
|
417
|
+
paymentItem: newPaymentItem
|
|
418
|
+
});
|
|
419
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
420
|
+
action: "payment_add",
|
|
421
|
+
order,
|
|
422
|
+
paymentItem: newPaymentItem
|
|
423
|
+
});
|
|
424
|
+
this.logInfo("pushPaymentAsync completed successfully", {
|
|
425
|
+
orderUuid,
|
|
426
|
+
paymentUuid: newPaymentItem.uuid,
|
|
427
|
+
newExpectAmount: order.expect_amount
|
|
428
|
+
});
|
|
429
|
+
} catch (error) {
|
|
430
|
+
console.error("[PaymentModule] 添加支付项失败", error);
|
|
431
|
+
this.logError("pushPaymentAsync failed", error, {
|
|
432
|
+
orderUuid,
|
|
433
|
+
paymentItem
|
|
434
|
+
});
|
|
435
|
+
throw error;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* 删除一个支付项
|
|
440
|
+
*/
|
|
441
|
+
async deletePaymentAsync(orderUuid, paymentUuid) {
|
|
442
|
+
this.logInfo("Starting deletePaymentAsync", { orderUuid, paymentUuid });
|
|
443
|
+
try {
|
|
444
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
445
|
+
if (!order) {
|
|
446
|
+
throw new Error("订单不存在");
|
|
447
|
+
}
|
|
448
|
+
const index = order.payment.findIndex(
|
|
449
|
+
(payment) => payment.uuid === paymentUuid
|
|
450
|
+
);
|
|
451
|
+
if (index > -1) {
|
|
452
|
+
const deletedPayment = order.payment.splice(index, 1)[0];
|
|
453
|
+
this.recalculateOrderAmount(order);
|
|
454
|
+
await this.dbManager.update("order", order);
|
|
455
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentDeleted, {
|
|
456
|
+
order,
|
|
457
|
+
paymentItem: deletedPayment
|
|
458
|
+
});
|
|
459
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
460
|
+
action: "payment_delete",
|
|
461
|
+
order,
|
|
462
|
+
paymentItem: deletedPayment
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
} catch (error) {
|
|
466
|
+
console.error("[PaymentModule] 删除支付项失败", error);
|
|
467
|
+
throw error;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* 更新一个支付项
|
|
472
|
+
*/
|
|
473
|
+
async updatePaymentAsync(orderUuid, paymentUuid, params) {
|
|
474
|
+
try {
|
|
475
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
476
|
+
if (!order) {
|
|
477
|
+
throw new Error("订单不存在");
|
|
478
|
+
}
|
|
479
|
+
const paymentItem = order.payment.find(
|
|
480
|
+
(payment) => payment.uuid === paymentUuid
|
|
481
|
+
);
|
|
482
|
+
if (paymentItem) {
|
|
483
|
+
const formattedParams = { ...params };
|
|
484
|
+
if ("amount" in formattedParams && formattedParams.amount !== void 0) {
|
|
485
|
+
formattedParams.amount = formatAmount(formattedParams.amount);
|
|
486
|
+
}
|
|
487
|
+
Object.assign(paymentItem, formattedParams);
|
|
488
|
+
this.recalculateOrderAmount(order);
|
|
489
|
+
await this.dbManager.update("order", order);
|
|
490
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentUpdated, {
|
|
491
|
+
order,
|
|
492
|
+
paymentItem
|
|
493
|
+
});
|
|
494
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
495
|
+
action: "payment_update",
|
|
496
|
+
order,
|
|
497
|
+
paymentItem
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
} catch (error) {
|
|
501
|
+
console.error("[PaymentModule] 更新支付项失败", error);
|
|
502
|
+
throw error;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* 提交支付
|
|
507
|
+
*/
|
|
508
|
+
async submitPayAsync(orderUuid) {
|
|
509
|
+
this.logInfo("Starting submitPayAsync", { orderUuid });
|
|
510
|
+
try {
|
|
511
|
+
let orderToSubmit;
|
|
512
|
+
if (orderUuid) {
|
|
513
|
+
const order = await this.dbManager.get("order", orderUuid);
|
|
514
|
+
orderToSubmit = order ? [order] : [];
|
|
515
|
+
} else {
|
|
516
|
+
const allOrders2 = await this.dbManager.getAll("order");
|
|
517
|
+
orderToSubmit = allOrders2.filter(
|
|
518
|
+
(order) => order.payment_status === import_types.PaymentStatus.Processing || order.payment_status === import_types.PaymentStatus.PartiallyPaid
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
for (const order of orderToSubmit) {
|
|
522
|
+
await this.submitSingleOrderPayment(order);
|
|
523
|
+
}
|
|
524
|
+
await this.runPaymentSyncQueue();
|
|
525
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
526
|
+
const allOrdersProcessed = allOrders.every(
|
|
527
|
+
(order) => order.payment.every((payment) => payment.isSynced === true)
|
|
528
|
+
);
|
|
529
|
+
if (allOrdersProcessed) {
|
|
530
|
+
const result = { status: "success" };
|
|
531
|
+
this.logInfo("submitPayAsync completed", {
|
|
532
|
+
result: result.status,
|
|
533
|
+
processedOrdersCount: allOrders.length
|
|
534
|
+
});
|
|
535
|
+
return result;
|
|
536
|
+
} else {
|
|
537
|
+
this.logWarning("submitPayAsync completed with unprocessed orders", {
|
|
538
|
+
totalOrders: allOrders.length
|
|
539
|
+
});
|
|
540
|
+
return { status: "failed" };
|
|
541
|
+
}
|
|
542
|
+
} catch (error) {
|
|
543
|
+
console.error("[PaymentModule] 提交支付失败", error);
|
|
544
|
+
this.logError("submitPayAsync failed", error, { orderUuid });
|
|
545
|
+
return { status: "failed" };
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* 提交单个订单的支付(推送到任务队列)
|
|
550
|
+
*/
|
|
551
|
+
async submitSingleOrderPayment(order) {
|
|
552
|
+
try {
|
|
553
|
+
const paymentData = {
|
|
554
|
+
payments: order.payment.map((payment) => payment),
|
|
555
|
+
payment_status: "pending"
|
|
556
|
+
};
|
|
557
|
+
const totalPaidAmount = paymentData.payments.reduce(
|
|
558
|
+
(sum, payment) => sum.plus(payment.amount),
|
|
559
|
+
new import_decimal.Decimal(0)
|
|
560
|
+
);
|
|
561
|
+
const orderTotalAmount = new import_decimal.Decimal(order.order_info.total_amount);
|
|
562
|
+
if (totalPaidAmount.gte(orderTotalAmount)) {
|
|
563
|
+
order.payment_status = import_types.PaymentStatus.Finished;
|
|
564
|
+
console.log(
|
|
565
|
+
`[PaymentModule] 订单 ${order.uuid} 支付完成,金额: ${totalPaidAmount.toString()}/${orderTotalAmount.toString()}`
|
|
566
|
+
);
|
|
567
|
+
} else {
|
|
568
|
+
order.payment_status = import_types.PaymentStatus.PartiallyPaid;
|
|
569
|
+
console.log(
|
|
570
|
+
`[PaymentModule] 订单 ${order.uuid} 部分支付,金额: ${totalPaidAmount.toString()}/${orderTotalAmount.toString()}`
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
paymentData.payment_status = order.payment_status === import_types.PaymentStatus.Finished ? "paid" : "partially_paid";
|
|
574
|
+
await this.dbManager.update("order", order);
|
|
575
|
+
await this.addToSyncQueue(order, paymentData);
|
|
576
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnPaymentSubmitted, order);
|
|
577
|
+
await this.core.effects.emit(import_types.PaymentHooks.OnOrderChanged, {
|
|
578
|
+
action: "submit",
|
|
579
|
+
order
|
|
580
|
+
});
|
|
581
|
+
} catch (error) {
|
|
582
|
+
console.error(`[PaymentModule] 订单 ${order.uuid} 支付提交失败`, error);
|
|
583
|
+
throw error;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* 添加到同步任务队列
|
|
588
|
+
*/
|
|
589
|
+
async addToSyncQueue(order, paymentData) {
|
|
590
|
+
try {
|
|
591
|
+
console.log(`[PaymentModule] 订单 ${order.uuid} 已添加到同步队列`);
|
|
592
|
+
if (this.app.tasksManager) {
|
|
593
|
+
this.app.tasksManager.addTask({
|
|
594
|
+
module: "payment",
|
|
595
|
+
queueId: "payment_sync",
|
|
596
|
+
tasks: [
|
|
597
|
+
{
|
|
598
|
+
id: `payment_${order.uuid}`,
|
|
599
|
+
action: "syncPayment",
|
|
600
|
+
payload: {
|
|
601
|
+
orderUuid: order.uuid,
|
|
602
|
+
orderId: order.order_info.order_id,
|
|
603
|
+
paymentData,
|
|
604
|
+
core: this.core
|
|
605
|
+
},
|
|
606
|
+
maxRetries: 3,
|
|
607
|
+
type: "cloud"
|
|
608
|
+
}
|
|
609
|
+
]
|
|
610
|
+
});
|
|
611
|
+
console.log(`[PaymentModule] 订单 ${order.uuid} 同步任务已添加到队列`);
|
|
612
|
+
} else {
|
|
613
|
+
console.warn("[PaymentModule] TasksManager 未初始化,无法添加同步任务");
|
|
614
|
+
}
|
|
615
|
+
} catch (error) {
|
|
616
|
+
console.error("[PaymentModule] 添加同步任务失败", error);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* 获取订单剩余待付金额
|
|
621
|
+
*/
|
|
622
|
+
async getRemainingOrderAmountAsync(orderUuid) {
|
|
623
|
+
const order = await this.getOrderByUuidAsync(orderUuid);
|
|
624
|
+
if (!order) {
|
|
625
|
+
throw new Error("订单不存在");
|
|
626
|
+
}
|
|
627
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* 在比如现金支付界面的地方,用户输入了一个金额,在下方显示剩余多少金额,通过此方法获取
|
|
631
|
+
*/
|
|
632
|
+
async getRemainingOrderAmountWithInputAsync(inputAmount, orderUuid) {
|
|
633
|
+
const order = await this.getOrderByUuidAsync(orderUuid);
|
|
634
|
+
if (!order) {
|
|
635
|
+
throw new Error("订单不存在");
|
|
636
|
+
}
|
|
637
|
+
if (inputAmount === null || inputAmount === void 0) {
|
|
638
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
639
|
+
}
|
|
640
|
+
if (typeof inputAmount === "string") {
|
|
641
|
+
if (inputAmount.trim() === "") {
|
|
642
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (typeof inputAmount === "number") {
|
|
646
|
+
if (isNaN(inputAmount) || !isFinite(inputAmount)) {
|
|
647
|
+
console.warn(
|
|
648
|
+
`[PaymentModule] 输入金额不是有效数字: ${inputAmount},返回原始待付金额`
|
|
649
|
+
);
|
|
650
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
try {
|
|
654
|
+
const inputDecimal = new import_decimal.Decimal(inputAmount);
|
|
655
|
+
return new import_decimal.Decimal(order.expect_amount).minus(inputDecimal).toNumber();
|
|
656
|
+
} catch (error) {
|
|
657
|
+
console.warn(
|
|
658
|
+
`[PaymentModule] 输入金额格式无效: ${inputAmount},返回原始待付金额`
|
|
659
|
+
);
|
|
660
|
+
return new import_decimal.Decimal(order.expect_amount).toNumber();
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* 重新计算订单金额
|
|
665
|
+
*/
|
|
666
|
+
recalculateOrderAmount(order) {
|
|
667
|
+
const totalAmount = new import_decimal.Decimal(order.order_info.total_amount);
|
|
668
|
+
const paidAmount = order.payment.reduce(
|
|
669
|
+
(sum, payment) => sum.plus(payment.amount),
|
|
670
|
+
new import_decimal.Decimal(0)
|
|
671
|
+
);
|
|
672
|
+
const remainingAmount = totalAmount.minus(paidAmount);
|
|
673
|
+
order.expect_amount = import_decimal.Decimal.max(0, remainingAmount).toFixed(2);
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* 获取现金支付方式
|
|
677
|
+
*/
|
|
678
|
+
async getCashPaymentMethod() {
|
|
679
|
+
try {
|
|
680
|
+
const payMethods = await this.dbManager.getAll("pay_method");
|
|
681
|
+
return payMethods.find(
|
|
682
|
+
(method) => method.code === import_types.PaymentMethodType.Cash
|
|
683
|
+
) || null;
|
|
684
|
+
} catch (error) {
|
|
685
|
+
console.error("[PaymentModule] 获取现金支付方式失败", error);
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* 获取Eftpos支付方式
|
|
691
|
+
*/
|
|
692
|
+
async getEftposPaymentMethod() {
|
|
693
|
+
try {
|
|
694
|
+
const payMethods = await this.dbManager.getAll("pay_method");
|
|
695
|
+
return payMethods.find(
|
|
696
|
+
(method) => method.code === import_types.PaymentMethodType.Eftpos
|
|
697
|
+
) || null;
|
|
698
|
+
} catch (error) {
|
|
699
|
+
console.error("[PaymentModule] 获取Eftpos支付方式失败", error);
|
|
700
|
+
return null;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* 获取钱包支付方式
|
|
705
|
+
*/
|
|
706
|
+
async getWalletPaymentMethod() {
|
|
707
|
+
try {
|
|
708
|
+
const payMethods = await this.dbManager.getAll("pay_method");
|
|
709
|
+
return payMethods.find(
|
|
710
|
+
(method) => method.code === import_types.PaymentMethodType.Wallet
|
|
711
|
+
) || null;
|
|
712
|
+
} catch (error) {
|
|
713
|
+
console.error("[PaymentModule] 获取钱包支付方式失败", error);
|
|
714
|
+
return null;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* 注册任务处理函数
|
|
719
|
+
*/
|
|
720
|
+
registerTaskHandlers() {
|
|
721
|
+
if (this.app.tasksManager) {
|
|
722
|
+
this.app.tasksManager.addTaskFunction(
|
|
723
|
+
"syncPayment",
|
|
724
|
+
PaymentModule.syncPaymentTask
|
|
725
|
+
);
|
|
726
|
+
console.log("[PaymentModule] 任务处理函数已注册");
|
|
727
|
+
} else {
|
|
728
|
+
console.warn(
|
|
729
|
+
"[PaymentModule] TasksManager 未初始化,无法注册任务处理函数"
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* 静态方法:处理支付同步任务(后台网络同步)
|
|
735
|
+
* 这个方法里不允许用 this,因为需要作为静态方法注入到任务队列里
|
|
736
|
+
*/
|
|
737
|
+
static async syncPaymentTask(payload) {
|
|
738
|
+
var _a, _b, _c, _d, _e, _f;
|
|
739
|
+
const { orderUuid, orderId, paymentData, core } = payload.task.payload;
|
|
740
|
+
console.log(`[PaymentModule] 开始后台同步支付,订单ID: ${orderId}`);
|
|
741
|
+
const app = window.app;
|
|
742
|
+
const logger = app == null ? void 0 : app.logger;
|
|
743
|
+
if (logger) {
|
|
744
|
+
logger.addLog({
|
|
745
|
+
type: "info",
|
|
746
|
+
title: "[PaymentModule] Starting syncPaymentTask",
|
|
747
|
+
metadata: { orderUuid, orderId, paymentDataKeys: Object.keys(paymentData) }
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
if (!app) {
|
|
751
|
+
throw new Error("App 实例未找到");
|
|
752
|
+
}
|
|
753
|
+
const request = app.request;
|
|
754
|
+
if (!request) {
|
|
755
|
+
throw new Error("Request 插件未找到");
|
|
756
|
+
}
|
|
757
|
+
const dbManager = app.dbManager;
|
|
758
|
+
if (!dbManager) {
|
|
759
|
+
throw new Error("DBManager 未找到");
|
|
760
|
+
}
|
|
761
|
+
try {
|
|
762
|
+
const requestRes = await request.post(
|
|
763
|
+
`/shop/order/${orderId}/order-payment`,
|
|
764
|
+
paymentData,
|
|
765
|
+
{ noToast: true }
|
|
766
|
+
);
|
|
767
|
+
if (requestRes.status === true) {
|
|
768
|
+
const order = await dbManager.get("order", orderUuid);
|
|
769
|
+
if (order) {
|
|
770
|
+
order.payment.forEach((payment) => {
|
|
771
|
+
payment.isSynced = true;
|
|
772
|
+
delete payment.syncError;
|
|
773
|
+
});
|
|
774
|
+
await dbManager.update("order", order);
|
|
775
|
+
console.log(`[PaymentModule] 订单 ${orderUuid} 同步状态已更新`);
|
|
776
|
+
}
|
|
777
|
+
console.log(`[PaymentModule] 订单 ${orderUuid} 后台同步成功`);
|
|
778
|
+
await core.effects.emit("payment:onPaymentSyncSuccess", {
|
|
779
|
+
orderUuid,
|
|
780
|
+
orderId,
|
|
781
|
+
timestamp: Date.now()
|
|
782
|
+
});
|
|
783
|
+
console.log(
|
|
784
|
+
`[PaymentModule] 已通过 effects 通知前端订单 ${orderUuid} 同步成功`
|
|
785
|
+
);
|
|
786
|
+
if (logger) {
|
|
787
|
+
logger.addLog({
|
|
788
|
+
type: "info",
|
|
789
|
+
title: "[PaymentModule] syncPaymentTask completed successfully",
|
|
790
|
+
metadata: { orderUuid, orderId }
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
return { status: "success", orderUuid, orderId };
|
|
794
|
+
} else {
|
|
795
|
+
console.log(`[PaymentModule] 接口返回失败,订单 ${orderUuid} 同步失败`);
|
|
796
|
+
const businessError = new Error(requestRes.message || "接口返回失败");
|
|
797
|
+
businessError.response = { status: 200 };
|
|
798
|
+
businessError.isBusinessError = true;
|
|
799
|
+
businessError.responseData = requestRes;
|
|
800
|
+
throw businessError;
|
|
801
|
+
}
|
|
802
|
+
} catch (error) {
|
|
803
|
+
console.error("[PaymentModule] 后台同步失败:", error);
|
|
804
|
+
const isNetworkError = !error.response && !error.status && (error.code === "NETWORK_ERROR" || error.code === "ERR_NETWORK" || error.code === "ECONNREFUSED" || error.code === "ENOTFOUND" || error.code === "ETIMEDOUT" || ((_a = error.message) == null ? void 0 : _a.includes("网络")) || ((_b = error.message) == null ? void 0 : _b.includes("network")) || ((_c = error.message) == null ? void 0 : _c.includes("fetch")));
|
|
805
|
+
if (isNetworkError) {
|
|
806
|
+
console.log(`[PaymentModule] 网络错误,订单 ${orderUuid} 将重试同步`);
|
|
807
|
+
if (logger) {
|
|
808
|
+
logger.addLog({
|
|
809
|
+
type: "warning",
|
|
810
|
+
title: "[PaymentModule] syncPaymentTask network error - will retry",
|
|
811
|
+
metadata: {
|
|
812
|
+
orderUuid,
|
|
813
|
+
orderId,
|
|
814
|
+
errorCode: error.code,
|
|
815
|
+
errorMessage: error.message
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
throw error;
|
|
820
|
+
} else {
|
|
821
|
+
console.log(
|
|
822
|
+
`[PaymentModule] 业务错误,订单 ${orderUuid} 同步失败,通知前端`
|
|
823
|
+
);
|
|
824
|
+
try {
|
|
825
|
+
if (core && core.effects) {
|
|
826
|
+
const errorData = {
|
|
827
|
+
orderUuid,
|
|
828
|
+
orderId,
|
|
829
|
+
error: error.message || "业务错误",
|
|
830
|
+
errorCode: error.code,
|
|
831
|
+
statusCode: ((_d = error.response) == null ? void 0 : _d.status) || error.status,
|
|
832
|
+
timestamp: Date.now()
|
|
833
|
+
};
|
|
834
|
+
if (error.isBusinessError && error.responseData) {
|
|
835
|
+
errorData.error = error.responseData.message || error.message || "接口返回失败";
|
|
836
|
+
errorData.responseData = error.responseData;
|
|
837
|
+
}
|
|
838
|
+
await core.effects.emit("payment:onPaymentSyncError", errorData);
|
|
839
|
+
console.log(
|
|
840
|
+
`[PaymentModule] 已通过 effects 通知前端订单 ${orderUuid} 同步错误`
|
|
841
|
+
);
|
|
842
|
+
} else {
|
|
843
|
+
console.warn(
|
|
844
|
+
`[PaymentModule] 无法获取 core 实例,无法通知前端错误`
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
if (logger) {
|
|
848
|
+
logger.addLog({
|
|
849
|
+
type: "error",
|
|
850
|
+
title: "[PaymentModule] syncPaymentTask business error",
|
|
851
|
+
metadata: {
|
|
852
|
+
orderUuid,
|
|
853
|
+
orderId,
|
|
854
|
+
errorCode: error.code,
|
|
855
|
+
errorMessage: error.message,
|
|
856
|
+
statusCode: ((_e = error.response) == null ? void 0 : _e.status) || error.status,
|
|
857
|
+
isBusinessError: error.isBusinessError
|
|
858
|
+
}
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
} catch (notifyError) {
|
|
862
|
+
console.error(`[PaymentModule] 通知前端同步错误失败:`, notifyError);
|
|
863
|
+
if (logger) {
|
|
864
|
+
logger.addLog({
|
|
865
|
+
type: "error",
|
|
866
|
+
title: "[PaymentModule] Failed to notify frontend of sync error",
|
|
867
|
+
metadata: {
|
|
868
|
+
orderUuid,
|
|
869
|
+
orderId,
|
|
870
|
+
notifyError: notifyError instanceof Error ? notifyError.message : String(notifyError)
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
const order = await dbManager.get("order", orderUuid);
|
|
876
|
+
if (order) {
|
|
877
|
+
order.payment.forEach((payment) => {
|
|
878
|
+
payment.isSynced = true;
|
|
879
|
+
});
|
|
880
|
+
await dbManager.update("order", order);
|
|
881
|
+
console.log(`[PaymentModule] 订单 ${orderUuid} 业务错误,已标记为已同步`);
|
|
882
|
+
}
|
|
883
|
+
return {
|
|
884
|
+
status: "completed_with_error",
|
|
885
|
+
orderUuid,
|
|
886
|
+
orderId,
|
|
887
|
+
error: error.message || "业务错误",
|
|
888
|
+
statusCode: ((_f = error.response) == null ? void 0 : _f.status) || error.status
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* 确保支付模块所需的数据库表已创建
|
|
895
|
+
*/
|
|
896
|
+
async ensurePaymentTables() {
|
|
897
|
+
try {
|
|
898
|
+
await this.dbManager.getAll("pay_method");
|
|
899
|
+
} catch (error) {
|
|
900
|
+
console.warn(
|
|
901
|
+
"[PaymentModule] pay_method 表不存在,请在数据库配置中添加以下配置:"
|
|
902
|
+
);
|
|
903
|
+
console.warn('{ name: "pay_method", keyPath: "id" }');
|
|
904
|
+
}
|
|
905
|
+
try {
|
|
906
|
+
await this.dbManager.getAll("order");
|
|
907
|
+
} catch (error) {
|
|
908
|
+
console.warn(
|
|
909
|
+
"[PaymentModule] order 表不存在,请在数据库配置中添加以下配置:"
|
|
910
|
+
);
|
|
911
|
+
console.warn('{ name: "order", keyPath: "uuid" }');
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* 获取部分支付的订单
|
|
916
|
+
*/
|
|
917
|
+
async getPartiallyPaidOrdersAsync() {
|
|
918
|
+
try {
|
|
919
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
920
|
+
return allOrders.filter(
|
|
921
|
+
(order) => order.payment_status === import_types.PaymentStatus.PartiallyPaid
|
|
922
|
+
);
|
|
923
|
+
} catch (error) {
|
|
924
|
+
console.error("[PaymentModule] 获取部分支付订单失败", error);
|
|
925
|
+
return [];
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* 获取未同步到服务器的订单
|
|
930
|
+
*/
|
|
931
|
+
async getUnsyncedOrdersAsync() {
|
|
932
|
+
try {
|
|
933
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
934
|
+
return allOrders.filter((order) => {
|
|
935
|
+
return (order.payment_status === import_types.PaymentStatus.Finished || order.payment_status === import_types.PaymentStatus.PartiallyPaid) && order.payment.some((payment) => !payment.isSynced);
|
|
936
|
+
});
|
|
937
|
+
} catch (error) {
|
|
938
|
+
console.error("[PaymentModule] 获取未同步订单失败", error);
|
|
939
|
+
return [];
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* 清理已完成且已同步的订单
|
|
944
|
+
* 注意:只清理完全支付的订单,部分支付的订单保留以便继续支付
|
|
945
|
+
*/
|
|
946
|
+
async cleanupFinishedOrders() {
|
|
947
|
+
try {
|
|
948
|
+
const allOrders = await this.dbManager.getAll("order");
|
|
949
|
+
const finishedAndSyncedOrders = allOrders.filter(
|
|
950
|
+
(order) => order.payment_status === import_types.PaymentStatus.Finished && order.payment.every((payment) => payment.isSynced)
|
|
951
|
+
);
|
|
952
|
+
for (const order of finishedAndSyncedOrders) {
|
|
953
|
+
await this.deleteOrderAsync(order.uuid);
|
|
954
|
+
}
|
|
955
|
+
console.log(
|
|
956
|
+
`[PaymentModule] 清理了 ${finishedAndSyncedOrders.length} 个已完成且已同步的订单`
|
|
957
|
+
);
|
|
958
|
+
} catch (error) {
|
|
959
|
+
console.error("[PaymentModule] 清理已完成订单失败", error);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* 手动执行支付同步队列
|
|
964
|
+
* 外部可以调用此方法来触发支付同步任务的执行
|
|
965
|
+
*/
|
|
966
|
+
async runPaymentSyncQueue() {
|
|
967
|
+
this.logInfo("Starting runPaymentSyncQueue");
|
|
968
|
+
try {
|
|
969
|
+
if (!this.app.tasksManager) {
|
|
970
|
+
console.warn("[PaymentModule] TasksManager 未初始化,无法执行同步队列");
|
|
971
|
+
this.logWarning("TasksManager not initialized, cannot run sync queue");
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
console.log("[PaymentModule] 开始执行支付同步队列");
|
|
975
|
+
await this.app.tasksManager.run({
|
|
976
|
+
module: "payment",
|
|
977
|
+
queueId: "payment_sync",
|
|
978
|
+
callback: () => {
|
|
979
|
+
console.log("[PaymentModule] 支付同步队列执行完成");
|
|
980
|
+
if (this.logger) {
|
|
981
|
+
this.logger.addLog({
|
|
982
|
+
type: "info",
|
|
983
|
+
title: "[PaymentModule] Payment sync queue completed",
|
|
984
|
+
metadata: {}
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
});
|
|
989
|
+
this.logInfo("runPaymentSyncQueue completed successfully");
|
|
990
|
+
} catch (error) {
|
|
991
|
+
console.error("[PaymentModule] 执行支付同步队列失败", error);
|
|
992
|
+
this.logError("runPaymentSyncQueue failed", error);
|
|
993
|
+
}
|
|
82
994
|
}
|
|
83
995
|
};
|
|
84
996
|
// Annotate the CommonJS export names for ESM import in node:
|
|
85
997
|
0 && (module.exports = {
|
|
86
|
-
PaymentModule
|
|
998
|
+
PaymentModule,
|
|
999
|
+
...require("./types")
|
|
87
1000
|
});
|