anderx 4 anos atrás
pai
commit
1f33e99388

+ 35 - 15
src/main/java/com/kede/common/controller/PublicApiController.java

@@ -3,9 +3,13 @@ package com.kede.common.controller;
 
 
 
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.util.Map;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.collections4.map.HashedMap;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -19,8 +23,11 @@ import com.kede.activity.service.ActivityService;
 import com.kede.banners.service.BannersService;
 import com.kede.common.bo.CallBackBo;
 import com.kede.common.bo.Result;
+import com.kede.common.constant.AFTConstants;
 import com.kede.common.constant.ErrorConstants;
+import com.kede.common.error.BusinessException;
 import com.kede.common.model.VisitingCustomers;
+import com.kede.common.utils.DateUtils;
 import com.kede.common.utils.LoggerUtils;
 import com.kede.common.utils.StringUtils;
 import com.kede.customerCase.bo.InputCustomerCase;
@@ -34,6 +41,8 @@ import com.kede.project.service.ProjectTypeService;
 import com.kede.recruitment.bo.InputRecruitment;
 import com.kede.recruitment.service.RecruitmentService;
 import com.kede.visitingCustomers.service.VisitingCustomersService;
+import com.kede.wxsdk.WXPayConstants;
+import com.kede.wxsdk.WXPayUtil;
 import com.kede.wxsdk.service.WxService;
 
 
