|  | @ -0,0 +1,130 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.patient.service.wechat;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.servicePackage.ServicePackageSignRecordDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.wx.WxWechatDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.servicepackage.ServicePackageSignRecordDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.patient.PatientRequestMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.XMLUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.wechat.WeiXinPayUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.dao.WechatDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.service.WxPayLogService;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class WechatPayService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WechatDao wechatDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private ServicePackageSignRecordDao servicePackageSignRecordDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private WxPayLogService wxPayLogService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 微信统一下单接口
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public Map<String,Object> unifiedorder(Map<String,String> par,String appKey) throws Exception {
 | 
	
		
			
				|  |  |         String xml = WeiXinPayUtils.getXmlBeforUnifiedorder(par, appKey);
 | 
	
		
			
				|  |  |         Map<String, Object> map = WeiXinPayUtils.unifiedorder(xml);
 | 
	
		
			
				|  |  |         //创建日志记录
 | 
	
		
			
				|  |  |         wxPayLogService.createLog(par,xml,map);
 | 
	
		
			
				|  |  |         return map;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 微信下单 服务包下单
 | 
	
		
			
				|  |  |      * @param wxId
 | 
	
		
			
				|  |  |      * @param openid
 | 
	
		
			
				|  |  |      * @param ip
 | 
	
		
			
				|  |  |      * @param orderId  服务包id
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public Map<String,Object> unifiedorder(String wxId, String openid, String ip, String orderId) throws Exception {
 | 
	
		
			
				|  |  |         Map<String, String> map = new HashMap<>();
 | 
	
		
			
				|  |  |         WxWechatDO wxWechat = wechatDao.findOne(wxId);
 | 
	
		
			
				|  |  |         if(wxWechat==null){
 | 
	
		
			
				|  |  |             throw new Exception("can't find wechat:the wxId is "+wxId);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String url = wxWechat.getBaseUrl();
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(url)){
 | 
	
		
			
				|  |  |             throw new Exception("wechat url is null:the wechatId is "+wxId);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String mchId = wxWechat.getMchId();
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(mchId)){
 | 
	
		
			
				|  |  |             throw new Exception("can't find mch_id from wecht: the wxId is "+wxId);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String appKey = wxWechat.getAppKey();
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(appKey)){
 | 
	
		
			
				|  |  |             throw new Exception("can't find app_key from wecht: the wxId is "+wxId);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         ServicePackageSignRecordDO spsr = servicePackageSignRecordDao.findOne(orderId);
 | 
	
		
			
				|  |  |         if(spsr == null){
 | 
	
		
			
				|  |  |             throw new Exception("can't find ServicePackageSignRecor by "+orderId);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //拼接商品名
 | 
	
		
			
				|  |  |         String body ="";
 | 
	
		
			
				|  |  |         String hospitalName = spsr.getHospitalName();
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(hospitalName)){
 | 
	
		
			
				|  |  |             body += hospitalName +"-";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String servicePackageName = spsr.getServicePackageName();
 | 
	
		
			
				|  |  |         if(StringUtils.isEmpty(servicePackageName)){
 | 
	
		
			
				|  |  |             servicePackageName = "服务包";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         body += servicePackageName;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //需要支付的总金额
 | 
	
		
			
				|  |  |         Long price = spsr.getPrice();
 | 
	
		
			
				|  |  |         if(price == null){
 | 
	
		
			
				|  |  |             throw new Exception("the price is not correct,the base_service_package_sign_record is "+orderId);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         long pric = price * 100;
 | 
	
		
			
				|  |  |         map.put("appid",wxWechat.getAppId());
 | 
	
		
			
				|  |  |         map.put("mch_id",wxWechat.getMchId());
 | 
	
		
			
				|  |  |         map.put("body",body);
 | 
	
		
			
				|  |  |         map.put("trade_type","JSAPI");
 | 
	
		
			
				|  |  |         map.put("spbill_create_ip",ip);
 | 
	
		
			
				|  |  |         map.put("out_trade_no",orderId);
 | 
	
		
			
				|  |  |         map.put("total_fee",pric+"");
 | 
	
		
			
				|  |  |         map.put("openid",openid);
 | 
	
		
			
				|  |  |         map.put("notify_url",url+PatientRequestMapping.WechatPay.PRE_URL+PatientRequestMapping.WechatPay.Notify);
 | 
	
		
			
				|  |  |         return unifiedorder(map,appKey);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public Map<String,String> getWxPayResultNotify(String result) {
 | 
	
		
			
				|  |  |         Map<String,String> rs = new HashedMap();
 | 
	
		
			
				|  |  |         Map<String,Object> wxrs =  XMLUtil.xmltoMap(result);
 | 
	
		
			
				|  |  |         if("SUCCESS".equals(wxrs.get("return_code").toString())){
 | 
	
		
			
				|  |  |             // 我方 订单号+时间差
 | 
	
		
			
				|  |  |             String seqNo = wxrs.get("out_trade_no")+"";
 | 
	
		
			
				|  |  |             // 微信充值流水号
 | 
	
		
			
				|  |  |             String wxSeqNo = wxrs.get("transaction_id")+"";
 | 
	
		
			
				|  |  |             // 微信分配的终端设备号
 | 
	
		
			
				|  |  |             String wxDeviceInfo = wxrs.get("device_info")+"";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //更改业务状态,做日志保存等操作
 | 
	
		
			
				|  |  |             ServicePackageSignRecordDO spsr = servicePackageSignRecordDao.findOne(seqNo);
 | 
	
		
			
				|  |  |             spsr.setStatus(2);
 | 
	
		
			
				|  |  |             servicePackageSignRecordDao.save(spsr);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             rs.put("code","1");
 | 
	
		
			
				|  |  |             rs.put("message","支付成功");
 | 
	
		
			
				|  |  |         }else{
 | 
	
		
			
				|  |  |             rs.put("code","-1");
 | 
	
		
			
				|  |  |             rs.put("return_msg","FAIL");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return rs;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |