From 14559237246497a91e9fa9eb66c0d7e4f27e2637 Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Mon, 2 Jun 2025 20:05:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=AE=8C=E5=96=84=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E9=80=BB=E8=BE=91=E5=8A=A8=E6=80=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=BE=AE=E4=BF=A1=E5=95=86=E6=88=B7=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zlt-business/order-center/pom.xml | 4 + .../order/controller/OrderController.java | 55 +--------- .../central/order/service/PaymentService.java | 103 ++++++++++++++++++ .../config/WechatAutoConfiguration.java | 57 ++-------- .../zlt/wechat/config/WxPayConfigFactory.java | 39 +++++++ .../TencentAuthenticationConverter.java | 4 +- 6 files changed, 165 insertions(+), 97 deletions(-) create mode 100644 zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java create mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java diff --git a/zlt-business/order-center/pom.xml b/zlt-business/order-center/pom.xml index 6a3cd9a6..af052ff2 100644 --- a/zlt-business/order-center/pom.xml +++ b/zlt-business/order-center/pom.xml @@ -145,6 +145,10 @@ org.springframework.boot spring-boot-starter-aop + + com.zlt + zlt-wechat-spring-boot-starter + diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java index 92d43754..d0f0d12f 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java @@ -7,7 +7,6 @@ import com.central.common.context.TenantContextHolder; import com.central.common.model.LoginAppUser; import com.central.common.model.PageResult; import com.central.common.model.Result; -import com.central.common.model.SysUser; import com.central.log.annotation.AuditLog; import com.central.order.config.TencentPayProperties; import com.central.order.enums.CommonStatus; @@ -18,7 +17,7 @@ import com.central.order.feign.UserCenterService; import com.central.order.model.*; import com.central.order.service.*; import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; -import com.wechat.pay.java.service.payments.jsapi.model.*; +import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -97,6 +96,9 @@ public class OrderController { @Autowired private IProfitSharingService profitSharingService; + @Autowired + private PaymentService paymentService; + /** * 列表 */ @@ -227,59 +229,12 @@ public class OrderController { * @param orderId 微信公众号对应微信付款用户的唯一标识 * @return 返回jsapi所需参数 */ - @Operation(summary = "生成预支付订单") @PutMapping("/{orderId}/pay") public Result payOrder(@PathVariable("orderId") Long orderId) { try { - // 1. 获取用户和订单信息 - SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId()); + PrepayWithRequestPaymentResponse response = paymentService.createPayment(orderId); Order order = orderService.getById(orderId); - if (order == null) { - return Result.failed("订单数据不存在"); - } - - // 检查是否需要开启分账 - QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId()); - if (qrCode == null) { - return Result.failed("二维码不存在"); - } - - // 3. 创建支付请求 - PrepayRequest request = new PrepayRequest(); - - // 设置基本支付信息 - Amount amount = new Amount(); - amount.setTotal(Integer.parseInt(order.getOrderAmount())); - request.setAmount(amount); - request.setAppid(tencentPayProperties.getAppId()); - request.setMchid(tencentPayProperties.getMerchantId()); - request.setDescription(order.getOrderNo()); - request.setNotifyUrl(tencentPayProperties.getNotifyUrl()); - request.setOutTradeNo(order.getOrderNo()); - - // 设置支付者信息 - Payer payer = new Payer(); - payer.setOpenid(user.getOpenId()); - request.setPayer(payer); - - // 设置分账信息 - SettleInfo settleInfo = new SettleInfo(); - if (isEnableProfit(qrCode)) { - settleInfo.setProfitSharing(true); - request.setSettleInfo(settleInfo); - - // 创建分账记录 - ProfitSharing profitSharing = profitSharingService.createProfitSharing(order,qrCode); - } else { - settleInfo.setProfitSharing(false); - request.setSettleInfo(settleInfo); - } - - // 5. 调用支付接口 - PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request); - - // 6. 返回结果 return Result.succeed(response, order.getOrderNo()); } catch (Exception e) { log.error("创建预支付订单失败", e); diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java b/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java new file mode 100644 index 00000000..80a25ad9 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java @@ -0,0 +1,103 @@ +package com.central.order.service; + +import com.central.common.context.LoginUserContextHolder; +import com.central.common.exception.BusinessException; +import com.central.common.model.SysUser; +import com.central.order.feign.UserCenterService; +import com.central.order.model.Order; +import com.central.order.model.QrCode; +import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.wechat.feign.WechatConfigService; +import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; +import com.wechat.pay.java.service.payments.jsapi.model.*; +import com.zlt.wechat.config.WxPayConfigFactory; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class PaymentService { + private final WxPayConfigFactory wxPayConfigFactory; + private final UserCenterService userCenterService; + private final IOrderService orderService; + private final IQrCodeService qrCodeService; + private final IProfitSharingService profitSharingService; + private final WechatConfigService configService; + + public PaymentService(WxPayConfigFactory wxPayConfigFactory, + UserCenterService userCenterService, + IOrderService orderService, + IQrCodeService qrCodeService, + IProfitSharingService profitSharingService, + WechatConfigService configService) { + this.wxPayConfigFactory = wxPayConfigFactory; + this.userCenterService = userCenterService; + this.orderService = orderService; + this.qrCodeService = qrCodeService; + this.profitSharingService = profitSharingService; + this.configService = configService; + } + + public PrepayWithRequestPaymentResponse createPayment(Long orderId) { + // 1. 获取用户和订单信息 + SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId()); + Order order = orderService.getById(orderId); + if (order == null) { + throw new BusinessException("订单数据不存在"); + } + + // 2. 获取二维码信息 + QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId()); + if (qrCode == null) { + throw new BusinessException("二维码不存在"); + } + + // 3. 创建支付请求 + PrepayRequest request = new PrepayRequest(); + + // 设置基本支付信息 + Amount amount = new Amount(); + amount.setTotal(Integer.parseInt(order.getOrderAmount())); + request.setAmount(amount); + + // 从二维码配置中获取支付配置 + WechatConfig payConfig = configService.getConfig("PAY", qrCode.getTenantId()); + request.setAppid(payConfig.getAppId()); + request.setMchid(payConfig.getMerchantId()); + request.setDescription(order.getOrderNo()); + request.setNotifyUrl(payConfig.getNotifyUrl()); + request.setOutTradeNo(order.getOrderNo()); + + // 设置支付者信息 + Payer payer = new Payer(); + payer.setOpenid(user.getOpenId()); + request.setPayer(payer); + + // 设置分账信息 + SettleInfo settleInfo = new SettleInfo(); + if (isEnableProfit(qrCode)) { + settleInfo.setProfitSharing(true); + request.setSettleInfo(settleInfo); + // 创建分账记录 + profitSharingService.createProfitSharing(order, qrCode); + } else { + settleInfo.setProfitSharing(false); + request.setSettleInfo(settleInfo); + } + + // 4. 创建对应租户的支付服务 + JsapiServiceExtension jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId()); + + // 5. 调用支付接口 + return jsapiService.prepayWithRequestPayment(request); + } + + 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; + } +} diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java index 3e9b453f..72177ace 100644 --- a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java +++ b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java @@ -16,6 +16,7 @@ import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; import com.wechat.pay.java.service.payments.jsapi.model.SettleInfo; import com.wechat.pay.java.service.profitsharing.ProfitsharingService; import com.wechat.pay.java.service.refund.RefundService; +import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; @@ -24,11 +25,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; +@Slf4j @Configuration @EnableConfigurationProperties(WechatProperties.class) public class WechatAutoConfiguration { @@ -37,6 +40,7 @@ public class WechatAutoConfiguration { @ConditionalOnMissingBean @ConditionalOnBean(WechatConfigService.class) public WxMpService wxMpService(WechatConfigService configService) { + log.info("WxMpService(公众号相关)被加载,tenantId:{}", getCurrentTenantId()); return new WxMpServiceImpl() { @Override public WxMpDefaultConfigImpl getWxMpConfigStorage() { @@ -62,6 +66,8 @@ public class WechatAutoConfiguration { @ConditionalOnMissingBean @ConditionalOnBean(WechatConfigService.class) public WxMaService wxMaService(WechatConfigService configService) { + log.info("WxMaService(小程序通知)被加载,tenantId:{}", getCurrentTenantId()); + // 小程序通知Service配置,type=MINI return new WxMaServiceImpl() { @Override public WxMaDefaultConfigImpl getWxMaConfig() { @@ -74,58 +80,18 @@ public class WechatAutoConfiguration { }; } - /*@Bean - @ConditionalOnMissingBean - public WxPayService wxPayService(WechatConfigService configService) { - return new WxPayServiceImpl() { - @Override - public WxPayConfig getConfig() { - WechatConfig config = configService.getConfig("PAY", getCurrentMerchantId()); - WxPayConfig payConfig = new WxPayConfig(); - payConfig.setAppId(config.getAppId()); - payConfig.setMchId(config.getMerchantId()); - payConfig.setMchKey(config.getMchKey()); - payConfig.setKeyPath(config.getPrivateKeyPath()); - payConfig.setNotifyUrl(config.getNotifyUrl()); - payConfig.setApiV3Key(config.getApiV3Key()); - payConfig.setPrivateKeyPath(config.getPrivateKeyPath()); - payConfig.setPrivateCertPath(config.getPrivateKeyPath()); - payConfig.setCertSerialNo(config.getMchSerialNo()); - return payConfig; - } - }; - }*/ - @Bean - @ConditionalOnMissingBean - @ConditionalOnBean(WechatConfigService.class) - public Config wxPayConfig(WechatConfigService configService, PrivateKeyService privateKeyService) { - WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); - String privateKey = privateKeyService.getPrivateKey(getCurrentTenantId(), "PAY"); - return new RSAAutoCertificateConfig.Builder() - .merchantId(config.getMerchantId()) - .privateKey(privateKey) - .merchantSerialNumber(config.getMchSerialNo()) - .apiV3Key(config.getApiV3Key()) - .build(); - } - - @Bean - @ConditionalOnMissingBean - public JsapiServiceExtension jsapiServiceExtension(Config config) { - return new JsapiServiceExtension.Builder().config(config).build(); - } - - @Bean - @ConditionalOnMissingBean - public RefundService refundService(Config config) { - return new RefundService.Builder().config(config).build(); + public WxPayConfigFactory wxPayConfigFactory( + WechatConfigService configService, + PrivateKeyService privateKeyService) { + return new WxPayConfigFactory(configService, privateKeyService); } @Bean @ConditionalOnMissingBean @ConditionalOnBean(WechatConfigService.class) public NotificationConfig notificationConfig(WechatConfigService configService, PrivateKeyService privateKeyService) { + log.info("NotificationConfig被加载,tenantId:{}", getCurrentTenantId()); WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); String privateKey = privateKeyService.getPrivateKey(getCurrentTenantId(), "PAY"); return new RSAAutoCertificateConfig.Builder() @@ -162,6 +128,7 @@ public class WechatAutoConfiguration { @ConditionalOnMissingBean @ConditionalOnBean(WechatConfigService.class) public Executor profitSharingExecutor(WechatConfigService configService) { + log.info("profitSharingExecutor被加载,tenantId:{}", getCurrentTenantId()); WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); if (!config.getAsyncEnabled()) { return null; diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java new file mode 100644 index 00000000..7e348225 --- /dev/null +++ b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java @@ -0,0 +1,39 @@ +package com.zlt.wechat.config; + +import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.wechat.feign.PrivateKeyService; +import com.central.wechat.wechat.feign.WechatConfigService; +import com.wechat.pay.java.core.Config; +import com.wechat.pay.java.core.RSAAutoCertificateConfig; +import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class WxPayConfigFactory { + private final WechatConfigService configService; + private final PrivateKeyService privateKeyService; + + public WxPayConfigFactory(WechatConfigService configService, PrivateKeyService privateKeyService) { + this.configService = configService; + this.privateKeyService = privateKeyService; + } + + public Config createConfig(String tenantId) { + log.info("创建微信支付配置,tenantId:{}", tenantId); + WechatConfig config = configService.getConfig("PAY", tenantId); + String privateKey = privateKeyService.getPrivateKey(tenantId, "PAY"); + return new RSAAutoCertificateConfig.Builder() + .merchantId(config.getMerchantId()) + .privateKey(privateKey) + .merchantSerialNumber(config.getMchSerialNo()) + .apiV3Key(config.getApiV3Key()) + .build(); + } + + public JsapiServiceExtension createJsapiService(String tenantId) { + Config config = createConfig(tenantId); + return new JsapiServiceExtension.Builder().config(config).build(); + } +} \ No newline at end of file diff --git a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java index 324639e2..4ef43783 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java +++ b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java @@ -33,8 +33,8 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter String code = OAuthEndpointUtils.getParam(parameters, OAuth2ParameterNames.CODE); String encryptedData = OAuthEndpointUtils.getParam(parameters,"encryptedData"); String iv = OAuthEndpointUtils.getParam(parameters,"iv"); - String appId = OAuthEndpointUtils.getParam(parameters,"appId"); - return new TencentAuthenticationToken(code, encryptedData, iv, appId); + String appCode = OAuthEndpointUtils.getParam(parameters,"appCode"); + return new TencentAuthenticationToken(code, encryptedData, iv, appCode); }