@reglow/reglow 0.12.0 → 0.13.0
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/reglow/core/config.js +1 -1
- package/dist/reglow/core/entities.d.ts +65 -0
- package/dist/reglow/core/models.d.ts +8 -0
- package/dist/reglow/core/models.js +4 -0
- package/dist/reglow/core/models.js.map +1 -1
- package/dist/reglow/core/payment.d.ts +93 -0
- package/dist/reglow/core/payment.js +1063 -11
- package/dist/reglow/core/payment.js.map +1 -1
- package/dist/reglow/modules/payment/controller.js +48 -1
- package/dist/reglow/modules/payment/controller.js.map +1 -1
- package/dist/reglow/modules/payment/events.d.ts +22 -0
- package/dist/reglow/modules/payment/events.js +60 -0
- package/dist/reglow/modules/payment/events.js.map +1 -0
- package/dist/reglow/modules/payment/schema.d.ts +89 -0
- package/dist/reglow/modules/payment/schema.js +85 -0
- package/dist/reglow/modules/payment/schema.js.map +1 -1
- package/dist/reglow/modules/payment/service.d.ts +90 -2
- package/dist/reglow/modules/payment/service.js +752 -3
- package/dist/reglow/modules/payment/service.js.map +1 -1
- package/dist/reglow/modules/photo/repository.d.ts +4 -0
- package/dist/reglow/modules/photo/repository.js +13 -0
- package/dist/reglow/modules/photo/repository.js.map +1 -1
- package/dist/reglow/modules/photo/service.d.ts +6 -1
- package/dist/reglow/modules/photo/service.js +13 -3
- package/dist/reglow/modules/photo/service.js.map +1 -1
- package/dist/reglow/modules/studio/mcp/client.js +2 -2
- package/dist/reglow/scripts/schema.js +81 -0
- package/dist/reglow/scripts/schema.js.map +1 -1
- package/package.json +1 -1
|
@@ -94,9 +94,52 @@ function parseForm(body) {
|
|
|
94
94
|
// ─── Provider 基类 ───
|
|
95
95
|
export class PaymentProvider {
|
|
96
96
|
provider_name = "base";
|
|
97
|
+
supportsRefund = true;
|
|
98
|
+
supportsSplit = false;
|
|
99
|
+
supportsClose = true;
|
|
100
|
+
supportedPlatforms = ["web", "native", "miniapp", "mpweb", "app", "h5"];
|
|
101
|
+
/** 渠道能力声明(业务按此渲染退款/分账/关单入口,避免硬编码渠道判断) */
|
|
102
|
+
capabilities() {
|
|
103
|
+
return {
|
|
104
|
+
provider: this.provider_name,
|
|
105
|
+
refund: this.supportsRefund,
|
|
106
|
+
split: this.supportsSplit,
|
|
107
|
+
close: this.supportsClose,
|
|
108
|
+
platforms: this.supportedPlatforms,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
97
111
|
async createTradeByPlatform(outTradeNo, amountCents, subject, notifyUrl, _platform = "web", _openid = "", _buyerId = "", _payerIp = "") {
|
|
98
112
|
return this.createTrade(outTradeNo, amountCents, subject, notifyUrl);
|
|
99
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* 发起退款(原路退回)。
|
|
116
|
+
* @param refundNo 商户退款单号(同一笔退款重试必须复用,渠道侧幂等)
|
|
117
|
+
* @param totalCents 原订单金额(分,微信退款必填)
|
|
118
|
+
*/
|
|
119
|
+
async refund(_outTradeNo, _refundNo, _amountCents, _totalCents, _reason = "") {
|
|
120
|
+
throw new Error("该支付渠道未实现退款");
|
|
121
|
+
}
|
|
122
|
+
async closeTrade(_outTradeNo) {
|
|
123
|
+
throw new Error("该支付渠道未实现关单");
|
|
124
|
+
}
|
|
125
|
+
async queryRefund(_outTradeNo, _refundNo) {
|
|
126
|
+
throw new Error("该支付渠道未实现退款查询");
|
|
127
|
+
}
|
|
128
|
+
async split(_outTradeNo, _splitNo, _receivers, _reason = "", _transactionId = "") {
|
|
129
|
+
throw new Error("该支付渠道未实现分账");
|
|
130
|
+
}
|
|
131
|
+
async querySplit(_outTradeNo, _splitNo, _transactionId = "") {
|
|
132
|
+
throw new Error("该支付渠道未实现分账查询");
|
|
133
|
+
}
|
|
134
|
+
async returnSplit(_outTradeNo, _splitNo, _returnNo, _amountCents, _receiverType, _receiverAccount, _reason = "", _transactionId = "") {
|
|
135
|
+
throw new Error("该支付渠道未实现分账回退");
|
|
136
|
+
}
|
|
137
|
+
async finishSplit(_outTradeNo, _splitNo, _transactionId = "", _reason = "") {
|
|
138
|
+
throw new Error("该支付渠道未实现完结分账");
|
|
139
|
+
}
|
|
140
|
+
async bindSplitReceiver(_receiverType, _account, _name = "", _relationType = "") {
|
|
141
|
+
throw new Error("该支付渠道未实现分账接收方绑定");
|
|
142
|
+
}
|
|
100
143
|
successResponse() {
|
|
101
144
|
return "success";
|
|
102
145
|
}
|
|
@@ -104,6 +147,7 @@ export class PaymentProvider {
|
|
|
104
147
|
// ─── Mock Provider ───
|
|
105
148
|
export class MockPaymentProvider extends PaymentProvider {
|
|
106
149
|
provider_name = "mock";
|
|
150
|
+
supportsSplit = true;
|
|
107
151
|
async createTrade(outTradeNo, amountCents, _subject, _notifyUrl) {
|
|
108
152
|
return {
|
|
109
153
|
success: true,
|
|
@@ -116,7 +160,7 @@ export class MockPaymentProvider extends PaymentProvider {
|
|
|
116
160
|
};
|
|
117
161
|
}
|
|
118
162
|
async createTradeByPlatform(outTradeNo, amountCents, subject, notifyUrl, platform = "web") {
|
|
119
|
-
if (platform === "web") {
|
|
163
|
+
if (platform === "web" || platform === "native") {
|
|
120
164
|
return this.createTrade(outTradeNo, amountCents, subject, notifyUrl);
|
|
121
165
|
}
|
|
122
166
|
return {
|
|
@@ -149,6 +193,104 @@ export class MockPaymentProvider extends PaymentProvider {
|
|
|
149
193
|
raw: {},
|
|
150
194
|
};
|
|
151
195
|
}
|
|
196
|
+
async refund(outTradeNo, refundNo, _amountCents, _totalCents, _reason = "") {
|
|
197
|
+
return {
|
|
198
|
+
success: true,
|
|
199
|
+
msg: "mock 退款成功",
|
|
200
|
+
refundNo,
|
|
201
|
+
outTradeNo,
|
|
202
|
+
channelRefundNo: `mock_refund_${refundNo}`,
|
|
203
|
+
status: "success",
|
|
204
|
+
raw: {},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
async closeTrade(outTradeNo) {
|
|
208
|
+
return {
|
|
209
|
+
...emptyResult(),
|
|
210
|
+
success: true,
|
|
211
|
+
msg: "mock 订单已关闭",
|
|
212
|
+
outTradeNo,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
async queryRefund(outTradeNo, refundNo) {
|
|
216
|
+
return {
|
|
217
|
+
success: true,
|
|
218
|
+
msg: "mock 退款成功",
|
|
219
|
+
refundNo,
|
|
220
|
+
outTradeNo,
|
|
221
|
+
channelRefundNo: `mock_refund_${refundNo}`,
|
|
222
|
+
status: "success",
|
|
223
|
+
raw: {},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
async split(outTradeNo, splitNo, receivers, _reason = "", _transactionId = "") {
|
|
227
|
+
return {
|
|
228
|
+
success: true,
|
|
229
|
+
msg: "mock 分账成功",
|
|
230
|
+
splitNo,
|
|
231
|
+
outTradeNo,
|
|
232
|
+
channelSplitNo: `mock_split_${splitNo}`,
|
|
233
|
+
status: "success",
|
|
234
|
+
receivers: receivers.map((r, i) => ({
|
|
235
|
+
receiver_type: r.receiverType,
|
|
236
|
+
receiverType: r.receiverType,
|
|
237
|
+
account: r.account,
|
|
238
|
+
amount_cents: r.amountCents,
|
|
239
|
+
amountCents: r.amountCents,
|
|
240
|
+
status: "success",
|
|
241
|
+
channel_detail_id: `mock_detail_${splitNo}_${i}`,
|
|
242
|
+
})),
|
|
243
|
+
raw: {},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
async querySplit(outTradeNo, splitNo, _transactionId = "") {
|
|
247
|
+
return {
|
|
248
|
+
success: true,
|
|
249
|
+
msg: "mock 分账成功",
|
|
250
|
+
splitNo,
|
|
251
|
+
outTradeNo,
|
|
252
|
+
channelSplitNo: `mock_split_${splitNo}`,
|
|
253
|
+
status: "success",
|
|
254
|
+
receivers: [],
|
|
255
|
+
raw: {},
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
async returnSplit(outTradeNo, splitNo, returnNo, _amountCents, _receiverType, _receiverAccount, _reason = "", _transactionId = "") {
|
|
259
|
+
return {
|
|
260
|
+
success: true,
|
|
261
|
+
msg: "mock 分账回退成功",
|
|
262
|
+
splitNo,
|
|
263
|
+
outTradeNo,
|
|
264
|
+
channelSplitNo: returnNo,
|
|
265
|
+
status: "success",
|
|
266
|
+
receivers: [],
|
|
267
|
+
raw: {},
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async finishSplit(outTradeNo, splitNo, _transactionId = "", _reason = "") {
|
|
271
|
+
return {
|
|
272
|
+
success: true,
|
|
273
|
+
msg: "mock 分账已完结",
|
|
274
|
+
splitNo,
|
|
275
|
+
outTradeNo,
|
|
276
|
+
channelSplitNo: `mock_split_${splitNo}`,
|
|
277
|
+
status: "finished",
|
|
278
|
+
receivers: [],
|
|
279
|
+
raw: {},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
async bindSplitReceiver(receiverType, account, _name = "", _relationType = "") {
|
|
283
|
+
return {
|
|
284
|
+
success: true,
|
|
285
|
+
msg: "mock 接收方已绑定",
|
|
286
|
+
splitNo: "",
|
|
287
|
+
outTradeNo: "",
|
|
288
|
+
channelSplitNo: "",
|
|
289
|
+
status: "success",
|
|
290
|
+
receivers: [{ receiverType, account, status: "success" }],
|
|
291
|
+
raw: {},
|
|
292
|
+
};
|
|
293
|
+
}
|
|
152
294
|
async verifyCallback(_headers, body) {
|
|
153
295
|
const data = body.length ? JSON.parse(body.toString("utf-8")) : {};
|
|
154
296
|
return {
|
|
@@ -164,6 +306,7 @@ export class MockPaymentProvider extends PaymentProvider {
|
|
|
164
306
|
// ─── 支付宝 Provider ───
|
|
165
307
|
export class AlipayProvider extends PaymentProvider {
|
|
166
308
|
provider_name = "alipay";
|
|
309
|
+
supportsSplit = true;
|
|
167
310
|
appId;
|
|
168
311
|
appPrivateKey;
|
|
169
312
|
alipayPublicKey;
|
|
@@ -225,6 +368,39 @@ export class AlipayProvider extends PaymentProvider {
|
|
|
225
368
|
params["sign"] = this.signParams(params);
|
|
226
369
|
return JSON.parse(await this.alipayPost(params));
|
|
227
370
|
}
|
|
371
|
+
async callAlipayOauth(extra) {
|
|
372
|
+
const params = {
|
|
373
|
+
app_id: this.appId,
|
|
374
|
+
method: "alipay.system.oauth.token",
|
|
375
|
+
charset: "utf-8",
|
|
376
|
+
sign_type: "RSA2",
|
|
377
|
+
timestamp: new Date().toISOString().slice(0, 19).replace("T", " "),
|
|
378
|
+
version: "1.0",
|
|
379
|
+
...extra,
|
|
380
|
+
};
|
|
381
|
+
params["sign"] = this.signParams(params);
|
|
382
|
+
return JSON.parse(await this.alipayPost(params));
|
|
383
|
+
}
|
|
384
|
+
/** 支付宝小程序 JSAPI 支付前置:my.getAuthCode 的 auth_code 换 user_id(buyer_id) */
|
|
385
|
+
async getUserIdByCode(authCode) {
|
|
386
|
+
if (!authCode)
|
|
387
|
+
throw new Error("缺少支付宝授权 code");
|
|
388
|
+
let result;
|
|
389
|
+
try {
|
|
390
|
+
result = await this.callAlipayOauth({
|
|
391
|
+
grant_type: "authorization_code",
|
|
392
|
+
code: authCode,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
catch (e) {
|
|
396
|
+
throw new Error(`支付宝授权请求失败: ${e.message}`);
|
|
397
|
+
}
|
|
398
|
+
const resp = (result["alipay_system_oauth_token_response"] || {});
|
|
399
|
+
const userId = String(resp["user_id"] || "");
|
|
400
|
+
if (resp["code"] === "10000" && userId)
|
|
401
|
+
return userId;
|
|
402
|
+
throw new Error(`支付宝授权失败: ${String(resp["sub_msg"] || resp["msg"] || "未知错误")}`);
|
|
403
|
+
}
|
|
228
404
|
async createTrade(outTradeNo, amountCents, subject, notifyUrl) {
|
|
229
405
|
const amountYuan = (amountCents / 100).toFixed(2);
|
|
230
406
|
const bizContent = JSON.stringify({
|
|
@@ -431,6 +607,13 @@ export class AlipayProvider extends PaymentProvider {
|
|
|
431
607
|
};
|
|
432
608
|
}
|
|
433
609
|
async createTradeByPlatform(outTradeNo, amountCents, subject, notifyUrl, platform = "web", _openid = "", buyerId = "") {
|
|
610
|
+
if (platform === "native") {
|
|
611
|
+
// 原生扫码(当面付 precreate);商户未开通当面付时回退电脑网站支付,保证仍能出二维码
|
|
612
|
+
const precreate = await this.createTrade(outTradeNo, amountCents, subject, notifyUrl);
|
|
613
|
+
if (precreate.success)
|
|
614
|
+
return precreate;
|
|
615
|
+
return this.createPageTrade(outTradeNo, amountCents, subject, notifyUrl, this.returnUrl);
|
|
616
|
+
}
|
|
434
617
|
if (platform === "miniapp") {
|
|
435
618
|
if (!buyerId) {
|
|
436
619
|
return { ...emptyResult(), msg: "支付宝小程序支付需要 buyer_id" };
|
|
@@ -480,6 +663,345 @@ export class AlipayProvider extends PaymentProvider {
|
|
|
480
663
|
return { ...emptyResult(), msg: `查询失败: ${e.message}` };
|
|
481
664
|
}
|
|
482
665
|
}
|
|
666
|
+
async refund(outTradeNo, refundNo, amountCents, _totalCents, reason = "") {
|
|
667
|
+
// 支付宝退款(out_request_no 保证幂等)
|
|
668
|
+
const bizContent = {
|
|
669
|
+
out_trade_no: outTradeNo,
|
|
670
|
+
refund_amount: (amountCents / 100).toFixed(2),
|
|
671
|
+
out_request_no: refundNo,
|
|
672
|
+
};
|
|
673
|
+
if (reason)
|
|
674
|
+
bizContent["refund_reason"] = reason;
|
|
675
|
+
try {
|
|
676
|
+
const result = await this.callAlipay("alipay.trade.refund", bizContent);
|
|
677
|
+
const resp = (result["alipay_trade_refund_response"] || {});
|
|
678
|
+
if (resp["code"] === "10000") {
|
|
679
|
+
return {
|
|
680
|
+
success: true,
|
|
681
|
+
msg: "支付宝退款成功",
|
|
682
|
+
refundNo,
|
|
683
|
+
outTradeNo,
|
|
684
|
+
channelRefundNo: String(resp["trade_no"] || ""),
|
|
685
|
+
status: "success",
|
|
686
|
+
raw: result,
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
const subMsg = String(resp["sub_msg"] || "");
|
|
690
|
+
return {
|
|
691
|
+
success: false,
|
|
692
|
+
msg: `支付宝退款失败: ${subMsg || String(resp["msg"] || "未知错误")}`,
|
|
693
|
+
refundNo,
|
|
694
|
+
outTradeNo,
|
|
695
|
+
channelRefundNo: "",
|
|
696
|
+
status: "failed",
|
|
697
|
+
raw: result,
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
catch (e) {
|
|
701
|
+
return {
|
|
702
|
+
success: false,
|
|
703
|
+
msg: `支付宝退款请求失败: ${e.message}`,
|
|
704
|
+
refundNo,
|
|
705
|
+
outTradeNo,
|
|
706
|
+
channelRefundNo: "",
|
|
707
|
+
status: "failed",
|
|
708
|
+
raw: {},
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
async closeTrade(outTradeNo) {
|
|
713
|
+
try {
|
|
714
|
+
const result = await this.callAlipay("alipay.trade.close", {
|
|
715
|
+
out_trade_no: outTradeNo,
|
|
716
|
+
});
|
|
717
|
+
const resp = (result["alipay_trade_close_response"] || {});
|
|
718
|
+
if (resp["code"] === "10000") {
|
|
719
|
+
return {
|
|
720
|
+
...emptyResult(),
|
|
721
|
+
success: true,
|
|
722
|
+
msg: "支付宝订单已关闭",
|
|
723
|
+
outTradeNo,
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
return {
|
|
727
|
+
...emptyResult(),
|
|
728
|
+
msg: `支付宝关单失败: ${resp["sub_msg"] || resp["msg"] || "未知错误"}`,
|
|
729
|
+
raw: result,
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
catch (e) {
|
|
733
|
+
return {
|
|
734
|
+
...emptyResult(),
|
|
735
|
+
msg: `支付宝关单请求失败: ${e.message}`,
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
async queryRefund(outTradeNo, refundNo) {
|
|
740
|
+
try {
|
|
741
|
+
const result = await this.callAlipay("alipay.trade.fastpay.refund.query", {
|
|
742
|
+
out_trade_no: outTradeNo,
|
|
743
|
+
out_request_no: refundNo,
|
|
744
|
+
});
|
|
745
|
+
const resp = (result["alipay_trade_fastpay_refund_query_response"] ||
|
|
746
|
+
{});
|
|
747
|
+
if (resp["code"] === "10000" &&
|
|
748
|
+
resp["refund_status"] === "REFUND_SUCCESS") {
|
|
749
|
+
return {
|
|
750
|
+
success: true,
|
|
751
|
+
msg: "退款成功",
|
|
752
|
+
refundNo,
|
|
753
|
+
outTradeNo,
|
|
754
|
+
channelRefundNo: String(resp["trade_no"] || ""),
|
|
755
|
+
status: "success",
|
|
756
|
+
raw: result,
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
return {
|
|
760
|
+
success: false,
|
|
761
|
+
msg: `支付宝退款状态: ${resp["refund_status"] || resp["sub_msg"] || "未知"}`,
|
|
762
|
+
refundNo,
|
|
763
|
+
outTradeNo,
|
|
764
|
+
channelRefundNo: "",
|
|
765
|
+
status: "processing",
|
|
766
|
+
raw: result,
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
catch (e) {
|
|
770
|
+
return {
|
|
771
|
+
success: false,
|
|
772
|
+
msg: `支付宝退款查询失败: ${e.message}`,
|
|
773
|
+
refundNo,
|
|
774
|
+
outTradeNo,
|
|
775
|
+
channelRefundNo: "",
|
|
776
|
+
status: "failed",
|
|
777
|
+
raw: {},
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
async bindSplitReceiver(receiverType, account, name = "", relationType = "") {
|
|
782
|
+
const biz = {
|
|
783
|
+
royalty_type: "transfer",
|
|
784
|
+
trans_in_type: receiverType,
|
|
785
|
+
trans_in: account,
|
|
786
|
+
relation_type: relationType || "transfer",
|
|
787
|
+
};
|
|
788
|
+
if (name)
|
|
789
|
+
biz["name"] = name;
|
|
790
|
+
try {
|
|
791
|
+
const result = await this.callAlipay("alipay.trade.royalty.relation.bind", biz);
|
|
792
|
+
const resp = (result["alipay_trade_royalty_relation_bind_response"] ||
|
|
793
|
+
{});
|
|
794
|
+
if (resp["code"] === "10000") {
|
|
795
|
+
return {
|
|
796
|
+
success: true,
|
|
797
|
+
msg: "分账接收方已绑定",
|
|
798
|
+
splitNo: "",
|
|
799
|
+
outTradeNo: "",
|
|
800
|
+
channelSplitNo: "",
|
|
801
|
+
status: "success",
|
|
802
|
+
receivers: [{ receiverType, account, status: "success" }],
|
|
803
|
+
raw: result,
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
return {
|
|
807
|
+
success: false,
|
|
808
|
+
msg: `支付宝分账绑定失败: ${resp["sub_msg"] || resp["msg"] || "未知错误"}`,
|
|
809
|
+
splitNo: "",
|
|
810
|
+
outTradeNo: "",
|
|
811
|
+
channelSplitNo: "",
|
|
812
|
+
status: "failed",
|
|
813
|
+
receivers: [],
|
|
814
|
+
raw: result,
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
catch (e) {
|
|
818
|
+
return {
|
|
819
|
+
success: false,
|
|
820
|
+
msg: `支付宝分账绑定失败: ${e.message}`,
|
|
821
|
+
splitNo: "",
|
|
822
|
+
outTradeNo: "",
|
|
823
|
+
channelSplitNo: "",
|
|
824
|
+
status: "failed",
|
|
825
|
+
receivers: [],
|
|
826
|
+
raw: {},
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
async split(outTradeNo, splitNo, receivers, reason = "", transactionId = "") {
|
|
831
|
+
const royalty = receivers.map((r) => {
|
|
832
|
+
const item = {
|
|
833
|
+
royalty_type: "transfer",
|
|
834
|
+
trans_in_type: r.receiverType,
|
|
835
|
+
trans_in: r.account,
|
|
836
|
+
amount: (r.amountCents / 100).toFixed(2),
|
|
837
|
+
};
|
|
838
|
+
if (r.description)
|
|
839
|
+
item["desc"] = r.description;
|
|
840
|
+
return item;
|
|
841
|
+
});
|
|
842
|
+
try {
|
|
843
|
+
const result = await this.callAlipay("alipay.trade.order.settle", {
|
|
844
|
+
out_request_no: splitNo,
|
|
845
|
+
trade_no: transactionId || outTradeNo,
|
|
846
|
+
royalty_parameters: royalty,
|
|
847
|
+
});
|
|
848
|
+
const resp = (result["alipay_trade_order_settle_response"] ||
|
|
849
|
+
{});
|
|
850
|
+
if (resp["code"] === "10000") {
|
|
851
|
+
return {
|
|
852
|
+
success: true,
|
|
853
|
+
msg: "支付宝分账成功",
|
|
854
|
+
splitNo,
|
|
855
|
+
outTradeNo,
|
|
856
|
+
channelSplitNo: String(resp["trade_no"] || ""),
|
|
857
|
+
status: "success",
|
|
858
|
+
receivers: [],
|
|
859
|
+
raw: result,
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
return {
|
|
863
|
+
success: false,
|
|
864
|
+
msg: `支付宝分账失败: ${resp["sub_msg"] || resp["msg"] || "未知错误"}`,
|
|
865
|
+
splitNo,
|
|
866
|
+
outTradeNo,
|
|
867
|
+
channelSplitNo: "",
|
|
868
|
+
status: "failed",
|
|
869
|
+
receivers: [],
|
|
870
|
+
raw: result,
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
catch (e) {
|
|
874
|
+
return {
|
|
875
|
+
success: false,
|
|
876
|
+
msg: `支付宝分账请求失败: ${e.message}`,
|
|
877
|
+
splitNo,
|
|
878
|
+
outTradeNo,
|
|
879
|
+
channelSplitNo: "",
|
|
880
|
+
status: "failed",
|
|
881
|
+
receivers: [],
|
|
882
|
+
raw: {},
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
async querySplit(outTradeNo, splitNo, _transactionId = "") {
|
|
887
|
+
try {
|
|
888
|
+
const result = await this.callAlipay("alipay.trade.query", {
|
|
889
|
+
out_trade_no: outTradeNo,
|
|
890
|
+
query_options: ["TRADE_SETTLE_INFO"],
|
|
891
|
+
});
|
|
892
|
+
const resp = (result["alipay_trade_query_response"] || {});
|
|
893
|
+
if (resp["code"] !== "10000") {
|
|
894
|
+
return {
|
|
895
|
+
success: false,
|
|
896
|
+
msg: `支付宝分账查询失败: ${resp["sub_msg"] || resp["msg"] || "未知错误"}`,
|
|
897
|
+
splitNo,
|
|
898
|
+
outTradeNo,
|
|
899
|
+
channelSplitNo: "",
|
|
900
|
+
status: "failed",
|
|
901
|
+
receivers: [],
|
|
902
|
+
raw: result,
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
const settleInfo = (resp["trade_settle_info"] || {});
|
|
906
|
+
const details = (settleInfo["settle_detail_list"] || []);
|
|
907
|
+
const mapped = details.map((d) => ({
|
|
908
|
+
receiverType: String(d["trans_in_type"] || ""),
|
|
909
|
+
receiver_type: String(d["trans_in_type"] || ""),
|
|
910
|
+
account: String(d["trans_in"] || ""),
|
|
911
|
+
amountCents: Math.round(Number(d["amount"] || 0) * 100),
|
|
912
|
+
amount_cents: Math.round(Number(d["amount"] || 0) * 100),
|
|
913
|
+
status: d["settle_status"] === "SUCCESS" ? "success" : "pending",
|
|
914
|
+
}));
|
|
915
|
+
return {
|
|
916
|
+
success: true,
|
|
917
|
+
msg: "支付宝分账查询成功",
|
|
918
|
+
splitNo,
|
|
919
|
+
outTradeNo,
|
|
920
|
+
channelSplitNo: "",
|
|
921
|
+
status: mapped.length ? "success" : "processing",
|
|
922
|
+
receivers: mapped,
|
|
923
|
+
raw: result,
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
catch (e) {
|
|
927
|
+
return {
|
|
928
|
+
success: false,
|
|
929
|
+
msg: `支付宝分账查询失败: ${e.message}`,
|
|
930
|
+
splitNo,
|
|
931
|
+
outTradeNo,
|
|
932
|
+
channelSplitNo: "",
|
|
933
|
+
status: "failed",
|
|
934
|
+
receivers: [],
|
|
935
|
+
raw: {},
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
async returnSplit(outTradeNo, splitNo, returnNo, amountCents, receiverType, receiverAccount, reason = "", _transactionId = "") {
|
|
940
|
+
try {
|
|
941
|
+
const result = await this.callAlipay("alipay.trade.refund", {
|
|
942
|
+
out_trade_no: outTradeNo,
|
|
943
|
+
refund_amount: (amountCents / 100).toFixed(2),
|
|
944
|
+
out_request_no: returnNo,
|
|
945
|
+
refund_reason: reason || "分账回退",
|
|
946
|
+
refund_royalty_parameters: [
|
|
947
|
+
{
|
|
948
|
+
royalty_type: "transfer",
|
|
949
|
+
trans_in_type: receiverType,
|
|
950
|
+
trans_in: receiverAccount,
|
|
951
|
+
amount: (amountCents / 100).toFixed(2),
|
|
952
|
+
desc: reason || "分账回退",
|
|
953
|
+
},
|
|
954
|
+
],
|
|
955
|
+
});
|
|
956
|
+
const resp = (result["alipay_trade_refund_response"] || {});
|
|
957
|
+
if (resp["code"] === "10000") {
|
|
958
|
+
return {
|
|
959
|
+
success: true,
|
|
960
|
+
msg: "分账回退成功",
|
|
961
|
+
splitNo,
|
|
962
|
+
outTradeNo,
|
|
963
|
+
channelSplitNo: returnNo,
|
|
964
|
+
status: "success",
|
|
965
|
+
receivers: [],
|
|
966
|
+
raw: result,
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
return {
|
|
970
|
+
success: false,
|
|
971
|
+
msg: `支付宝分账回退失败: ${resp["sub_msg"] || resp["msg"] || "未知错误"}`,
|
|
972
|
+
splitNo,
|
|
973
|
+
outTradeNo,
|
|
974
|
+
channelSplitNo: "",
|
|
975
|
+
status: "failed",
|
|
976
|
+
receivers: [],
|
|
977
|
+
raw: result,
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
catch (e) {
|
|
981
|
+
return {
|
|
982
|
+
success: false,
|
|
983
|
+
msg: `支付宝分账回退失败: ${e.message}`,
|
|
984
|
+
splitNo,
|
|
985
|
+
outTradeNo,
|
|
986
|
+
channelSplitNo: "",
|
|
987
|
+
status: "failed",
|
|
988
|
+
receivers: [],
|
|
989
|
+
raw: {},
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
async finishSplit(outTradeNo, splitNo, _transactionId = "", _reason = "") {
|
|
994
|
+
return {
|
|
995
|
+
success: true,
|
|
996
|
+
msg: "支付宝无需完结分账",
|
|
997
|
+
splitNo,
|
|
998
|
+
outTradeNo,
|
|
999
|
+
channelSplitNo: "",
|
|
1000
|
+
status: "finished",
|
|
1001
|
+
receivers: [],
|
|
1002
|
+
raw: {},
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
483
1005
|
async verifyCallback(_headers, body) {
|
|
484
1006
|
const params = parseForm(body.toString("utf-8"));
|
|
485
1007
|
const sign = params["sign"] || "";
|
|
@@ -527,6 +1049,8 @@ export class AlipayProvider extends PaymentProvider {
|
|
|
527
1049
|
// ─── 银联云闪付 Provider ───
|
|
528
1050
|
export class UnionPayProvider extends PaymentProvider {
|
|
529
1051
|
provider_name = "unionpay";
|
|
1052
|
+
supportsSplit = false;
|
|
1053
|
+
supportsClose = false;
|
|
530
1054
|
static TEST_GATEWAY = "https://gateway.test.95516.com/gateway/api/backTransReq.do";
|
|
531
1055
|
static PROD_GATEWAY = "https://gateway.95516.com/gateway/api/backTransReq.do";
|
|
532
1056
|
merId;
|
|
@@ -701,6 +1225,62 @@ export class UnionPayProvider extends PaymentProvider {
|
|
|
701
1225
|
}
|
|
702
1226
|
return { ...emptyResult(), msg: `状态: ${origRespCode}`, raw: result };
|
|
703
1227
|
}
|
|
1228
|
+
async refund(outTradeNo, refundNo, amountCents, _totalCents, _reason = "") {
|
|
1229
|
+
// 银联退款(txnType=04,orderId 为原商户订单号)
|
|
1230
|
+
const params = {
|
|
1231
|
+
version: "5.1.0",
|
|
1232
|
+
encoding: "utf-8",
|
|
1233
|
+
txnType: "04",
|
|
1234
|
+
txnSubType: "00",
|
|
1235
|
+
bizType: "000000",
|
|
1236
|
+
channelType: "08",
|
|
1237
|
+
merId: this.merId,
|
|
1238
|
+
orderId: outTradeNo,
|
|
1239
|
+
txnAmt: String(amountCents),
|
|
1240
|
+
txnTime: new Date().toISOString().slice(0, 19).replace(/[-:T]/g, ""),
|
|
1241
|
+
backUrl: this.notifyUrl,
|
|
1242
|
+
reqReserved: refundNo,
|
|
1243
|
+
signatureMethod: "SHA256",
|
|
1244
|
+
signature: "",
|
|
1245
|
+
};
|
|
1246
|
+
params["signature"] = this.signParams(params);
|
|
1247
|
+
let result;
|
|
1248
|
+
try {
|
|
1249
|
+
result = await this.post(params);
|
|
1250
|
+
}
|
|
1251
|
+
catch (e) {
|
|
1252
|
+
return {
|
|
1253
|
+
success: false,
|
|
1254
|
+
msg: `银联退款请求失败: ${e.message}`,
|
|
1255
|
+
refundNo,
|
|
1256
|
+
outTradeNo,
|
|
1257
|
+
channelRefundNo: "",
|
|
1258
|
+
status: "failed",
|
|
1259
|
+
raw: {},
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
const respCode = result["respCode"] || "";
|
|
1263
|
+
if (respCode === "00") {
|
|
1264
|
+
return {
|
|
1265
|
+
success: true,
|
|
1266
|
+
msg: "银联退款受理成功",
|
|
1267
|
+
refundNo,
|
|
1268
|
+
outTradeNo,
|
|
1269
|
+
channelRefundNo: result["queryId"] || "",
|
|
1270
|
+
status: "success",
|
|
1271
|
+
raw: result,
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
return {
|
|
1275
|
+
success: false,
|
|
1276
|
+
msg: `银联退款失败: ${result["respMsg"] || "未知错误"} (code=${respCode})`,
|
|
1277
|
+
refundNo,
|
|
1278
|
+
outTradeNo,
|
|
1279
|
+
channelRefundNo: "",
|
|
1280
|
+
status: "failed",
|
|
1281
|
+
raw: result,
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
704
1284
|
async verifyCallback(_headers, body) {
|
|
705
1285
|
const params = parseForm(body.toString("utf-8"));
|
|
706
1286
|
const sign = params["signature"] || "";
|
|
@@ -750,6 +1330,7 @@ export class UnionPayProvider extends PaymentProvider {
|
|
|
750
1330
|
// ─── 微信支付 V3 Provider ───
|
|
751
1331
|
export class WeChatPayProvider extends PaymentProvider {
|
|
752
1332
|
provider_name = "wechat";
|
|
1333
|
+
supportsSplit = true;
|
|
753
1334
|
static BASE_URL = "https://api.mch.weixin.qq.com";
|
|
754
1335
|
appid;
|
|
755
1336
|
miniAppId;
|
|
@@ -1095,6 +1676,449 @@ export class WeChatPayProvider extends PaymentProvider {
|
|
|
1095
1676
|
}
|
|
1096
1677
|
return { ...emptyResult(), msg: `状态: ${tradeState}`, raw: result };
|
|
1097
1678
|
}
|
|
1679
|
+
async refund(outTradeNo, refundNo, amountCents, totalCents, reason = "") {
|
|
1680
|
+
// 微信支付 V3 退款(out_refund_no 保证幂等)
|
|
1681
|
+
const urlPath = "/v3/refund/domestic/refunds";
|
|
1682
|
+
let result;
|
|
1683
|
+
try {
|
|
1684
|
+
result = await this.v3Post(urlPath, {
|
|
1685
|
+
out_trade_no: outTradeNo,
|
|
1686
|
+
out_refund_no: refundNo,
|
|
1687
|
+
reason: reason || "商户退款",
|
|
1688
|
+
amount: {
|
|
1689
|
+
refund: amountCents,
|
|
1690
|
+
total: totalCents,
|
|
1691
|
+
currency: "CNY",
|
|
1692
|
+
},
|
|
1693
|
+
});
|
|
1694
|
+
}
|
|
1695
|
+
catch (e) {
|
|
1696
|
+
return {
|
|
1697
|
+
success: false,
|
|
1698
|
+
msg: `微信退款请求失败: ${e.message}`,
|
|
1699
|
+
refundNo,
|
|
1700
|
+
outTradeNo,
|
|
1701
|
+
channelRefundNo: "",
|
|
1702
|
+
status: "failed",
|
|
1703
|
+
raw: {},
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
const status = String(result["status"] || "");
|
|
1707
|
+
if (result["refund_id"] &&
|
|
1708
|
+
(status === "SUCCESS" || status === "PROCESSING")) {
|
|
1709
|
+
return {
|
|
1710
|
+
success: true,
|
|
1711
|
+
msg: status === "SUCCESS" ? "微信退款成功" : "微信退款受理中",
|
|
1712
|
+
refundNo,
|
|
1713
|
+
outTradeNo,
|
|
1714
|
+
channelRefundNo: String(result["refund_id"] || ""),
|
|
1715
|
+
status: status === "SUCCESS" ? "success" : "processing",
|
|
1716
|
+
raw: result,
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
return {
|
|
1720
|
+
success: false,
|
|
1721
|
+
msg: `微信退款失败: ${result["message"] || "未知错误"} (code=${result["code"] || ""})`,
|
|
1722
|
+
refundNo,
|
|
1723
|
+
outTradeNo,
|
|
1724
|
+
channelRefundNo: "",
|
|
1725
|
+
status: "failed",
|
|
1726
|
+
raw: result,
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
async closeTrade(outTradeNo) {
|
|
1730
|
+
const urlPath = `/v3/pay/transactions/out-trade-no/${outTradeNo}/close`;
|
|
1731
|
+
const body = JSON.stringify({ mchid: this.mchid });
|
|
1732
|
+
const auth = this.buildAuth("POST", urlPath, body);
|
|
1733
|
+
try {
|
|
1734
|
+
const resp = await fetch(`${WeChatPayProvider.BASE_URL}${urlPath}`, {
|
|
1735
|
+
method: "POST",
|
|
1736
|
+
headers: {
|
|
1737
|
+
Authorization: auth,
|
|
1738
|
+
"Content-Type": "application/json",
|
|
1739
|
+
Accept: "application/json",
|
|
1740
|
+
},
|
|
1741
|
+
body,
|
|
1742
|
+
});
|
|
1743
|
+
if (resp.status === 200 || resp.status === 204) {
|
|
1744
|
+
return {
|
|
1745
|
+
...emptyResult(),
|
|
1746
|
+
success: true,
|
|
1747
|
+
msg: "微信订单已关闭",
|
|
1748
|
+
outTradeNo,
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
const data = (await resp.json());
|
|
1752
|
+
return {
|
|
1753
|
+
...emptyResult(),
|
|
1754
|
+
msg: `微信关单失败: ${data["message"] || "未知错误"} (code=${data["code"] || ""})`,
|
|
1755
|
+
raw: data,
|
|
1756
|
+
};
|
|
1757
|
+
}
|
|
1758
|
+
catch (e) {
|
|
1759
|
+
return {
|
|
1760
|
+
...emptyResult(),
|
|
1761
|
+
msg: `微信关单请求失败: ${e.message}`,
|
|
1762
|
+
};
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
async v3Get(urlPath) {
|
|
1766
|
+
const auth = this.buildAuth("GET", urlPath, "");
|
|
1767
|
+
const resp = await fetch(`${WeChatPayProvider.BASE_URL}${urlPath}`, {
|
|
1768
|
+
method: "GET",
|
|
1769
|
+
headers: { Authorization: auth, Accept: "application/json" },
|
|
1770
|
+
});
|
|
1771
|
+
return (await resp.json());
|
|
1772
|
+
}
|
|
1773
|
+
async queryRefund(outTradeNo, refundNo) {
|
|
1774
|
+
try {
|
|
1775
|
+
const result = await this.v3Get(`/v3/refund/domestic/refunds/${refundNo}`);
|
|
1776
|
+
const status = String(result["status"] || "");
|
|
1777
|
+
if (status === "SUCCESS") {
|
|
1778
|
+
return {
|
|
1779
|
+
success: true,
|
|
1780
|
+
msg: "退款成功",
|
|
1781
|
+
refundNo,
|
|
1782
|
+
outTradeNo,
|
|
1783
|
+
channelRefundNo: String(result["refund_id"] || ""),
|
|
1784
|
+
status: "success",
|
|
1785
|
+
raw: result,
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1788
|
+
return {
|
|
1789
|
+
success: false,
|
|
1790
|
+
msg: `微信退款状态: ${status}`,
|
|
1791
|
+
refundNo,
|
|
1792
|
+
outTradeNo,
|
|
1793
|
+
channelRefundNo: String(result["refund_id"] || ""),
|
|
1794
|
+
status: status === "PROCESSING" || status === "ABNORMAL"
|
|
1795
|
+
? "processing"
|
|
1796
|
+
: "failed",
|
|
1797
|
+
raw: result,
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
catch (e) {
|
|
1801
|
+
return {
|
|
1802
|
+
success: false,
|
|
1803
|
+
msg: `微信退款查询失败: ${e.message}`,
|
|
1804
|
+
refundNo,
|
|
1805
|
+
outTradeNo,
|
|
1806
|
+
channelRefundNo: "",
|
|
1807
|
+
status: "failed",
|
|
1808
|
+
raw: {},
|
|
1809
|
+
};
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
async bindSplitReceiver(receiverType, account, name = "", relationType = "") {
|
|
1813
|
+
const body = {
|
|
1814
|
+
appid: this.appid,
|
|
1815
|
+
type: receiverType,
|
|
1816
|
+
account,
|
|
1817
|
+
};
|
|
1818
|
+
if (name)
|
|
1819
|
+
body["name"] = name;
|
|
1820
|
+
if (relationType)
|
|
1821
|
+
body["relation_type"] = relationType;
|
|
1822
|
+
try {
|
|
1823
|
+
const result = await this.v3Post("/v3/profitsharing/receivers/add", body);
|
|
1824
|
+
if (result["account"] || result["type"]) {
|
|
1825
|
+
return {
|
|
1826
|
+
success: true,
|
|
1827
|
+
msg: "分账接收方已绑定",
|
|
1828
|
+
splitNo: "",
|
|
1829
|
+
outTradeNo: "",
|
|
1830
|
+
channelSplitNo: "",
|
|
1831
|
+
status: "success",
|
|
1832
|
+
receivers: [{ receiverType, account, status: "success" }],
|
|
1833
|
+
raw: result,
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
return {
|
|
1837
|
+
success: false,
|
|
1838
|
+
msg: `微信分账绑定失败: ${result["message"] || "未知错误"} (code=${result["code"] || ""})`,
|
|
1839
|
+
splitNo: "",
|
|
1840
|
+
outTradeNo: "",
|
|
1841
|
+
channelSplitNo: "",
|
|
1842
|
+
status: "failed",
|
|
1843
|
+
receivers: [],
|
|
1844
|
+
raw: result,
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
catch (e) {
|
|
1848
|
+
return {
|
|
1849
|
+
success: false,
|
|
1850
|
+
msg: `微信分账绑定失败: ${e.message}`,
|
|
1851
|
+
splitNo: "",
|
|
1852
|
+
outTradeNo: "",
|
|
1853
|
+
channelSplitNo: "",
|
|
1854
|
+
status: "failed",
|
|
1855
|
+
receivers: [],
|
|
1856
|
+
raw: {},
|
|
1857
|
+
};
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
mapWechatReceivers(items) {
|
|
1861
|
+
const mapping = {
|
|
1862
|
+
SUCCESS: "success",
|
|
1863
|
+
PENDING: "pending",
|
|
1864
|
+
CLOSED: "failed",
|
|
1865
|
+
};
|
|
1866
|
+
return items.map((i) => ({
|
|
1867
|
+
receiverType: String(i["type"] || ""),
|
|
1868
|
+
receiver_type: String(i["type"] || ""),
|
|
1869
|
+
account: String(i["account"] || ""),
|
|
1870
|
+
amountCents: Number(i["amount"] || 0),
|
|
1871
|
+
amount_cents: Number(i["amount"] || 0),
|
|
1872
|
+
status: mapping[String(i["result"] || "")] || "pending",
|
|
1873
|
+
channel_detail_id: String(i["detail_id"] || ""),
|
|
1874
|
+
error_msg: String(i["fail_reason"] || ""),
|
|
1875
|
+
}));
|
|
1876
|
+
}
|
|
1877
|
+
async split(outTradeNo, splitNo, receivers, reason = "", transactionId = "") {
|
|
1878
|
+
if (!transactionId) {
|
|
1879
|
+
return {
|
|
1880
|
+
success: false,
|
|
1881
|
+
msg: "微信分账需要渠道交易号 transaction_id",
|
|
1882
|
+
splitNo,
|
|
1883
|
+
outTradeNo,
|
|
1884
|
+
channelSplitNo: "",
|
|
1885
|
+
status: "failed",
|
|
1886
|
+
receivers: [],
|
|
1887
|
+
raw: {},
|
|
1888
|
+
};
|
|
1889
|
+
}
|
|
1890
|
+
try {
|
|
1891
|
+
const result = await this.v3Post("/v3/profitsharing/orders", {
|
|
1892
|
+
appid: this.appid,
|
|
1893
|
+
transaction_id: transactionId,
|
|
1894
|
+
out_order_no: splitNo,
|
|
1895
|
+
receivers: receivers.map((r) => ({
|
|
1896
|
+
type: r.receiverType,
|
|
1897
|
+
account: r.account,
|
|
1898
|
+
amount: r.amountCents,
|
|
1899
|
+
description: r.description || reason || "分账",
|
|
1900
|
+
})),
|
|
1901
|
+
unfreeze_unsplit: false,
|
|
1902
|
+
});
|
|
1903
|
+
const status = String(result["status"] || "");
|
|
1904
|
+
if (result["order_id"] &&
|
|
1905
|
+
(status === "PROCESSING" || status === "FINISHED")) {
|
|
1906
|
+
return {
|
|
1907
|
+
success: true,
|
|
1908
|
+
msg: status === "FINISHED" ? "微信分账成功" : "微信分账受理中",
|
|
1909
|
+
splitNo,
|
|
1910
|
+
outTradeNo,
|
|
1911
|
+
channelSplitNo: String(result["order_id"] || ""),
|
|
1912
|
+
status: status === "FINISHED" ? "success" : "processing",
|
|
1913
|
+
receivers: this.mapWechatReceivers((result["receivers"] || [])),
|
|
1914
|
+
raw: result,
|
|
1915
|
+
};
|
|
1916
|
+
}
|
|
1917
|
+
return {
|
|
1918
|
+
success: false,
|
|
1919
|
+
msg: `微信分账失败: ${result["message"] || "未知错误"} (code=${result["code"] || ""})`,
|
|
1920
|
+
splitNo,
|
|
1921
|
+
outTradeNo,
|
|
1922
|
+
channelSplitNo: "",
|
|
1923
|
+
status: "failed",
|
|
1924
|
+
receivers: [],
|
|
1925
|
+
raw: result,
|
|
1926
|
+
};
|
|
1927
|
+
}
|
|
1928
|
+
catch (e) {
|
|
1929
|
+
return {
|
|
1930
|
+
success: false,
|
|
1931
|
+
msg: `微信分账请求失败: ${e.message}`,
|
|
1932
|
+
splitNo,
|
|
1933
|
+
outTradeNo,
|
|
1934
|
+
channelSplitNo: "",
|
|
1935
|
+
status: "failed",
|
|
1936
|
+
receivers: [],
|
|
1937
|
+
raw: {},
|
|
1938
|
+
};
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
async querySplit(outTradeNo, splitNo, transactionId = "") {
|
|
1942
|
+
if (!transactionId) {
|
|
1943
|
+
return {
|
|
1944
|
+
success: false,
|
|
1945
|
+
msg: "微信分账查询需要渠道交易号 transaction_id",
|
|
1946
|
+
splitNo,
|
|
1947
|
+
outTradeNo,
|
|
1948
|
+
channelSplitNo: "",
|
|
1949
|
+
status: "failed",
|
|
1950
|
+
receivers: [],
|
|
1951
|
+
raw: {},
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
try {
|
|
1955
|
+
const result = await this.v3Get(`/v3/profitsharing/orders/${splitNo}?transaction_id=${transactionId}`);
|
|
1956
|
+
const status = String(result["status"] || "");
|
|
1957
|
+
if (result["order_id"] &&
|
|
1958
|
+
(status === "PROCESSING" || status === "FINISHED")) {
|
|
1959
|
+
return {
|
|
1960
|
+
success: true,
|
|
1961
|
+
msg: "微信分账查询成功",
|
|
1962
|
+
splitNo,
|
|
1963
|
+
outTradeNo,
|
|
1964
|
+
channelSplitNo: String(result["order_id"] || ""),
|
|
1965
|
+
status: status === "FINISHED" ? "success" : "processing",
|
|
1966
|
+
receivers: this.mapWechatReceivers((result["receivers"] || [])),
|
|
1967
|
+
raw: result,
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
return {
|
|
1971
|
+
success: false,
|
|
1972
|
+
msg: `微信分账查询失败: ${result["message"] || "未知错误"} (code=${result["code"] || ""})`,
|
|
1973
|
+
splitNo,
|
|
1974
|
+
outTradeNo,
|
|
1975
|
+
channelSplitNo: "",
|
|
1976
|
+
status: "failed",
|
|
1977
|
+
receivers: [],
|
|
1978
|
+
raw: result,
|
|
1979
|
+
};
|
|
1980
|
+
}
|
|
1981
|
+
catch (e) {
|
|
1982
|
+
return {
|
|
1983
|
+
success: false,
|
|
1984
|
+
msg: `微信分账查询失败: ${e.message}`,
|
|
1985
|
+
splitNo,
|
|
1986
|
+
outTradeNo,
|
|
1987
|
+
channelSplitNo: "",
|
|
1988
|
+
status: "failed",
|
|
1989
|
+
receivers: [],
|
|
1990
|
+
raw: {},
|
|
1991
|
+
};
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
async returnSplit(outTradeNo, splitNo, returnNo, amountCents, receiverType, receiverAccount, reason = "", transactionId = "") {
|
|
1995
|
+
if (!transactionId) {
|
|
1996
|
+
return {
|
|
1997
|
+
success: false,
|
|
1998
|
+
msg: "微信分账回退需要渠道交易号 transaction_id",
|
|
1999
|
+
splitNo,
|
|
2000
|
+
outTradeNo,
|
|
2001
|
+
channelSplitNo: "",
|
|
2002
|
+
status: "failed",
|
|
2003
|
+
receivers: [],
|
|
2004
|
+
raw: {},
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
if (receiverType !== "MERCHANT_ID") {
|
|
2008
|
+
return {
|
|
2009
|
+
success: false,
|
|
2010
|
+
msg: "微信分账回退仅支持商户号接收方(MERCHANT_ID)",
|
|
2011
|
+
splitNo,
|
|
2012
|
+
outTradeNo,
|
|
2013
|
+
channelSplitNo: "",
|
|
2014
|
+
status: "failed",
|
|
2015
|
+
receivers: [],
|
|
2016
|
+
raw: {},
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
try {
|
|
2020
|
+
const result = await this.v3Post("/v3/profitsharing/return-orders", {
|
|
2021
|
+
out_order_no: splitNo,
|
|
2022
|
+
out_return_no: returnNo,
|
|
2023
|
+
transaction_id: transactionId,
|
|
2024
|
+
return_mchid: receiverAccount,
|
|
2025
|
+
amount: amountCents,
|
|
2026
|
+
description: reason || "分账回退",
|
|
2027
|
+
});
|
|
2028
|
+
const status = String(result["status"] || "");
|
|
2029
|
+
if (result["return_id"] ||
|
|
2030
|
+
status === "PROCESSING" ||
|
|
2031
|
+
status === "SUCCESS") {
|
|
2032
|
+
return {
|
|
2033
|
+
success: true,
|
|
2034
|
+
msg: status === "SUCCESS" ? "分账回退成功" : "分账回退受理中",
|
|
2035
|
+
splitNo,
|
|
2036
|
+
outTradeNo,
|
|
2037
|
+
channelSplitNo: String(result["return_id"] || ""),
|
|
2038
|
+
status: status === "SUCCESS" ? "success" : "processing",
|
|
2039
|
+
receivers: [],
|
|
2040
|
+
raw: result,
|
|
2041
|
+
};
|
|
2042
|
+
}
|
|
2043
|
+
return {
|
|
2044
|
+
success: false,
|
|
2045
|
+
msg: `微信分账回退失败: ${result["message"] || "未知错误"} (code=${result["code"] || ""})`,
|
|
2046
|
+
splitNo,
|
|
2047
|
+
outTradeNo,
|
|
2048
|
+
channelSplitNo: "",
|
|
2049
|
+
status: "failed",
|
|
2050
|
+
receivers: [],
|
|
2051
|
+
raw: result,
|
|
2052
|
+
};
|
|
2053
|
+
}
|
|
2054
|
+
catch (e) {
|
|
2055
|
+
return {
|
|
2056
|
+
success: false,
|
|
2057
|
+
msg: `微信分账回退失败: ${e.message}`,
|
|
2058
|
+
splitNo,
|
|
2059
|
+
outTradeNo,
|
|
2060
|
+
channelSplitNo: "",
|
|
2061
|
+
status: "failed",
|
|
2062
|
+
receivers: [],
|
|
2063
|
+
raw: {},
|
|
2064
|
+
};
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
async finishSplit(outTradeNo, splitNo, transactionId = "", reason = "") {
|
|
2068
|
+
if (!transactionId) {
|
|
2069
|
+
return {
|
|
2070
|
+
success: false,
|
|
2071
|
+
msg: "微信完结分账需要渠道交易号 transaction_id",
|
|
2072
|
+
splitNo,
|
|
2073
|
+
outTradeNo,
|
|
2074
|
+
channelSplitNo: "",
|
|
2075
|
+
status: "failed",
|
|
2076
|
+
receivers: [],
|
|
2077
|
+
raw: {},
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
try {
|
|
2081
|
+
const result = await this.v3Post("/v3/profitsharing/finish-orders", {
|
|
2082
|
+
transaction_id: transactionId,
|
|
2083
|
+
out_order_no: splitNo,
|
|
2084
|
+
description: reason || "分账完结",
|
|
2085
|
+
});
|
|
2086
|
+
if (result["order_id"] || !result["code"]) {
|
|
2087
|
+
return {
|
|
2088
|
+
success: true,
|
|
2089
|
+
msg: "分账已完结",
|
|
2090
|
+
splitNo,
|
|
2091
|
+
outTradeNo,
|
|
2092
|
+
channelSplitNo: String(result["order_id"] || ""),
|
|
2093
|
+
status: "finished",
|
|
2094
|
+
receivers: [],
|
|
2095
|
+
raw: result,
|
|
2096
|
+
};
|
|
2097
|
+
}
|
|
2098
|
+
return {
|
|
2099
|
+
success: false,
|
|
2100
|
+
msg: `微信完结分账失败: ${result["message"] || "未知错误"} (code=${result["code"] || ""})`,
|
|
2101
|
+
splitNo,
|
|
2102
|
+
outTradeNo,
|
|
2103
|
+
channelSplitNo: "",
|
|
2104
|
+
status: "failed",
|
|
2105
|
+
receivers: [],
|
|
2106
|
+
raw: result,
|
|
2107
|
+
};
|
|
2108
|
+
}
|
|
2109
|
+
catch (e) {
|
|
2110
|
+
return {
|
|
2111
|
+
success: false,
|
|
2112
|
+
msg: `微信完结分账失败: ${e.message}`,
|
|
2113
|
+
splitNo,
|
|
2114
|
+
outTradeNo,
|
|
2115
|
+
channelSplitNo: "",
|
|
2116
|
+
status: "failed",
|
|
2117
|
+
receivers: [],
|
|
2118
|
+
raw: {},
|
|
2119
|
+
};
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
1098
2122
|
async verifyCallback(_headers, body) {
|
|
1099
2123
|
let bodyJson;
|
|
1100
2124
|
try {
|
|
@@ -1164,18 +2188,43 @@ export class WeChatPayProvider extends PaymentProvider {
|
|
|
1164
2188
|
return JSON.stringify({ code: "SUCCESS", message: "成功" });
|
|
1165
2189
|
}
|
|
1166
2190
|
}
|
|
1167
|
-
|
|
2191
|
+
const paymentProviders = new Map();
|
|
2192
|
+
/** 注册/覆盖支付渠道;注册后通用回调 /payment/notify/{name} 自动生效 */
|
|
2193
|
+
export function registerPaymentProvider(name, factory) {
|
|
2194
|
+
paymentProviders.set(name, factory);
|
|
2195
|
+
}
|
|
2196
|
+
export function unregisterPaymentProvider(name) {
|
|
2197
|
+
paymentProviders.delete(name);
|
|
2198
|
+
}
|
|
2199
|
+
export function listPaymentProviders() {
|
|
2200
|
+
return [...paymentProviders.keys()].sort();
|
|
2201
|
+
}
|
|
1168
2202
|
export function createPaymentProvider(providerName, config) {
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
2203
|
+
const factory = paymentProviders.get(providerName);
|
|
2204
|
+
if (!factory) {
|
|
2205
|
+
throw new Error(`不支持的支付服务商: ${providerName}(已注册: ${listPaymentProviders().join(", ")})`);
|
|
2206
|
+
}
|
|
2207
|
+
return factory(config);
|
|
2208
|
+
}
|
|
2209
|
+
export function getProviderCapabilities(providerName, config) {
|
|
2210
|
+
try {
|
|
2211
|
+
return createPaymentProvider(providerName, config).capabilities();
|
|
2212
|
+
}
|
|
2213
|
+
catch {
|
|
2214
|
+
return {
|
|
2215
|
+
provider: providerName,
|
|
2216
|
+
refund: false,
|
|
2217
|
+
split: false,
|
|
2218
|
+
close: false,
|
|
2219
|
+
platforms: [],
|
|
2220
|
+
};
|
|
2221
|
+
}
|
|
1178
2222
|
}
|
|
2223
|
+
// 内置渠道注册
|
|
2224
|
+
registerPaymentProvider("mock", () => new MockPaymentProvider());
|
|
2225
|
+
registerPaymentProvider("alipay", (config) => new AlipayProvider(config));
|
|
2226
|
+
registerPaymentProvider("wechat", (config) => new WeChatPayProvider(config));
|
|
2227
|
+
registerPaymentProvider("unionpay", (config) => new UnionPayProvider(config));
|
|
1179
2228
|
export async function getPaymentSettings() {
|
|
1180
2229
|
const rows = (await knex(ModelRegistry.get("SysSetting"))
|
|
1181
2230
|
.where("group_key", "payment")
|
|
@@ -1186,6 +2235,9 @@ export async function getPaymentSettings() {
|
|
|
1186
2235
|
return map;
|
|
1187
2236
|
}
|
|
1188
2237
|
export function getActiveProvider(config) {
|
|
2238
|
+
const explicit = (config["active_provider"] || "").trim();
|
|
2239
|
+
if (explicit && paymentProviders.has(explicit))
|
|
2240
|
+
return explicit;
|
|
1189
2241
|
if (config["wechat_enabled"] === "true")
|
|
1190
2242
|
return "wechat";
|
|
1191
2243
|
if (config["alipay_enabled"] === "true")
|