|
@@ -6,6 +6,7 @@ import com.zanxiang.common.domain.ResultMap;
|
|
|
import com.zanxiang.common.enums.ResEnum;
|
|
|
import com.zanxiang.common.utils.JsonUtil;
|
|
|
import com.zanxiang.common.utils.RandomStringUtil;
|
|
|
+import com.zanxiang.common.utils.URIUtil;
|
|
|
import com.zanxiang.sdk.common.util.*;
|
|
|
import com.zanxiang.sdk.domain.bo.ProductPayAttachParamBO;
|
|
|
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 lombok.extern.slf4j.Slf4j;
|
|
|
import org.jdom.JDOMException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
import weixin.popular.api.SnsAPI;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.SortedMap;
|
|
|
import java.util.TreeMap;
|
|
@@ -45,6 +49,11 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
*/
|
|
|
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;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 支付调起
|
|
|
*
|
|
@@ -92,10 +105,9 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
//手机端H5
|
|
|
return this.mobile(product, WX_PAY_MWEB);
|
|
|
// case 3:
|
|
|
-// return this.mobile(product, "MWEB");
|
|
|
case 4:
|
|
|
- //微信小程序
|
|
|
- return this.mp(product);
|
|
|
+ //微信商城小程序支付
|
|
|
+ return this.miniAppPay(product);
|
|
|
default:
|
|
|
throw new RuntimeException("未知支付方式");
|
|
|
}
|
|
@@ -122,7 +134,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
inputStream.close();
|
|
|
log.info("微信支付回调 body:{}", sb);
|
|
|
// 解析xml成map
|
|
|
- Map m = XMLUtil.doXmlParse(sb.toString());
|
|
|
+ Map m = XMLUtil.xmlToMap(sb.toString());
|
|
|
// 过滤空 设置 TreeMap
|
|
|
SortedMap<Object, Object> packageParams = new TreeMap<>();
|
|
|
for (Object parameter : m.keySet()) {
|
|
@@ -196,6 +208,72 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
return ResultMap.ok(result);
|
|
|
}
|
|
|
|
|
|
+ private ResultMap miniAppPay(ProductPayParamBO product) {
|
|
|
+ try {
|
|
|
+ Map<Object, Object> miniMap = new HashMap<>(5);
|
|
|
+ // 请求微信服务器,使用code获取openid和session_key
|
|
|
+ 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);
|
|
|
+ // 解析相应内容(转换成json对象)
|
|
|
+ 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;
|
|
|
+ // 这边要将返回的时间戳转化成字符串,不然小程序端调用wx.requestPayment方法会报签名错误
|
|
|
+ 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());
|
|
|
+ // 再次签名sign,这个签名用于小程序端调用支付方法
|
|
|
+ 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)
|
|
|
*
|
|
@@ -228,7 +306,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
packageParams.put("sign", sign);
|
|
|
String requestXml = PayCommonUtil.getRequestXml(packageParams);
|
|
|
String resXml = HttpUtil.postData(WxPayUrl.UNIFIED_ORDER_URL, requestXml);
|
|
|
- Map map = XMLUtil.doXmlParse(resXml);
|
|
|
+ Map map = XMLUtil.xmlToMap(resXml);
|
|
|
if (map == null) {
|
|
|
log.error("微信支付通信异常, 订单号:{}", product.getOutTradeNo());
|
|
|
return ResultMap.error();
|