|
@@ -6,6 +6,7 @@ import com.zanxiang.common.domain.ResultMap;
|
|
import com.zanxiang.common.enums.ResEnum;
|
|
import com.zanxiang.common.enums.ResEnum;
|
|
import com.zanxiang.common.utils.JsonUtil;
|
|
import com.zanxiang.common.utils.JsonUtil;
|
|
import com.zanxiang.common.utils.RandomStringUtil;
|
|
import com.zanxiang.common.utils.RandomStringUtil;
|
|
|
|
+import com.zanxiang.common.utils.URIUtil;
|
|
import com.zanxiang.sdk.common.util.*;
|
|
import com.zanxiang.sdk.common.util.*;
|
|
import com.zanxiang.sdk.domain.bo.ProductPayAttachParamBO;
|
|
import com.zanxiang.sdk.domain.bo.ProductPayAttachParamBO;
|
|
import com.zanxiang.sdk.domain.bo.ProductPayParamBO;
|
|
import com.zanxiang.sdk.domain.bo.ProductPayParamBO;
|
|
@@ -13,15 +14,18 @@ import com.zanxiang.sdk.domain.bo.WxPayConfigBO;
|
|
import com.zanxiang.sdk.service.OrderPayService;
|
|
import com.zanxiang.sdk.service.OrderPayService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.jdom.JDOMException;
|
|
import org.jdom.JDOMException;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
import weixin.popular.api.SnsAPI;
|
|
import weixin.popular.api.SnsAPI;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.SortedMap;
|
|
import java.util.SortedMap;
|
|
import java.util.TreeMap;
|
|
import java.util.TreeMap;
|
|
@@ -45,6 +49,11 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
*/
|
|
*/
|
|
private static final String WX_PAY_MWEB = "MWEB";
|
|
private static final String WX_PAY_MWEB = "MWEB";
|
|
|
|
|
|
|
|
+
|
|
|
|
+ * JSAPI支付
|
|
|
|
+ */
|
|
|
|
+ private static final String WX_PAY_JSAPI = "JSAPI";
|
|
|
|
+
|
|
|
|
|
|
* 服务器域名
|
|
* 服务器域名
|
|
*/
|
|
*/
|
|
@@ -70,6 +79,10 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
|
|
|
private String outTradeNo;
|
|
private String outTradeNo;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
* 支付调起
|
|
* 支付调起
|
|
*
|
|
*
|
|
@@ -92,10 +105,9 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
|
|
|
return this.mobile(product, WX_PAY_MWEB);
|
|
return this.mobile(product, WX_PAY_MWEB);
|
|
|
|
|
|
-
|
|
|
|
case 4:
|
|
case 4:
|
|
-
|
|
+
|
|
- return this.mp(product);
|
|
+ return this.miniAppPay(product);
|
|
default:
|
|
default:
|
|
throw new RuntimeException("未知支付方式");
|
|
throw new RuntimeException("未知支付方式");
|
|
}
|
|
}
|
|
@@ -122,7 +134,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
inputStream.close();
|
|
inputStream.close();
|
|
log.info("微信支付回调 body:{}", sb);
|
|
log.info("微信支付回调 body:{}", sb);
|
|
|
|
|
|
- Map m = XMLUtil.doXmlParse(sb.toString());
|
|
+ Map m = XMLUtil.xmlToMap(sb.toString());
|
|
|
|
|
|
SortedMap<Object, Object> packageParams = new TreeMap<>();
|
|
SortedMap<Object, Object> packageParams = new TreeMap<>();
|
|
for (Object parameter : m.keySet()) {
|
|
for (Object parameter : m.keySet()) {
|
|
@@ -196,6 +208,72 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
return ResultMap.ok(result);
|
|
return ResultMap.ok(result);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private ResultMap miniAppPay(ProductPayParamBO product) {
|
|
|
|
+ try {
|
|
|
|
+ Map<Object, Object> miniMap = new HashMap<>(5);
|
|
|
|
+
|
|
|
|
+ Map<String, String> paramMap = new HashMap<>(4);
|
|
|
|
+ paramMap.put("appid", config.getAppId());
|
|
|
|
+ paramMap.put("secret", config.getAppSecret());
|
|
|
|
+ paramMap.put("js_code", product.getCode());
|
|
|
|
+ paramMap.put("grant_type", "authorization_code");
|
|
|
|
+
|
|
|
|
+ String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/sns/jscode2session", paramMap, false);
|
|
|
|
+ String sr = restTemplate.getForObject(url, String.class);
|
|
|
|
+
|
|
|
|
+ Map<String, String> userMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
+
|
|
|
|
+ String nonceStr = WxPayUtil.generateNonceStr();
|
|
|
|
+
|
|
|
|
+ Map<String, String> paramData = new HashMap<>(13);
|
|
|
|
+ paramData.put("appid", config.getAppId());
|
|
|
|
+ paramData.put("mch_id", config.getMchId());
|
|
|
|
+ paramData.put("nonce_str", WxPayUtil.generateNonceStr());
|
|
|
|
+ paramData.put("openid", userMap == null ? null : userMap.get("openid"));
|
|
|
|
+ paramData.put("body", body);
|
|
|
|
+ paramData.put("out_trade_no", outTradeNo);
|
|
|
|
+ paramData.put("fee_type", "CNY");
|
|
|
|
+ paramData.put("total_fee", totalFee);
|
|
|
|
+ paramData.put("spbill_create_ip", product.getSpbillCreateIp());
|
|
|
|
+ paramData.put("notify_url", notifyUrl);
|
|
|
|
+ paramData.put("trade_type", WX_PAY_JSAPI);
|
|
|
|
+ paramData.put("sign_type", config.getSignType());
|
|
|
|
+ paramData.put("sign", WxPayUtil.generateSignature(paramData, config.getApiKey()));
|
|
|
|
+
|
|
|
|
+ String result = restTemplate.postForObject(WxPayUrl.UNIFIED_ORDER_URL, XMLUtil.mapToXml(paramData), String.class);
|
|
|
|
+
|
|
|
|
+ long timeStamp = System.currentTimeMillis() / 1000;
|
|
|
|
+
|
|
|
|
+ String createTime = String.valueOf(timeStamp);
|
|
|
|
+ Map<String, String> successMap = XMLUtil.xmlToMap(result);
|
|
|
|
+
|
|
|
|
+ String returnCode = successMap.get("return_code");
|
|
|
|
+
|
|
|
|
+ String resultCode = successMap.get("result_code");
|
|
|
|
+ String prepayId = successMap.get("prepay_id");
|
|
|
|
+
|
|
|
|
+ if (Constants.SUCCESS.equalsIgnoreCase(returnCode) && returnCode.equals(resultCode)) {
|
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
|
+ map.put("appId", config.getAppId());
|
|
|
|
+ map.put("timeStamp", createTime);
|
|
|
|
+ map.put("nonceStr", nonceStr);
|
|
|
|
+ map.put("package", "prepay_id=" + prepayId);
|
|
|
|
+ map.put("signType", config.getSignType());
|
|
|
|
+
|
|
|
|
+ String sign = WxPayUtil.generateSignature(map, config.getApiKey());
|
|
|
|
+ miniMap.put("paySign", sign);
|
|
|
|
+ }
|
|
|
|
+ miniMap.put("signType", config.getSignType());
|
|
|
|
+ miniMap.put("timeStamp", createTime);
|
|
|
|
+ miniMap.put("nonceStr", nonceStr);
|
|
|
|
+ miniMap.put("package", XMLUtil.xmlToMap(result));
|
|
|
|
+ return ResultMap.ok(miniMap);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("微信小程序支付通信异常, 订单号:{}, e : {}", product.getOutTradeNo(), e.getMessage());
|
|
|
|
+ return ResultMap.error();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
* H5手机支付(jspapi)
|
|
* H5手机支付(jspapi)
|
|
*
|
|
*
|
|
@@ -228,7 +306,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
packageParams.put("sign", sign);
|
|
packageParams.put("sign", sign);
|
|
String requestXml = PayCommonUtil.getRequestXml(packageParams);
|
|
String requestXml = PayCommonUtil.getRequestXml(packageParams);
|
|
String resXml = HttpUtil.postData(WxPayUrl.UNIFIED_ORDER_URL, requestXml);
|
|
String resXml = HttpUtil.postData(WxPayUrl.UNIFIED_ORDER_URL, requestXml);
|
|
- Map map = XMLUtil.doXmlParse(resXml);
|
|
+ Map map = XMLUtil.xmlToMap(resXml);
|
|
if (map == null) {
|
|
if (map == null) {
|
|
log.error("微信支付通信异常, 订单号:{}", product.getOutTradeNo());
|
|
log.error("微信支付通信异常, 订单号:{}", product.getOutTradeNo());
|
|
return ResultMap.error();
|
|
return ResultMap.error();
|