微信支付回调处理

xiaoxiao2021-02-28  103

通过上篇博文已经知道通过微信支付接口获取预支付id进行微信支付已经配置好回调地址了,下面我讲下回调相关代码,通过微信返回信息进行业务处理,包括用户送出的礼物信息放在attach字段里面的。

@RequestMapping(value = "/notice.do", produces = "text/html;charset=utf-8") public void weixin_notify(HttpServletRequest request, HttpServletResponse response) throws Exception { //读取参数 InputStream inputStream; StringBuffer sb = new StringBuffer(); inputStream = request.getInputStream(); String s; BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); while ((s = in.readLine()) != null) { sb.append(s); } in.close(); inputStream.close(); //解析xml成map Map<String, String> m = new HashMap<String, String>(); m = XMLUtil.doXMLParse(sb.toString()); //过滤空 设置 TreeMap SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>(); Iterator it = m.keySet().iterator(); while (it.hasNext()) { String parameter = (String) it.next(); String parameterValue = m.get(parameter); String v = ""; if (null != parameterValue) { v = parameterValue.trim(); } packageParams.put(parameter, v); } // 账号信息 String key = "r22dum9drtxtd78btskxslu6tzlsqtrl"; // key logger.info("回调--------" + packageParams); //判断签名是否正确 if (PayCommonUtil.isTenpaySign("UTF-8", packageParams, key)) { //------------------------------ //处理业务开始 //------------------------------ String resXml = ""; String activityId = ""; String userId = ""; String total_fee = ""; if ("SUCCESS".equals((String) packageParams.get("result_code"))) { // 这里是支付成功 //执行自己的业务逻辑 String mch_id = (String) packageParams.get("mch_id"); String openid = (String) packageParams.get("openid"); String is_subscribe = (String) packageParams.get("is_subscribe"); String out_trade_no = (String) packageParams.get("out_trade_no"); String attach = (String) packageParams.get("attach");//用户id&活动号 userId = attach.substring(0, attach.lastIndexOf("/")); total_fee = (String) packageParams.get("total_fee"); logger.info("mch_id:" + mch_id); logger.info("openid:" + openid); logger.info("is_subscribe:" + is_subscribe); logger.info("out_trade_no:" + out_trade_no); logger.info("total_fee:" + total_fee); activityId = attach.substring(attach.lastIndexOf("/") + 1, attach.length()); logger.info("支付成功"); //通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了. resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> "; } else { logger.info("支付失败,错误信息:" + packageParams.get("err_code")); resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> "; } //------------------------------ List<BoxInfoJson> boxInfoJsons = new ArrayList<>(); try { Account account = accountService.getAccount(Integer.parseInt(userId)); //取得活动信息 String liveId = douyaLiveEventTempService.getLiveIdByEventNo(activityId); //取得会场信息 List<LiveVenue> venueList = liveVenueService.getEventVenueList(liveId); for (LiveVenue liveVenue : venueList) { BoxInfoJson json = new BoxInfoJson(); LiveGift liveGift = new LiveGift(); liveGift.setHeadImg(account.getAvatar()); liveGift.setNickName(account.getNick_name()); if (total_fee.equals("66")) { liveGift.setLiveGift(YINGGUANGBANG); } else if (total_fee.equals("100")) { liveGift.setLiveGift(XUANHUA); } else if (total_fee.equals("200")) { liveGift.setLiveGift(DAMAI); } else if (total_fee.equals("666")) { liveGift.setLiveGift(YANJICHAOQUN); } else if (total_fee.equals("1000")) { liveGift.setLiveGift(YANHUO); } else if (total_fee.equals("2000")) { liveGift.setLiveGift(ZUIJIAZHUJIAO); } else if (total_fee.equals("6660")) { liveGift.setLiveGift(ZUIJIAYINGPIAN); } else if (total_fee.equals("10000")) { liveGift.setLiveGift(AOSIKAJIANGBEI); } else { liveGift.setLiveGift(""); } Box b = boxService.getBoxById(liveVenue.getBox_id()); String tMacaddress = b.getMac_address(); json.setType(13); json.setVenueType(String.valueOf(liveVenue.getType())); json.setMacAddress(tMacaddress); json.setLiveGift(liveGift); boxInfoJsons.add(json); } LiveInfo liveInfo = new LiveInfo(); liveInfo.setBoxInfoJsonList(boxInfoJsons); //转换通知内容为json格式并发送给业务接口 String jsonValue = JSONObject.toJSONString(liveInfo); String url = live_boss_client_url; JSONObject liveRtn = PayCommonUtil.sendHttpRequest(url, jsonValue); if (liveRtn.get("code").equals("00")) { logger.info("送出礼物成功" + new Date()); } else { String msg = ""; JSONArray ja = (JSONArray) liveRtn.get("data"); for (Object aJa : ja) { String mac = aJa.toString(); Box box = boxService.queryNameByMac(mac); msg += box.getHall_name() + ","; } msg = msg.substring(0, msg.length() - 1) + "启动失败!"; logger.error("送出礼物失败了", msg); } } catch (Exception e) { e.printStackTrace(); logger.error("系统异常", e); } //------------------------------ BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream()); out.write(resXml.getBytes()); out.flush(); out.close(); } else { logger.info("通知签名验证失败"); } }
转载请注明原文地址: https://www.6miu.com/read-32657.html

最新回复(0)