@RequestMapping("/wechatScanPay.shtml")
@ResponseBody
public Object wechatScanPay(HttpServletRequest req, HttpServletResponse resp) throws IOException, DocumentException, WriterException {
try {
String reqId = req.getParameter("id");
Preconditions.checkArgument(StringUtils.isNotBlank(reqId), "支付请求号为空");
OnlinePaymentRequest opr = onlinePaymentRequestManager.selectByPrimaryKey(new Long(reqId));
if (opr == null)
return new ResultModel(ResultCode.NOT_FIND_PAY_REQUEST).createResp();
Integer operateStatus = opr.getOperateStatus();
if (operateStatus.intValue() == PayDict.OPERATE_STATUS_1)
return new ResultModel(ResultCode.DOUBLE_PAID).createResp();
Map map = Maps.newHashMap();
map.put("appid", WeChatConstant.APP_ID);
map.put("mch_id", WeChatConstant.MCH_ID);
map.put("nonce_str", UUIDUtil.getUUID());
//map.put("sign_type", WeChatConstant.SIGN_TYPE);
map.put("body", opr.getBody());//商品描述
//todo
// map.put("out_trade_no", String.valueOf(opr.getOnlinePaymentRequestId()));
map.put("out_trade_no", String.valueOf(DateUtil.getCurrentTimeMillis()));
map.put("fee_type", WeChatConstant.FEE_TYPE);
map.put("total_fee", String.valueOf(opr.getPayMoney()));
map.put("spbill_create_ip", CusAccessObjectUtil.getIpAddress(req));//终端IP
map.put("notify_url", WeChatConstant.NOTIFY_URL);
map.put("trade_type", "NATIVE");
String reqXml = WechatUtil.getRequestData(map, WeChatConstant.KEY);
String result = HttpRequestUtil.sendHttpPostRequestXML(WeChatConstant.WECHAT_UNIFIEDORDER_URL, reqXml);
Map<String, String> orderMap = XMLUtil.testGetRoot(result);
logger.info("orderMap===========================" + orderMap);
if ("FAIL".equals(orderMap.get("return_code"))) {
logger.info("微信创建支付订单失败,错误原因为:" + orderMap.get("return_msg"));
return new ResultModel(ResultCode.FAIL, orderMap.get("return_msg")).createResp();
}
if ("FAIL".equals(orderMap.get("result_code"))) {
logger.info("微信创建支付订单失败,错误原因为:" + orderMap.get("err_code_des"));
return new ResultModel(ResultCode.FAIL, orderMap.get("err_code_des")).createResp();
}
//生成分账信息
SplitFundInfo info = new SplitFundInfo();
info.setCurrency(WeChatConstant.FEE_TYPE);
info.setAccount(WeChatConstant.APP_ID);
info.setAccountType("1");
info.setAmount(opr.getPayMoney());
info.setRatio(100L);
info.setOnlinePaymentRequestId(opr.getOnlinePaymentRequestId());
List<SplitFundInfo> infos = splitFundInfoManager.queryEntity(info);
if (infos != null && infos.size() > 0) {
info.setId(infos.get(0).getId());
splitFundInfoManager.updateByPrimaryKeySelective(info);
} else {
splitFundInfoManager.insertSelective(info);
}
//将url连接生成二维码图片
// QRCode.writeToFile(code_url, "png", 200, 200, resp.getOutputStream());
//得到的扫码支付url
Object code_url = orderMap.get("code_url");
return new ResultModel(ResultCode.OK, code_url).createResp();
} catch (IllegalArgumentException iae) {
logger.error("前置检查条件错误", iae);
return new ResultModel(ResultCode.MISSING_OR_INVALID_ARGUMENTS).createResp();
}
}
打开App,阅读手记