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.
		
		
		
		
			
				
					
					
						
							171 lines
						
					
					
						
							8.7 KiB
						
					
					
				
			
		
		
	
	
							171 lines
						
					
					
						
							8.7 KiB
						
					
					
				| //package com.ynxbd.wx.servlet.remove;
 | |
| //
 | |
| //import com.ynxbd.api.service.HCodeService;
 | |
| //import com.ynxbd.common.bean.PayInfoBean;
 | |
| //import com.ynxbd.common.bean.ReservationBean4Local;
 | |
| //import com.ynxbd.common.dao.DBDao;
 | |
| //import com.ynxbd.common.dao.WSDao;
 | |
| //import com.ynxbd.api.utils.DateGenerate;
 | |
| //import com.ynxbd.api.config.WeChatConfig;
 | |
| //import com.ynxbd.api.utils.CommonUtil;
 | |
| //import com.ynxbd.api.utils.Global;
 | |
| //import org.slf4j.Logger;
 | |
| //import org.slf4j.LoggerFactory;
 | |
| //import org.slf4j.MDC;
 | |
| //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.ServletException;
 | |
| //import javax.servlet.http.HttpServlet;
 | |
| //import javax.servlet.http.HttpServletRequest;
 | |
| //import javax.servlet.http.HttpServletResponse;
 | |
| //import java.io.IOException;
 | |
| //import java.nio.charset.StandardCharsets;
 | |
| //import java.util.HashMap;
 | |
| //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 PayMchNotifyServlet extends HttpServlet {
 | |
| //
 | |
| //    private static final Logger logger = LoggerFactory.getLogger(PayMchNotifyServlet.class);
 | |
| //
 | |
| //    @Override
 | |
| //    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 | |
| //        MDC.remove("ip");
 | |
| //        MDC.put("ip", Global.getIpAddr(request));
 | |
| //
 | |
| //        // 获取请求数据
 | |
| //        String xmlData = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
 | |
| //        // 将XML转为MAP,确保所有字段都参与签名验证
 | |
| //        Map<String, String> mapData = XMLConverUtil.convertToMap(xmlData);
 | |
| //        StringBuilder reStr = new StringBuilder();
 | |
| //        for (String key : mapData.keySet()) {
 | |
| //            if (reStr.toString().equals(""))
 | |
| //                reStr = new StringBuilder(key + "=" + mapData.get(key));
 | |
| //            else
 | |
| //                reStr.append(";").append(key).append("=").append(mapData.get(key));
 | |
| //        }
 | |
| //        // 转换数据对象
 | |
| //        MchPayNotify payNotify = XMLConverUtil.convertToObject(MchPayNotify.class, xmlData);
 | |
| //
 | |
| //        // 已处理 去重
 | |
| //        if (Global.keys.exists(payNotify.getTransaction_id())) {
 | |
| //            return;
 | |
| //        } else {
 | |
| //            Global.keys.add(payNotify.getTransaction_id(), Global.expire);
 | |
| //        }
 | |
| //        // @since 2.8.5
 | |
| //        payNotify.buildDynamicField(mapData);
 | |
| //        // 签名验证
 | |
| //        if (SignatureUtil.validateSign(mapData, WeChatConfig.getMchKey())) {
 | |
| //            MchBaseResult baseResult = new MchBaseResult();
 | |
| //            baseResult.setReturn_code("SUCCESS");
 | |
| //            baseResult.setReturn_msg("OK");
 | |
| //            response.getOutputStream().write(XMLConverUtil.convertToXML(baseResult).getBytes());
 | |
| //            // 保存支付结果
 | |
| //            PayInfoBean payInfo = new PayInfoBean();
 | |
| //            payInfo.setOpenID(payNotify.getOpenid());
 | |
| //            payInfo.setTradeNo(payNotify.getOut_trade_no());
 | |
| //            payInfo.setInfo(reStr.toString());
 | |
| //            DBDao dbDao = new DBDao();
 | |
| //            if (dbDao.hasPayResult(payInfo)) {
 | |
| //                return;
 | |
| //            }
 | |
| //            if (dbDao.hasReserved(payNotify.getOut_trade_no())) {
 | |
| //                return;
 | |
| //            }
 | |
| //            dbDao.savePayResult(payInfo);
 | |
| //
 | |
| //            String openId = payInfo.getOpenID();
 | |
| //
 | |
| //            // 更新挂号信息
 | |
| //            logger.info(String.format("更新挂号信息(after pay),%s", payNotify.getTransaction_id()));
 | |
| //            List<String> lstDate = DateGenerate.getDateAndTime(payNotify.getTime_end());
 | |
| //            ReservationBean4Local reservation = dbDao.updateReserve(payNotify.getOut_trade_no(),
 | |
| //                    lstDate.get(0), lstDate.get(1), payNotify.getTransaction_id());
 | |
| //            if (reservation == null) {
 | |
