| | |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.allinpay.syb.lib.SybConstants; |
| | | import com.allinpay.syb.lib.SybUtil; |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.core.constants.Constants; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.model.PageData; |
| | | import com.doumee.core.model.PageWrap; |
| | | import com.doumee.core.utils.DateUtil; |
| | | import com.doumee.dao.business.dto.PayOrderNotifyParam; |
| | | import com.doumee.dao.business.dto.PayOrderParam; |
| | | import com.doumee.dao.business.dto.PayOrderResponse; |
| | | import com.doumee.dao.business.model.Orders; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.TreeMap; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 订单记录表信息表Service实现 |
| | |
| | | private OrdersMapper ordersMapper; |
| | | @Autowired |
| | | private SystemUserMapper systemUserMapper; |
| | | @Autowired |
| | | private SystemDictDataBiz systemDictDataBiz; |
| | | |
| | | @Override |
| | | @Transactional |
| | |
| | | } |
| | | } |
| | | Date date = new Date(); |
| | | orders.setMoney(orders.getMoney().multiply(new BigDecimal(100)));//单位分 |
| | | orders.setCreateTime(date); |
| | | orders.setPayStatus(Constants.ZERO); |
| | | orders.setStatus(Constants.ZERO); |
| | |
| | | try { |
| | | PayOrderResponse r = new PayOrderResponse(); |
| | | PayOrderParam param = new PayOrderParam(); |
| | | param.setAppid(SybConstants.SYB_APPID); |
| | | param.setCusid(SybConstants.SYB_CUSID); |
| | | param.setVersion(SybConstants.VERSION); |
| | | param.setAppid(systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_SYB_APPID).getCode()); |
| | | param.setCusid(systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_SYB_CUSID).getCode()); |
| | | param.setVersion(systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_VERSION).getCode()); |
| | | param.setCharset("utf-8"); |
| | | param.setTrxamt("1"); |
| | | param.setTrxamt("1");//单位分 |
| | | param.setRandomstr(UUID.randomUUID().toString().replace("-","")); |
| | | param.setReqsn(""+orders.getId()); |
| | | param.setReturl("http://localhost:8080/#/page/success/success"); |
| | | param.setNotify_url(SybConstants.NOTFIY_URL); |
| | | param.setNotify_url(""); |
| | | param.setReturl(systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_RETURN_URL).getCode()); |
| | | param.setNotify_url( systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_NOTIFY_URL).getCode()); |
| | | param.setBody(orders.getRemark()); |
| | | param.setValidtime( SybConstants.VALID_TIME); |
| | | param.setSigntype(SybConstants.SIGN_TYPE); |
| | | param.setValidtime( systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_VALID_TIME).getCode()); |
| | | param.setSigntype( systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_SIGN_TYPE).getCode()); |
| | | |
| | | String appkey = ""; |
| | | if(SybConstants.SIGN_TYPE.equals("RSA")){ |
| | | appkey = SybConstants.SYB_RSACUSPRIKEY; |
| | | } else if(SybConstants.SIGN_TYPE.equals("SM2")){ |
| | | appkey = SybConstants.SYB_SM2PPRIVATEKEY; |
| | | } else{ |
| | | appkey = SybConstants.SYB_MD5_APPKEY; |
| | | } |
| | | String appkey = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_APPKEY).getCode(); |
| | | String sign = SybUtil.unionSign(getTreeMapByParam(param),appkey,param.getSigntype()); |
| | | param.setSign( sign); |
| | | r.setUrl(SybConstants.SYB_APIURL+"/h5unionpay/unionorder"); |
| | | r.setUrl(systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_SYB_APIURL).getCode()+"/h5unionpay/unionorder"); |
| | | r.setParam(param); |
| | | // String r = HttpsUtil.postJson(SybConstants.SYB_APIURL+"/h5unionpay/unionorder",JSONObject.toJSONString(param)); |
| | | // log.error(param); |
| | | return r; |
| | | }catch (Exception e){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,发起收款请求失败,请稍后重试!"); |
| | | } |
| | | } |
| | | |
| | | private TreeMap<String, String> getTreeMapByParam(PayOrderParam param) { |
| | | private TreeMap<String, String> getTreeMapByParam(Object param) { |
| | | |
| | | TypeReference< TreeMap<String, String> > type = new TypeReference< TreeMap<String, String> >() { }; |
| | | TreeMap<String, String> obj = JSONObject.parseObject(JSON.toJSONString(param), type.getType()); |
| | | return obj; |
| | | } |
| | | |
| | | private TreeMap<String, String> getTreeMapByParamStr(String paramStr) { |
| | | TreeMap<String, String> obj = new TreeMap<>(); |
| | | String params[] =paramStr.split("&"); |
| | | if(params.length ==0){ |
| | | return obj; |
| | | } |
| | | for(String tp : params){ |
| | | String tt[] = tp.split("="); |
| | | if(tt.length>=2){ |
| | | obj.put(tt[0],tt[1]); |
| | | } |
| | | } |
| | | return obj; |
| | | } |
| | | |
| | | @Override |
| | | public void deleteById(Integer id) { |
| | |
| | | QueryWrapper<Orders> wrapper = new QueryWrapper<>(orders); |
| | | return ordersMapper.selectCount(wrapper); |
| | | } |
| | | @Override |
| | | public void returnPage(PayOrderParam param, HttpServletResponse response) throws IOException { |
| | | String url =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_RETURN_WEB_URL).getCode(); |
| | | response.sendRedirect(url+"?reqsn="+param.getReqsn()); |
| | | } |
| | | @Override |
| | | public void dealOrdreResult(String paramStr, HttpServletResponse response) throws IOException { |
| | | String r = "success"; |
| | | try { |
| | | response.setCharacterEncoding("UTF-8"); |
| | | TreeMap<String,String> params = getTreeMapByParamStr(paramStr);//动态遍历获取所有收到的参数,此步非常关键,因为收银宝以后可能会加字段,动态获取可以兼容 |
| | | String appkey = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PAY_SYB_RSATLPUBKEY).getCode(); |
| | | log.error("支付通知参数======"+paramStr); |
| | | boolean isSign = SybUtil.validSign(params, appkey, params.get("signtype"));// 接受到推送通知,首先验签 |
| | | PayOrderNotifyParam map = JSONObject.toJavaObject(JSONObject.parseObject(JSONObject.toJSONString(params)),PayOrderNotifyParam.class); |
| | | System.out.println("验签结果:"+isSign); |
| | | if(isSign){ |
| | | log.error("支付验签成功======"); |
| | | //处理订单状态 |
| | | if(StringUtils.isBlank(map.getCusorderid())){ |
| | | r ="支付结果异常,如有疑问请联系客服人员"; |
| | | return; |
| | | } |
| | | Orders model = ordersMapper.selectOne(new QueryWrapper<Orders>().lambda() |
| | | .eq(Orders::getId,Integer.parseInt(map.getCusorderid())) |
| | | ); |
| | | if(model ==null){ |
| | | log.error("支付处理失败======未找到交易订单"+map.getCusorderid()); |
| | | r ="支付结果异常,订单查询失败,如有疑问请联系客服人员"; |
| | | return; |
| | | } |
| | | if(Constants.equalsInteger(model.getPayStatus(),Constants.ONE)){ |
| | | log.error("支付处理======订单已支付"+map.getCusorderid()); |
| | | return; |
| | | } |
| | | String info = SybUtil.getPayInfoByStatus(map); |
| | | if(StringUtils.equals(map.getTrxcode(),"VSP501") |
| | | ||StringUtils.equals(map.getTrxcode(),"VSP511") |
| | | ||StringUtils.equals(map.getTrxcode(),"VSP551")){ |
| | | //交易类型是 微信支付 、支付宝支付或者银联扫码支付,记录支付状态 |
| | | if(StringUtils.equals(map.getTrxstatus(),"0000")){ |
| | | //交易成功 |
| | | model.setPayStatus(Constants.ONE); |
| | | model.setStatus(Constants.ONE); |
| | | }else{ |
| | | //交易失败 |
| | | model.setStatus(Constants.TWO); |
| | | } |
| | | }else{ |
| | | model.setStatus(Constants.TWO); |
| | | } |
| | | model.setPayFee(map.getFee()); |
| | | model.setPayDate(DateUtil.fromStringToDate("yyyyMMddHHmmss",map.getPaytime())); |
| | | model.setPayOrderId(map.getSrctrxid());//通联订单号 |
| | | model.setPayThirdOrderId(map.getChnltrxid());//支付宝微信订单号 |
| | | model.setPayThirdOrderData(map.getChnldata());//三方交易渠道数据 |
| | | model.setPayInfo(info); |
| | | ordersMapper.updateById(model); |
| | | }else{ |
| | | log.error("支付验签失败======"); |
| | | r ="支付失败,如有疑问请联系客服人员"; |
| | | return; |
| | | } |
| | | //验签完毕进行业务处理 |
| | | } catch (Exception e) {//处理异常 |
| | | // TODO: handle exception |
| | | e.printStackTrace(); |
| | | log.error("支付通知处理异常:",e.getMessage()); |
| | | r = "error"; |
| | | } finally{//收到通知,返回success |
| | | response.getOutputStream().write(r.getBytes()); |
| | | response.flushBuffer(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public TreeMap<String, String> getParams(HttpServletRequest request){ |
| | | TreeMap<String, String> map = new TreeMap<String, String>(); |
| | | Map reqMap = request.getParameterMap(); |
| | | for(Object key:reqMap.keySet()){ |
| | | String value = ((String[])reqMap.get(key))[0]; |
| | | System.out.println(key+";"+value); |
| | | map.put(key.toString(),value); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | } |