From 04177acfafaf90bfd077fe24dc6d8529629d85d7 Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Tue, 14 Jan 2025 17:44:35 +0800 Subject: [PATCH] =?UTF-8?q?feat[=E6=94=AF=E4=BB=98=E6=A8=A1=E5=9D=97]:?= =?UTF-8?q?=E8=85=BE=E8=AE=AF=E5=AE=98=E6=96=B9v3=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E9=9B=86=E6=88=90=EF=BC=8C=E6=9B=BF=E4=BB=A3=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E9=9B=86=E6=88=90=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zlt-business/order-center/pom.xml | 17 +--- .../order/config/TencentPayConfiguration.java | 61 +++++++++++++ .../order/controller/OrderController.java | 83 +++++++++-------- .../order/controller/WeixinPayController.java | 77 ---------------- .../controller/WxPayNotifyController.java | 89 ++++++++++--------- .../java/com/central/order/model/Order.java | 2 +- .../src/main/resources/application.yml | 12 +-- .../main/resources/application-dev.properties | 16 ++-- 8 files changed, 169 insertions(+), 188 deletions(-) create mode 100644 zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java delete mode 100644 zlt-business/order-center/src/main/java/com/central/order/controller/WeixinPayController.java diff --git a/zlt-business/order-center/pom.xml b/zlt-business/order-center/pom.xml index 6d337f11..9b7d9cf7 100644 --- a/zlt-business/order-center/pom.xml +++ b/zlt-business/order-center/pom.xml @@ -89,22 +89,11 @@ org.hibernate hibernate-validator - - - com.squareup.okhttp3 - okhttp - - - - com.github.binarywang - weixin-java-pay - 4.1.0 - - com.egzosn - pay-java-wx - 2.14.7 + com.github.wechatpay-apiv3 + wechatpay-java + 0.2.15 diff --git a/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java b/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java new file mode 100644 index 00000000..377f3209 --- /dev/null +++ b/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java @@ -0,0 +1,61 @@ +package com.central.order.config; + +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.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 支付配置类 + * + * @author lsl + * @date 2025-01-14 + * + */ +@Getter +@Setter +@Configuration +@ConfigurationProperties(prefix = "tencent") +public class TencentPayConfiguration { + + private String merchantId; + + private String merchantSerialNumber; + + private String apiV3Key; + + private String privateKeyPath; + + /** + * jsapi支付方式 支持小程序公众号支付 + * @return + */ + @Bean + public JsapiServiceExtension initialJsapiServiceExtension() { + Config config = + new RSAAutoCertificateConfig.Builder() + .merchantId(merchantId) + .privateKeyFromPath(privateKeyPath) + .merchantSerialNumber(merchantSerialNumber) + .apiV3Key(apiV3Key) + .build(); + // 构建service + return new JsapiServiceExtension.Builder().config(config).build(); + } + +// public RequestParam initRequestParam() { +// // 构造 RequestParam +// RequestParam requestParam = new RequestParam.Builder() +// .serialNumber(wechatPaySerial) +// .nonce(wechatpayNonce) +// .signature(wechatSignature) +// .timestamp(wechatTimestamp) +// .body(requestBody) +// .build(); +// +// } +} 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 dc78a85d..aef85a1a 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 @@ -8,26 +8,23 @@ import com.central.common.model.SysUser; import com.central.order.feign.UserCenterService; import com.central.order.model.Order; import com.central.order.service.IOrderService; -import com.egzosn.pay.common.api.PayService; -import com.egzosn.pay.common.bean.PayOrder; -import com.egzosn.pay.wx.bean.WxTransactionType; -import com.egzosn.pay.wx.v3.api.WxPayConfigStorage; -import com.egzosn.pay.wx.v3.api.WxPayService; +import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; +import com.wechat.pay.java.service.payments.jsapi.model.Amount; +import com.wechat.pay.java.service.payments.jsapi.model.Payer; +import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest; +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; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.math.BigDecimal; import java.util.List; import java.util.Map; -import java.util.UUID; /** * Order information @@ -44,27 +41,11 @@ public class OrderController { @Autowired private IOrderService orderService; - private PayService payService; - @Autowired private UserCenterService userCenterService; - /** - * 这里对 PayService 与 router进行构造 - */ - @PostConstruct - public void init() { - WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage(); - wxPayConfigStorage.setMchId("1703796518"); - wxPayConfigStorage.setAppId("wx30a3c8de94a85584"); -// wxPayConfigStorage.setKeyPublic("转账公钥,转账时必填"); -// wxPayConfigStorage.setApiKey("密钥"); - wxPayConfigStorage.setNotifyUrl("https://img.goflytec.com/pay/unifiedorder"); - wxPayConfigStorage.setReturnUrl("https://img.goflytec.com/pay/unifiedorder"); - wxPayConfigStorage.setSignType("MD5"); - wxPayConfigStorage.setInputCharset("utf-8"); - payService = new WxPayService(wxPayConfigStorage); - } + @Autowired + private JsapiServiceExtension service; /** * 列表 */ @@ -81,10 +62,10 @@ public class OrderController { // 创建订单--》 // 返回前端订单信息--》 - // 前端发起支付(api-order/unifiedOrder)--》 + // 前端发起支付(PUT api-order/{id}/pay)--》 // 等待回调更新订单状态(微信支付回调接口/api-order/pay/notify)--》 - // 前端支付成功后创建支付记录(/api-order/payment) - // 进入排队队列中(/api-order/queue)已支付订单列表 + // 同时创建支付记录(/api-order/payment) + // 前端进入排队队列中(/api-order/queue)已支付订单列表 // 设备执行完成后更新订单状态已完成(根据设备上报状态触发或者设备直接调用接口进行更新任务状态) @PostMapping public Result createOrder(@RequestBody Order order) { @@ -107,20 +88,29 @@ public class OrderController { * @return 返回jsapi所需参数 */ @PutMapping("/{orderId}/pay") - public Result payOrder(@PathVariable("orderId") Long orderId) { + public Result payOrder(@PathVariable("orderId") Long orderId) { SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId()); + Order order = orderService.getById(orderId); - PayOrder order = new PayOrder("订单title", "摘要", new BigDecimal(0.01) , UUID.randomUUID().toString().replace("-", "")); - //公众号支付 - order.setTransactionType(WxTransactionType.JSAPI); - //微信公众号对应微信付款用户的唯一标识 - order.setOpenid(user.getOpenId()); + // request.setXxx(val)设置所需参数,具体参数可见Request定义 + PrepayRequest request = new PrepayRequest(); + Amount amount = new Amount(); + amount.setTotal(100); + request.setAmount(amount); + request.setAppid("wx30a3c8de94a85584"); + request.setMchid("1703796518"); + request.setDescription("测试商品标题"); + request.setNotifyUrl("https://notify_url"); + request.setOutTradeNo(order.getOrderNo()); + Payer payer = new Payer(); + payer.setOpenid(user.getOpenId()); + request.setPayer(payer); + // 调用下单方法,得到应答 + // response包含了调起支付所需的所有参数,可直接用于前端调起支付 + PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request); - Map orderInfo = payService.orderInfo(order); - orderInfo.put("code", 0); - - return Result.succeed(orderInfo,"成功"); + return Result.succeed(response,"成功"); } // 获取排队中的订单列表 @@ -136,4 +126,19 @@ public class OrderController { orderService.processOrdersInQueue(); return Result.succeed("排队订单处理已启动"); } + +// public void getTencentOrderById(String setTransactionId) { +// QueryOrderByIdRequest queryRequest = new QueryOrderByIdRequest(); +// queryRequest.setMchid("190000****"); +// queryRequest.setTransactionId("4200001569202208304701234567"); +// +// try { +// Transaction result = service.queryOrderById(queryRequest); +// System.out.println(result.getTradeState()); +// } catch (ServiceException e) { +// // API返回失败, 例如ORDER_NOT_EXISTS +// System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage()); +// System.out.printf("reponse body=[%s]\n", e.getResponseBody()); +// } +// } } diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/WeixinPayController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/WeixinPayController.java deleted file mode 100644 index 28fb29c7..00000000 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/WeixinPayController.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.central.order.controller; - -import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; -import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult; -import com.github.binarywang.wxpay.config.WxPayConfig; -import com.github.binarywang.wxpay.service.WxPayService; -import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; -import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -import java.math.BigDecimal; -import java.util.HashMap; -import java.util.Map; - -/** - * pay information - * - * @author lsl - * @date 2024-12-19 17:51:47 - */ -@Slf4j -@RestController -@Tag(name = "Pay information") -@RequiredArgsConstructor -public class WeixinPayController { - // 此处简单示例,实际可从配置中心等获取配置构建WxPayService - private static final WxPayService wxPayService = new WxPayServiceImpl(); - - static { - WxPayConfig payConfig = new WxPayConfig(); - payConfig.setMchId("YOUR_MCH_ID"); - payConfig.setMchKey("YOUR_MCH_KEY"); - payConfig.setAppId("YOUR_APP_ID"); - payConfig.setNotifyUrl("http://your_domain/weixin/pay/notify"); - // 设置证书等其他配置信息(如果需要) - ((WxPayServiceImpl) wxPayService).setConfig(payConfig); - } - - // 接收前端传来的订单信息,调用微信支付统一下单接口 - @PostMapping("/unifiedOrder") - public Map unifiedOrder(@RequestBody Map orderInfo) { - try { - String orderNumber = (String) orderInfo.get("orderNumber"); - BigDecimal amount = new BigDecimal((String) orderInfo.get("amount")); - - WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); - request.setBody("商品描述"); // 可根据实际订单详情填写更准确的描述 - request.setOutTradeNo(orderNumber); - request.setTotalFee(amount.multiply(new BigDecimal(100)).intValue()); // 微信支付金额单位为分 -// request.setSpbillCreateDate(System.currentTimeMillis() / 1000 + ""); - request.setNotifyUrl("http://your_domain/weixin/pay/notify"); - request.setTradeType("JSAPI"); // 这里以公众号支付为例,根据实际情况选择支付类型,如APP支付、小程序支付等 - - WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(request); - if ("SUCCESS".equals(result.getReturnCode()) && "SUCCESS".equals(result.getResultCode())) { - Map response = new HashMap<>(); - response.put("prepayId", result.getPrepayId()); - // 此处还需按照微信支付要求对返回信息进行进一步处理,生成前端调起支付所需的完整参数,如签名等,此处简化示例 - return response; - } else { - // 处理统一下单失败情况,返回相应错误信息给前端 - Map errorResponse = new HashMap<>(); - errorResponse.put("errorMsg", "统一下单失败"); - return errorResponse; - } - } catch (Exception e) { - e.printStackTrace(); - Map errorResponse = new HashMap<>(); - errorResponse.put("errorMsg", "支付请求处理异常"); - return errorResponse; - } - } -} diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayNotifyController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayNotifyController.java index 477c261c..10a864ef 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayNotifyController.java +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayNotifyController.java @@ -1,43 +1,46 @@ -//package com.central.order.controller; -// -// -//import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; -//import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; -//import com.github.binarywang.wxpay.service.WxPayService; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.web.bind.annotation.PostMapping; -//import org.springframework.web.bind.annotation.RequestBody; -//import org.springframework.web.bind.annotation.RestController; -// -//import javax.servlet.http.HttpServletRequest; -//import java.util.HashMap; -//import java.util.Map; -// -//@RestController -//public class WxPayNotifyController { -// -//// @Autowired -// private WxPayService wxPayService; -//// -// @PostMapping("/pay/notify") -// public String payNotify(HttpServletRequest request, @RequestBody String body) throws Exception { -// // 将XML格式的请求体转换为Map -// Map params = new HashMap<>(); -// // 这里需要你自己实现一个将XML转换为Map的工具方法 -// // 例如使用Dom4j或JAXB等库来解析XML -// -// // 验证签名 -// boolean signVerified = wxPayService.verifyNotify(params); -// if (!signVerified) { -// return WxPayNotifyResponse.fail("签名验证失败").toXml(); -// } -// -// // 转换为WxPayOrderNotifyResult对象(可选,但推荐) -// WxPayOrderNotifyResult notifyResult = WxPayOrderNotifyResult.fromXml(body); -// -// // 处理支付结果 -// // 根据notifyResult中的数据进行业务处理,如更新订单状态等 -// -// return WxPayNotifyResponse.success("处理成功").toXml(); -// } -//} +package com.central.order.controller; + + +import com.central.common.model.Result; +import com.wechat.pay.java.core.exception.ValidationException; +import com.wechat.pay.java.core.notification.NotificationConfig; +import com.wechat.pay.java.core.notification.NotificationParser; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@RequestMapping("/pay") +public class WxPayNotifyController { + + + NotificationConfig config; + + @PostMapping("/notify") + public Result payNotify(HttpServletRequest request, @RequestBody String body) throws Exception { + // 初始化 NotificationParser + NotificationParser parser = new NotificationParser(config); + + try { + // 以支付通知回调为例,验签、解密并转换成 Transaction +// Transaction transaction = parser.parse(requestParam, Transaction.class); + } catch (ValidationException e) { + // 签名验证失败,返回 401 UNAUTHORIZED 状态码 +// logger.error("sign verification failed", e); +// return ResponseEntity.status(HttpStatus.UNAUTHORIZED); + } + +// 如果处理失败,应返回 4xx/5xx 的状态码,例如 500 INTERNAL_SERVER_ERROR + if (true) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR); + } + +// 处理成功,返回 200 OK 状态码 + return ResponseEntity.status(HttpStatus.OK); + } +} diff --git a/zlt-business/order-center/src/main/java/com/central/order/model/Order.java b/zlt-business/order-center/src/main/java/com/central/order/model/Order.java index f742502d..286511b9 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/model/Order.java +++ b/zlt-business/order-center/src/main/java/com/central/order/model/Order.java @@ -38,7 +38,7 @@ public class Order extends SuperEntity { @TableField(fill = FieldFill.INSERT_UPDATE) private Date updateTime; - private Long createId; + private String tenantId; private String company; private Integer isDel; diff --git a/zlt-business/order-center/src/main/resources/application.yml b/zlt-business/order-center/src/main/resources/application.yml index 2cf7a260..8728ef56 100644 --- a/zlt-business/order-center/src/main/resources/application.yml +++ b/zlt-business/order-center/src/main/resources/application.yml @@ -26,18 +26,18 @@ knife4j: swagger-model-name: 订单中心接口文档 # 微信支付商户号 -wxpay: - mchId: ${zlt.wxpay.mchId} +tencent: + merchantId: ${zlt.wxpay.merchantId} # 微信支付商户密钥 - mchKey: ${zlt.wxpay.mchKey} + merchantSerialNumber: ${zlt.wxpay.merchantSerialNumber} # 微信支付申请的应用ID appId: ${zlt.wxpay.appId} # 微信支付异步通知地址(需外网可访问且与微信支付平台配置一致) notifyUrl: ${zlt.wxpay.notifyUrl} # 微信支付API证书路径(如果需要证书的情况下,这里填写证书相关文件的路径) - certPath: ${zlt.wxpay.certPath} -# 微信支付API证书密码(如果有) - certPassword: ${zlt.wxpay.certPassword} + privateKeyPath: ${zlt.wxpay.privateKeyPath} + apiV3Key: ${zlt.wxpay.apiV3Key} + #审计日志 # audit-log: # enabled: true diff --git a/zlt-config/src/main/resources/application-dev.properties b/zlt-config/src/main/resources/application-dev.properties index f81b8f88..4087cdd5 100644 --- a/zlt-config/src/main/resources/application-dev.properties +++ b/zlt-config/src/main/resources/application-dev.properties @@ -54,14 +54,14 @@ zlt.security.tencent.appSecret=9d344e6c86b42105b6e4a018f242e233 zlt.security.tencent.jscode2sessionUrl=https://api.weixin.qq.com/sns/jscode2session # 微信支付商户号 -zlt.wxpay.mchId=YOUR_MCH_ID -# 微信支付商户密钥 -zlt.wxpay.mchKey=YOUR_MCH_KEY +zlt.wxpay.merchantId=1703796518 +# 商户证书序列号 +zlt.wxpay.merchantSerialNumber=11F4AC03C50EB582D3EDE082591D1F9A65388D51 # 微信支付申请的应用ID -zlt.wxpay.appId=YOUR_APP_ID +zlt.wxpay.appId=wx30a3c8de94a85584 # 微信支付异步通知地址(需外网可访问且与微信支付平台配置一致) zlt.wxpay.notifyUrl=http://your_domain/weixin/pay/notify -# 微信支付API证书路径(如果需要证书的情况下,这里填写证书相关文件的路径) -zlt.wxpay.certPath=/path/to/your/cert.pem -# 微信支付API证书密码(如果有) -zlt.wxpay.certPassword=YOUR_CERT_PASSWORD +# 商户API私钥路径 +zlt.wxpay.privateKeyPath=D:\\wx\\WXCertUtil\\cert\\1703796518_20250114_cert\\apiclient_key.pem +# 商户APIV3密钥 +zlt.wxpay.apiV3Key=aIt5Vw3cYXSCSd4L5h14kIyDsoXBow7r