| //                logger.info(String.format("挂号失败(reservation=null),开始申请退款:%s", payNotify.getTransaction_id()));
 | |
| //                boolean refundResult = dbDao.refund(1, UUID.randomUUID().toString().replace("-", ""),
 | |
| //                        payInfo.getTradeNo(), "", payNotify.getTotal_fee(), payNotify.getTotal_fee(), "挂号信息更新失败");
 | |
| //                if (refundResult) {
 | |
| //                    logger.info(String.format("自动退款申请成功:tradeNo=%s", payNotify.getTransaction_id()));
 | |
| //                    try {
 | |
| //                        CommonUtil.sendMessage(openId, String.format("挂号失败,已申请退款:%s", payNotify.getTransaction_id()));
 | |
| //                    } catch (Exception ex) {
 | |
| //                        logger.info(ex.getMessage());
 | |
| //                    }
 | |
| //                }
 | |
| //                return;
 | |
| //            }
 | |
| //
 | |
| //            if (reservation.getPatientId() == null) {
 | |
| //                logger.info(String.format("patientId is null, tradeno=%s, transno=%s", payNotify.getOut_trade_no(), payNotify.getTransaction_id()));
 | |
| //                return;
 | |
| //            }
 | |
| //
 | |
| //            // 预约
 | |
| //            logger.info(String.format("开始调用HIS挂号:patientid=%s", reservation.getPatientId()));
 | |
| //            Map<Object, Object> params = new HashMap<>();
 | |
| //            params.put("patientid", reservation.getPatientId());
 | |
| //            params.put("reservationdate", reservation.getReservationDate());
 | |
| //            params.put("deptcode", reservation.getDeptCode());
 | |
| //            params.put("doctcode", reservation.getDoctCode());
 | |
| //            params.put("cliniccode", reservation.getClinicCode());
 | |
| //            params.put("tid", reservation.getTID());
 | |
| //            params.put("regfee", reservation.getRegFee());
 | |
| //            params.put("clinicfee", reservation.getClinicFee());
 | |
| //            params.put("paymoney", reservation.getPayMoney());
 | |
| //            params.put("paydate", reservation.getPayDate());
 | |
| //            params.put("paytime", reservation.getPayTime());
 | |
| //            // String transNo = java.util.UUID.randomUUID().toString();
 | |
| //            String transNo = reservation.getBankTransNo();
 | |
| //            params.put("transno", transNo);
 | |
| //            params.put("paydeviceid", reservation.getPayDeviceId());
 | |
| //            params.put("banktransno", reservation.getBankTransNo());
 | |
| //
 | |
| //            WSDao wsDao = new WSDao();
 | |
| //            Map<Object, Object> reservationResult = wsDao.reservation(params);
 | |
| //            String hisTransNo = reservationResult.get("histransno").toString();
 | |
| //            if (!hisTransNo.equals("")) {
 | |
| //                // 更新预约信息
 | |
| //                logger.info(String.format("更新挂号信息(after his reservation):patientid=%s", reservation.getPatientId()));
 | |
| //                dbDao.updateReservationAfterReservation(reservation.getTradeNo(), transNo, hisTransNo);
 | |
| //                logger.info(String.format("挂号成功,patientid=%s", reservation.getPatientId()));
 | |
| //
 | |
| //                HCodeService.regPayReportHISData(reservation.getOpenId(), reservation.getPatientId(), reservation.getDeptName(), reservation.getReservationDate()); // 电子健康卡上报数据
 | |
| //            } else {
 | |
| //                dbDao.updateReserveAfterFail(reservation.getTradeNo(), reservationResult.get("message").toString());
 | |
| //                logger.info(String.format("挂号失败,开始申请退款:patientid=%s", reservation.getPatientId()));
 | |
| //                boolean refundResult = dbDao.refund(1, UUID.randomUUID().toString().replace("-", ""),
 | |
| //                        payInfo.getTradeNo(), "", payNotify.getTotal_fee(), payNotify.getTotal_fee(), reservationResult.get("message").toString());
 | |
| //                if (refundResult) {
 | |
| //                    logger.info(String.format("退款申请成功:patientid=%s", reservation.getPatientId()));
 | |
| //                    try {
 | |
| //                        CommonUtil.sendMessage(openId, "挂号失败,已申请退款!  " + reservationResult.get("message").toString());
 | |
| //                    } catch (Exception ex) {
 | |
| //                        logger.info(ex.getMessage());
 | |
| //                    }
 | |
| //
 | |
| //                }
 | |
| //            }
 | |
| //        } else {
 | |
| //            MchBaseResult baseResult = new MchBaseResult();
 | |
| //            baseResult.setReturn_code("FAIL");
 | |
| //            baseResult.setReturn_msg("ERROR");
 | |
| //            response.getOutputStream().write(XMLConverUtil.convertToXML(baseResult).getBytes());
 | |
| //            logger.info("支付失败!");
 | |
| //        }
 | |
| //    }
 | |
| //
 | |
| //}
 | |
| 
 |