|
@@ -20,7 +20,15 @@ import weixin.popular.api.SnsAPI;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
-import java.util.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.SortedMap;
|
|
|
+import java.util.TreeMap;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* @author xufeng
|
|
@@ -66,10 +74,12 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
this.configInit(getPayConfig(product.getGameId(), product.getPayWay(), product.getPayDevice()));
|
|
|
switch (product.getPayDevice()) {
|
|
|
case 1:
|
|
|
+ //PC端
|
|
|
return this.mobile(product, "NATIVE");
|
|
|
case 2:
|
|
|
- case 3:
|
|
|
return this.mobile(product, "MWEB");
|
|
|
+// case 3:
|
|
|
+// return this.mobile(product, "MWEB");
|
|
|
case 4:
|
|
|
return this.mp(product);
|
|
|
default:
|
|
@@ -117,7 +127,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
// 账号信息
|
|
|
String key = config.getApiKey();
|
|
|
// 判断签名是否正确
|
|
|
- if (PayCommonUtil.isTenpaySign(Constants.UTF8, packageParams, key)) {
|
|
|
+ if (PayCommonUtil.isTenPaySign(Constants.UTF8, packageParams, key)) {
|
|
|
log.info("微信支付成功回调");
|
|
|
// 处理业务开始
|
|
|
String resXml;
|
|
@@ -202,12 +212,12 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
* @param product
|
|
|
* @return
|
|
|
*/
|
|
|
- public ResultMap mp(ProductPayParamBO product) {
|
|
|
+ private ResultMap mp(ProductPayParamBO product) {
|
|
|
//redirect_uri 需要在微信支付端添加认证网址
|
|
|
totalFee = WxPayUtil.subZeroAndDot(this.totalFee);
|
|
|
- String redirect_uri = serverUrl + "weixinMobile/dopay?outTradeNo=" + product.getOutTradeNo() + "&totalFee=" + totalFee;
|
|
|
+ String redirectUri = serverUrl + "weixinMobile/dopay?outTradeNo=" + product.getOutTradeNo() + "&totalFee=" + totalFee;
|
|
|
//也可以通过state传递参数 redirect_uri 后面加参数未经过验证
|
|
|
- String result = SnsAPI.connectOauth2Authorize(config.getAppId(), redirect_uri, true, null);
|
|
|
+ String result = SnsAPI.connectOauth2Authorize(config.getAppId(), redirectUri, true, null);
|
|
|
return ResultMap.ok(result);
|
|
|
}
|
|
|
|
|
@@ -247,40 +257,62 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
Map map = XMLUtil.doXMLParse(resXml);
|
|
|
log.info("订单号:{} 微信响应 map:{}", product.getOutTradeNo(), map);
|
|
|
String returnCode = (String) map.get("return_code");
|
|
|
- if (Constants.SUCCESS.equalsIgnoreCase(returnCode)) {
|
|
|
- String resultCode = (String) map.get("result_code");
|
|
|
- if (Constants.SUCCESS.equalsIgnoreCase(resultCode)) {
|
|
|
- log.info("订单号:{}生成微信支付码成功", product.getOutTradeNo());
|
|
|
- String urlCode = tradeType.equals("MWEB") ? (String) map.get("mweb_url") : (String) map.get("code_url");
|
|
|
- HashMap<String, String> result = new HashMap<>();
|
|
|
- if (tradeType.equals("MWEB")) {
|
|
|
- try {
|
|
|
- String shortUrl = wxPayUtil.shortUrl(urlCode);
|
|
|
- if (!Constants.FAIL.equalsIgnoreCase(shortUrl)) {
|
|
|
- result.put("AppLink", shortUrl);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("订单号:{} 生成微信app支付链接失败:{}", product.getOutTradeNo(), e);
|
|
|
- }
|
|
|
- }
|
|
|
- result.put("fromData", urlCode);
|
|
|
- return ResultMap.ok(product.getPayDevice(), urlCode);
|
|
|
- } else {
|
|
|
- String errCodeDes = (String) map.get("err_code_des");
|
|
|
- log.error("订单号:{}生成微信支付码(系统)失败:{}", product.getOutTradeNo(), errCodeDes);
|
|
|
- return ResultMap.error(errCodeDes);
|
|
|
- }
|
|
|
- } else {
|
|
|
+ //返回失败
|
|
|
+ if (!Constants.SUCCESS.equalsIgnoreCase(returnCode)) {
|
|
|
String returnMsg = (String) map.get("return_msg");
|
|
|
log.error("(订单号:{}生成微信支付码(通信)失败:{}", product.getOutTradeNo(), returnMsg);
|
|
|
return ResultMap.error(returnMsg);
|
|
|
}
|
|
|
+ //结果失败
|
|
|
+ String resultCode = (String) map.get("result_code");
|
|
|
+ if (!Constants.SUCCESS.equalsIgnoreCase(resultCode)) {
|
|
|
+ String errCodeDes = (String) map.get("err_code_des");
|
|
|
+ log.error("订单号:{}生成微信支付码(系统)失败:{}", product.getOutTradeNo(), errCodeDes);
|
|
|
+ return ResultMap.error(errCodeDes);
|
|
|
+ }
|
|
|
+ //成功
|
|
|
+ log.info("订单号:{}生成微信支付码成功", product.getOutTradeNo());
|
|
|
+ String urlCode = tradeType.equals("MWEB") ? (String) map.get("mweb_url") : (String) map.get("code_url");
|
|
|
+ //转成deepLink
|
|
|
+ if (tradeType.equals("MWEB")) {
|
|
|
+ urlCode = this.readUrl(urlCode);
|
|
|
+ }
|
|
|
+ return ResultMap.ok(product.getPayDevice(), urlCode);
|
|
|
} catch (Exception e) {
|
|
|
log.error("订单号:{}生成微信支付码失败(系统异常))", product.getOutTradeNo(), e);
|
|
|
return ResultMap.error();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 转换deepLink
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private String readUrl(String url) throws Exception {
|
|
|
+ // 定义好匹配规则
|
|
|
+ String regex = "weixin://wap/pay?.[^\\x{4e00}-\\x{9fa5}-\"]+";
|
|
|
+ Pattern pattern = Pattern.compile(regex);
|
|
|
+ URL payUrl = new URL(url);
|
|
|
+ URLConnection con = payUrl.openConnection();
|
|
|
+ // 加referer防盗链,必须是你申请的支付白名单
|
|
|
+ con.setRequestProperty("Referer", "https://test.game.hzshengmel.com");
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String result = "";
|
|
|
+ String realLineStr = null;
|
|
|
+ while ((realLineStr = br.readLine()) != null) {
|
|
|
+ // 一行行查找
|
|
|
+ Matcher matcher = pattern.matcher(realLineStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ result = matcher.group();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 配置初始化
|
|
|
*/
|