You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					255 lines
				
				11 KiB
			
		
		
			
		
	
	
					255 lines
				
				11 KiB
			| 
											3 years ago
										 | //package com.ynxbd.wx.servlet;
 | ||
|  | //
 | ||
|  | //import com.ynxbd.api.bean.SmsGeneral;
 | ||
|  | //import com.ynxbd.api.service.HCodeService;
 | ||
|  | //import com.ynxbd.api.helper.SmsService;
 | ||
|  | //import com.ynxbd.common.bean.PayInfoBean;
 | ||
|  | //import com.ynxbd.common.bean.ReservationBean4Local;
 | ||
|  | //import com.ynxbd.common.bean.remove.Time_RegResponseBean;
 | ||
|  | //import com.ynxbd.common.dao.DBDao;
 | ||
|  | //import com.ynxbd.common.dao.WSDao;
 | ||
|  | //import com.ynxbd.api.utils.DateGenerate;
 | ||
|  | //import com.ynxbd.ws.utils.WsResult;
 | ||
|  | //import com.ynxbd.api.config.WxConfig;
 | ||
|  | //import com.ynxbd.api.utils.CommonUtil;
 | ||
|  | //import com.ynxbd.api.utils.Global;
 | ||
|  | //import org.apache.log4j.Logger;
 | ||
|  | //import org.apache.log4j.NDC;
 | ||
|  | //import weixin.popular.bean.paymch.MchBaseResult;
 | ||
|  | //import weixin.popular.bean.paymch.MchPayNotify;
 | ||
|  | //import weixin.popular.util.SignatureUtil;
 | ||
|  | //import weixin.popular.util.StreamUtils;
 | ||
|  | //import weixin.popular.util.XMLConverUtil;
 | ||
|  | //
 | ||
|  | //import javax.servlet.ServletInputStream;
 | ||
|  | //import javax.servlet.ServletOutputStream;
 | ||
|  | //import javax.servlet.http.HttpServlet;
 | ||
|  | //import javax.servlet.http.HttpServletRequest;
 | ||
|  | //import javax.servlet.http.HttpServletResponse;
 | ||
|  | //import java.nio.charset.StandardCharsets;
 | ||
|  | //import java.util.List;
 | ||
|  | //import java.util.Map;
 | ||
|  | //import java.util.UUID;
 | ||
|  | //
 | ||
|  | ///**
 | ||
|  | // * @author 张剑峰
 | ||
|  | // * @version v1.0.0
 | ||
|  | // * @Project:微信公众号
 | ||
|  | // * @date 2017年7月23日下午4:28:39
 | ||
|  | // * @Copyright: 2017云南新八达科技有限公司 All rights reserved.
 | ||
|  | // */
 | ||
|  | //public class Time_RegPayMchNotifyServlet extends HttpServlet {
 | ||
|  | //
 | ||
|  | //    private static final Logger logger = Logger.getLogger(Time_RegPayMchNotifyServlet.class);
 | ||
|  | //
 | ||
|  | //    @Override
 | ||
|  | //    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
 | ||
|  | //        String remoteAddr = Global.getIpAddr(request);
 | ||
|  | //        NDC.remove();
 | ||
|  | //        NDC.push(remoteAddr);
 | ||
|  | //
 | ||
|  | //        ServletInputStream inputStream = null;
 | ||
|  | //        ServletOutputStream outputStream = null;
 | ||
|  | //        try {
 | ||
|  | //            inputStream = request.getInputStream();
 | ||
|  | //            outputStream = response.getOutputStream();
 | ||
|  | //
 | ||
|  | //            // 获取请求数据
 | ||
|  | //            String wxXmlData = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
 | ||
|  | //
 | ||
|  | //            // 将XML转为MAP,确保所有字段都参与签名验证
 | ||
|  | //            Map<String, String> wxMapData = XMLConverUtil.convertToMap(wxXmlData);
 | ||
|  | //            StringBuilder wxResp = new StringBuilder();
 | ||
|  | //            for (String key : wxMapData.keySet()) {
 | ||
|  | //                wxResp.append(key).append("=").append(wxMapData.get(key)).append(";"); // 拼接微信返回的信息
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            // 转换数据对象
 | ||
|  | //            MchPayNotify wxPayNotify = XMLConverUtil.convertToObject(MchPayNotify.class, wxXmlData);
 | ||
|  | //            String wxOpenid = wxPayNotify.getOpenid();
 | ||
|  | //            String wxTradeNo = wxPayNotify.getOut_trade_no();
 | ||
|  | //            String wxTransactionId = wxPayNotify.getTransaction_id();
 | ||
|  | //            Integer wxTotalFee = wxPayNotify.getTotal_fee(); // 金额
 | ||
|  | //
 | ||
|  | //            // 已处理 去重
 | ||
|  | //            if (Global.keys.exists(wxTransactionId)) {
 | ||
|  | //                return;
 | ||
|  | //            } else {
 | ||
|  | //                Global.keys.add(wxTransactionId, Global.expire);
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            // @since 2.8.5
 | ||
|  | //            wxPayNotify.buildDynamicField(wxMapData);
 | ||
