fix(服务商模式支付管理): 服务商支付流程打通
This commit is contained in:
@@ -57,3 +57,37 @@ UPDATE qrcodes SET material_count = 1 WHERE material_count IS NULL;
|
|||||||
-- - material_count > 0:表示该二维码需要上传的素材数量
|
-- - material_count > 0:表示该二维码需要上传的素材数量
|
||||||
-- - 当订单的文件数量达到或超过此数量时,订单状态将自动更新为"素材上传完成"
|
-- - 当订单的文件数量达到或超过此数量时,订单状态将自动更新为"素材上传完成"
|
||||||
-- - 默认值为1,表示每个二维码至少需要1个素材文件
|
-- - 默认值为1,表示每个二维码至少需要1个素材文件
|
||||||
|
|
||||||
|
-- ========== 微信平台证书序列号字段迁移脚本 ==========
|
||||||
|
-- 执行时间:2024-01-XX
|
||||||
|
-- 说明:为sys_wechat_config表添加微信平台证书序列号字段
|
||||||
|
|
||||||
|
-- 1. 添加微信平台证书序列号字段
|
||||||
|
ALTER TABLE sys_wechat_config ADD COLUMN serial_number VARCHAR(64) COMMENT '微信平台证书序列号(用于回调验证)';
|
||||||
|
|
||||||
|
-- 2. 创建索引以提高查询性能
|
||||||
|
CREATE INDEX idx_wechat_config_serial_number ON sys_wechat_config(serial_number);
|
||||||
|
|
||||||
|
-- 3. 添加注释说明
|
||||||
|
-- 微信平台证书序列号字段说明:
|
||||||
|
-- - serial_number:微信平台证书的序列号,用于验证回调请求的签名
|
||||||
|
-- - 在微信支付回调时,通过此字段可以快速定位对应的配置信息
|
||||||
|
-- - 与回调请求头中的 Wechatpay-Serial 字段对应
|
||||||
|
-- - 支持一个商户配置多个平台证书,通过序列号区分
|
||||||
|
|
||||||
|
-- ========== wechat_config表微信平台证书序列号字段迁移脚本 ==========
|
||||||
|
-- 执行时间:2024-01-XX
|
||||||
|
-- 说明:为wechat_config表添加微信平台证书序列号字段
|
||||||
|
|
||||||
|
-- 1. 添加微信平台证书序列号字段
|
||||||
|
ALTER TABLE wechat_config ADD COLUMN serial_number VARCHAR(64) COMMENT '微信平台证书序列号(用于回调验证)';
|
||||||
|
|
||||||
|
-- 2. 创建索引以提高查询性能
|
||||||
|
CREATE INDEX idx_wechat_config_serial_number_2 ON wechat_config(serial_number);
|
||||||
|
|
||||||
|
-- 3. 添加注释说明
|
||||||
|
-- 微信平台证书序列号字段说明:
|
||||||
|
-- - serial_number:微信平台证书的序列号,用于验证回调请求的签名
|
||||||
|
-- - 在微信支付回调时,通过此字段可以快速定位对应的配置信息
|
||||||
|
-- - 与回调请求头中的 Wechatpay-Serial 字段对应
|
||||||
|
-- - 支持一个商户配置多个平台证书,通过序列号区分
|
||||||
+47
-274
@@ -1,13 +1,10 @@
|
|||||||
package com.central.order.controller;
|
package com.central.order.controller;
|
||||||
|
|
||||||
import com.central.order.constants.ProfitSharingConstants;
|
|
||||||
import com.central.order.enums.OrderStatus;
|
import com.central.order.enums.OrderStatus;
|
||||||
import com.central.order.enums.RefundState;
|
import com.central.order.enums.RefundState;
|
||||||
import com.central.order.model.Order;
|
import com.central.order.model.Order;
|
||||||
import com.central.order.model.ProfitSharing;
|
|
||||||
import com.central.order.model.QrCode;
|
import com.central.order.model.QrCode;
|
||||||
import com.central.order.service.IOrderService;
|
import com.central.order.service.IOrderService;
|
||||||
import com.central.order.service.IProfitSharingService;
|
|
||||||
import com.central.order.service.IQrCodeService;
|
import com.central.order.service.IQrCodeService;
|
||||||
import com.central.order.service.IRefundService;
|
import com.central.order.service.IRefundService;
|
||||||
import com.central.wechat.config.WxPayConfigFactory;
|
import com.central.wechat.config.WxPayConfigFactory;
|
||||||
@@ -19,18 +16,13 @@ import com.wechat.pay.java.service.payments.model.Transaction;
|
|||||||
import com.wechat.pay.java.service.refund.model.RefundNotification;
|
import com.wechat.pay.java.service.refund.model.RefundNotification;
|
||||||
import com.wechat.pay.java.service.refund.model.Status;
|
import com.wechat.pay.java.service.refund.model.Status;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@@ -44,8 +36,6 @@ public class WxPayController {
|
|||||||
private IRefundService refundService;
|
private IRefundService refundService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private IQrCodeService qrCodeService;
|
private IQrCodeService qrCodeService;
|
||||||
@Autowired
|
|
||||||
private IProfitSharingService profitSharingService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WxPayConfigFactory wxPayConfigFactory;
|
private WxPayConfigFactory wxPayConfigFactory;
|
||||||
@@ -77,10 +67,8 @@ public class WxPayController {
|
|||||||
|
|
||||||
// 根据证书序列号获取配置
|
// 根据证书序列号获取配置
|
||||||
WechatConfig config = wechatConfigService.getConfigBySerialNumber(serial);
|
WechatConfig config = wechatConfigService.getConfigBySerialNumber(serial);
|
||||||
String tenantId = defaultTenantId;
|
Assert.notNull(config,"支付配置参数为空,请确认参数是否正确配置");
|
||||||
if (config != null) {
|
String tenantId = config.getTenantId();
|
||||||
tenantId = config.getTenantId();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建通知解析器
|
// 创建通知解析器
|
||||||
NotificationParser notificationParser;
|
NotificationParser notificationParser;
|
||||||
@@ -144,48 +132,39 @@ public class WxPayController {
|
|||||||
* 处理普通商户模式下的支付结果
|
* 处理普通商户模式下的支付结果
|
||||||
*/
|
*/
|
||||||
private void handleMerchantPaymentResult(Transaction transaction, WechatConfig config) {
|
private void handleMerchantPaymentResult(Transaction transaction, WechatConfig config) {
|
||||||
// TODO: 实现普通商户模式下的支付结果处理逻辑
|
|
||||||
log.info("处理普通商户模式支付结果,商户号:{}", config.getMerchantId());
|
log.info("处理普通商户模式支付结果,商户号:{}", config.getMerchantId());
|
||||||
|
|
||||||
// 1. 更新订单状态
|
String outTradeNo = transaction.getOutTradeNo();
|
||||||
// 2. 处理分账逻辑(如果需要)
|
String tradeState = transaction.getTradeState().toString();
|
||||||
// 3. 发送通知
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查是否开启分润
|
|
||||||
*/
|
|
||||||
private boolean isEnableProfit(QrCode qrCode) {
|
|
||||||
return qrCode != null &&
|
|
||||||
Integer.valueOf(1).equals(qrCode.getEnableProfit()) &&
|
|
||||||
qrCode.getPayeeStatus() != null &&
|
|
||||||
qrCode.getPayeeStatus() == 1 && // 确保分账账号已验证
|
|
||||||
qrCode.getPayeeAccount() != null && // 确保有收款账号
|
|
||||||
qrCode.getProfitRate() != null; // 确保有分润比例
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送分账异常告警
|
|
||||||
*/
|
|
||||||
private void sendProfitSharingAlert(Order order, String errorMessage) {
|
|
||||||
try {
|
try {
|
||||||
String alertMessage = String.format(
|
if ("SUCCESS".equals(tradeState)) {
|
||||||
"分账处理异常告警\n" +
|
// 1. 获取订单和二维码信息
|
||||||
"订单号:%s\n" +
|
Order order = orderService.getOrderByNumber(outTradeNo);
|
||||||
"异常信息:%s\n" +
|
if (order == null) {
|
||||||
"时间:%s",
|
log.error("订单不存在: {}", outTradeNo);
|
||||||
order.getOrderNo(),
|
return;
|
||||||
errorMessage,
|
}
|
||||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
|
|
||||||
);
|
|
||||||
|
|
||||||
log.error(alertMessage);
|
QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId());
|
||||||
// TODO: 实现具体的告警通知逻辑
|
if (qrCode == null) {
|
||||||
} catch (Exception e) {
|
log.error("二维码不存在: {}", order.getCodeId());
|
||||||
log.error("发送分账异常告警失败", e);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 更新订单状态
|
||||||
|
order.setOrderStatus(OrderStatus.PAID.getId());
|
||||||
|
order.setPayTime(new Date());
|
||||||
|
orderService.updateOrder(order);
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/refund/notify")
|
@PostMapping("/refund/notify")
|
||||||
public String refundCallback(@RequestHeader("Wechatpay-Signature") String signature,
|
public String refundCallback(@RequestHeader("Wechatpay-Signature") String signature,
|
||||||
@RequestHeader("Wechatpay-Timestamp") String timestamp,
|
@RequestHeader("Wechatpay-Timestamp") String timestamp,
|
||||||
@@ -272,214 +251,31 @@ public class WxPayController {
|
|||||||
* 处理普通商户模式下的退款结果
|
* 处理普通商户模式下的退款结果
|
||||||
*/
|
*/
|
||||||
private void handleMerchantRefundResult(RefundNotification notification, WechatConfig config) {
|
private void handleMerchantRefundResult(RefundNotification notification, WechatConfig config) {
|
||||||
// TODO: 实现普通商户模式下的退款结果处理逻辑
|
|
||||||
log.info("处理普通商户模式退款结果,商户号:{}", config.getMerchantId());
|
log.info("处理普通商户模式退款结果,商户号:{}", config.getMerchantId());
|
||||||
|
|
||||||
// 1. 更新退款状态
|
// 获取退款通知中的关键信息
|
||||||
// 2. 处理相关业务逻辑
|
String outRefundNo = notification.getOutRefundNo();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
log.info("回调返回结果"+notification);
|
||||||
* 构建请求参数
|
// 根据退款状态进行相应处理
|
||||||
*/
|
if (Status.SUCCESS.equals(notification.getRefundStatus())) {
|
||||||
private RequestParam buildRequestParam(Map<String, String> headers, String body) {
|
// 退款成功,更新订单状态等操作
|
||||||
return new RequestParam.Builder()
|
com.central.order.model.Refund refund = refundService.getRefundByNo(outRefundNo);
|
||||||
.serialNumber(headers.get("Wechatpay-Serial"))
|
Assert.notNull(refund,"退款订单数据不存在");
|
||||||
.nonce(headers.get("Wechatpay-Nonce"))
|
refund.setStatus(OrderStatus.REFUNDED.getId());
|
||||||
.signature(headers.get("Wechatpay-Signature"))
|
refund.setState(RefundState.APPROVED.getId());
|
||||||
.timestamp(headers.get("Wechatpay-Timestamp"))
|
refund.setRefundTime(new Date());
|
||||||
.body(body)
|
refundService.updateById(refund);
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
Order order = orderService.getOrderByNumber(refund.getOrderNo());
|
||||||
* 解析通知数据
|
Assert.notNull(order,"订单数据不存在");
|
||||||
*/
|
order.setOrderStatus(OrderStatus.REFUNDED.getId());
|
||||||
private ProfitSharingNotification parseNotification(RequestParam requestParam) {
|
orderService.updateOrder(order);
|
||||||
try {
|
} else if ("CLOSED".equals(notification.getRefundStatus())) {
|
||||||
NotificationParser parser = wxPayConfigFactory.createNotificationParser(defaultTenantId);
|
// 退款关闭,处理退款关闭逻辑
|
||||||
// 解析为Map后转换为ProfitSharingNotification对象
|
|
||||||
Map<String, Object> result = parser.parse(requestParam, Map.class);
|
|
||||||
|
|
||||||
ProfitSharingNotification notification = new ProfitSharingNotification();
|
|
||||||
notification.setMchid((String) result.get("mchid"));
|
|
||||||
notification.setTransactionId((String) result.get("transaction_id"));
|
|
||||||
notification.setOutOrderNo((String) result.get("out_order_no"));
|
|
||||||
notification.setState((String) result.get("state"));
|
|
||||||
notification.setFailReason((String) result.get("fail_reason"));
|
|
||||||
|
|
||||||
// 处理接收方列表
|
|
||||||
if (result.containsKey("receivers")) {
|
|
||||||
List<Map<String, Object>> receiversMap = (List<Map<String, Object>>) result.get("receivers");
|
|
||||||
List<ProfitSharingNotification.Receiver> receivers = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Map<String, Object> receiverMap : receiversMap) {
|
|
||||||
ProfitSharingNotification.Receiver receiver = new ProfitSharingNotification.Receiver();
|
|
||||||
receiver.setType((String) receiverMap.get("type"));
|
|
||||||
receiver.setAccount((String) receiverMap.get("account"));
|
|
||||||
receiver.setAmount(Long.valueOf(receiverMap.get("amount").toString()));
|
|
||||||
receiver.setDescription((String) receiverMap.get("description"));
|
|
||||||
receiver.setResult((String) receiverMap.get("result"));
|
|
||||||
receiver.setFailReason((String) receiverMap.get("fail_reason"));
|
|
||||||
receiver.setDetailId((String) receiverMap.get("detail_id"));
|
|
||||||
receivers.add(receiver);
|
|
||||||
}
|
|
||||||
notification.setReceivers(receivers);
|
|
||||||
}
|
|
||||||
|
|
||||||
return notification;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("解析分账回调通知失败", e);
|
|
||||||
throw new RuntimeException("解析分账回调通知失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 优化后的分账回调处理方法
|
|
||||||
*/
|
|
||||||
@PostMapping("/profitsharing/notify")
|
|
||||||
public String handleProfitSharingNotify(@RequestHeader Map<String, String> headers,
|
|
||||||
@RequestBody String body) {
|
|
||||||
log.info("收到分账回调通知: headers={}, body={}", headers, body);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 1. 验证通知签名
|
|
||||||
RequestParam requestParam = buildRequestParam(headers, body);
|
|
||||||
|
|
||||||
// 2. 解析通知数据
|
|
||||||
ProfitSharingNotification notification = parseNotification(requestParam);
|
|
||||||
|
|
||||||
// 3. 处理分账结果
|
|
||||||
handleProfitSharingNotification(notification);
|
|
||||||
|
|
||||||
// 4. 返回成功响应
|
|
||||||
return buildSuccessResponse();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("处理分账回调失败", e);
|
|
||||||
return buildErrorResponse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleProfitSharingNotification(ProfitSharingNotification notification) {
|
|
||||||
String orderNo = notification.getOutOrderNo();
|
|
||||||
String state = notification.getState();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 1. 获取分账记录
|
|
||||||
ProfitSharing profitSharing = profitSharingService.getByOrderNo(orderNo);
|
|
||||||
if (profitSharing == null) {
|
|
||||||
log.error("分账记录不存在: {}", orderNo);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 根据状态处理
|
|
||||||
switch (state) {
|
|
||||||
case "FINISHED":
|
|
||||||
// 分账成功
|
|
||||||
handleProfitSharingSuccess(profitSharing, notification);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "FAILED":
|
|
||||||
// 分账失败
|
|
||||||
handleProfitSharingFailure(profitSharing, notification);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.warn("未知的分账状态: orderNo={}, state={}", orderNo, state);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("处理分账回调异常: orderNo={}", orderNo, e);
|
|
||||||
// 确保更新状态
|
|
||||||
profitSharingService.updateProfitSharingStatus(
|
|
||||||
orderNo,
|
|
||||||
ProfitSharingConstants.Status.FAILED
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理分账成功
|
|
||||||
*/
|
|
||||||
private void handleProfitSharingSuccess(ProfitSharing profitSharing,
|
|
||||||
ProfitSharingNotification notification) {
|
|
||||||
// 1. 更新分账状态
|
|
||||||
profitSharing.setStatus(ProfitSharingConstants.Status.SUCCESS);
|
|
||||||
profitSharing.setFinishTime(new Date());
|
|
||||||
|
|
||||||
// 2. 如果有接收方信息,更新接收方结果
|
|
||||||
if (notification.getReceivers() != null && !notification.getReceivers().isEmpty()) {
|
|
||||||
ProfitSharingNotification.Receiver receiver = notification.getReceivers().get(0);
|
|
||||||
profitSharing.setReceiverAmount(receiver.getAmount());
|
|
||||||
profitSharing.setProfitSharingId(receiver.getDetailId());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 保存更新
|
|
||||||
profitSharingService.updateById(profitSharing);
|
|
||||||
|
|
||||||
log.info("分账成功: orderNo={}", profitSharing.getOrderNo());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理分账失败
|
|
||||||
*/
|
|
||||||
private void handleProfitSharingFailure(ProfitSharing profitSharing,
|
|
||||||
ProfitSharingNotification notification) {
|
|
||||||
// 1. 更新分账状态和失败原因
|
|
||||||
profitSharing.setStatus(ProfitSharingConstants.Status.FAILED);
|
|
||||||
profitSharing.setFailReason(notification.getFailReason());
|
|
||||||
profitSharing.setUpdateTime(new Date());
|
|
||||||
|
|
||||||
// 2. 判断是否需要重试
|
|
||||||
if (profitSharing.canRetry()) {
|
|
||||||
log.info("准备重试分账: orderNo={}, retryTimes={}",
|
|
||||||
profitSharing.getOrderNo(), profitSharing.getRetryTimes());
|
|
||||||
|
|
||||||
// 更新重试信息
|
|
||||||
profitSharing.setRetryTimes(profitSharing.getRetryTimes() + 1);
|
|
||||||
profitSharing.setLastRetryTime(new Date());
|
|
||||||
profitSharingService.updateById(profitSharing);
|
|
||||||
|
|
||||||
// 发起重试
|
|
||||||
profitSharingService.retryFailedProfitSharing(profitSharing.getOrderNo());
|
|
||||||
} else {
|
} else {
|
||||||
log.warn("分账失败且不再重试: orderNo={}, retryTimes={}",
|
// 其他退款状态,如退款中、失败等
|
||||||
profitSharing.getOrderNo(), profitSharing.getRetryTimes());
|
|
||||||
|
|
||||||
// 发送告警
|
|
||||||
sendFailureAlert(profitSharing);
|
|
||||||
|
|
||||||
// 保存最终状态
|
|
||||||
profitSharingService.updateById(profitSharing);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送失败告警
|
|
||||||
*/
|
|
||||||
private void sendFailureAlert(ProfitSharing profitSharing) {
|
|
||||||
try {
|
|
||||||
String alertMessage = String.format(
|
|
||||||
"分账失败告警\n" +
|
|
||||||
"订单号:%s\n" +
|
|
||||||
"交易号:%s\n" +
|
|
||||||
"失败原因:%s\n" +
|
|
||||||
"重试次数:%d\n" +
|
|
||||||
"分账金额:%d分\n" +
|
|
||||||
"失败时间:%s",
|
|
||||||
profitSharing.getOrderNo(),
|
|
||||||
profitSharing.getTransactionId(),
|
|
||||||
profitSharing.getFailReason(),
|
|
||||||
profitSharing.getRetryTimes(),
|
|
||||||
profitSharing.getQrCodeAmount(),
|
|
||||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
|
|
||||||
);
|
|
||||||
|
|
||||||
log.error(alertMessage);
|
|
||||||
// TODO: 实现具体的告警通知逻辑,如发送邮件、钉钉消息等
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("发送分账失败告警异常", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -490,28 +286,5 @@ public class WxPayController {
|
|||||||
private String buildErrorResponse() {
|
private String buildErrorResponse() {
|
||||||
return "{\"code\":\"FAIL\",\"message\":\"失败\"}";
|
return "{\"code\":\"FAIL\",\"message\":\"失败\"}";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 分账通知解析实体类
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
class ProfitSharingNotification {
|
|
||||||
private String mchid; // 商户号
|
|
||||||
private String transactionId; // 微信订单号
|
|
||||||
private String outOrderNo; // 商户分账单号
|
|
||||||
private String state; // 分账状态
|
|
||||||
private String failReason; // 失败原因
|
|
||||||
private List<Receiver> receivers; // 分账接收方列表
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class Receiver {
|
|
||||||
private String type; // 分账接收方类型
|
|
||||||
private String account; // 分账接收方账号
|
|
||||||
private Long amount; // 分账金额
|
|
||||||
private String description; // 分账描述
|
|
||||||
private String result; // 分账结果
|
|
||||||
private String failReason; // 分账失败原因
|
|
||||||
private String detailId; // 分账明细单号
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ import java.util.Map;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Tag(name = "微信配置管理")
|
@Tag(name = "微信配置管理")
|
||||||
@RequestMapping("/wechat/config")
|
@RequestMapping("/wechat/config")
|
||||||
public class SysWechatConfigController {
|
public class WechatConfigController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private WechatConfigService wechatConfigService;
|
private WechatConfigService wechatConfigService;
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
package com.central.user.model;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("sys_wechat_config")
|
|
||||||
public class SysWechatConfig {
|
|
||||||
|
|
||||||
@TableId(type = IdType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 租户ID
|
|
||||||
*/
|
|
||||||
private String tenantId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置类型:MP-公众号,MINI-小程序,PAY-支付
|
|
||||||
*/
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 商户ID
|
|
||||||
*/
|
|
||||||
private String merchantId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 应用ID
|
|
||||||
*/
|
|
||||||
private String appId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 应用密钥
|
|
||||||
*/
|
|
||||||
private String appSecret;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 商户API密钥
|
|
||||||
*/
|
|
||||||
private String mchKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 商户证书序列号
|
|
||||||
*/
|
|
||||||
private String mchSerialNo;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 商户私钥文件路径
|
|
||||||
*/
|
|
||||||
private String privateKeyPath;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* APIv3密钥
|
|
||||||
*/
|
|
||||||
private String apiV3Key;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 支付回调地址
|
|
||||||
*/
|
|
||||||
private String notifyUrl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信公众号token
|
|
||||||
*/
|
|
||||||
private String token;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信公众号aesKey
|
|
||||||
*/
|
|
||||||
private String aesKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 支付成功通知模板ID
|
|
||||||
*/
|
|
||||||
private String paymentSuccessTemplateId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 视频处理完成通知模板ID
|
|
||||||
*/
|
|
||||||
private String videoCompleteTemplateId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 视频处理失败通知模板ID
|
|
||||||
*/
|
|
||||||
private String videoFailTemplateId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否启用分账
|
|
||||||
*/
|
|
||||||
private Boolean profitSharingEnabled;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否启用异步处理
|
|
||||||
*/
|
|
||||||
private Boolean asyncEnabled;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步处理线程池大小
|
|
||||||
*/
|
|
||||||
private Integer asyncPoolSize;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 状态:0-禁用,1-启用
|
|
||||||
*/
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 备注
|
|
||||||
*/
|
|
||||||
private String remark;
|
|
||||||
}
|
|
||||||
+1
-1
@@ -48,7 +48,7 @@ public class WechatConfigServiceImpl extends ServiceImpl<WechatConfigMapper, Wec
|
|||||||
@Override
|
@Override
|
||||||
public WechatConfig getConfigBySerialNumber(String serialNumber) {
|
public WechatConfig getConfigBySerialNumber(String serialNumber) {
|
||||||
return getOne(new LambdaQueryWrapper<WechatConfig>()
|
return getOne(new LambdaQueryWrapper<WechatConfig>()
|
||||||
.eq(WechatConfig::getMchSerialNo, serialNumber)
|
.eq(WechatConfig::getSerialNumber, serialNumber)
|
||||||
.eq(WechatConfig::getStatus, 1));
|
.eq(WechatConfig::getStatus, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
@@ -49,6 +49,11 @@ public class WechatConfig {
|
|||||||
*/
|
*/
|
||||||
private String mchSerialNo;
|
private String mchSerialNo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信平台证书序列号(用于回调验证)
|
||||||
|
*/
|
||||||
|
private String serialNumber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商户私钥文件路径
|
* 商户私钥文件路径
|
||||||
*/
|
*/
|
||||||
|
|||||||
+199
@@ -0,0 +1,199 @@
|
|||||||
|
# 微信平台证书序列号配置说明
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
在微信支付回调中,微信会通过请求头 `Wechatpay-Serial` 传递平台证书的序列号,用于验证回调请求的签名。为了支持多个平台证书的场景,系统新增了 `serialNumber` 字段来存储微信平台证书的序列号。
|
||||||
|
|
||||||
|
## 字段说明
|
||||||
|
|
||||||
|
### serialNumber 字段
|
||||||
|
- **用途**: 存储微信平台证书的序列号
|
||||||
|
- **来源**: 微信支付回调请求头中的 `Wechatpay-Serial` 字段
|
||||||
|
- **作用**: 用于快速定位对应的微信配置信息
|
||||||
|
- **支持**: 一个商户可以配置多个平台证书,通过序列号区分
|
||||||
|
|
||||||
|
## 配置流程
|
||||||
|
|
||||||
|
### 1. 获取微信平台证书序列号
|
||||||
|
|
||||||
|
#### 方法一:通过微信支付API获取
|
||||||
|
```bash
|
||||||
|
# 获取平台证书列表
|
||||||
|
curl -X GET "https://api.mch.weixin.qq.com/v3/certificates" \
|
||||||
|
-H "Accept: application/json" \
|
||||||
|
-H "Authorization: WECHATPAY2-SHA256-RSA2048 ..."
|
||||||
|
```
|
||||||
|
|
||||||
|
响应示例:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"serial_no": "5157F09EFDC096DE15EBE81A47057A7232F1B8E1",
|
||||||
|
"effective_time": "2018-06-08T10:34:56+08:00",
|
||||||
|
"expire_time": "2023-06-08T10:34:56+08:00",
|
||||||
|
"encrypt_certificate": {
|
||||||
|
"algorithm": "AEAD_AES_256_GCM",
|
||||||
|
"nonce": "61f9c719728a",
|
||||||
|
"associated_data": "certificate",
|
||||||
|
"ciphertext": "..."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 方法二:从回调请求头获取
|
||||||
|
在微信支付回调时,请求头中会包含:
|
||||||
|
```
|
||||||
|
Wechatpay-Serial: 5157F09EFDC096DE15EBE81A47057A7232F1B8E1
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 配置到系统
|
||||||
|
|
||||||
|
#### 通过管理后台配置
|
||||||
|
1. 登录管理后台
|
||||||
|
2. 进入"微信配置管理"
|
||||||
|
3. 选择对应的租户和配置类型
|
||||||
|
4. 在"微信平台证书序列号"字段中填入获取到的序列号
|
||||||
|
5. 保存配置
|
||||||
|
|
||||||
|
#### 通过API配置
|
||||||
|
```http
|
||||||
|
PUT /wechat/config
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"tenantId": "webApp",
|
||||||
|
"type": "PAY",
|
||||||
|
"serialNumber": "5157F09EFDC096DE15EBE81A47057A7232F1B8E1",
|
||||||
|
"merchantId": "1900000109",
|
||||||
|
"appId": "wx1234567890abcdef",
|
||||||
|
"mchSerialNo": "1234567890ABCDEF",
|
||||||
|
"privateKey": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----",
|
||||||
|
"apiV3Key": "your_api_v3_key",
|
||||||
|
"notifyUrl": "https://your.domain.com/notify",
|
||||||
|
"status": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 回调处理流程
|
||||||
|
|
||||||
|
### 1. 接收回调请求
|
||||||
|
```java
|
||||||
|
@PostMapping("/pay/notify")
|
||||||
|
public String handleCallback(@RequestHeader("Wechatpay-Signature") String signature,
|
||||||
|
@RequestHeader("Wechatpay-Timestamp") String timestamp,
|
||||||
|
@RequestHeader("Wechatpay-Nonce") String nonce,
|
||||||
|
@RequestHeader("Wechatpay-Serial") String serial,
|
||||||
|
@RequestBody String body) {
|
||||||
|
// serial 参数就是微信平台证书的序列号
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 根据序列号获取配置
|
||||||
|
```java
|
||||||
|
// 根据证书序列号获取配置
|
||||||
|
WechatConfig config = wechatConfigService.getConfigBySerialNumber(serial);
|
||||||
|
if (config == null) {
|
||||||
|
log.error("未找到对应的微信配置,序列号: {}", serial);
|
||||||
|
return "{\"code\":\"FAIL\",\"message\":\"配置不存在\"}";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 验证回调签名
|
||||||
|
```java
|
||||||
|
// 创建通知解析器
|
||||||
|
NotificationParser notificationParser = wxPayConfigFactory.createNotificationParser(config.getTenantId());
|
||||||
|
|
||||||
|
// 构建请求参数
|
||||||
|
RequestParam requestParam = new RequestParam
|
||||||
|
.Builder()
|
||||||
|
.body(body)
|
||||||
|
.nonce(nonce)
|
||||||
|
.signature(signature)
|
||||||
|
.serialNumber(serial) // 使用平台证书序列号
|
||||||
|
.timestamp(timestamp)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 解析通知
|
||||||
|
Transaction transaction = notificationParser.parse(requestParam, Transaction.class);
|
||||||
|
```
|
||||||
|
|
||||||
|
## 多证书支持
|
||||||
|
|
||||||
|
### 场景说明
|
||||||
|
当一个商户需要支持多个平台证书时(例如证书轮换),可以通过配置多个 `serialNumber` 来支持:
|
||||||
|
|
||||||
|
### 配置示例
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"tenantId": "webApp",
|
||||||
|
"type": "PAY",
|
||||||
|
"serialNumber": "5157F09EFDC096DE15EBE81A47057A7232F1B8E1",
|
||||||
|
"merchantId": "1900000109",
|
||||||
|
"appId": "wx1234567890abcdef",
|
||||||
|
"status": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"tenantId": "webApp",
|
||||||
|
"type": "PAY",
|
||||||
|
"serialNumber": "5157F09EFDC096DE15EBE81A47057A7232F1B8E2",
|
||||||
|
"merchantId": "1900000109",
|
||||||
|
"appId": "wx1234567890abcdef",
|
||||||
|
"status": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 处理逻辑
|
||||||
|
系统会根据回调请求中的 `Wechatpay-Serial` 自动匹配对应的配置:
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Override
|
||||||
|
public WechatConfig getConfigBySerialNumber(String serialNumber) {
|
||||||
|
return getOne(new LambdaQueryWrapper<WechatConfig>()
|
||||||
|
.eq(WechatConfig::getSerialNumber, serialNumber)
|
||||||
|
.eq(WechatConfig::getStatus, 1));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
### 1. 证书有效期
|
||||||
|
- 微信平台证书有有效期限制
|
||||||
|
- 需要在证书过期前更新配置
|
||||||
|
- 建议配置证书过期提醒
|
||||||
|
|
||||||
|
### 2. 序列号唯一性
|
||||||
|
- 每个平台证书的序列号是唯一的
|
||||||
|
- 同一租户下不能配置重复的序列号
|
||||||
|
- 建议在配置时进行唯一性校验
|
||||||
|
|
||||||
|
### 3. 配置验证
|
||||||
|
- 配置完成后建议进行测试
|
||||||
|
- 可以通过模拟回调请求验证配置是否正确
|
||||||
|
- 确保回调处理逻辑能正常工作
|
||||||
|
|
||||||
|
### 4. 日志记录
|
||||||
|
- 建议记录证书序列号的使用情况
|
||||||
|
- 便于排查问题和监控系统状态
|
||||||
|
- 可以统计各证书的使用频率
|
||||||
|
|
||||||
|
## 错误处理
|
||||||
|
|
||||||
|
### 常见错误
|
||||||
|
1. **配置不存在**: 回调请求中的序列号在系统中找不到对应配置
|
||||||
|
2. **证书过期**: 使用的平台证书已过期
|
||||||
|
3. **签名验证失败**: 回调请求的签名验证失败
|
||||||
|
|
||||||
|
### 处理建议
|
||||||
|
1. 记录详细的错误日志
|
||||||
|
2. 提供配置检查工具
|
||||||
|
3. 实现自动化的证书更新机制
|
||||||
|
4. 建立监控告警机制
|
||||||
Reference in New Issue
Block a user