Browse Source

feature 支付成功回调函数处理

xiahan 4 months ago
parent
commit
f6b0d8c5e9

+ 5 - 1
zhsw-common/src/main/java/com/rongwei/zhsw/system/config/WeChatLoginApiPara.java

@@ -13,7 +13,7 @@ import org.springframework.stereotype.Component;
 @Component
 @ConfigurationProperties(prefix="wechat")
 @Data
-public class WeChatLoginApiPara {
+public class WeChatAboutApiPara {
     private String appid;
 
     private String secret;
@@ -22,4 +22,8 @@ public class WeChatLoginApiPara {
 
     // 业主token是否长期有效 方便测试环境测试
     private boolean neverExpires;
+
+    private String refreshToken;
+
+    private String paymentCallbackUrl;
 }

+ 5 - 5
zhsw-common/src/main/java/com/rongwei/zhsw/system/utils/WeChatUtils.java

@@ -5,7 +5,7 @@ import com.rongwe.zhsw.system.vo.WeChatLoginReturnVo;
 import com.rongwei.commonservice.service.RedisService;
 import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import com.rongwei.rwcommon.base.exception.CustomException;
-import com.rongwei.zhsw.system.config.WeChatLoginApiPara;
+import com.rongwei.zhsw.system.config.WeChatAboutApiPara;
 import org.apache.commons.lang.StringUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.config.RequestConfig;
@@ -38,16 +38,16 @@ import static com.rongwei.zhsw.system.utils.SaveConstans.REDIS_OWNER_KEY;
 public class WeChatUtils {
     private static final Logger log = LoggerFactory.getLogger(WeChatUtils.class);
     @Autowired
-    private WeChatLoginApiPara autoWeChatLoginApiPara;
+    private WeChatAboutApiPara autoWeChatAboutApiPara;
     @Autowired
     private RedisService  autoRedisService;
 
-    private static WeChatLoginApiPara weChatLoginApiPara;
+    private static WeChatAboutApiPara weChatAboutApiPara;
     private static RedisService redisService;
 
     @PostConstruct
     public void init() {
-        weChatLoginApiPara = autoWeChatLoginApiPara;
+        weChatAboutApiPara = autoWeChatAboutApiPara;
         redisService= autoRedisService;
     }
     public static final String WECHAT_LOGIN_URL="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
@@ -55,7 +55,7 @@ public class WeChatUtils {
 
 
     public static WeChatLoginReturnVo getWeChatOpenId(String jsCode){
-        String url = String.format(WECHAT_LOGIN_URL, weChatLoginApiPara.getAppid(), weChatLoginApiPara.getSecret(), jsCode);
+        String url = String.format(WECHAT_LOGIN_URL, weChatAboutApiPara.getAppid(), weChatAboutApiPara.getSecret(), jsCode);
 
         RequestConfig config = RequestConfig.custom().setConnectTimeout(35000).setSocketTimeout(35000).build();
         CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

+ 11 - 8
zhsw-common/src/main/java/com/rongwei/zhsw/system/utils/WxPayApi.java

@@ -35,19 +35,22 @@ public class WxPayApi {
 
     private String appId;
 
-    public WxPayApi(String merchantId, String privateKeyPath , String merchantSerialNumber, String apiV3Key, String appId) {
-       this.merchantId = merchantId;
-       this.privateKeyPath = privateKeyPath;
-       this.merchantSerialNumber = merchantSerialNumber;
-       this.apiV3Key = apiV3Key;
-       this.appId = appId;
+    private String notifyUrl;
+
+    public WxPayApi(String merchantId, String privateKeyPath, String merchantSerialNumber, String apiV3Key, String appId, String notifyUrl) {
+        this.merchantId = merchantId;
+        this.privateKeyPath = privateKeyPath;
+        this.merchantSerialNumber = merchantSerialNumber;
+        this.apiV3Key = apiV3Key;
+        this.appId = appId;
+        this.notifyUrl = notifyUrl;
     }
 
     public JsapiServiceExtension service;
 
     @SneakyThrows
     public void initMerchant() {
-        String keyPath = WxPayApi.class.getClassLoader().getResource("").getPath() + privateKeyPath+"/apiclient_key.pem";
+        String keyPath = WxPayApi.class.getClassLoader().getResource("").getPath() + privateKeyPath + "/apiclient_key.pem";
         keyPath = URLDecoder.decode(keyPath, StandardCharsets.UTF_8.toString());
         Config config =
                 new RSAAutoCertificateConfig.Builder()
@@ -72,7 +75,7 @@ public class WxPayApi {
         request.setDescription(vo.getDescription());
         request.setOutTradeNo(vo.getOutTradeNo());
         // 接收通知的url
-        request.setNotifyUrl("http://61.177.40.178:8000/zhsw/wechat/payment/prepay/notice/"+vo.getDskey());
+        request.setNotifyUrl(notifyUrl + vo.getDskey());
         Amount amount = new Amount();
         amount.setTotal(vo.getTotalAmount());
         amount.setCurrency("CNY");

+ 16 - 20
zhsw-common/src/main/java/com/rongwei/zhsw/system/wechat/impl/PayMentServiceImpl.java

@@ -11,7 +11,7 @@ import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.base.exception.CustomException;
 import com.rongwei.rwcommonentity.commonservers.domain.TenantDo;
-import com.rongwei.zhsw.system.config.WeChatLoginApiPara;
+import com.rongwei.zhsw.system.config.WeChatAboutApiPara;
 import com.rongwei.zhsw.system.dao.CommonBusinessDao;
 import com.rongwei.zhsw.system.service.impl.SwBillingRecordServiceImpl;
 import com.rongwei.zhsw.system.service.impl.SwEnterpriseConfigInfoServiceImpl;
@@ -23,7 +23,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 
 import javax.crypto.Cipher;
 import javax.crypto.spec.GCMParameterSpec;
@@ -46,7 +45,7 @@ public class PayMentServiceImpl implements PayMentService {
     @Autowired
     private SwEnterpriseConfigInfoServiceImpl swEnterpriseConfigInfoService;
     @Autowired
-    private WeChatLoginApiPara weChatLoginApiPara;
+    private WeChatAboutApiPara weChatAboutApiPara;
     @Autowired
     private RedisServiceImpl redisService;
     @Autowired
@@ -101,7 +100,7 @@ public class PayMentServiceImpl implements PayMentService {
             throw new CustomException("商户支付配置错误,请联系系统管理员!");
         }
 
-        WxPayApi wxPayApi = new WxPayApi(merchantid, merchantprivatekey, merchantserialnumber, merchantsecretkey, weChatLoginApiPara.getAppid());
+        WxPayApi wxPayApi = new WxPayApi(merchantid, merchantprivatekey, merchantserialnumber, merchantsecretkey, weChatAboutApiPara.getAppid(), weChatAboutApiPara.getPaymentCallbackUrl());
         wxPayApi.initMerchant();
         // 生成订单号 规则
         String nonceStr = UUID.randomUUID().toString().replaceAll("-", "");
@@ -122,16 +121,15 @@ public class PayMentServiceImpl implements PayMentService {
      * @return
      */
     @Override
-    @Transactional
-    public R prepayNotice(PrepayNoticeVo prepayNoticeVo,String dskey)  {
+    public R prepayNotice(PrepayNoticeVo prepayNoticeVo, String dskey) {
         log.info("获取到的回调信息接口:{}", prepayNoticeVo);
         List<String> dsKeys;
-        if(StringUtils.isBlank(dskey)){
+        if (StringUtils.isBlank(dskey)) {
             List<TenantDo> tenantList = (List<TenantDo>) redisService.getRedisCatchObj("allTenants");
             // 获取 所有的主库信息
-            dsKeys= tenantList.stream().map(TenantDo::getDskey).collect(Collectors.toList());
-        }else{
-            dsKeys= Collections.singletonList(dskey);
+            dsKeys = tenantList.stream().map(TenantDo::getDskey).collect(Collectors.toList());
+        } else {
+            dsKeys = Collections.singletonList(dskey);
         }
         dsKeys.remove("incontrol");
         // 获取所有商户的密钥
@@ -139,14 +137,12 @@ public class PayMentServiceImpl implements PayMentService {
         String analysisStr = null;
         String deKey = null;
         for (SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo : secretKeyList) {
-            byte[] apiV3Key = Base64.getDecoder().decode(swEnterpriseConfigInfoDo.getMerchantsecretkey());
-            byte[] nonce = Base64.getDecoder().decode(prepayNoticeVo.getResource().getNonce());
-            byte[] ciphertext = Base64.getDecoder().decode(prepayNoticeVo.getResource().getCiphertext());
-            byte[] associatedData = Base64.getDecoder().decode(prepayNoticeVo.getResource().getAssociated_data());
-            log.error("微信回调函数");
-            Cipher cipher = null;
             try {
-                cipher = Cipher.getInstance("AES/GCM/NoPadding");
+                byte[] apiV3Key = swEnterpriseConfigInfoDo.getMerchantsecretkey().getBytes("UTF8");
+                byte[] nonce = prepayNoticeVo.getResource().getNonce().getBytes("UTF8");
+                String ciphertext = prepayNoticeVo.getResource().getCiphertext();
+                byte[] associatedData = prepayNoticeVo.getResource().getAssociated_data().getBytes("UTF8");
+                Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
                 SecretKeySpec key = new SecretKeySpec(apiV3Key, "AES");
                 GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);
                 cipher.init(Cipher.DECRYPT_MODE, key, spec);
@@ -157,7 +153,7 @@ public class PayMentServiceImpl implements PayMentService {
                     break;
                 }
             } catch (Exception e) {
-                log.error("解密异常");
+                log.error("当前租户:{}解密异常",swEnterpriseConfigInfoDo.getTenantid());
             }
         }
         if (StringUtils.isBlank(analysisStr)) {
@@ -169,7 +165,7 @@ public class PayMentServiceImpl implements PayMentService {
             transaction = mapper.readValue(analysisStr, WeChatPayTransactionVo.class);
         } catch (IOException e) {
             e.printStackTrace();
-           log.error("JSON转换异常");
+            log.error("JSON转换异常");
             return R.error();
         }
         // 微信订单号
@@ -177,7 +173,7 @@ public class PayMentServiceImpl implements PayMentService {
         // 商户号
         String outTradeNo = transaction.getOutTradeNo();
         // 更新缴费记录相关信息
-        swBillingRecordService.getBaseMapper().updateWeChatPayInfo(deKey,outTradeNo,transactionId,analysisStr);
+        swBillingRecordService.getBaseMapper().updateWeChatPayInfo(deKey, outTradeNo, transactionId, analysisStr);
         return R.ok();
     }
 }

+ 3 - 5
zhsw-common/src/main/java/com/rongwei/zhsw/system/wechat/impl/WeChatLoginServiceImpl.java

@@ -1,9 +1,7 @@
 package com.rongwei.zhsw.system.wechat.impl;
 
-import com.google.common.base.Strings;
 import com.rongwe.zhsw.system.domain.SwBusinesshallDo;
 import com.rongwe.zhsw.system.domain.SwEnterpriseConfigInfoDo;
-import com.rongwe.zhsw.system.domain.SwNotificationAnnouncementDo;
 import com.rongwe.zhsw.system.vo.*;
 import com.rongwei.commonservice.service.RedisService;
 import com.rongwei.rwadmincommon.system.vo.JwtAuthenticationRequest;
@@ -11,7 +9,7 @@ import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.base.exception.CustomException;
 import com.rongwei.rwcommonentity.commonservers.domain.TenantDo;
-import com.rongwei.zhsw.system.config.WeChatLoginApiPara;
+import com.rongwei.zhsw.system.config.WeChatAboutApiPara;
 import com.rongwei.zhsw.system.dao.CommonBusinessDao;
 import com.rongwei.zhsw.system.dao.SwNotificationAnnouncementDao;
 import com.rongwei.zhsw.system.fegin.LoginAuth;
@@ -51,7 +49,7 @@ public class WeChatLoginServiceImpl implements WeChatLoginService {
     @Autowired
     private SwUserManagementServiceImpl swUserManagementService;
     @Autowired
-    private WeChatLoginApiPara autoWeChatLoginApiPara;
+    private WeChatAboutApiPara autoWeChatAboutApiPara;
     @Autowired
     private SwBusinesshallServiceImpl swBusinesshallService;
     @Autowired
@@ -91,7 +89,7 @@ public class WeChatLoginServiceImpl implements WeChatLoginService {
             tenantDo.setId(userByDsKey.get(0).getDsId());
         }
         sysUserVo.setTenantDo(tenantDo);
-        if(autoWeChatLoginApiPara.isNeverExpires()){
+        if(autoWeChatAboutApiPara.isNeverExpires()){
             redisService.redisCatchInit("token-" + token, sysUserVo, -1);
         }else {
             redisService.redisCatchInit("token-" + token, sysUserVo, DEFAULT_OWNER_CACHE_SECONDS);

+ 5 - 8
zhsw-entity/src/main/java/com/rongwe/zhsw/system/vo/WeChatPayTransactionVo.java

@@ -19,6 +19,7 @@ public class WeChatPayTransactionVo {
     private Amount amount;
     @JsonProperty("mchid")
     private String merchantId;
+    private String appid;
     @JsonProperty("trade_state")
     private String tradeState;
     @JsonProperty("bank_type")
@@ -30,7 +31,6 @@ public class WeChatPayTransactionVo {
     private Payer payer;
     @JsonProperty("out_trade_no")
     private String outTradeNo;
-    private String appid;
     @JsonProperty("trade_state_desc")
     private String tradeStateDesc;
     @JsonProperty("trade_type")
@@ -50,7 +50,7 @@ public class WeChatPayTransactionVo {
 
         // Getters and Setters
     }
-
+    @Data
     class PromotionDetail {
         private int amount;
         @JsonProperty("wechatpay_contribute")
@@ -71,7 +71,7 @@ public class WeChatPayTransactionVo {
 
         // Getters and Setters
     }
-
+    @Data
     class GoodsDetail {
         @JsonProperty("goods_remark")
         private String goodsRemark;
@@ -85,17 +85,14 @@ public class WeChatPayTransactionVo {
 
         // Getters and Setters
     }
-
+    @Data
     class Payer {
         private String openid;
-
-        // Getters and Setters
     }
 
+    @Data
     class SceneInfo {
         @JsonProperty("device_id")
         private String deviceId;
-
-        // Getters and Setters
     }
 }