@@ -62,8 +71,14 @@ public class PublicApiController extends BaseController {
 	@Resource
 	private WxService	wxService;
 	
-	
-
+	/**
+	 * 微信支付,通知--- 成功结果
+	 */
+	public static String NOTIFY_SUCCESS = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
+    /**
+	 * 微信支付,通知--- 失败结果
+	 */
+	public static String NOTIFY_FAIL = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[报文为空]]></return_msg></xml>";
 	
 	@RequestMapping(value = "/listNews", method = RequestMethod.GET)
 	public Result listNews(InputNews in,Integer pageSize,Integer pageNo) {
@@ -147,19 +162,24 @@ public class PublicApiController extends BaseController {
 	
 	
 	@RequestMapping(value = "/payCallBack", method = RequestMethod.POST)
-	public Map<String, String> wxPayCallBack(@RequestBody String data) {
-		CallBackBo cb = JSON.parseObject(data, CallBackBo.class);
-		LoggerUtils.debug(getClass(), "返回参数 request=%s",cb);
-		Map<String, String> map=new HashedMap<>();
-		if (cb.getSummary().equals("支付成功")) {
-			wxService.wxPayCallBack(cb);
-			map.put("code", "SUCCESS");
-			map.put("message", "成功");
-		}else {
-			map.put("code", "FAIL");
-			map.put("message", "失败");
-		}
-		return map;
+	public String wxPayCallBack(HttpServletRequest req, HttpServletResponse response) {
+		LoggerUtils.debug(getClass(), "===============微信支付回调==========================");
+//		CallBackBo cb = JSON.parseObject(data, CallBackBo.class);
+//		LoggerUtils.debug(getClass(), "返回参数 request=%s",cb);
+//		Map<String, String> map=new HashedMap<>();
+//		if (cb.getSummary().equals("支付成功")) {
+//			wxService.wxPayCallBack(cb);
+//			map.put("code", "SUCCESS");
+//			map.put("message", "成功");
+//		}else {
+//			map.put("code", "FAIL");
+//			map.put("message", "失败");
+//		}
+		
+		// 1、读取微信通知 --- xml 格式字符串
+		wxService.wxPayCallBack(req);
+		
+		return NOTIFY_FAIL;
 	}
 
 	

+ 1 - 1
src/main/java/com/kede/user/controller/UserApiController.java

@@ -18,7 +18,7 @@ import com.kede.user.service.UserService;
 import com.kede.wxsdk.service.WxService;
 
 @RestController
-@RequestMapping(value = "/api/user")
+@RequestMapping(value = "/open/user")
 public class UserApiController extends BaseController{
 
 	

+ 108 - 15
src/main/java/com/kede/wxsdk/service/WxService.java

@@ -1,6 +1,9 @@
 package com.kede.wxsdk.service;
 
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.net.InetAddress;
 import java.security.GeneralSecurityException;
@@ -9,9 +12,15 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
@@ -26,6 +35,7 @@ import com.kede.common.utils.WxAPIV3AesUtil;
 import com.kede.core.mybatis.JDBCIdGenerator;
 import com.kede.core.shiro.token.TokenManager;
 import com.kede.order.service.impl.OrderService;
+import com.kede.wxsdk.WXPayConstants;
 import com.kede.wxsdk.WXPayUtil;
 
 @Service
@@ -137,7 +147,8 @@ public class WxService {
 					IWxPayConfig iWxPayConfig=new IWxPayConfig(appId,mchId,apiKiy,certPath);
 				    wxpay = new WXPay(iWxPayConfig,null, true, false); // *** 注入自己实现的微信配置类, 创建WXPay核心类, WXPay 包括统一下单接口
 					Map<String, String> data = new HashMap<String, String>();
-					data.put("transaction_id", orderNo); 
+//					data.put("transaction_id", orderNo);
+					data.put("out_trade_no", orderNo);
 					data.put("mchid", mchId); 
 					map = wxpay.orderQuery(data); 
 					LoggerUtils.debug(getClass(),"微信查询成功, 返回值 response={%s}", map);
@@ -172,26 +183,108 @@ public class WxService {
 	}
 
 	
-	public void wxPayCallBack(CallBackBo cb) {
-		//解密回调信息
-		byte[] bytes = StringUtils.strToByte(apiv3);
-		byte[] associatedData=StringUtils.strToByte(cb.getResource().getAssociatedData());
-		byte[] nonce=StringUtils.strToByte(cb.getResource().getNonce());
-		WxAPIV3AesUtil aesUtil = new WxAPIV3AesUtil(bytes);
-		String decryptToString = "";
+//	public void wxPayCallBack(CallBackBo cb) {
+//		//解密回调信息
+//		byte[] bytes = StringUtils.strToByte(apiv3);
+//		byte[] associatedData=StringUtils.strToByte(cb.getResource().getAssociatedData());
+//		byte[] nonce=StringUtils.strToByte(cb.getResource().getNonce());
+//		WxAPIV3AesUtil aesUtil = new WxAPIV3AesUtil(bytes);
+//		String decryptToString = "";
+//		try {
+//			decryptToString = aesUtil.decryptToString(associatedData,nonce,cb.getResource().getCiphertext());
+//		} catch (GeneralSecurityException | IOException e) {
+//			e.printStackTrace();
+//		}
+//		JSONObject json=JSON.parseObject(decryptToString);
+//		String transaction_id=(String) json.get("transaction_id");
+//		String out_trade_no=(String) json.get("out_trade_no");
+//		LoggerUtils.debug(getClass(), "transaction_id="+transaction_id+",out_trade_no="+out_trade_no);
+//		orderMapper.updateByOrderNo(out_trade_no,transaction_id);
+//	}
+
+	public void wxPayCallBack(HttpServletRequest req) {
+		BufferedReader reader;
 		try {
-			decryptToString = aesUtil.decryptToString(associatedData,nonce,cb.getResource().getCiphertext());
-		} catch (GeneralSecurityException | IOException e) {
-			e.printStackTrace();
+			reader = req.getReader();
+		String line ; 
+		StringBuilder sb = new  StringBuilder();
+		while((line = reader.readLine()) != null) {
+			sb.append(line);
+		}
+		LoggerUtils.debug(getClass(),"accept wechat pay return message , {}" , sb.toString());
+		// 2、xml字符串 转换为 map对象
+		Map<String, String> map = WXPayUtil.xmlToMap(sb.toString());
+		LoggerUtils.debug(getClass(), "获取参数%s",map.toString());
+		// 3、尝试从缓存中获取,判断是否已经接收过通知
+		if (map.get("result_code").equals(WXPayConstants.SUCCESS)
+				&& WXPayConstants.SUCCESS.equals(map.get("return_code"))) {
 		}
-		JSONObject json=JSON.parseObject(decryptToString);
-		String transaction_id=(String) json.get("transaction_id");
-		String out_trade_no=(String) json.get("out_trade_no");
-		LoggerUtils.debug(getClass(), "transaction_id="+transaction_id+",out_trade_no="+out_trade_no);
+		String out_trade_no=map.get("out_trade_no");
+		String transaction_id=map.get("transaction_id");
+		LoggerUtils.debug(getClass(),"nofity error , {}" , sb.toString());
 		orderMapper.updateByOrderNo(out_trade_no,transaction_id);
+		LoggerUtils.debug(getClass(),"\n\n\n\n\n>>>>>>>>>>>>>>>>>>>>>>>>微信回调结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n\n\n");
+		} catch ( Exception e) {
+			e.printStackTrace();
+			throw new BusinessException("回调解析异常");
+		}
 	}
+	
+	/**
+	 * XML格式字符串转换为Map
+	 *
+	 * @param strXML XML字符串
+	 * @return XML数据转换后的Map
+	 * @throws Exception
+	 */
+	public Map<String, Object> xmlToMap(String strXML) throws Exception {
+		Map<String, Object> data = new HashMap<String, Object>();
+		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
+		InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
+		org.w3c.dom.Document doc = documentBuilder.parse(stream);
+		doc.getDocumentElement().normalize();
+		NodeList nodeList = doc.getDocumentElement().getChildNodes();
+		for (int idx = 0; idx < nodeList.getLength(); ++idx) {
+			Node node = nodeList.item(idx);
+			if (node.getNodeType() == Node.ELEMENT_NODE) {
+				org.w3c.dom.Element element = (org.w3c.dom.Element) node;
+				data.put(element.getNodeName(), element.getTextContent());
+			}
+		}
+		try {
+			stream.close();
+		} catch (Exception ex) {
 
+		}
+		return data;
+	}
 	
+	 /**
+     * IO解析获取微信的数据
+     * 
+     * @param request
+     * @return
+     */
+    public  String getXmlString(HttpServletRequest request) {
+        BufferedReader reader = null;
+        String line = "";
+        String xmlString = null;
+        try {
+            reader = request.getReader();
+            StringBuffer inputString = new StringBuffer();
+
+            while ((line = reader.readLine()) != null) {
+                inputString.append(line);
+            }
+            xmlString = inputString.toString();
+        } catch (Exception e) {
+            
+        }
 
+        return xmlString;
+    }
+
+	
 	
 }