195 lines
9.4 KiB
195 lines
9.4 KiB
package com.cweb.controller.pay;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.hai.common.pay.WechatPayUtil;
|
|
import com.hai.common.pay.util.IOUtil;
|
|
import com.hai.common.pay.util.XmlUtil;
|
|
import com.hai.common.pay.util.sdk.WXPayConstants;
|
|
import com.hai.common.utils.HttpsUtils;
|
|
import com.hai.common.utils.WxUtils;
|
|
import com.hai.model.WxSharingOrderRequestModel;
|
|
import com.hai.model.WxSharingReceiversVO;
|
|
import com.hai.service.pay.NotifyService;
|
|
import com.hai.service.pay.PayRecordService;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import net.sf.cglib.beans.BeanMap;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.BufferedOutputStream;
|
|
import java.util.*;
|
|
|
|
@Controller
|
|
@RequestMapping(value = "/wechatpay")
|
|
@Api(value = "微信支付")
|
|
public class WechatPayController {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(WechatPayController.class);
|
|
|
|
private WXPayConstants.SignType signType;
|
|
|
|
@Resource
|
|
private NotifyService notifyService;
|
|
@Resource
|
|
private PayRecordService payRecordService;
|
|
@Resource
|
|
private WechatPayUtil wechatPayUtil;
|
|
|
|
@RequestMapping(value = "/notify", method = RequestMethod.POST)
|
|
@ApiOperation(value = "微信支付 -> 异步回调")
|
|
public void wechatNotify(HttpServletRequest request, HttpServletResponse response) {
|
|
try {
|
|
log.info("微信支付 -> 异步通知:处理开始");
|
|
|
|
String resXml = ""; // 反馈给微信服务器
|
|
String notifyXml = null; // 微信支付系统发送的数据(<![CDATA[product_001]]>格式)
|
|
notifyXml = IOUtil.inputStreamToString(request.getInputStream(), "UTF-8");
|
|
|
|
log.info("微信支付系统发送的数据:" + notifyXml);
|
|
SortedMap<String, String> map = XmlUtil.parseXmlToTreeMap(notifyXml, "UTF-8");
|
|
|
|
resXml = notifyService.wechatNotify(map);
|
|
|
|
/* if (SignatureUtil.reCheckIsSignValidFromWeiXin(notifyXml, SysConst.getSysConfig().getWxApiKey(), "UTF-8")) {
|
|
log.info("微信支付系统发送的数据:" + notifyXml);
|
|
SortedMap<String, String> map = XmlUtil.parseXmlToTreeMap(notifyXml, "UTF-8");
|
|
|
|
resXml = notifyService.wechatNotify(map);
|
|
} else {
|
|
log.error("微信支付 -> 异步通知:验签失败");
|
|
log.error("apiKey:" + SysConst.getSysConfig().getWxApiKey());
|
|
log.error("返回信息:" + notifyXml);
|
|
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();
|
|
log.info("微信支付 -> 异步通知:处理完成");
|
|
} catch (Exception e) {
|
|
log.error("WechatPayController --> wechatNotify() error!", e);
|
|
}
|
|
}
|
|
|
|
/*@RequestMapping(value = "/testCreateOrder", method = RequestMethod.GET)
|
|
@ApiOperation(value = "微信测试 创建支付信息(发版时注释此接口)")
|
|
@ResponseBody
|
|
public SortedMap<Object, Object> testCreateOrder(@RequestParam(value = "subject", required = true) String subject,
|
|
@RequestParam(value = "orderNo", required = true) String orderNo,
|
|
@RequestParam(value = "totalPrice", required = true) Integer totalPrice,
|
|
HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
|
|
try {
|
|
String nonce_str = MD5Util.MD5Encode(String.valueOf(ThreadLocalRandom.current().nextInt(10000)), "UTF-8");
|
|
WeChatPayReqInfo weChatPayReqInfo = new WeChatPayReqInfo();
|
|
weChatPayReqInfo.setAppid(wechatConfig.getApp_id()); //公众号id
|
|
weChatPayReqInfo.setMch_id(wechatConfig.getMch_id()); //商户号
|
|
weChatPayReqInfo.setNonce_str(nonce_str); //随机字符串
|
|
weChatPayReqInfo.setBody(subject); //商品描述
|
|
weChatPayReqInfo.setOut_trade_no(orderNo); //商户订单号
|
|
weChatPayReqInfo.setTotal_fee(totalPrice); //总
|
|
weChatPayReqInfo.setSpbill_create_ip("182.43.154.4"); //终端ip
|
|
weChatPayReqInfo.setNotify_url(wechatConfig.getNotify_url()); //通知url
|
|
//weChatPayReqInfo.setTrade_type("APP"); //交易类型
|
|
weChatPayReqInfo.setTrade_type("JSAPI");
|
|
//weChatPayReqInfo.setOpenid("");
|
|
weChatPayReqInfo.setAttach("Test");
|
|
|
|
*//** 获取沙箱key start *//*
|
|
String sandboxKey = GetSignKey();
|
|
*//** 获取沙箱key end *//*
|
|
String sign = SignatureUtil.createSign(weChatPayReqInfo, sandboxKey, "UTF-8"); // 测试 todo del
|
|
weChatPayReqInfo.setSign(sign);
|
|
String unifiedXmL = XmlUtil.toSplitXml(weChatPayReqInfo);
|
|
|
|
String unifiedOrderResultXmL = HttpReqUtil.HttpsDefaultExecute("POST", wechatConfig.getUnified_order_url(), null, unifiedXmL, null);
|
|
log.error("支付信息:" + unifiedOrderResultXmL);
|
|
// 签名校验
|
|
SortedMap<Object,Object> sortedMap = null;
|
|
// 组装支付参数
|
|
WechatCallBackInfo wechatCallBackInfo = XmlUtil.getObjectFromXML(unifiedOrderResultXmL, WechatCallBackInfo.class);
|
|
Long timeStamp = System.currentTimeMillis()/1000;
|
|
sortedMap = new TreeMap<>();
|
|
sortedMap.put("appid",wechatConfig.getApp_id());
|
|
sortedMap.put("partnerid",wechatConfig.getMch_id());
|
|
sortedMap.put("prepayid",wechatCallBackInfo.getPrepay_id());
|
|
sortedMap.put("noncestr",wechatCallBackInfo.getNonce_str());
|
|
sortedMap.put("timestamp",timeStamp);
|
|
sortedMap.put("package","Sign=WXPay");
|
|
String secondSign = SignatureUtil.createSign(sortedMap, wechatConfig.getApi_key(), "UTF-8");
|
|
sortedMap.put("sign",secondSign);
|
|
|
|
return sortedMap;
|
|
} catch (Exception e) {
|
|
log.error("AlipayTest -> createOrder:生成订单错误", e);
|
|
}
|
|
return null;
|
|
}*/
|
|
|
|
|
|
/**
|
|
* @desc 获取沙箱key
|
|
*/
|
|
/*public String GetSignKey() throws Exception {
|
|
String nonce_str = WXPayUtil.generateNonceStr(); //生成随机字符
|
|
Map<String, String> param = new HashMap<>();
|
|
param.put("mch_id", wechatConfig.getMch_id()); //需要真实商户号
|
|
param.put("nonce_str", nonce_str); //随机字符
|
|
String sign = WXPayUtil.generateSignature(param, wechatConfig.getApi_key(), WXPayConstants.SignType.MD5); //通过SDK生成签名其中API_KEY为商户对应的真实密钥
|
|
param.put("sign", sign);
|
|
String xml = WXPayUtil.mapToXml(param); //将map转换为xml格式
|
|
String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"; //沙箱密钥获取api
|
|
String signKey = HttpUtil.post(url, xml);
|
|
System.out.println("signkey+"+signKey);
|
|
Map<String, String> param1 = new HashMap<String, String>();
|
|
param1 = WXPayUtil.xmlToMap(signKey.toString());
|
|
String key = param1.get("sandbox_signkey");
|
|
return key;
|
|
}*/
|
|
|
|
@RequestMapping(value = "/addLedgerReceiver", method = RequestMethod.POST)
|
|
@ApiOperation(value = "微信支付 -> 添加分账接收方")
|
|
public void addLedgerReceiver(HttpServletRequest request, HttpServletResponse response) {
|
|
try {
|
|
|
|
WxSharingReceiversVO receiversVO = new WxSharingReceiversVO();
|
|
receiversVO.setAccount("1609882817");
|
|
receiversVO.setType("MERCHANT_ID");
|
|
receiversVO.setName("个体户黎杨珍");
|
|
receiversVO.setRelation_type("SERVICE_PROVIDER");
|
|
|
|
Map<String , String> map = new HashMap<>();
|
|
|
|
map.put("mch_id" , "1289663601");
|
|
map.put("sub_mch_id" , "1609882817");
|
|
map.put("appid" , "wx637bd6f7314daa46");
|
|
map.put("nonce_str" , WxUtils.makeNonStr());
|
|
map.put("sign_type" , "HMAC-SHA256");
|
|
map.put("receiver" , JSONObject.toJSONString(receiversVO));
|
|
|
|
String sign = WxUtils.generateSignature(map, "Skufk5oi85wDFGl888i6wsRSTkdd5df5" , WXPayConstants.SignType.HMACSHA256);
|
|
|
|
map.put("sign" , sign);
|
|
|
|
String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
|
|
String xm = WxUtils.mapToXml(map);
|
|
String rest = String.valueOf(HttpsUtils.postData(url, xm));
|
|
|
|
System.out.println(rest);
|
|
log.info("微信支付 -> 添加分账接收方:处理开始");
|
|
} catch (Exception e) {
|
|
log.error("WechatPayController --> wechatNotify() error!", e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|