|
|
@@ -211,35 +211,13 @@ export class PaymentMtService extends GenericCrudService<PaymentMtEntity> {
|
|
|
rawBody
|
|
|
});
|
|
|
|
|
|
- // 首先从回调数据中获取商户订单号
|
|
|
- const outTradeNo = callbackData.out_trade_no;
|
|
|
- if (!outTradeNo) {
|
|
|
- console.debug('回调数据中缺少商户订单号,回调数据:', callbackData);
|
|
|
- throw new Error('回调数据中缺少商户订单号');
|
|
|
- }
|
|
|
-
|
|
|
- console.debug(`开始处理支付回调,商户订单号: ${outTradeNo}`);
|
|
|
-
|
|
|
- // 从数据库中查找支付记录以获取租户ID
|
|
|
- const paymentRepository = this.dataSource.getRepository(PaymentMtEntity);
|
|
|
- const payment = await paymentRepository.findOne({
|
|
|
- where: { outTradeNo }
|
|
|
- });
|
|
|
-
|
|
|
- if (!payment) {
|
|
|
- console.debug(`支付记录不存在,商户订单号: ${outTradeNo}`);
|
|
|
- throw new Error('支付记录不存在');
|
|
|
- }
|
|
|
-
|
|
|
- console.debug(`[租户${payment.tenantId}] 找到支付记录,外部订单ID: ${payment.externalOrderId}, 当前支付状态: ${payment.paymentStatus}`);
|
|
|
-
|
|
|
- // 重新初始化微信支付SDK
|
|
|
- console.debug(`[租户${payment.tenantId}] 开始初始化微信支付SDK`);
|
|
|
- await this.initializeWxPay(payment.tenantId);
|
|
|
- console.debug(`[租户${payment.tenantId}] 微信支付SDK初始化完成`);
|
|
|
+ // 重新初始化微信支付SDK(先初始化以进行签名验证和解密)
|
|
|
+ console.debug(`开始初始化微信支付SDK`);
|
|
|
+ await this.initializeWxPay(1); // 先使用默认租户ID进行初始化
|
|
|
+ console.debug(`微信支付SDK初始化完成`);
|
|
|
|
|
|
// 验证回调签名
|
|
|
- console.debug(`[租户${payment.tenantId}] 开始验证回调签名`);
|
|
|
+ console.debug(`开始验证回调签名`);
|
|
|
const isValid = await this.wxPay.verifySign({
|
|
|
timestamp: headers['wechatpay-timestamp'],
|
|
|
nonce: headers['wechatpay-nonce'],
|
|
|
@@ -248,25 +226,25 @@ export class PaymentMtService extends GenericCrudService<PaymentMtEntity> {
|
|
|
signature: headers['wechatpay-signature']
|
|
|
});
|
|
|
|
|
|
- console.debug(`[租户${payment.tenantId}] 回调签名验证结果:`, isValid);
|
|
|
+ console.debug(`回调签名验证结果:`, isValid);
|
|
|
|
|
|
if (!isValid) {
|
|
|
- console.debug(`[租户${payment.tenantId}] 回调签名验证失败,headers:`, headers);
|
|
|
+ console.debug(`回调签名验证失败,headers:`, headers);
|
|
|
throw new Error('回调签名验证失败');
|
|
|
}
|
|
|
|
|
|
- console.debug(`[租户${payment.tenantId}] 回调签名验证成功`);
|
|
|
+ console.debug(`回调签名验证成功`);
|
|
|
|
|
|
// 解密回调数据
|
|
|
- console.debug(`[租户${payment.tenantId}] 开始解密回调数据`);
|
|
|
+ console.debug(`开始解密回调数据`);
|
|
|
const decryptedData = this.wxPay.decipher_gcm(
|
|
|
callbackData.resource.ciphertext,
|
|
|
callbackData.resource.associated_data || '',
|
|
|
callbackData.resource.nonce
|
|
|
);
|
|
|
|
|
|
- console.debug(`[租户${payment.tenantId}] 解密回调数据:`, decryptedData);
|
|
|
- console.debug(`[租户${payment.tenantId}] 解密回调数据类型:`, typeof decryptedData);
|
|
|
+ console.debug(`解密回调数据:`, decryptedData);
|
|
|
+ console.debug(`解密回调数据类型:`, typeof decryptedData);
|
|
|
|
|
|
// 处理解密后的数据,可能是字符串或对象
|
|
|
let parsedData;
|
|
|
@@ -276,7 +254,34 @@ export class PaymentMtService extends GenericCrudService<PaymentMtEntity> {
|
|
|
parsedData = decryptedData;
|
|
|
}
|
|
|
|
|
|
- console.debug(`[租户${payment.tenantId}] 解析后的回调数据:`, parsedData);
|
|
|
+ console.debug(`解析后的回调数据:`, parsedData);
|
|
|
+
|
|
|
+ // 从解密后的数据中获取商户订单号
|
|
|
+ const outTradeNo = parsedData.out_trade_no;
|
|
|
+ if (!outTradeNo) {
|
|
|
+ console.debug('解密后的回调数据中缺少商户订单号,回调数据:', parsedData);
|
|
|
+ throw new Error('回调数据中缺少商户订单号');
|
|
|
+ }
|
|
|
+
|
|
|
+ console.debug(`开始处理支付回调,商户订单号: ${outTradeNo}`);
|
|
|
+
|
|
|
+ // 从数据库中查找支付记录以获取租户ID
|
|
|
+ const paymentRepository = this.dataSource.getRepository(PaymentMtEntity);
|
|
|
+ const payment = await paymentRepository.findOne({
|
|
|
+ where: { outTradeNo }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!payment) {
|
|
|
+ console.debug(`支付记录不存在,商户订单号: ${outTradeNo}`);
|
|
|
+ throw new Error('支付记录不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ console.debug(`[租户${payment.tenantId}] 找到支付记录,外部订单ID: ${payment.externalOrderId}, 当前支付状态: ${payment.paymentStatus}`);
|
|
|
+
|
|
|
+ // 重新使用正确的租户ID初始化微信支付SDK
|
|
|
+ console.debug(`[租户${payment.tenantId}] 重新初始化微信支付SDK`);
|
|
|
+ await this.initializeWxPay(payment.tenantId);
|
|
|
+ console.debug(`[租户${payment.tenantId}] 微信支付SDK重新初始化完成`);
|
|
|
|
|
|
// 根据回调结果更新支付状态
|
|
|
const originalPaymentStatus = payment.paymentStatus;
|