he2802 4 سال پیش
والد
کامیت
a17df43564

+ 22 - 0
zhongzheng-common/src/main/java/com/zhongzheng/common/utils/AES.java

@@ -62,4 +62,26 @@ public class AES {
         return params;
     }
 
+    public static String SHA1(String decript) {
+        try {
+            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
+            digest.update(decript.getBytes());
+            byte messageDigest[] = digest.digest();
+            // Create Hex String
+            StringBuffer hexString = new StringBuffer();
+            // 字节数组转换为 十六进制 数
+            for (int i = 0; i < messageDigest.length; i++) {
+                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
+                if (shaHex.length() < 2) {
+                    hexString.append(0);
+                }
+                hexString.append(shaHex);
+            }
+            return hexString.toString();
+
+        } catch (NoSuchAlgorithmException e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
 }

+ 42 - 6
zhongzheng-system/src/main/java/com/zhongzheng/modules/wx/service/impl/WxPayServiceImpl.java

@@ -15,6 +15,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.github.pagehelper.Page;
 import com.zhongzheng.common.config.WxSmallConfig;
 import com.zhongzheng.common.core.redis.RedisCache;
+import com.zhongzheng.common.utils.AES;
 import com.zhongzheng.common.utils.DateUtils;
 import com.zhongzheng.common.utils.ServletUtils;
 import com.zhongzheng.common.utils.SnowflakeIdUtils;
@@ -78,6 +79,9 @@ public class WxPayServiceImpl  implements IWxPayService {
     @Value("${wx.gzh.appid}")
     private String gzhAppid;
 
+    @Value("${wx.gzh.appsecret}")
+    private String gzh_appsrcret;
+
     @Value("${wx.wepay.mchid}")
     private String mchid;
 
@@ -112,15 +116,17 @@ public class WxPayServiceImpl  implements IWxPayService {
 
     @Autowired
     private ICourseService iCourseService;
+    
+
+    private String gzh_tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
 
+    private String gzh_tokenParam = "grant_type=client_credential&appid=%s&secret=%s";
 
     @Override
     public Map<String, String> payment(String out_trade_no,String openid,String body,String price) {
         WxSmallConfig config = new WxSmallConfig(appid,mchid,key);
         try {
             WXPay wxpay = new WXPay(config);
-            SnowflakeIdUtils idWorker = new SnowflakeIdUtils(3, 1);
-
             Map<String, String> data = new HashMap<String, String>();
             data.put("body", body);
             data.put("out_trade_no", out_trade_no);
@@ -155,10 +161,8 @@ public class WxPayServiceImpl  implements IWxPayService {
         WxSmallConfig config = new WxSmallConfig(gzhAppid,mchid,key);
         try {
             WXPay wxpay = new WXPay(config);
-            SnowflakeIdUtils idWorker = new SnowflakeIdUtils(3, 1);
 
             Map<String, String> data = new HashMap<String, String>();
-            data.put("appId", gzhAppid);
             data.put("attach", "pay");
             data.put("body", body);
             data.put("out_trade_no", out_trade_no);
@@ -170,7 +174,6 @@ public class WxPayServiceImpl  implements IWxPayService {
             data.put("notify_url", notifyUrl);
             data.put("trade_type", "JSAPI");
             Map<String, String> resp = wxpay.unifiedOrder(data);
-
             Map<String, String> result = new HashMap<>();
             //         result.put("provider","wxpay");
             result.put("appId",gzhAppid);
@@ -178,8 +181,9 @@ public class WxPayServiceImpl  implements IWxPayService {
             result.put("nonceStr",resp.get("nonce_str"));
             result.put("package","prepay_id="+resp.get("prepay_id"));
             result.put("signType","MD5");
+            result.put("configPaySign", wx_config_sign(result));
             result.put("paySign", WXPayUtil.generateSignature(result, key, WXPayConstants.SignType.MD5));
-            //       System.out.println(resp);
+             System.out.println(result);
             return result;
         } catch (Exception e) {
             e.printStackTrace();
@@ -188,6 +192,14 @@ public class WxPayServiceImpl  implements IWxPayService {
         return null;
     }
 
+    public String wx_config_sign(Map<String, String> result) {
+        String url="https://m.xyyxt.net/";
+        String jsapi_ticket = wx_get_jsapi_ticket();
+        String str = "jsapi_ticket="+jsapi_ticket+"&noncestr="+result.get("nonceStr")+"&timestamp="+result.get("timeStamp")+"&url="+url;
+        String signature = AES.SHA1(str);
+        return signature;
+    }
+
     @Override
     public String paymentCallBack(String notifyData) {
         WxSmallConfig config = new WxSmallConfig(appid,mchid,key);
@@ -253,4 +265,28 @@ public class WxPayServiceImpl  implements IWxPayService {
         }
         return false;
     }
+
+    public String wx_get_jsapi_ticket() {
+        String access_token = wx_get_token();
+        String param = String.format("access_token=%s&type=jsapi", access_token);
+        String resultString  = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket",param);
+        JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
+        return String.valueOf(jsonObject.get("ticket"));
+    }
+
+    //获取微信公众号access_token
+    public String wx_get_token()
+    {
+        String key = "gzh_access_token";
+        String access_token = redisCache.getCacheObject(key);
+        if(!Validator.isNotNull(access_token)){
+            String param = String.format(gzh_tokenParam, gzhAppid, gzh_appsrcret);
+            String resultString  = HttpUtils.sendGet(gzh_tokenUrl,param);
+            //解析json
+            JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
+            access_token = String.valueOf(jsonObject.get("access_token"));  //这里应该把access_token缓存起来,至于要怎么缓存就看各位了,有效期是7200s
+            redisCache.setCacheObject(key, access_token, 2*50, TimeUnit.MINUTES);//2个小时
+        }
+        return access_token;
+    }
 }