package com.minpay.shouhuo.orderaction; import com.min.util.DateUtil; import com.minpay.common.bean.User; import com.minpay.common.exception.BusinessCodeException; import com.minpay.common.pay.ShouhuoPay; import com.minpay.common.pay.bean.Huilianpay; import com.minpay.common.service.*; import com.minpay.common.service.impl.DrawServiceImpl; import com.minpay.common.service.impl.HuilianPayServiceImpl; import com.minpay.common.util.CommonUtil; import com.minpay.db.table.mapper.*; import com.minpay.db.table.model.*; import com.minpay.shouhuo.paymentaction.PaymentAction; import com.startup.minpay.frame.business.IMINAction; import com.startup.minpay.frame.business.MINHttpServletRequestContext; import com.startup.minpay.frame.business.res.MINActionResult; import com.startup.minpay.frame.constant.IMINTransactionEnum; import com.startup.minpay.frame.exception.MINBusinessException; import com.startup.minpay.frame.service.base.IMINDataBaseService; import com.startup.minpay.frame.service.base.Service; import com.startup.minpay.frame.session.MINSession; import com.startup.minpay.frame.target.MINAction; import com.startup.minpay.frame.target.MINComponent; import com.startup.minpay.frame.target.MINParam; import io.netty.util.internal.StringUtil; import java.util.List; import java.util.Map; /** * 订单管理 * @author xbh * */ @MINComponent public class PayManageAction implements IMINAction { /**充值*/ public final static String PAY_RECHARGE = "payRecharge"; /**回调(充值)*/ public static final String CALLBACK_RECHARGE = "callbackRecharge"; /**余额支付*/ public static final String BALANCE_PAY = "balancePay"; /** * 充值 * @param session * @param equipmentId 机器编号 * @param ruleId 规则号 * @param payType 支付方式 91微信支付92支付宝支付 * @param dates 日期(区间) * @param state 状态 * @param fapRequest * @return * @throws MINBusinessException */ @MINAction(value = PAY_RECHARGE, transaction = IMINTransactionEnum.CMT) public MINActionResult payRecharge( MINSession session, @MINParam(key = "equipmentId") String equipmentId, @MINParam(key = "ruleId") String ruleId, @MINParam(key = "payType") String payType, @MINParam(key = "dates") String dates, @MINParam(key = "state") String state, MINHttpServletRequestContext fapRequest ) throws MINBusinessException { MINActionResult res = new MINActionResult(); /**查询机器*/ //校验机器编号是否存在 VmEquipmentInf vmEquipmentInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmEquipmentInfMapper.class).selectByPrimaryKey(equipmentId); if(vmEquipmentInf == null){ throw new MINBusinessException("机器不存在或异常!"); } //获取操作员 User user = session.getUser(); //获取当前时间 String nowTime = com.minpay.common.util.DateUtil.getCurrentDateTimeString(); //根据规则编号查询充值金额 VmRechargeRuleExample vmRechargeRuleExample = new VmRechargeRuleExample(); vmRechargeRuleExample.createCriteria().andIdEqualTo(ruleId); // 查询 List ls = Service.lookup(IMINDataBaseService.class) .getMybatisMapper(VmRechargeRuleMapper.class).selectByExample(vmRechargeRuleExample); if(ls.size() != 1){ throw new BusinessCodeException("规则编号有误!"); } //赠送金额 String giveAmt = ls.get(0).getGiveAmt(); //充值金额 String rechargeAmt = ls.get(0).getRechargeAmt(); String allAmt = CommonUtil.add(giveAmt, rechargeAmt); //创建订单 //获取订单主键 String ordId = Service.lookup(IPublicService.class).getSequence("VM_ORDER_INF_ID"); //初始化订单数据 VmOrderInf ordInf = new VmOrderInf(); ordInf.setId(ordId); //订单编号 ordInf.setChannel(user.getChannel()); //渠道:V01售货机 ordInf.setPersonId(user.getId()); //下单人编号 ordInf.setEquipmentId(equipmentId); //虚拟售货机号 ordInf.setState("91"); //91未支付 ordInf.setRemarks("会员充值(加赠送金额):" + rechargeAmt +"赠送金额:"+giveAmt); //备注 ordInf.setIsDraw("00"); //是否中奖:00未中奖01中奖 ordInf.setIsUsed("00"); //是否使用:00已使用01未使用 ordInf.setProType("02"); //00正常商品,01抽奖商品,02:充值 ordInf.setRecordId(""); //结算编号 ordInf.setCreateUser(user.getId()); //创建人 ordInf.setCreateTime(DateUtil.getCurrentDateTimeString()); //创建时间 ordInf.setModifyUser(user.getId()); //修改人 ordInf.setModifyTime(DateUtil.getCurrentDateTimeString()); //修改时间 ordInf.setOrderAmt(rechargeAmt); //订单总金额 ordInf.setPickupStt("00"); ordInf.setBranchid(vmEquipmentInf.getBranchid()); ordInf.setRechareRuleId(ruleId); // 执行新增订单数据 Service.lookup(IMINDataBaseService.class) .getMybatisMapper(VmOrderInfMapper.class).insertSelective(ordInf); //记录充值流水表 String flowno = Service.lookup(IPublicService.class).getSequence("VM_PAYMENT_INF_ID"); VmPaymentInf payMentInf = new VmPaymentInf(); payMentInf.setFlowno(nowTime.concat(flowno)); payMentInf.setChannel(user.getChannel()); payMentInf.setTranstype("00"); //充值 payMentInf.setPaytype(payType); payMentInf.setType("99"); //用户充值 payMentInf.setTranflowno(ordId); payMentInf.setOrderamout(rechargeAmt); //充值金额 payMentInf.setTranmissiondatetime(nowTime); payMentInf.setStatus("3"); // 交易状态(0 初始状态,1成功,2失败,3在途,4待审核,5已审核) Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPaymentInfMapper.class).insert(payMentInf); //动账 // IAccountService service = Service.lookup(IAccountService.class); // service.modfiyAccount(user.getChannel(), user.getId(), "01", rechargeAmt, "00", payMentInf.getFlowno(), "充值",""); //查询机器所属机构的收款账户 String payeeNo = vmEquipmentInf.getPayeeNo(); if(StringUtil.isNullOrEmpty(payeeNo)){ throw new MINBusinessException("商户收款账户异常"); } VmHlAccount vmHlAccount = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmHlAccountMapper.class).selectByPrimaryKey(payeeNo); String accNo = vmHlAccount.getNumber(); if(StringUtil.isNullOrEmpty(accNo)){ throw new MINBusinessException("商户收款账户异常"); } Map resMap = null; try { VmPaymentInf paymentInf = new VmPaymentInf(); // 支付通道 String payChannel = Service.lookup(IPublicService.class).getSysParValue(user.getChannel() +"_PERSONALPAY_PAY_CHANNEL"); //汇联 if("HLZF".equals(payChannel)) { VmEquipmentInf equipmentInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmEquipmentInfMapper.class).selectByPrimaryKey(ordInf.getEquipmentId()); if(equipmentInf == null) { throw new MINBusinessException("没有找到此售货机"); } String branchid = equipmentInf.getBranchid(); //查询商户开通了那个支付渠道 String pay_channel = Service.lookup(IPublicService.class).getSysParValue( "PAY_CHANNEL"); //支付后台回调地址 String notify_url = Service.lookup(IPublicService.class).getSysParValue(user.getChannel() + "_SHOUHUO_NOTIFY_URL"); //notify_url = "http://xubing2.minpay.cc/services"; notify_url = notify_url + "/ShouhuoPay/personalPayCallBack"; if("05".equals(pay_channel)){ pay_channel = "09"; } String turntablePage = Service.lookup(IPublicService.class).getSysParValue(user.getChannel() + "_SHOUHUO_TURNTABLE_PAGE"); turntablePage = turntablePage.concat("orderId="+ordId); Huilianpay prePay = new Huilianpay(); IHuilianPayService huilianPayService = new HuilianPayServiceImpl(); if("91".equals(payType)){ payType = "WX"; } if("92".equals(payType)){ payType = "ALI"; } prePay.setChannel(payType); prePay.setNotifyUrl(notify_url); prePay.setOpenId(user.getOpenid()); prePay.setOutTradeNo(ordId); prePay.setProviderType(pay_channel); prePay.setTotalAmount(rechargeAmt); prePay.setHlMerchantId(accNo); prePay.setGoodName(ordId); prePay.setSucUrl(turntablePage); resMap = huilianPayService.prePay(prePay); } } catch (Exception e) { throw new MINBusinessException("调用支付失败"); } res.set("payMap",resMap); return res; } /** * 余额支付 * @param session * @return * @throws MINBusinessException */ @MINAction(value = BALANCE_PAY,transaction = IMINTransactionEnum.CMT) public MINActionResult balancePay( @MINParam(key = "payAmt") String payAmt, @MINParam(key = "orderId") String orderId, @MINParam(key = "payType", defaultValue = "93") String payType, MINSession session) throws MINBusinessException{ //创建返回值对象 MINActionResult res = new MINActionResult(); //获取操作员信息 User user = session.getUser(); if(CommonUtil.isEmpty(orderId)){ throw new MINBusinessException("订单号不能为空"); } /**1.查询订单*/ VmOrderInf orderInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmOrderInfMapper.class).selectByPrimaryKey(orderId); if(orderInf == null){ throw new MINBusinessException("无此订单!"); } if(CommonUtil.compare(payAmt, orderInf.getOrderAmt()) != 0){ throw new MINBusinessException("交易异常");//支付金额不对 } if(!user.getId().equals(orderInf.getPersonId())){ throw new MINBusinessException("登录账号和下单账户不符"); } if(orderInf.getState().equals("00")){ throw new MINBusinessException("订单已支付"); } VmPaymentInfExample paymentExp = new VmPaymentInfExample(); paymentExp.createCriteria().andChannelEqualTo(user.getChannel()).andTranflownoEqualTo(orderId); List paymenList = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPaymentInfMapper.class).selectByExample(paymentExp); if(paymenList.size() > 1){ throw new MINBusinessException("交易异常"); } VmPaymentInf payment = paymenList.get(0); /**2.调账*/ Service.lookup(IAccountService.class).modfiyAccount(orderInf.getChannel(), orderInf.getPersonId(), "01", "-".concat(payAmt), "00", payment.getFlowno(), "充值",orderInf.getEquipmentId()); /**3.更新订单表和支付表*/ /*payment.setStatus("1"); Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPaymentInfMapper.class).updateByPrimaryKeySelective(payment); // 变更待结算账户 orderInf.setId(orderId); orderInf.setState("00"); Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmOrderInfMapper.class).updateByPrimaryKeySelective(orderInf);*/ /**4.抽奖*/ //调用真实回调 IDrawService drawService = new DrawServiceImpl(); drawService.backEx(orderId, null, "TRADE_SUCCESS", payAmt,null, "", "93"); // 记录操作日志 //String logInfo = "操作员:" + user.getName() + "会员下单,售货机编号:" + equNo+ "下单商品信息:" + detData; //Service.lookup(ILogService.class).logging(session, logInfo); return res; } }