|
@@ -1,5 +1,6 @@
|
|
|
package com.fdkankan.pay.util.wx;
|
|
package com.fdkankan.pay.util.wx;
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fdkankan.pay.common.ResultCode;
|
|
import com.fdkankan.pay.common.ResultCode;
|
|
|
import com.fdkankan.pay.entity.Order;
|
|
import com.fdkankan.pay.entity.Order;
|
|
|
import com.fdkankan.pay.entity.WxConfig;
|
|
import com.fdkankan.pay.entity.WxConfig;
|
|
@@ -15,6 +16,8 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.BufferedOutputStream;
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -189,50 +192,56 @@ public class WechatPayService {
|
|
|
}
|
|
}
|
|
|
String inputLine = "";
|
|
String inputLine = "";
|
|
|
String notifyXml = "";
|
|
String notifyXml = "";
|
|
|
- while((inputLine = request.getReader().readLine()) != null){
|
|
|
|
|
- notifyXml += inputLine;
|
|
|
|
|
- }
|
|
|
|
|
- //关闭流
|
|
|
|
|
- request.getReader().close();
|
|
|
|
|
- log.info("wx-callBack--msg:{}",notifyXml);
|
|
|
|
|
- //解析成Map
|
|
|
|
|
- Map<String,String> map = WXPayUtil.xmlToMap(notifyXml);
|
|
|
|
|
-
|
|
|
|
|
- if (WXPayUtil.isSignatureValid(map,wxConfig.getMchKey(), WXPayConstants.SignType.MD5)
|
|
|
|
|
- || WXPayUtil.isSignatureValid(map, wxConfig.getMchKey(), WXPayConstants.SignType.HMACSHA256)) {
|
|
|
|
|
- //判断 支付是否成功
|
|
|
|
|
- if("SUCCESS".equals(map.get("result_code"))){
|
|
|
|
|
- //获得 返回的商户订单号
|
|
|
|
|
- String out_trade_no = map.get("out_trade_no");
|
|
|
|
|
- trade_no = map.get("transaction_id");
|
|
|
|
|
- openId = map.get("openid");
|
|
|
|
|
- log.info("微信回调返回商户订单号:out_trade_no:{},transaction_id:{},openid:{}" , out_trade_no,trade_no,openId);
|
|
|
|
|
- //修改订单状态
|
|
|
|
|
- //todo 支付成功
|
|
|
|
|
- if(!out_trade_no.equals(order.getOrderSn())){
|
|
|
|
|
- log.error("wx-call-back error :orderSn:{},out_trade_no:{}" ,order.getOrderSn(), out_trade_no);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- payFlag = true;
|
|
|
|
|
|
|
+ if(request.getContentType().contains("application/json")){
|
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(ReadAsChars(request));
|
|
|
|
|
+ log.info("wx-callBack--msgJson:{}",jsonObject);
|
|
|
|
|
+
|
|
|
|
|
+ }else {
|
|
|
|
|
+ while((inputLine = request.getReader().readLine()) != null){
|
|
|
|
|
+ notifyXml += inputLine;
|
|
|
|
|
+ }
|
|
|
|
|
+ //关闭流
|
|
|
|
|
+ request.getReader().close();
|
|
|
|
|
+ log.info("wx-callBack--msgXml:{}",notifyXml);
|
|
|
|
|
+ Map<String,String> map = WXPayUtil.xmlToMap(notifyXml);
|
|
|
|
|
+ //解析成Map
|
|
|
|
|
|
|
|
- log.info("微信支付成功,订单号:" + out_trade_no);
|
|
|
|
|
- //通知微信服务器已经支付成功
|
|
|
|
|
- notifyXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code>"
|
|
|
|
|
- + "<return_msg><![CDATA[OK]]></return_msg></xml> ";
|
|
|
|
|
- }else{
|
|
|
|
|
|
|
+ if (WXPayUtil.isSignatureValid(map,wxConfig.getMchKey(), WXPayConstants.SignType.MD5)
|
|
|
|
|
+ || WXPayUtil.isSignatureValid(map, wxConfig.getMchKey(), WXPayConstants.SignType.HMACSHA256)) {
|
|
|
|
|
+ //判断 支付是否成功
|
|
|
|
|
+ if("SUCCESS".equals(map.get("result_code"))){
|
|
|
|
|
+ //获得 返回的商户订单号
|
|
|
|
|
+ String out_trade_no = map.get("out_trade_no");
|
|
|
|
|
+ trade_no = map.get("transaction_id");
|
|
|
|
|
+ openId = map.get("openid");
|
|
|
|
|
+ log.info("微信回调返回商户订单号:out_trade_no:{},transaction_id:{},openid:{}" , out_trade_no,trade_no,openId);
|
|
|
|
|
+ //修改订单状态
|
|
|
|
|
+ //todo 支付成功
|
|
|
|
|
+ if(!out_trade_no.equals(order.getOrderSn())){
|
|
|
|
|
+ log.error("wx-call-back error :orderSn:{},out_trade_no:{}" ,order.getOrderSn(), out_trade_no);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ payFlag = true;
|
|
|
|
|
+
|
|
|
|
|
+ log.info("微信支付成功,订单号:" + out_trade_no);
|
|
|
|
|
+ //通知微信服务器已经支付成功
|
|
|
|
|
+ notifyXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code>"
|
|
|
|
|
+ + "<return_msg><![CDATA[OK]]></return_msg></xml> ";
|
|
|
|
|
+ }else{
|
|
|
|
|
+ notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
|
|
|
|
|
+ + "<return_msg><![CDATA[报文为空]]></return_msg></xml> ";
|
|
|
|
|
+ }
|
|
|
|
|
+ } else{
|
|
|
|
|
+ log.error("签名验证错误");
|
|
|
notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
|
|
notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
|
|
|
- + "<return_msg><![CDATA[报文为空]]></return_msg></xml> ";
|
|
|
|
|
|
|
+ + "<return_msg><![CDATA[签名验证错误]]></return_msg></xml>";
|
|
|
}
|
|
}
|
|
|
- } else{
|
|
|
|
|
- log.error("签名验证错误");
|
|
|
|
|
- notifyXml = "<xml><return_code><![CDATA[FAIL]]></return_code>"
|
|
|
|
|
- + "<return_msg><![CDATA[签名验证错误]]></return_msg></xml>";
|
|
|
|
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(
|
|
|
|
|
+ response.getOutputStream());
|
|
|
|
|
+ out.write(notifyXml.getBytes());
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ out.close();
|
|
|
}
|
|
}
|
|
|
- BufferedOutputStream out = new BufferedOutputStream(
|
|
|
|
|
- response.getOutputStream());
|
|
|
|
|
- out.write(notifyXml.getBytes());
|
|
|
|
|
- out.flush();
|
|
|
|
|
- out.close();
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("微信支付回调数据异常, error:", e);
|
|
log.error("微信支付回调数据异常, error:", e);
|
|
@@ -246,4 +255,39 @@ public class WechatPayService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public static String ReadAsChars(HttpServletRequest request) throws Exception{
|
|
|
|
|
+
|
|
|
|
|
+ BufferedReader br = null;
|
|
|
|
|
+ StringBuilder sb = new StringBuilder("");
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ br = request.getReader();
|
|
|
|
|
+ String str;
|
|
|
|
|
+ while ((str = br.readLine()) != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ sb.append(str);
|
|
|
|
|
+ }
|
|
|
|
|
+ br.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e)
|
|
|
|
|
+ {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ finally
|
|
|
|
|
+ {
|
|
|
|
|
+ if (null != br)
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ br.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e)
|
|
|
|
|
+ {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return sb.toString();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|