| package com.matrix.system.shopXcx.api.action; | 
|   | 
| import com.matrix.component.rabbitmq.RabiitMqTemplate; | 
| import com.matrix.component.wechat.externalInterface.common.Signature; | 
| import com.matrix.component.wechat.externalInterface.common.Util; | 
| import com.matrix.component.wechat.externalInterface.protocol.queryProtocol.NotifyData; | 
| import com.matrix.config.RabbitMqConfig; | 
| import com.matrix.core.pojo.AjaxResult; | 
| import com.matrix.core.tools.LogUtil; | 
| import com.matrix.system.common.bean.BusParameterSettings; | 
| import com.matrix.system.common.constance.AppConstance; | 
| import com.matrix.system.common.dao.BusParameterSettingsDao; | 
| import com.matrix.system.hive.bean.MoneyCardUse; | 
| import com.matrix.system.hive.bean.MoneyCardUseFlow; | 
| import com.matrix.system.hive.bean.SysVipInfo; | 
| import com.matrix.system.hive.dao.MoneyCardUseDao; | 
| import com.matrix.system.hive.dao.MoneyCardUseFlowDao; | 
| import com.matrix.system.hive.dao.SysVipInfoDao; | 
| import com.matrix.system.score.service.ScoreVipDetailService; | 
| import com.matrix.system.shopXcx.bean.ShopActivitiesGroupJoin; | 
| import com.matrix.system.shopXcx.bean.ShopActivitiesGroupJoinUser; | 
| import com.matrix.system.shopXcx.bean.ShopOrder; | 
| import com.matrix.system.shopXcx.bean.ShopPayLog; | 
| import com.matrix.system.shopXcx.dao.ShopActivitiesGroupJoinDao; | 
| import com.matrix.system.shopXcx.dao.ShopActivitiesGroupJoinUserDao; | 
| import com.matrix.system.shopXcx.dao.ShopOrderDao; | 
| import com.matrix.system.shopXcx.dao.ShopPayLogDao; | 
| import com.matrix.system.shopXcx.mqTask.MQTaskRouting; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Controller; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.web.bind.annotation.PathVariable; | 
| import org.springframework.web.bind.annotation.RequestMapping; | 
| import org.springframework.web.bind.annotation.ResponseBody; | 
|   | 
| import javax.servlet.ServletOutputStream; | 
| import javax.servlet.http.HttpServletRequest; | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.io.IOException; | 
| import java.math.BigDecimal; | 
| import java.math.RoundingMode; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * @author jyy | 
|  * @description 微信公共接口 | 
|  * @date 2018-05-23 00:08 | 
|  */ | 
| @Controller | 
| @RequestMapping(value = "wxCommon") | 
| public class WxCommonAction { | 
|   | 
|   | 
|     /** | 
|      * 错误报文占位符 | 
|      */ | 
|     private static final String ERRORMSG = "ERRORMSG"; | 
|     /** | 
|      * 异步任务和订单类型的映射关系 | 
|      */ | 
|     private static Map<Integer, String> TASK_ORDER_MAPPING = new HashMap<>(); | 
|   | 
|     @Autowired | 
|     private ShopOrderDao shopOrderDao; | 
|     @Autowired | 
|     private ShopPayLogDao shopPayLogDao; | 
|   | 
|     @Autowired | 
|     private RabiitMqTemplate rabiitMqTemplate; | 
|   | 
|     @Autowired | 
|     private ShopActivitiesGroupJoinUserDao shopActivitiesGroupJoinUserDao; | 
|   | 
|     @Autowired | 
|     private ShopActivitiesGroupJoinDao shopActivitiesGroupJoinDao; | 
|   | 
|     @Autowired | 
|     private SysVipInfoDao sysVipInfoDao; | 
|   | 
|     @Autowired | 
|     BusParameterSettingsDao busParameterSettingsDao; | 
|   | 
|     @Autowired | 
|     ScoreVipDetailService scoreVipDetailService; | 
|   | 
|   | 
|   | 
|     @Value("${evn}") | 
|     private String evn; | 
|     /** | 
|      * 微信支付回调接口 | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     @RequestMapping(value = "/wxapi/wxpayCallback") | 
|     public void payCallBack(HttpServletResponse response, HttpServletRequest request) throws IOException { | 
|         LogUtil.info("微信支付回调start...."); | 
|   | 
|         // 获取输入参数 | 
|         String inputLine; | 
|         StringBuffer notityXml = new StringBuffer(); | 
|         String resXml = ""; | 
|         String orderId = ""; | 
|   | 
|         AjaxResult threadResult = new AjaxResult(AjaxResult.STATUS_SUCCESS, ""); | 
|         try { | 
|             while ((inputLine = request.getReader().readLine()) != null) { | 
|                 notityXml.append(inputLine); | 
|             } | 
|             request.getReader().close(); | 
|             LogUtil.info("notityXml ---- :{} ", notityXml); | 
|   | 
|   | 
|             // XMl转对象 | 
|             Object bb = Util.getObjectFromXML(notityXml.toString(), NotifyData.class); | 
|             NotifyData    data = new NotifyData(); | 
|             BeanUtils.copyProperties(bb,data); | 
|             LogUtil.info("----return_code = {}", data.getReturn_code()); | 
|   | 
|   | 
|             // 返回状态码 SUCCESS/FAIL | 
|             if (AppConstance.CODE_SUCCESS.equals(data.getReturn_code())) { | 
|   | 
|                 orderId = data.getAttach(); | 
|                 // 检验订单状态 | 
|                 ShopOrder order = shopOrderDao.selectById(Integer.valueOf(orderId)); | 
|   | 
|                 // 校验签名 | 
|                 BusParameterSettings paySecret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_SECRET, order.getCompanyId()); | 
|                 if (Signature.checkIsSignValidFromResponseString(notityXml.toString(),paySecret.getParamValue())) { | 
|                     // 校验业务结果 | 
|                     if (AppConstance.CODE_SUCCESS.equals(data.getResult_code())) { | 
|                         // 返回SUCCESS报文 | 
|                         resXml = AppConstance.RESULT_XML_SUCCESS; | 
|                         // 支付费用 | 
|                         Double total_fee = Double.parseDouble(data.getTotal_fee()); | 
|                         // 商户订单号 | 
|                         String payNum = data.getOut_trade_no(); | 
|   | 
|                         LogUtil.info("支付回调关键信息---total_fee:{},payNum:{},orderId:{}", total_fee, payNum, orderId); | 
|                         // 订单ID | 
|                         BigDecimal payMoney = new BigDecimal(total_fee).divide(new BigDecimal(100), 2, | 
|                                 RoundingMode.HALF_UP); | 
|   | 
|   | 
|                         if (order != null && ShopOrder.ORDER_PAY_STATUS_WAIT == order.getPayStatus()) { | 
|                             ShopPayLog payLog = new ShopPayLog(); | 
|                             // TODO 校验支付金额 | 
|                             LogUtil.debug("检查支付金额payMoney={},order.getPayMoney()={}", payMoney, order.getOrderMoney()); | 
|   | 
|                             //构建需要修改订单信息Map | 
|                             Map<String, Object> modifyMap = new HashMap<>(); | 
|                             modifyMap.put("id", order.getId()); | 
|                             //设置微信支付状态为已支付 | 
|                             modifyMap.put("payResult", ShopOrder.ORDER_WX_STATUS_PAY_SUCCESS); | 
|                             //设置支付状态为支付成功 | 
|                             modifyMap.put("payStatus", ShopOrder.ORDER_PAY_STATUS_SUCCESS); | 
|                             //设置订单状态 | 
|                             if (ShopOrder.SHIPPING_METHOD_WL == order.getShippingMethod()) { | 
|                                 //如果是商品订单则进入待发货 | 
|                                 modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_SEND); | 
|                             } else if (ShopOrder.SHIPPING_METHOD_MDZT == order.getShippingMethod()) { | 
|                                 //如果是门店自提则进入待收款 | 
|                                 modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_RECEIVE); | 
|                             } | 
|                             shopOrderDao.updateByMap(modifyMap); | 
|   | 
|                             // 判断该订单是否为拼团产生的订单 | 
|                             ShopActivitiesGroupJoinUser joinUser = shopActivitiesGroupJoinUserDao.selectGroupJoinUserByOrderId(order.getId().longValue()); | 
|                             if (joinUser != null) { | 
|                                 joinUser.setIsHasPay(ShopActivitiesGroupJoinUser.IS_HAS_PAY_Y); | 
|                                 shopActivitiesGroupJoinUserDao.updateByModel(joinUser); | 
|   | 
|                                 ShopActivitiesGroupJoin groupJoin = shopActivitiesGroupJoinDao.selectGroupAndPriceById(joinUser.getGjId()); | 
|                                 List<ShopActivitiesGroupJoinUser> joinUserList = shopActivitiesGroupJoinUserDao.selectGroupJoinUserListByGjId(joinUser.getGjId()); | 
|                                 LogUtil.info("#当前已支付完成:{}#", joinUserList.size()); | 
|                                 // 若拼团中已支付的数量等于几人团预设值,则更新拼团状态为拼团成功 | 
|                                 if (groupJoin.getGroupPrice().getGpCount()  == joinUserList.size()) { | 
|                                     groupJoin.setGjStatus(ShopActivitiesGroupJoin.ACTIVITIES_JOIN_SUCCESS); | 
|                                 } else { | 
|                                     if (joinUser.getUserId().equals(groupJoin.getGjHeadId())) { | 
|                                         groupJoin.setGjStatus(ShopActivitiesGroupJoin.ACTIVITIES_JOIN_ING); | 
|                                     } | 
|                                 } | 
|                                 shopActivitiesGroupJoinDao.updateByModel(groupJoin); | 
|   | 
|                                 SysVipInfo sysVipInfo = sysVipInfoDao.selectByPhone(order.getUserTel(), order.getCompanyId()); | 
|                                 if(sysVipInfo!=null){ | 
|                                     sysVipInfo.setIsDeal(SysVipInfo.DEAL_VIP); | 
|                                     sysVipInfoDao.update(sysVipInfo); | 
|                                 } | 
|   | 
|                             } | 
|   | 
|                             // 根据订单类型创建不同的处理任务 | 
|                             rabiitMqTemplate.sendTopicMsg(RabbitMqConfig.MQ_EXCHANGE_TOPIC +evn, MQTaskRouting.CREATE_ORDER+evn, orderId); | 
|   | 
|   | 
|   | 
|                             threadResult.putInMap("status", "success"); | 
|                             resXml = AppConstance.RESULT_XML_SUCCESS; | 
|   | 
|                             //支付记录 | 
|                             recordPayLog(Integer.valueOf(orderId), 1, order.getOrderNo(), payMoney,  "支付成功", ShopOrder.ORDER_PAY_STATUS_SUCCESS); | 
|   | 
|   | 
|                         } else { | 
|                             LogUtil.info("订单状态不为待付款,order status=", order.getOrderStatus()); | 
|                         } | 
|   | 
|                     } else { | 
|                         LogUtil.info("微信标识业务是失败"); | 
|                         threadResult.putInMap("status", "err"); | 
|                         threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:001)"); | 
|                         resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败"); | 
|                     } | 
|                 } else { | 
|                     LogUtil.info("无效签名"); | 
|                     threadResult.putInMap("status", "err"); | 
|                     threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:002)"); | 
|                     resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败"); | 
|                 } | 
|             } else { | 
|                 LogUtil.info("通信标识失败"); | 
|                 threadResult.putInMap("status", "err"); | 
|                 threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:003)"); | 
|                 resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "通信标识失败"); | 
|             } | 
|         } catch (Exception e) { | 
|             LogUtil.error("支付回调签名错误", e); | 
|             threadResult.putInMap("status", "err"); | 
|             threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:004)"); | 
|             resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "支付回调签名错误"); | 
|         } finally { | 
|             // 通知线程消息 | 
|             PayThreadPool.notifyThread(Integer.valueOf(orderId), threadResult); | 
|             sendResultBack(response, resXml); | 
|         } | 
|         return; | 
|   | 
|     } | 
|   | 
|   | 
|     @Autowired | 
|     MoneyCardUseFlowDao moneyCardUseFlowDao; | 
|   | 
|     @Autowired | 
|     MoneyCardUseDao moneyCardUseDao; | 
|   | 
|     /** | 
|      * 微信充值回调 | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     @RequestMapping(value = "/wxapi/rechargeCallBack") | 
|     public void rechargeCallBack(HttpServletResponse response, HttpServletRequest request) throws IOException { | 
|         LogUtil.info("微信充值回调start...."); | 
|   | 
|         // 获取输入参数 | 
|         String inputLine; | 
|         StringBuffer notityXml = new StringBuffer(); | 
|         String resXml = ""; | 
|         String moneyCardUseFlowId = ""; | 
|   | 
|         AjaxResult threadResult = new AjaxResult(AjaxResult.STATUS_SUCCESS, ""); | 
|         try { | 
|             while ((inputLine = request.getReader().readLine()) != null) { | 
|                 notityXml.append(inputLine); | 
|             } | 
|             request.getReader().close(); | 
|             LogUtil.info("notityXml ---- :{} ", notityXml); | 
|   | 
|   | 
|             // XMl转对象 | 
|             Object bb = Util.getObjectFromXML(notityXml.toString(), NotifyData.class); | 
|             NotifyData    data = new NotifyData(); | 
|             BeanUtils.copyProperties(bb,data); | 
|             LogUtil.info("----return_code = {}", data.getReturn_code()); | 
|   | 
|   | 
|             // 返回状态码 SUCCESS/FAIL | 
|             if (AppConstance.CODE_SUCCESS.equals(data.getReturn_code())) { | 
|   | 
|                 moneyCardUseFlowId = data.getAttach(); | 
|                 // 检验订单状态 | 
|                 MoneyCardUseFlow moneyCardUseFlow = moneyCardUseFlowDao.selectById(Long.valueOf(moneyCardUseFlowId)); | 
|                 SysVipInfo user=sysVipInfoDao.selectById(moneyCardUseFlow.getVipId()); | 
|   | 
|                 // 校验签名 | 
|                 BusParameterSettings paySecret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_SECRET, user.getCompanyId()); | 
|                 if (Signature.checkIsSignValidFromResponseString(notityXml.toString(),paySecret.getParamValue())) { | 
|                     // 校验业务结果 | 
|                     if (AppConstance.CODE_SUCCESS.equals(data.getResult_code())) { | 
|                         // 返回SUCCESS报文 | 
|                         resXml = AppConstance.RESULT_XML_SUCCESS; | 
|                         // 支付费用 | 
|                         Double total_fee = Double.parseDouble(data.getTotal_fee()); | 
|                         // 商户订单号 | 
|                         String payNum = data.getOut_trade_no(); | 
|   | 
|                         LogUtil.info("支付回调关键信息---total_fee:{},payNum:{},moneyCardUseFlowId:{}", total_fee, payNum, moneyCardUseFlowId); | 
|                         // 订单ID | 
|                         Double payMoney = new BigDecimal(total_fee).divide(new BigDecimal(100), 2, | 
|                                 BigDecimal.ROUND_HALF_DOWN).doubleValue(); | 
|   | 
|                         if (moneyCardUseFlow.getCarUseId()==null) { | 
|   | 
|                             if(payMoney.equals(moneyCardUseFlow.getTotal())){ | 
|                                 threadResult.putInMap("status", "success"); | 
|                                 resXml = AppConstance.RESULT_XML_SUCCESS; | 
|                                 MoneyCardUse moneyCardUse = moneyCardUseDao.selectVipCard(user.getId()); | 
|                                 moneyCardUseFlow.setCarUseId(moneyCardUse.getId()); | 
|                                 moneyCardUseFlow.setCardName(moneyCardUse.getCardName()); | 
|                                 Double balance=moneyCardUse.getGiftMoney() + moneyCardUse.getRealMoney(); | 
|                                 moneyCardUseFlow.setBalance(balance==null?0:balance); | 
|                                 moneyCardUseFlowDao.update(moneyCardUseFlow); | 
|   | 
|                                 moneyCardUse.setRealMoney(moneyCardUse.getRealMoney()+payMoney.doubleValue()); | 
|                                 moneyCardUseDao.update(moneyCardUse); | 
|   | 
|                                 //充值记录 | 
|                                 recordPayLog(Integer.valueOf(moneyCardUseFlowId),2,moneyCardUseFlow.getPayNo(),new BigDecimal(payMoney), "充值成功", ShopOrder.ORDER_PAY_STATUS_SUCCESS); | 
|                             }else{ | 
|                                 LogUtil.debug("支付金额不一致,检查支付金额payMoney={},order.getPayMoney()={}", payMoney, moneyCardUseFlow.getTotal()); | 
|                                 threadResult.putInMap("status", "err"); | 
|                                 threadResult.putInMap("msg", "支付金额不一致"); | 
|                                 resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败"); | 
|                             } | 
|   | 
|                         } else { | 
|                             LogUtil.info("充值已经确认"); | 
|                         } | 
|   | 
|                     } else { | 
|                         LogUtil.info("微信标识业务是失败"); | 
|                         threadResult.putInMap("status", "err"); | 
|                         threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:001)"); | 
|                         resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败"); | 
|                     } | 
|                 } else { | 
|                     LogUtil.info("无效签名"); | 
|                     threadResult.putInMap("status", "err"); | 
|                     threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:002)"); | 
|                     resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "微信标识业务是失败"); | 
|                 } | 
|             } else { | 
|                 LogUtil.info("通信标识失败"); | 
|                 threadResult.putInMap("status", "err"); | 
|                 threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:003)"); | 
|                 resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "通信标识失败"); | 
|             } | 
|         } catch (Exception e) { | 
|             LogUtil.error("支付回调签名错误", e); | 
|             threadResult.putInMap("status", "err"); | 
|             threadResult.putInMap("msg", "查询支付信息失败,请联系客服或者刷新支付信息(错误码:004)"); | 
|             resXml = AppConstance.RESULT_XML_FAIL.replace(ERRORMSG, "支付回调签名错误"); | 
|         } finally { | 
|             // 通知线程消息 | 
|             PayThreadPool.notifyThread(Integer.valueOf(moneyCardUseFlowId), threadResult); | 
|             sendResultBack(response, resXml); | 
|         } | 
|         return; | 
|   | 
|     } | 
|   | 
|   | 
|   | 
|     private void recordPayLog(Integer orderId, Integer type,String orderNo, BigDecimal payMoney, String payRemark, int payStatus) { | 
|         //设置支付类型为收款 | 
|         ShopPayLog payLog=new ShopPayLog(); | 
|         payLog.setPayType(type); | 
|         payLog.setJoinId(orderId); | 
|         payLog.setPayMoney(payMoney); | 
|         payLog.setPayOrderNo(orderNo); | 
|         payLog.setPayRemark(payRemark); | 
|         payLog.setPayStatus(payStatus); | 
|         payLog.setCreateBy(AppConstance.SYSTEM_USER); | 
|         payLog.setUpdateBy(AppConstance.SYSTEM_USER); | 
|         shopPayLogDao.insert(payLog); | 
|     } | 
|   | 
|   | 
|     private void sendResultBack(HttpServletResponse response, String resXml) throws IOException { | 
|         LogUtil.info("返回微信数据={}", resXml); | 
|         ServletOutputStream out = response.getOutputStream(); | 
|         out.write(resXml.getBytes()); | 
|         out.flush(); | 
|         out.close(); | 
|     } | 
|   | 
|     /** | 
|      * 二维码消核 | 
|      * | 
|      * @param id | 
|      * @return | 
|      * @author HEMING | 
|      * @email 910000889@qq.com | 
|      * @date 2018年7月7日 | 
|      */ | 
|     @RequestMapping(value = "/verify/{keys}") | 
|     public @ResponseBody | 
|     AjaxResult verify(@PathVariable("keys") String id) { | 
|         LogUtil.debug("消核请求:{}", id); | 
|         return null; | 
|     } | 
|   | 
|   | 
|   | 
| } |