|  | //            // 签名验证
 | ||
|  | //            MchBaseResult baseResult = new MchBaseResult();
 | ||
|  | //            if (!SignatureUtil.validateSign(wxMapData, WxConfig.getMchKey())) { // 验证未通过-->通知支付失败
 | ||
|  | //                baseResult.setReturn_code("FAIL");
 | ||
|  | //                baseResult.setReturn_msg("ERROR");
 | ||
|  | //                outputStream.write(XMLConverUtil.convertToXML(baseResult).getBytes());
 | ||
|  | //
 | ||
|  | //                logger.info("支付失败!");
 | ||
|  | //                return;
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            // 验证通过
 | ||
|  | //            baseResult.setReturn_code("SUCCESS");
 | ||
|  | //            baseResult.setReturn_msg("OK");
 | ||
|  | //            outputStream.write(XMLConverUtil.convertToXML(baseResult).getBytes());
 | ||
|  | //
 | ||
|  | //            // 保存支付结果
 | ||
|  | //            PayInfoBean payInfo = new PayInfoBean();
 | ||
|  | //            payInfo.setOpenID(wxOpenid);
 | ||
|  | //            payInfo.setTradeNo(wxTradeNo);
 | ||
|  | //            payInfo.setInfo(wxResp.toString());
 | ||
|  | //
 | ||
|  | //            DBDao dbDao = new DBDao();
 | ||
|  | //            if (dbDao.hasPayResult(payInfo)) {
 | ||
