|
@@ -12,8 +12,6 @@ import com.zanxiang.sdk.domain.bo.WxPayConfigBO;
|
|
|
import com.zanxiang.sdk.service.OrderPayService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jdom.JDOMException;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -32,7 +30,6 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
@Service("WxPayService")
|
|
|
public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(WxPayServiceImpl.class);
|
|
|
|
|
|
private WxPayConfigBO config;
|
|
|
|
|
@@ -44,12 +41,14 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
@Value("${payConfig.wx.notifyUrl}")
|
|
|
private String notifyUrl;
|
|
|
|
|
|
- private String filePath = "/tmp/wxpay/";
|
|
|
+ @Value("${payConfig.wx.imgFilePath}")
|
|
|
+ private String filePath;
|
|
|
|
|
|
private String body;
|
|
|
+
|
|
|
private String totalFee;
|
|
|
- private String outTradeNo;
|
|
|
|
|
|
+ private String outTradeNo;
|
|
|
|
|
|
/**
|
|
|
* 支付调起
|
|
@@ -59,15 +58,12 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
*/
|
|
|
@Override
|
|
|
public ResultMap create(ProductPayParamBO product) {
|
|
|
- System.out.println("WxpayService payCreate");
|
|
|
-
|
|
|
this.body = product.getSubject();
|
|
|
this.totalFee = String.valueOf(Float.parseFloat(product.getTotalFee()) * 100);
|
|
|
this.outTradeNo = product.getOutTradeNo() + RandomStringUtil.randomNumStr(5);
|
|
|
this.attach = product.getAttach();
|
|
|
//初始化配置
|
|
|
this.configInit(getPayConfig(product.getGameId(), product.getPayWay(), product.getPayDevice()));
|
|
|
-
|
|
|
switch (product.getPayDevice()) {
|
|
|
case 1:
|
|
|
return this.mobile(product, "NATIVE");
|
|
@@ -100,7 +96,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
}
|
|
|
in.close();
|
|
|
inputStream.close();
|
|
|
- logger.info("微信支付回调 body:{}", sb);
|
|
|
+ log.info("微信支付回调 body:{}", sb);
|
|
|
// 解析xml成map
|
|
|
Map<String, String> m = XMLUtil.doXMLParse(sb.toString());
|
|
|
// 过滤空 设置 TreeMap
|
|
@@ -119,37 +115,33 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
ProductPayAttachParamBO attachBO = this.decodeAttach(packageParams.get("attach"));
|
|
|
configInit(getPayConfig(attachBO.getGamePayWayId()));
|
|
|
// 账号信息
|
|
|
- String key = config.getApiKey(); // key
|
|
|
+ String key = config.getApiKey();
|
|
|
// 判断签名是否正确
|
|
|
if (PayCommonUtil.isTenpaySign(Constants.UTF8, packageParams, key)) {
|
|
|
- logger.info("微信支付成功回调");
|
|
|
- // ------------------------------
|
|
|
+ log.info("微信支付成功回调");
|
|
|
// 处理业务开始
|
|
|
- // ------------------------------
|
|
|
String resXml;
|
|
|
if (Constants.SUCCESS.equalsIgnoreCase(packageParams.get("result_code").toString())) {
|
|
|
// 这里是支付成功
|
|
|
String orderNo = (String) packageParams.get("out_trade_no");
|
|
|
- logger.info("微信订单号{}付款成功", orderNo);
|
|
|
+ log.info("微信订单号{}付款成功", orderNo);
|
|
|
if (paySuccess(attachBO.getOrderId(), String.valueOf(Float.parseFloat(packageParams.get("total_amount").toString()) / 100), packageParams.get("trade_no").toString(), attachBO.getGamePayWayId())) {
|
|
|
- logger.info("支付宝异步回调成功 request:{},商户订单号为:{}", request, outTradeNo);
|
|
|
+ log.info("支付宝异步回调成功 request:{},商户订单号为:{}", request, outTradeNo);
|
|
|
return ResEnum.SUCCESS.getMsg();
|
|
|
}
|
|
|
- // 通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.
|
|
|
+ // 通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了
|
|
|
resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
|
|
|
} else {
|
|
|
- logger.info("支付失败,错误信息:{}", packageParams.get("err_code"));
|
|
|
+ log.info("支付失败,错误信息:{}", packageParams.get("err_code"));
|
|
|
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
|
|
|
}
|
|
|
- // ------------------------------
|
|
|
// 处理业务完毕
|
|
|
- // ------------------------------
|
|
|
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
out.write(resXml.getBytes());
|
|
|
out.flush();
|
|
|
out.close();
|
|
|
} else {
|
|
|
- logger.info("通知签名验证失败");
|
|
|
+ log.info("通知签名验证失败");
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -163,7 +155,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
|
|
|
@Override
|
|
|
public ResultMap synNotify(HttpServletRequest request) {
|
|
|
- logger.info("微信支付无同步回调 request:{}", request);
|
|
|
+ log.info("微信支付无同步回调 request:{}", request);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -179,7 +171,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
SortedMap<Object, Object> packageParams = new TreeMap<>();
|
|
|
//封装通用参数
|
|
|
wxPayUtil.commonParams(packageParams);
|
|
|
- packageParams.put("attach", this.attach);
|
|
|
+ packageParams.put("attach", JSONObject.toJSONString(this.attach));
|
|
|
//生成签名
|
|
|
String sign = PayCommonUtil.createSign(Constants.UTF8, packageParams, config.getApiKey());
|
|
|
//组装二维码信息
|
|
@@ -188,14 +180,17 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
qrCode.append("appid=" + config.getAppId());
|
|
|
qrCode.append("&mch_id=" + config.getMchId());
|
|
|
qrCode.append("&nonce_str=" + packageParams.get("nonce_str"));
|
|
|
-// qrCode.append("&attach=" + this.attach);
|
|
|
qrCode.append("&out_trade_no=" + this.outTradeNo);
|
|
|
qrCode.append("&time_stamp=" + packageParams.get("time_stamp"));
|
|
|
qrCode.append("&sign=" + sign);
|
|
|
String imgName = this.outTradeNo + ".png";
|
|
|
+ File pathFile = new File(this.filePath);
|
|
|
+ if (!pathFile.exists()) {
|
|
|
+ pathFile.mkdirs();
|
|
|
+ }
|
|
|
String imgPath = filePath + imgName;
|
|
|
- logger.info("小程序支付调起,请求参数:qrCode:{}, imgPath:{}", qrCode, imgPath);
|
|
|
- ZxingUtil.createQRCodeImage(qrCode.toString(), imgPath);
|
|
|
+ log.info("小程序支付调起,请求参数:qrCode:{}, imgPath:{}", qrCode, imgPath);
|
|
|
+ QrCode.createQrCodeImage(qrCode.toString(), imgPath);
|
|
|
System.out.println(imgPath);
|
|
|
return ResultMap.ok(product.getPayDevice(), qrCode.toString());
|
|
|
}
|
|
@@ -209,7 +204,7 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
*/
|
|
|
public ResultMap mp(ProductPayParamBO product) {
|
|
|
//redirect_uri 需要在微信支付端添加认证网址
|
|
|
- totalFee = wxPayUtil.subZeroAndDot(this.totalFee);
|
|
|
+ totalFee = WxPayUtil.subZeroAndDot(this.totalFee);
|
|
|
String redirect_uri = serverUrl + "weixinMobile/dopay?outTradeNo=" + product.getOutTradeNo() + "&totalFee=" + totalFee;
|
|
|
//也可以通过state传递参数 redirect_uri 后面加参数未经过验证
|
|
|
String result = SnsAPI.connectOauth2Authorize(config.getAppId(), redirect_uri, true, null);
|
|
@@ -222,75 +217,66 @@ public class WxPayServiceImpl extends PayService implements OrderPayService {
|
|
|
* @param product
|
|
|
* @return
|
|
|
*/
|
|
|
- public ResultMap mobile(ProductPayParamBO product, String trade_type) {
|
|
|
- logger.info("订单号:{}生成微信支付码", product.getOutTradeNo());
|
|
|
+ public ResultMap mobile(ProductPayParamBO product, String tradeType) {
|
|
|
+ //商户支付回调URL设置指引:进入公众平台-->微信支付-->开发配置-->扫码支付-->修改 加入回调URL
|
|
|
+ log.info("订单号:{}生成微信支付码", product.getOutTradeNo());
|
|
|
try {
|
|
|
// 账号信息
|
|
|
String key = config.getApiKey();
|
|
|
-// String trade_type = "NATIVE";// 交易类型 原生扫码支付
|
|
|
-// String trade_type = "MWEB";// 交易类型 原生扫码支付
|
|
|
SortedMap<Object, Object> packageParams = new TreeMap<>();
|
|
|
wxPayUtil.commonParams(packageParams);
|
|
|
- packageParams.put("body", this.body);// 商品描述
|
|
|
- packageParams.put("out_trade_no", this.outTradeNo);// 商户订单号
|
|
|
- totalFee = wxPayUtil.subZeroAndDot(this.totalFee);
|
|
|
- packageParams.put("total_fee", totalFee);// 总金额
|
|
|
- packageParams.put("spbill_create_ip", product.getSpbillCreateIp());// 发起人IP地址
|
|
|
- packageParams.put("notify_url", notifyUrl);// 回调地址 notifyUrl
|
|
|
- packageParams.put("trade_type", trade_type);// 交易类型
|
|
|
+ // 商品描述
|
|
|
+ packageParams.put("body", this.body);
|
|
|
+ // 商户订单号
|
|
|
+ packageParams.put("out_trade_no", this.outTradeNo);
|
|
|
+ totalFee = WxPayUtil.subZeroAndDot(this.totalFee);
|
|
|
+ // 总金额
|
|
|
+ packageParams.put("total_fee", totalFee);
|
|
|
+ // 发起人IP地址
|
|
|
+ packageParams.put("spbill_create_ip", product.getSpbillCreateIp());
|
|
|
+ // 回调地址 notifyUrl
|
|
|
+ packageParams.put("notify_url", notifyUrl);
|
|
|
+ // 交易类型
|
|
|
+ packageParams.put("trade_type", tradeType);
|
|
|
packageParams.put("attach", JSONObject.toJSONString(this.attach));
|
|
|
-
|
|
|
-// if (trade_type.equals("MWEB")) {
|
|
|
-// //H5支付专用
|
|
|
-// JSONObject value = new JSONObject();
|
|
|
-// value.put("type", "WAP");
|
|
|
-// value.put("wap_url", "https://localhost");////WAP网站URL地址
|
|
|
-// value.put("wap_name", "111");//WAP 网站名
|
|
|
-// JSONObject scene_info = new JSONObject();
|
|
|
-// scene_info.put("h5_info", value);
|
|
|
-// packageParams.put("scene_info", scene_info.toString());
|
|
|
-// }
|
|
|
String sign = PayCommonUtil.createSign(Constants.UTF8, packageParams, key);
|
|
|
- packageParams.put("sign", sign);// 签名
|
|
|
- logger.info("订单号:{} 加密请求参数 packageParams:{}", product.getOutTradeNo(), packageParams);
|
|
|
- String requestXML = PayCommonUtil.getRequestXml(packageParams);
|
|
|
- String resXml = HttpUtil.postData(WxPayUrl.UNIFIED_ORDER_URL, requestXML);
|
|
|
+ packageParams.put("sign", sign);
|
|
|
+ log.info("订单号:{} 加密请求参数 packageParams:{}", product.getOutTradeNo(), packageParams);
|
|
|
+ String requestXml = PayCommonUtil.getRequestXml(packageParams);
|
|
|
+ String resXml = HttpUtil.postData(WxPayUrl.UNIFIED_ORDER_URL, requestXml);
|
|
|
Map map = XMLUtil.doXMLParse(resXml);
|
|
|
- logger.info("订单号:{} 微信响应 map:{}", product.getOutTradeNo(), map);
|
|
|
+ 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)) {
|
|
|
- logger.info("订单号:{}生成微信支付码成功", product.getOutTradeNo());
|
|
|
- String urlCode = trade_type.equals("MWEB") ? (String) map.get("mweb_url") : (String) map.get("code_url");
|
|
|
- String imgName = this.outTradeNo + ".png";
|
|
|
- String imgPath = filePath + imgName;
|
|
|
+ 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 (trade_type.equals("MWEB")) {
|
|
|
+ if (tradeType.equals("MWEB")) {
|
|
|
try {
|
|
|
String shortUrl = wxPayUtil.shortUrl(urlCode);
|
|
|
if (!Constants.FAIL.equalsIgnoreCase(shortUrl)) {
|
|
|
result.put("AppLink", shortUrl);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- logger.error("订单号:{} 生成微信app支付链接失败:{}", product.getOutTradeNo(), e);
|
|
|
+ log.error("订单号:{} 生成微信app支付链接失败:{}", product.getOutTradeNo(), e);
|
|
|
}
|
|
|
}
|
|
|
result.put("fromData", urlCode);
|
|
|
-// return ResultMap.ok(product.getPayDevice(), result);
|
|
|
return ResultMap.ok(product.getPayDevice(), urlCode);
|
|
|
} else {
|
|
|
String errCodeDes = (String) map.get("err_code_des");
|
|
|
- logger.error("订单号:{}生成微信支付码(系统)失败:{}", product.getOutTradeNo(), errCodeDes);
|
|
|
+ log.error("订单号:{}生成微信支付码(系统)失败:{}", product.getOutTradeNo(), errCodeDes);
|
|
|
return ResultMap.error(errCodeDes);
|
|
|
}
|
|
|
} else {
|
|
|
String returnMsg = (String) map.get("return_msg");
|
|
|
- logger.error("(订单号:{}生成微信支付码(通信)失败:{}", product.getOutTradeNo(), returnMsg);
|
|
|
+ log.error("(订单号:{}生成微信支付码(通信)失败:{}", product.getOutTradeNo(), returnMsg);
|
|
|
return ResultMap.error(returnMsg);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- logger.error("订单号:{}生成微信支付码失败(系统异常))", product.getOutTradeNo(), e);
|
|
|
+ log.error("订单号:{}生成微信支付码失败(系统异常))", product.getOutTradeNo(), e);
|
|
|
return ResultMap.error();
|
|
|
}
|
|
|
}
|