1.微信预支付交易单(unifiedorder)
/**
* @ 生成预支付交易单
* @Title: unifiedorder
* @param type 1 预约支付;2 购买次卡支付
* @param orderCode
* @param openid
* @param total_fee
* @return
* @return JSONObject
* @date 2019年9月9日 下午4:38:09
* @author Minglij
*/
@ApiOperation("生成预支付交易单")
@RequestMapping(value = "/unifiedorder", method = RequestMethod.GET)
@ResponseBody
public JSONObject unifiedorder(int type, String categoryId, String orderCode, String openid, String total_fee) {
JSONObject result = new JSONObject();
try {
Map<String, Object> map = new HashMap<String, Object>();
/**
* 生成预支付交易单
*/
String prepay_id = "";
String nonce_str = "";
nonce_str = ToolsUtil.randomString(32);
long timeStamp = new Date().getTime();
String body = "燃奇球馆预约支付";
if(type == 2) { // 购买次卡订单编号
orderCode = "K"+new Date().getTime()+""+ToolsUtil.randomInt(3);
}
String out_trade_no = orderCode;
long total_fee_ = (long)(Float.parseFloat(total_fee)*100);
prepay_id = wxServerService.unifiedorder(nonce_str, body, out_trade_no, total_fee_, SystemConstants.WX_NOTIFY_URL, "JSAPI", openid);
map.put("timeStamp", timeStamp);
map.put("nonceStr", nonce_str);
map.put("package", prepay_id);
//顺序按照ASCII字典序排序
String signA = "appId="+ SystemConstants.APPID_CUSTOMER +"&nonceStr="+ nonce_str +"&package=prepay_id="+ prepay_id
+"&signType=MD5&timeStamp="+ timeStamp +"&key="+SystemConstants.MCH_KEY;
String paySign = ToolsUtil.md5(signA).toUpperCase();
// 缓存签名,微信支付通知校验
// log.info("cache_wxPaySign:"+JSONObject.toJSONString(SystemCache.cache_wxPaySign));
// SystemCache.setPaySignCache(out_trade_no, paySign);
// 保存为统一支付记录
UnifiedorderRecord unifiedorderRecord = new UnifiedorderRecord();
unifiedorderRecord.setType(type);
unifiedorderRecord.setCategoryId(categoryId);
unifiedorderRecord.setOrderCode(orderCode);
unifiedorderRecord.setOpenid(openid);
unifiedorderRecord.setTotalFee(new BigDecimal(total_fee_));
unifiedorderRecord.setInsertDate(new Date());
unifiedorderRecord.setFlag(0);
if(type == 1) {
unifiedorderRecord.setRemark("微信用户("+ openid +")预约球馆,需预支付:"+total_fee_+"分");
} else if(type == 2) {
unifiedorderRecord.setRemark("微信用户("+ openid +")购买次卡,需预支付:"+total_fee_+"分");
}
EntityWrapper<UnifiedorderRecord> wrapper = new EntityWrapper<UnifiedorderRecord>();
wrapper.eq("orderCode", orderCode);
wrapper.eq("type", type);
wrapper.eq("flag", 0);
UnifiedorderRecord unifiedorderRecord_ = unifiedorderRecordService.selectOne(wrapper);
if(null != unifiedorderRecord_) {
unifiedorderRecord_.setFlag(-1);
unifiedorderRecordService.update(unifiedorderRecord_, wrapper);
}
unifiedorderRecordService.insert(unifiedorderRecord);
map.put("paySign", paySign);
result.put("obj", map);
result.put("success", true);
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
result.put("success", false);
}
return result;
}
2.微信支付成功回调(wxNotify)
/**
* @ 接收支付成功后,微信返回
* @Title: wxNotify
* @Description: TODO
* @return void
* @date 2019年9月9日 下午4:41:15
* @author Minglij
* @throws Exception
*/
@ApiOperation("支付成功后通知")
@RequestMapping(value = "/wxNotify", method = RequestMethod.POST)
@ResponseBody
public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
String result = new String(outSteam.toByteArray(), "utf-8");
log.info(">>>>>>>>支付成功,微信回调结果:"+result);
SortedMap<String, String> map = ToolsUtil.xmlToMap(result);
PrintWriter writer = response.getWriter();
// 是否已经接收过通知
EntityWrapper<WxPayNotice> wrapper_wxpn = new EntityWrapper<WxPayNotice>();
wrapper_wxpn.eq("transaction_id", map.get("transaction_id"));
WxPayNotice wxPayNotice = wxPayNoticeService.selectOne(wrapper_wxpn);
if(null != wxPayNotice) {
// 微信到账通知已经收到
log.info("===============支付通知重复==============");
String noticeStr = setXML("SUCCESS", "");
writer.write(noticeStr);
writer.flush();
} else {
// 签名校验
if(WxHandler.isTenpaySign(map.get("sign").toString(), map)) {
log.info("===============支付失败,签名不一致==============");
String noticeStr = setXML("FAIL", "签名错误");
writer.write(noticeStr);
writer.flush();
} else {
// 校验订单是否存在,订单金额是否一致
boolean isSucc = reqOrderQueryResult(map);
if (!isSucc) {
// 支付失败, 记录流水失败
log.info("===============支付失败,到账金额和订单金额不一致==============");
String noticeStr = setXML("FAIL", "");
writer.write(noticeStr);
writer.flush();
} else {
// 支出成功,处理业务
EntityWrapper<User> wrapper = new EntityWrapper<User>();
wrapper.eq("openid", map.get("openid"));
User user = userService.selectOne(wrapper);
String out_trade_no = map.get("out_trade_no");
//
EntityWrapper<UnifiedorderRecord> wrapper_un = new EntityWrapper<UnifiedorderRecord>();
wrapper_un.eq("orderCode", out_trade_no);
wrapper_un.eq("openid",map.get("openid"));
wrapper_un.eq("flag", 0);
UnifiedorderRecord unifiedorderRecord_ = unifiedorderRecordService.selectOne(wrapper_un);
if(null == unifiedorderRecord_) {
log.info("===============微信通知错误,为找到统一支付订单==============");
String noticeStr = setXML("FAIL", "");
writer.write(noticeStr);
writer.flush();
} else {
// 更新统一支付记录
unifiedorderRecord_.setFlag(1);
unifiedorderRecordService.update(unifiedorderRecord_, wrapper_un);
//
if(unifiedorderRecord_.getType() == 1) {
// 预约订单业务
boolean uc = businessOrderService.doWXPayNotice(user.getId(), map.get("out_trade_no"), map.get("transaction_id"));
log.info("===============付款成功,业务处理完毕(预约)=============="+uc);
} else if(unifiedorderRecord_.getType() == 2) {
// 购买次卡业务
boolean uc = cardOrderService.doWXPayNotice(user.getId(), map.get("out_trade_no"), unifiedorderRecord_.getCategoryId(), map.get("transaction_id"));
log.info("===============付款成功,业务处理完毕(购买次卡)=============="+uc);
}
String noticeStr = setXML("SUCCESS", "");
writer.write(noticeStr);
writer.flush();
}
// 删除缓存中签名
// SystemCache.removePaySignCache(map.get("out_trade_no"));
// 通知微信已经收到消息,不要再给我发消息了,否则微信会8连击调用本接口
}
}
}
}