|  | //                return;
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            if (dbDao.hasReserved(wxTradeNo)) { // 当前订单号是否已挂号成功
 | ||
|  | //                return;
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            // 保存支付结果信息
 | ||
|  | //            dbDao.savePayResult(payInfo);
 | ||
|  | //
 | ||
|  | //            List<String> lstDate = DateGenerate.getDateAndTime(wxPayNotify.getTime_end());
 | ||
|  | //
 | ||
|  | //            logger.info(String.format("更新挂号信息(after pay),%s", wxTradeNo));
 | ||
|  | //            ReservationBean4Local reserve = dbDao.updateReserve(wxTradeNo, lstDate.get(0), lstDate.get(1), wxTransactionId);
 | ||
|  | //
 | ||
|  | //            if (reserve == null) {
 | ||
|  | //                logger.info("更新挂号信息失败");
 | ||
|  | //                return;
 | ||
|  | //            }
 | ||
|  | //            String reserveTradeNo = reserve.getTradeNo();
 | ||
|  | //            String patientId = reserve.getPatientId();
 | ||
|  | //            if ("".equals(patientId) || reserveTradeNo == null) {
 | ||
|  | //                logger.info(String.format("挂号失败reservation=null,patientId=%s, tradeNo=%s, transNo=%s", patientId, reserveTradeNo, wxTransactionId));
 | ||
|  | //                return;
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            // 挂号
 | ||
|  | //            logger.info(String.format("开始调用HIS挂号:patientId=%s", patientId));
 | ||
|  | //            WsResult wsResult = new WSDao().reg4Time(reserve);
 | ||
|  | //
 | ||
|  | //            if (wsResult.getResponseCode() == -1) { // 调用HIS失败-->自动退款
 | ||
|  | //                logger.info(String.format("调用HIS挂号失败:patientId=%s", patientId));
 | ||
|  | //                // 自动退款
 | ||
|  | //                wxRefund(wxOpenid, wxTradeNo, wxTotalFee, wxTransactionId, wsResult.getResponseMessage());
 | ||
|  | //                return;
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            Time_RegResponseBean time_reg = wsResult.getDataMapBean(Time_RegResponseBean.class);
 | ||
|  | //
 | ||
|  | //            String hisQueueNo = time_reg.getQueueNo(); // 门诊号
 | ||
|  | //            String hisTransNo = time_reg.getHISTransNo();
 | ||
|  | //            logger.info(String.format("HIS返回 hisTransNo=%s, hisQueueNo=%s", hisTransNo, hisQueueNo));
 | ||
|  | //
 | ||
|  | //            // 更新挂号信息
 | ||
|  | //            logger.info(String.format("更新挂号信息(after his reservation):tradeNo=%s, patientId=%s, transNo=%s", wxTradeNo, patientId, wxTransactionId));
 | ||
|  | //            dbDao.updateRegAfter(wxTradeNo, wxTransactionId, hisTransNo, hisQueueNo);
 | ||
|  | //            logger.info(String.format("挂号成功,patientId=%s", patientId));
 | ||
|  | //
 | ||
|  | //            if ("wxadc09f1c9d0bccee".equals(WxConfig.getAppId())) { // 德宏州二院,挂号成功发短信
 | ||
|  | //                ReservationBean4Local bean4Local = dbDao.getReservationInfo4Time(wxTransactionId);
 | ||
|  | //                // 短信通知
 | ||
|  | //                if (bean4Local != null && !bean4Local.getOpenId().equals("")) { // 关注过,并且填写过电话号码
 | ||
|  | //                    String tip = smsNotice(bean4Local) ? "成功" : "失败";
 | ||
|  | //                    logger.info("发送预约短信通知-" + tip);
 | ||
|  | //                }
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            HCodeService.regPayReportHISData(wxOpenid, patientId, reserve.getDeptName(), reserve.getReservationDate()); // 电子健康卡上报数据
 | ||
|  | //        } catch (Exception e) {
 | ||
|  | //            logger.info("回复微信支付成功通知异常");
 | ||
|  | //            e.printStackTrace();
 | ||
|  | //        } finally {
 | ||
|  | //            try {
 | ||
|  | //                if (inputStream != null) inputStream.close();
 | ||
|  | //
 | ||
|  | //                if (outputStream != null) outputStream.close();
 | ||
|  | //            } catch (Exception e) {
 | ||
|  | //                e.printStackTrace();
 | ||
|  | //            }
 | ||
|  | //        }
 | ||
|  | //    }
 | ||
|  | //
 | ||
|  | //
 | ||
|  | //    /**
 | ||
|  | //     * 预约短信通知
 | ||
|  | //     *
 | ||
|  | //     * @param reserve 预约信息
 | ||
|  | //     */
 | ||
|  | //    private boolean smsNotice(ReservationBean4Local reserve) {
 | ||
|  | //        String tel = reserve.getTel();
 | ||
|  | //        String date = reserve.getReservationDate();
 | ||
|  | //        String begTime = reserve.getBegTime();
 | ||
|  | //        String endTime = reserve.getEndTime();
 | ||
|  | //
 | ||
|  | //        if (tel == null || tel.length() != 11) {
 | ||
|  | //            logger.info("tel为空或长度异常");
 | ||
|  | //            return false;
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //        SmsService smsService = new SmsService();
 | ||
|  | //        SmsGeneral sms = new SmsGeneral();
 | ||
|  | //        sms.setTime(date + " " + begTime + "-" + endTime);
 | ||
|  | //        sms.setAddress(reserve.getAddress());
 | ||
|  | //        sms.setDeptName(reserve.getDeptName());
 | ||
|  | //        sms.setDoctorName(reserve.getDoctName());
 | ||
|  | //        sms.setSeq(String.valueOf(reserve.getCallNo()));
 | ||
|  | //
 | ||
|  | //        // 发送
 | ||
|  | //        return smsService.smsGeneral("SMS_180046170", tel, sms);
 | ||
|  | //    }
 | ||
|  | //
 | ||
|  | //    /**
 | ||
|  | //     * 自动退款
 | ||
|  | //     *
 | ||
|  | //     * @param openid   openid
 | ||
|  | //     * @param tradeNo  商户单号
 | ||
|  | //     * @param payMoney 退款金额
 | ||
|  | //     * @param transNo  交易流水号
 | ||
|  | //     * @param message  退款原因
 | ||
|  | //     * @return 是否成功
 | ||
|  | //     */
 | ||
|  | //    private boolean wxRefund(String openid, String tradeNo, Integer payMoney, String transNo, String message) {
 | ||
|  | //        boolean result = false;
 | ||
|  | //
 | ||
|  | //        // 如果HIS交过费返回true-->就不向下执行(自动退款)
 | ||
|  | //        String dateShort = DateGenerate.getStringDateShort();
 | ||
|  | //        if (new WSDao().hasOutPayed(DateGenerate.getNextDay(dateShort, "-7"), DateGenerate.getNextDay(dateShort, "7"), transNo)) {
 | ||
|  | //            logger.info(String.format("His已经缴费成功,不退费, tradeNo=%s, transNo=%s", tradeNo, transNo));
 | ||
|  | //            return false;
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //        logger.info(String.format("挂号失败,开始申请退款transNo=%s", transNo));
 | ||
|  | //        DBDao dbDao = new DBDao();
 | ||
|  | //        // 预约失败后记录
 | ||
|  | //        if (!dbDao.updateReserveAfterFail(tradeNo, message)) { // 记录失败
 | ||
|  | //            logger.info(String.format("预约记录失败transNo=%s", transNo));
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //        // 退款
 | ||
|  | //        boolean refundResult = dbDao.refund(1,
 | ||
|  | //                UUID.randomUUID().toString().replace("-", ""),
 | ||
|  | //                tradeNo,
 | ||
|  | //                payMoney,
 | ||
|  | //                message);
 | ||
|  | //
 | ||
|  | //        if (refundResult) { // 退款成功
 | ||
|  | //            logger.info(String.format("自动退款申请成功:tradeNo=%s", transNo));
 | ||
|  | //            try {
 | ||
|  | //                CommonUtil.sendMessage(openid, String.format("挂号失败,%s,已申请退款:%s", message, transNo)); // 推送消息
 | ||
|  | //                result = true;
 | ||
|  | //            } catch (Exception ex) {
 | ||
|  | //                logger.info(String.format("自动退款失败:tradeNo=%s", transNo));
 | ||
|  | //                logger.info(ex.getMessage());
 | ||
|  | //            }
 | ||
|  | //        }
 | ||
|  | //        return result;
 | ||
|  | //    }
 | ||
|  | //}
 |