|
@ -0,0 +1,359 @@
|
|
|
|
|
|
package com.yihu.jw.order;
|
|
|
|
|
|
import com.yihu.jw.entity.base.patient.BasePatientDO;
|
|
|
import com.yihu.jw.entity.base.servicePackage.ServicePackageSignRecordDO;
|
|
|
import com.yihu.jw.entity.base.wx.BasePatientWechatDo;
|
|
|
import com.yihu.jw.entity.base.wx.WxPayLogDO;
|
|
|
import com.yihu.jw.entity.base.wx.WxWechatDO;
|
|
|
import com.yihu.jw.entity.order.BusinessOrderDO;
|
|
|
import com.yihu.jw.entity.order.BusinessOrderRefundDO;
|
|
|
import com.yihu.jw.order.dao.BusinessOrderDao;
|
|
|
import com.yihu.jw.order.dao.BusinessOrderRefundDao;
|
|
|
import com.yihu.jw.order.pay.wx.WeChatConfig;
|
|
|
import com.yihu.jw.patient.dao.BasePatientDao;
|
|
|
import com.yihu.jw.rm.base.BaseRequestMapping;
|
|
|
import com.yihu.jw.rm.hospital.BaseHospitalRequestMapping;
|
|
|
import com.yihu.jw.rm.patient.PatientRequestMapping;
|
|
|
import com.yihu.jw.util.common.XMLUtil;
|
|
|
import com.yihu.jw.util.date.DateUtil;
|
|
|
import com.yihu.jw.util.wechat.WeiXinPayUtils;
|
|
|
import com.yihu.jw.utils.StringUtil;
|
|
|
import com.yihu.jw.wechat.dao.BasePatientWechatDao;
|
|
|
import com.yihu.jw.wechat.dao.WechatDao;
|
|
|
import com.yihu.jw.wechat.dao.WxPayLogDao;
|
|
|
import com.yihu.jw.wechat.service.WxPayLogService;
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.checkerframework.checker.units.qual.A;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* 业务订单
|
|
|
*/
|
|
|
@Service
|
|
|
public class BusinessOrderService {
|
|
|
|
|
|
@Autowired
|
|
|
private BusinessOrderDao businessOrderDao;
|
|
|
@Autowired
|
|
|
private WechatDao wechatDao;
|
|
|
@Autowired
|
|
|
private WxPayLogDao wxPayLogDao;
|
|
|
@Autowired
|
|
|
private BasePatientWechatDao patientWechatDao;
|
|
|
@Autowired
|
|
|
private BasePatientDao patientDao;
|
|
|
@Autowired
|
|
|
private BusinessOrderRefundDao orderRefundDao;
|
|
|
|
|
|
|
|
|
public BusinessOrderDO saveOrder(BusinessOrderDO businessOrderDO) throws Exception {
|
|
|
businessOrderDO.setStatus(0);
|
|
|
businessOrderDO.setCreateTime(new Date());
|
|
|
businessOrderDO.setUpdateTime(new Date());
|
|
|
businessOrderDO.setOrderNo(getGuid());
|
|
|
businessOrderDO = businessOrderDao.save(businessOrderDO);
|
|
|
return businessOrderDO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建订单并输出微信JSAPI参数
|
|
|
* @param businessOrderDO
|
|
|
* @param wechatId
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public Map<String, Object> saveOrderAndPay(BusinessOrderDO businessOrderDO,String wechatId) throws Exception {
|
|
|
businessOrderDO.setStatus(0);
|
|
|
businessOrderDO.setCreateTime(new Date());
|
|
|
businessOrderDO.setUpdateTime(new Date());
|
|
|
businessOrderDO.setOrderNo(getGuid());
|
|
|
businessOrderDO = businessOrderDao.save(businessOrderDO);
|
|
|
List<BasePatientWechatDo> patientWechatDos = patientWechatDao.findByWechatIdAndPatientId(wechatId,businessOrderDO.getPatient());
|
|
|
String openId = "";
|
|
|
if (patientWechatDos!=null&&patientWechatDos.size()!=0){
|
|
|
openId = patientWechatDos.get(0).getOpenid();
|
|
|
}
|
|
|
WxWechatDO wxWechatDO = wechatDao.findById(wechatId);
|
|
|
if(wxWechatDO==null){
|
|
|
throw new Exception("can't find wechat:the wxId is "+wechatId);
|
|
|
}
|
|
|
String url = wxWechatDO.getBaseUrl();
|
|
|
String notifyUrl =url+"/ykyy"+BaseHospitalRequestMapping.WxPay.Notify;
|
|
|
return unifiedorder(wechatId,businessOrderDO.getDescription(),businessOrderDO.getPayPrice().toString(),WeChatConfig.TRADE_TYPE_JSAPI,openId,businessOrderDO.getOrderNo(),notifyUrl);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信统一下单接口
|
|
|
*
|
|
|
* @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,true);
|
|
|
//创建日志记录
|
|
|
createLog(par,xml,map);
|
|
|
map.remove("wxPayResult");
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信统一退款接口
|
|
|
*
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Map<String,Object> refund(Map<String,String> par,String appKey) throws Exception {
|
|
|
String xml = WeiXinPayUtils.getXmlBeforUnifiedorder(par, appKey);
|
|
|
Map<String, Object> map = WeiXinPayUtils.refund(xml,true);
|
|
|
//创建日志记录
|
|
|
createLog(par,xml,map);
|
|
|
map.remove("wxPayResult");
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信下单
|
|
|
* @param wechatId
|
|
|
* @param body
|
|
|
* @param totalFee
|
|
|
* @param tradeType
|
|
|
* @param openId
|
|
|
* @param ourTradeNo
|
|
|
* @param notifyUrl
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public Map<String,Object> unifiedorder(String wechatId,String body,String totalFee,String tradeType,String openId,String ourTradeNo,String notifyUrl) throws Exception {
|
|
|
WxWechatDO wxWechatDO = wechatDao.findById(wechatId);
|
|
|
if(wxWechatDO==null){
|
|
|
throw new Exception("can't find wechat:the wxId is "+wechatId);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(notifyUrl)){
|
|
|
throw new Exception("notifyUrl is null :the notifyUrl is "+notifyUrl);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(body)){
|
|
|
throw new Exception("body is null :the body is "+body);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(tradeType)){
|
|
|
throw new Exception("tradeType is null :the tradeType is "+tradeType);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(openId)){
|
|
|
throw new Exception("openId is null :the openId is "+openId);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(ourTradeNo)){
|
|
|
throw new Exception("ourTradeNo is null :the ourTradeNo is "+ourTradeNo);
|
|
|
}
|
|
|
HashMap<String, String> map = new HashMap<>();
|
|
|
map.put("appid",wxWechatDO.getAppId());
|
|
|
map.put("mch_id",wxWechatDO.getMchId());
|
|
|
map.put("body",body);
|
|
|
map.put("out_trade_no",ourTradeNo);
|
|
|
map.put("total_fee",totalFee);
|
|
|
map.put("spbill_create_ip",getServerIp());
|
|
|
map.put("notify_url",notifyUrl);
|
|
|
map.put("trade_type", tradeType);
|
|
|
map.put("openid", openId);
|
|
|
Map<String, Object> pay = unifiedorder(map, wxWechatDO.getAppKey());
|
|
|
String returnCode = pay.get("return_code").toString();
|
|
|
if (returnCode.equalsIgnoreCase("SUCCESS")){
|
|
|
String appid = pay.get("appid").toString();
|
|
|
String mchId = pay.get("mch_id").toString();
|
|
|
String nonceStr = pay.get("nonce_str").toString();
|
|
|
String prepayId = pay.get("prepay_id").toString();
|
|
|
|
|
|
Map<String,String> param = new HashedMap();
|
|
|
param.put("appid",appid);
|
|
|
param.put("partnerid",mchId);
|
|
|
param.put("prepayid",prepayId);
|
|
|
param.put("package","Sign=WXPay");
|
|
|
param.put("noncestr",nonceStr);
|
|
|
param.put("timestamp",getTimeStamp());
|
|
|
param = WeiXinPayUtils.getMapAfterUnifiedorder(param,wxWechatDO.getAppKey());
|
|
|
pay.put("jsapi",param);
|
|
|
}
|
|
|
return pay;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 微信退款
|
|
|
* @param wechatId
|
|
|
* @param patient
|
|
|
* @param orderNo
|
|
|
* @param refundPrice
|
|
|
* @param description
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public Map<String,Object> orderRefund(String wechatId,String patient,String orderNo,Double refundPrice,String description) throws Exception {
|
|
|
WxWechatDO wxWechatDO = wechatDao.findById(wechatId);
|
|
|
if (wxWechatDO==null){
|
|
|
throw new Exception("this wechatId is null");
|
|
|
}
|
|
|
BusinessOrderDO businessOrderDO = businessOrderDao.selectByOrderNo(orderNo);
|
|
|
if (businessOrderDO==null){
|
|
|
throw new Exception("this orderId not exit");
|
|
|
}
|
|
|
BasePatientDO patientDO = patientDao.findById(patient);
|
|
|
if (patientDO==null){
|
|
|
throw new Exception("this patient not exit");
|
|
|
}
|
|
|
BusinessOrderRefundDO orderRefundDO = new BusinessOrderRefundDO();
|
|
|
orderRefundDO.setCreateTime(new Date());
|
|
|
orderRefundDO.setUpdateTime(new Date());
|
|
|
orderRefundDO.setStatus(1);
|
|
|
orderRefundDO.setOrderNo(orderNo);
|
|
|
orderRefundDO.setOrderPrice(businessOrderDO.getPayPrice());
|
|
|
orderRefundDO.setRefundPrice(refundPrice);
|
|
|
orderRefundDO.setAppId(wxWechatDO.getAppId());
|
|
|
orderRefundDO.setMchId(wxWechatDO.getMchId());
|
|
|
orderRefundDO.setOutRefundNo(getGuid());
|
|
|
orderRefundDO.setPatient(patient);
|
|
|
orderRefundDO.setPatientName(patientDO.getName());
|
|
|
orderRefundDO.setRefundDesc(description);
|
|
|
orderRefundDO = orderRefundDao.save(orderRefundDO);
|
|
|
Map<String,Object> map = refund(wechatId,orderRefundDO.getOrderNo(),orderRefundDO.getOutRefundNo(),orderRefundDO.getOrderPrice().toString(),orderRefundDO.getRefundPrice().toString(),orderRefundDO.getRefundDesc());
|
|
|
if (map.get("return_code").toString().equalsIgnoreCase("SUCCESS")){
|
|
|
orderRefundDO.setStatus(2);
|
|
|
orderRefundDO.setRefundTime(new Date());
|
|
|
orderRefundDao.save(orderRefundDO);
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
public Map<String,Object> refund(String wechatId,String outTradeNo,String outRefundNo,String totalFee,String refundFee,String descrption) throws Exception {
|
|
|
WxWechatDO wxWechatDO = wechatDao.findById(wechatId);
|
|
|
if(wxWechatDO==null){
|
|
|
throw new Exception("can't find wechat:the wxId is "+wechatId);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(refundFee)){
|
|
|
throw new Exception("refundFee is null :the refundFee is "+refundFee);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(outRefundNo)){
|
|
|
throw new Exception("outRefundNo is null :the outRefundNo is "+outRefundNo);
|
|
|
}
|
|
|
if(!StringUtils.isNoneBlank(outTradeNo)){
|
|
|
throw new Exception("outTradeNo is null :the outTradeNo is "+outTradeNo);
|
|
|
}
|
|
|
HashMap<String, String> map = new HashMap<>();
|
|
|
map.put("appid",wxWechatDO.getAppId());
|
|
|
map.put("mch_id",wxWechatDO.getMchId());
|
|
|
map.put("out_trade_no",outTradeNo);
|
|
|
map.put("out_refund_no",outRefundNo);
|
|
|
map.put("refund_fee",refundFee);
|
|
|
map.put("total_fee",totalFee);
|
|
|
map.put("refund_desc",descrption);
|
|
|
Map<String, Object> refund = refund(map, wxWechatDO.getAppKey());
|
|
|
return refund;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加微信支付日志
|
|
|
* @param map
|
|
|
* @param orderPar
|
|
|
* @param orderRes
|
|
|
*/
|
|
|
public void createLog(Map<String,String> map,String orderPar,Map<String,Object> orderRes){
|
|
|
|
|
|
WxPayLogDO wxPayLogDO = new WxPayLogDO();
|
|
|
wxPayLogDO.setSeqNo(map.get("out_trade_no"));
|
|
|
wxPayLogDO.setOpenId(map.get("openid"));
|
|
|
wxPayLogDO.setType(0);
|
|
|
|
|
|
wxPayLogDO.setOrderParams(orderPar);
|
|
|
wxPayLogDO.setOrderResponse(orderRes.get("wxPayResult").toString());
|
|
|
String return_code = orderRes.get("return_code").toString();
|
|
|
if("SUCCESS".equalsIgnoreCase(return_code)){
|
|
|
wxPayLogDO.setOrderStatus(1);
|
|
|
}else{
|
|
|
wxPayLogDO.setOrderStatus(-1);
|
|
|
}
|
|
|
wxPayLogDO.setCreateTime(new Date());
|
|
|
wxPayLogDO.setPayStatus(0);
|
|
|
wxPayLogDao.save(wxPayLogDO);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取服务器ip地址
|
|
|
*
|
|
|
* @return
|
|
|
* @throws UnknownHostException
|
|
|
*/
|
|
|
public String getServerIp() throws UnknownHostException {
|
|
|
InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
|
|
|
String hostAddress = address.getHostAddress();//192.168.0.121
|
|
|
return hostAddress;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取当前时间戳
|
|
|
* @return 当前时间戳字符串
|
|
|
*/
|
|
|
public static String getTimeStamp(){
|
|
|
return String.valueOf(System.currentTimeMillis()/1000);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取唯一的支付流水号
|
|
|
*/
|
|
|
|
|
|
public static int Guid=100;
|
|
|
|
|
|
synchronized public String getGuid() {
|
|
|
return DateUtil.dateToStr(new Date(),"yyyyMMddHHmmssSSS")+(new Random().nextInt(10));
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 微信支付回调
|
|
|
*
|
|
|
*
|
|
|
* @param result
|
|
|
* @return
|
|
|
*/
|
|
|
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")+"";
|
|
|
String timeEnd= wxrs.get("time_end")+"";
|
|
|
//更改业务状态,做日志保存等操作
|
|
|
BusinessOrderDO businessOrderDO = businessOrderDao.selectByOrderNo(seqNo);
|
|
|
businessOrderDO.setPayTime(DateUtil.strToDate(timeEnd,DateUtil.YYYY_MM_DD_HH_MM_SS));
|
|
|
businessOrderDO.setStatus(1);
|
|
|
//操作微信日志表记录
|
|
|
List<WxPayLogDO> wxPayLogDOs = wxPayLogDao.findBySeqNo(seqNo);
|
|
|
WxPayLogDO wxPayLogDO = wxPayLogDOs.get(0);
|
|
|
wxPayLogDO.setPayResponse(result);
|
|
|
wxPayLogDO.setPayStatus(1);
|
|
|
wxPayLogDO.setPayTime(new Date());
|
|
|
wxPayLogDO.setPayStatus(1);
|
|
|
wxPayLogDao.save(wxPayLogDO);
|
|
|
rs.put("code","1");
|
|
|
rs.put("message","支付成功");
|
|
|
}else{
|
|
|
rs.put("code","-1");
|
|
|
rs.put("return_msg","FAIL");
|
|
|
}
|
|
|
return rs;
|
|
|
}
|
|
|
|
|
|
}
|