PayManageAction.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package com.minpay.shouhuo.orderaction;
  2. import com.min.util.DateUtil;
  3. import com.minpay.common.bean.User;
  4. import com.minpay.common.exception.BusinessCodeException;
  5. import com.minpay.common.pay.ShouhuoPay;
  6. import com.minpay.common.pay.bean.Huilianpay;
  7. import com.minpay.common.service.*;
  8. import com.minpay.common.service.impl.DrawServiceImpl;
  9. import com.minpay.common.service.impl.HuilianPayServiceImpl;
  10. import com.minpay.common.util.CommonUtil;
  11. import com.minpay.db.table.mapper.*;
  12. import com.minpay.db.table.model.*;
  13. import com.minpay.shouhuo.paymentaction.PaymentAction;
  14. import com.startup.minpay.frame.business.IMINAction;
  15. import com.startup.minpay.frame.business.MINHttpServletRequestContext;
  16. import com.startup.minpay.frame.business.res.MINActionResult;
  17. import com.startup.minpay.frame.constant.IMINTransactionEnum;
  18. import com.startup.minpay.frame.exception.MINBusinessException;
  19. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  20. import com.startup.minpay.frame.service.base.Service;
  21. import com.startup.minpay.frame.session.MINSession;
  22. import com.startup.minpay.frame.target.MINAction;
  23. import com.startup.minpay.frame.target.MINComponent;
  24. import com.startup.minpay.frame.target.MINParam;
  25. import io.netty.util.internal.StringUtil;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * 订单管理
  30. * @author xbh
  31. *
  32. */
  33. @MINComponent
  34. public class PayManageAction implements IMINAction {
  35. /**充值*/
  36. public final static String PAY_RECHARGE = "payRecharge";
  37. /**回调(充值)*/
  38. public static final String CALLBACK_RECHARGE = "callbackRecharge";
  39. /**余额支付*/
  40. public static final String BALANCE_PAY = "balancePay";
  41. /**
  42. * 充值
  43. * @param session
  44. * @param equipmentId 机器编号
  45. * @param ruleId 规则号
  46. * @param payType 支付方式 91微信支付92支付宝支付
  47. * @param dates 日期(区间)
  48. * @param state 状态
  49. * @param fapRequest
  50. * @return
  51. * @throws MINBusinessException
  52. */
  53. @MINAction(value = PAY_RECHARGE, transaction = IMINTransactionEnum.CMT)
  54. public MINActionResult payRecharge(
  55. MINSession session,
  56. @MINParam(key = "equipmentId") String equipmentId,
  57. @MINParam(key = "ruleId") String ruleId,
  58. @MINParam(key = "payType") String payType,
  59. @MINParam(key = "dates") String dates,
  60. @MINParam(key = "state") String state,
  61. MINHttpServletRequestContext fapRequest
  62. ) throws MINBusinessException {
  63. MINActionResult res = new MINActionResult();
  64. /**查询机器*/
  65. //校验机器编号是否存在
  66. VmEquipmentInf vmEquipmentInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmEquipmentInfMapper.class).selectByPrimaryKey(equipmentId);
  67. if(vmEquipmentInf == null){
  68. throw new MINBusinessException("机器不存在或异常!");
  69. }
  70. //获取操作员
  71. User user = session.getUser();
  72. //获取当前时间
  73. String nowTime = com.minpay.common.util.DateUtil.getCurrentDateTimeString();
  74. //根据规则编号查询充值金额
  75. VmRechargeRuleExample vmRechargeRuleExample = new VmRechargeRuleExample();
  76. vmRechargeRuleExample.createCriteria().andIdEqualTo(ruleId);
  77. // 查询
  78. List<VmRechargeRule> ls = Service.lookup(IMINDataBaseService.class)
  79. .getMybatisMapper(VmRechargeRuleMapper.class).selectByExample(vmRechargeRuleExample);
  80. if(ls.size() != 1){
  81. throw new BusinessCodeException("规则编号有误!");
  82. }
  83. //赠送金额
  84. String giveAmt = ls.get(0).getGiveAmt();
  85. //充值金额
  86. String rechargeAmt = ls.get(0).getRechargeAmt();
  87. String allAmt = CommonUtil.add(giveAmt, rechargeAmt);
  88. //创建订单
  89. //获取订单主键
  90. String ordId = Service.lookup(IPublicService.class).getSequence("VM_ORDER_INF_ID");
  91. //初始化订单数据
  92. VmOrderInf ordInf = new VmOrderInf();
  93. ordInf.setId(ordId); //订单编号
  94. ordInf.setChannel(user.getChannel()); //渠道:V01售货机
  95. ordInf.setPersonId(user.getId()); //下单人编号
  96. ordInf.setEquipmentId(equipmentId); //虚拟售货机号
  97. ordInf.setState("91"); //91未支付
  98. ordInf.setRemarks("会员充值(加赠送金额):" + rechargeAmt +"赠送金额:"+giveAmt); //备注
  99. ordInf.setIsDraw("00"); //是否中奖:00未中奖01中奖
  100. ordInf.setIsUsed("00"); //是否使用:00已使用01未使用
  101. ordInf.setProType("02"); //00正常商品,01抽奖商品,02:充值
  102. ordInf.setRecordId(""); //结算编号
  103. ordInf.setCreateUser(user.getId()); //创建人
  104. ordInf.setCreateTime(DateUtil.getCurrentDateTimeString()); //创建时间
  105. ordInf.setModifyUser(user.getId()); //修改人
  106. ordInf.setModifyTime(DateUtil.getCurrentDateTimeString()); //修改时间
  107. ordInf.setOrderAmt(rechargeAmt); //订单总金额
  108. ordInf.setPickupStt("00");
  109. ordInf.setBranchid(vmEquipmentInf.getBranchid());
  110. ordInf.setRechareRuleId(ruleId);
  111. // 执行新增订单数据
  112. Service.lookup(IMINDataBaseService.class)
  113. .getMybatisMapper(VmOrderInfMapper.class).insertSelective(ordInf);
  114. //记录充值流水表
  115. String flowno = Service.lookup(IPublicService.class).getSequence("VM_PAYMENT_INF_ID");
  116. VmPaymentInf payMentInf = new VmPaymentInf();
  117. payMentInf.setFlowno(nowTime.concat(flowno));
  118. payMentInf.setChannel(user.getChannel());
  119. payMentInf.setTranstype("00"); //充值
  120. payMentInf.setPaytype(payType);
  121. payMentInf.setType("99"); //用户充值
  122. payMentInf.setTranflowno(ordId);
  123. payMentInf.setOrderamout(rechargeAmt); //充值金额
  124. payMentInf.setTranmissiondatetime(nowTime);
  125. payMentInf.setStatus("3"); // 交易状态(0 初始状态,1成功,2失败,3在途,4待审核,5已审核)
  126. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPaymentInfMapper.class).insert(payMentInf);
  127. //动账
  128. // IAccountService service = Service.lookup(IAccountService.class);
  129. // service.modfiyAccount(user.getChannel(), user.getId(), "01", rechargeAmt, "00", payMentInf.getFlowno(), "充值","");
  130. //查询机器所属机构的收款账户
  131. String payeeNo = vmEquipmentInf.getPayeeNo();
  132. if(StringUtil.isNullOrEmpty(payeeNo)){
  133. throw new MINBusinessException("商户收款账户异常");
  134. }
  135. VmHlAccount vmHlAccount = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmHlAccountMapper.class).selectByPrimaryKey(payeeNo);
  136. String accNo = vmHlAccount.getNumber();
  137. if(StringUtil.isNullOrEmpty(accNo)){
  138. throw new MINBusinessException("商户收款账户异常");
  139. }
  140. Map<String, Object> resMap = null;
  141. try {
  142. VmPaymentInf paymentInf = new VmPaymentInf();
  143. // 支付通道
  144. String payChannel = Service.lookup(IPublicService.class).getSysParValue(user.getChannel() +"_PERSONALPAY_PAY_CHANNEL");
  145. //汇联
  146. if("HLZF".equals(payChannel)) {
  147. VmEquipmentInf equipmentInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmEquipmentInfMapper.class).selectByPrimaryKey(ordInf.getEquipmentId());
  148. if(equipmentInf == null) {
  149. throw new MINBusinessException("没有找到此售货机");
  150. }
  151. String branchid = equipmentInf.getBranchid();
  152. //查询商户开通了那个支付渠道
  153. String pay_channel = Service.lookup(IPublicService.class).getSysParValue( "PAY_CHANNEL");
  154. //支付后台回调地址
  155. String notify_url = Service.lookup(IPublicService.class).getSysParValue(user.getChannel() + "_SHOUHUO_NOTIFY_URL");
  156. //notify_url = "http://xubing2.minpay.cc/services";
  157. notify_url = notify_url + "/ShouhuoPay/personalPayCallBack";
  158. if("05".equals(pay_channel)){
  159. pay_channel = "09";
  160. }
  161. String turntablePage = Service.lookup(IPublicService.class).getSysParValue(user.getChannel() + "_SHOUHUO_TURNTABLE_PAGE");
  162. turntablePage = turntablePage.concat("orderId="+ordId);
  163. Huilianpay prePay = new Huilianpay();
  164. IHuilianPayService huilianPayService = new HuilianPayServiceImpl();
  165. if("91".equals(payType)){
  166. payType = "WX";
  167. }
  168. if("92".equals(payType)){
  169. payType = "ALI";
  170. }
  171. prePay.setChannel(payType);
  172. prePay.setNotifyUrl(notify_url);
  173. prePay.setOpenId(user.getOpenid());
  174. prePay.setOutTradeNo(ordId);
  175. prePay.setProviderType(pay_channel);
  176. prePay.setTotalAmount(rechargeAmt);
  177. prePay.setHlMerchantId(accNo);
  178. prePay.setGoodName(ordId);
  179. prePay.setSucUrl(turntablePage);
  180. resMap = huilianPayService.prePay(prePay);
  181. }
  182. } catch (Exception e) {
  183. throw new MINBusinessException("调用支付失败");
  184. }
  185. res.set("payMap",resMap);
  186. return res;
  187. }
  188. /**
  189. * 余额支付
  190. * @param session
  191. * @return
  192. * @throws MINBusinessException
  193. */
  194. @MINAction(value = BALANCE_PAY,transaction = IMINTransactionEnum.CMT)
  195. public MINActionResult balancePay(
  196. @MINParam(key = "payAmt") String payAmt,
  197. @MINParam(key = "orderId") String orderId,
  198. @MINParam(key = "payType", defaultValue = "93") String payType,
  199. MINSession session) throws MINBusinessException{
  200. //创建返回值对象
  201. MINActionResult res = new MINActionResult();
  202. //获取操作员信息
  203. User user = session.getUser();
  204. if(CommonUtil.isEmpty(orderId)){
  205. throw new MINBusinessException("订单号不能为空");
  206. }
  207. /**1.查询订单*/
  208. VmOrderInf orderInf = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmOrderInfMapper.class).selectByPrimaryKey(orderId);
  209. if(orderInf == null){
  210. throw new MINBusinessException("无此订单!");
  211. }
  212. if(CommonUtil.compare(payAmt, orderInf.getOrderAmt()) != 0){
  213. throw new MINBusinessException("交易异常");//支付金额不对
  214. }
  215. if(!user.getId().equals(orderInf.getPersonId())){
  216. throw new MINBusinessException("登录账号和下单账户不符");
  217. }
  218. if(orderInf.getState().equals("00")){
  219. throw new MINBusinessException("订单已支付");
  220. }
  221. VmPaymentInfExample paymentExp = new VmPaymentInfExample();
  222. paymentExp.createCriteria().andChannelEqualTo(user.getChannel()).andTranflownoEqualTo(orderId);
  223. List<VmPaymentInf> paymenList = Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPaymentInfMapper.class).selectByExample(paymentExp);
  224. if(paymenList.size() > 1){
  225. throw new MINBusinessException("交易异常");
  226. }
  227. VmPaymentInf payment = paymenList.get(0);
  228. /**2.调账*/
  229. Service.lookup(IAccountService.class).modfiyAccount(orderInf.getChannel(), orderInf.getPersonId(), "01", "-".concat(payAmt), "00", payment.getFlowno(), "充值",orderInf.getEquipmentId());
  230. /**3.更新订单表和支付表*/
  231. /*payment.setStatus("1");
  232. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmPaymentInfMapper.class).updateByPrimaryKeySelective(payment);
  233. // 变更待结算账户
  234. orderInf.setId(orderId);
  235. orderInf.setState("00");
  236. Service.lookup(IMINDataBaseService.class).getMybatisMapper(VmOrderInfMapper.class).updateByPrimaryKeySelective(orderInf);*/
  237. /**4.抽奖*/
  238. //调用真实回调
  239. IDrawService drawService = new DrawServiceImpl();
  240. drawService.backEx(orderId, null, "TRADE_SUCCESS", payAmt,null, "", "93");
  241. // 记录操作日志
  242. //String logInfo = "操作员:" + user.getName() + "会员下单,售货机编号:" + equNo+ "下单商品信息:" + detData;
  243. //Service.lookup(ILogService.class).logging(session, logInfo);
  244. return res;
  245. }
  246. }