调解系统后端服务

WxPayElegentValid.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.ruoyi.wx;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ruoyi.annotation.TradePlatform;
  4. import com.ruoyi.constant.Platform;
  5. import com.ruoyi.core.ElegentValid;
  6. import com.ruoyi.dto.ValidResponse;
  7. import com.ruoyi.exceptions.TradeException;
  8. import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
  9. import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
  10. import com.wechat.pay.contrib.apache.httpclient.notification.Notification;
  11. import com.wechat.pay.contrib.apache.httpclient.notification.NotificationHandler;
  12. import com.wechat.pay.contrib.apache.httpclient.notification.NotificationRequest;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.HttpEntity;
  16. import org.springframework.http.HttpHeaders;
  17. import org.springframework.stereotype.Component;
  18. import javax.servlet.http.HttpServletRequest;
  19. import java.nio.charset.StandardCharsets;
  20. import java.util.Map;
  21. @Component
  22. @Slf4j
  23. @TradePlatform(Platform.WX)
  24. public class WxPayElegentValid implements ElegentValid {
  25. @Autowired
  26. private WxpayConfig wxpayConfig;
  27. @Override
  28. public ValidResponse validPay(HttpEntity<String> httpEntity, HttpServletRequest httpServletRequest) throws TradeException {
  29. ValidResponse validResponse=new ValidResponse();
  30. try {
  31. //获取请求头
  32. HttpHeaders headers = httpEntity.getHeaders();
  33. //构建微信请求数据对象
  34. NotificationRequest request = new NotificationRequest.Builder()
  35. .withSerialNumber(headers.getFirst("Wechatpay-Serial")) //证书序列号(微信平台)
  36. .withNonce(headers.getFirst("Wechatpay-Nonce")) //随机串
  37. .withTimestamp(headers.getFirst("Wechatpay-Timestamp")) //时间戳
  38. .withSignature(headers.getFirst("Wechatpay-Signature")) //签名字符串
  39. .withBody(httpEntity.getBody())
  40. .build();
  41. //微信通知的业务处理
  42. //验证签名,确保请求来自微信
  43. Map jsonData = null;
  44. try {
  45. //确保在管理器中存在自动更新的商户证书
  46. CertificatesManager certificatesManager = CertificatesManager.getInstance();
  47. Verifier verifier = certificatesManager.getVerifier(wxpayConfig.getMchId());
  48. //验签和解析请求数据
  49. NotificationHandler notificationHandler = new NotificationHandler(verifier, wxpayConfig.getApiV3Key().getBytes(StandardCharsets.UTF_8));
  50. Notification notification = notificationHandler.parse(request);
  51. if (!"TRANSACTION.SUCCESS".equals(notification.getEventType())) {
  52. validResponse.setValid(false);
  53. return validResponse;
  54. }
  55. //获取解密后的数据
  56. jsonData = JSON.parseObject(notification.getDecryptData(),Map.class );
  57. log.info("解密后的数据为:"+jsonData);
  58. } catch (Exception e) {
  59. throw new TradeException("验签失败");
  60. }
  61. if (!"SUCCESS".equals(jsonData.get("trade_state"))) {
  62. validResponse.setValid(false);
  63. return validResponse;
  64. }
  65. validResponse.setValid(true);
  66. validResponse.setOrderSn( (String) jsonData.get("out_trade_no") ); //订单号
  67. return validResponse;
  68. } catch (Exception e) {
  69. validResponse.setValid(false);
  70. return validResponse;
  71. }
  72. }
  73. @Override
  74. public ValidResponse validRefund(HttpEntity<String> httpEntity, HttpServletRequest httpServletRequest) throws TradeException {
  75. ValidResponse validResponse=new ValidResponse();
  76. try {
  77. //获取请求头
  78. HttpHeaders headers = httpEntity.getHeaders();
  79. //构建微信请求数据对象
  80. NotificationRequest request = new NotificationRequest.Builder()
  81. .withSerialNumber(headers.getFirst("Wechatpay-Serial")) //证书序列号(微信平台)
  82. .withNonce(headers.getFirst("Wechatpay-Nonce")) //随机串
  83. .withTimestamp(headers.getFirst("Wechatpay-Timestamp")) //时间戳
  84. .withSignature(headers.getFirst("Wechatpay-Signature")) //签名字符串
  85. .withBody(httpEntity.getBody())
  86. .build();
  87. //微信通知的业务处理
  88. Map jsonData = null;
  89. //验证签名,确保请求来自微信
  90. try {
  91. //确保在管理器中存在自动更新的商户证书
  92. CertificatesManager certificatesManager = CertificatesManager.getInstance();
  93. Verifier verifier = certificatesManager.getVerifier(wxpayConfig.getMchId());
  94. //验签和解析请求数据
  95. NotificationHandler notificationHandler = new NotificationHandler(verifier, wxpayConfig.getApiV3Key().getBytes(StandardCharsets.UTF_8));
  96. Notification notification = notificationHandler.parse(request);
  97. if (!"REFUND.SUCCESS".equals(notification.getEventType())) {
  98. //非成功请求直接返回,理论上都是成功的请求
  99. validResponse.setValid(false);
  100. return validResponse;
  101. }
  102. //获取解密后的数据
  103. jsonData = JSON.parseObject( notification.getDecryptData(),Map.class );
  104. } catch (Exception e) {
  105. throw new TradeException("验签失败");
  106. }
  107. if (!"SUCCESS".equals(jsonData.get("refund_status"))) {
  108. //非成功请求直接返回,理论上都是成功的请求
  109. validResponse.setValid(false);
  110. return validResponse;
  111. }
  112. //交易单号
  113. validResponse.setValid(true);
  114. validResponse.setOrderSn( (String) jsonData.get("out_trade_no") ); //订单号
  115. return validResponse;
  116. } catch (Exception e) {
  117. //非成功请求直接返回,理论上都是成功的请求
  118. validResponse.setValid(false);
  119. return validResponse;
  120. }
  121. }
  122. @Override
  123. public String successResult() {
  124. return JSON.toJSONString(WxpayConstant.SUCCESS ) ;
  125. }
  126. @Override
  127. public String failResult() {
  128. return JSON.toJSONString( WxpayConstant.FAIL);
  129. }
  130. }