|
|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
}
|