package com.ym.mec.collectfee.controller; import com.alibaba.fastjson.JSON; import com.ym.mec.collectfee.common.web.BaseController; import com.ym.mec.collectfee.entity.Account; import com.ym.mec.collectfee.entity.Order; import com.ym.mec.collectfee.service.AccountService; import com.ym.mec.collectfee.service.OrderService; import com.ym.mec.collectfee.service.YqPayService; import com.ym.mec.collectfee.service.YqQueryService; import com.ym.mec.collectfee.utils.GenerateNum; import com.ym.mec.collectfee.utils.yqpay.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import java.math.BigDecimal; import java.util.*; @Api("支付") @Slf4j @RestController @RequestMapping("yqpay") public class YqPayController extends BaseController { @Autowired @Lazy private YqPayService yqPayService; @Autowired @Lazy private YqQueryService yqQueryService; @Autowired private OrderService orderService; @Autowired private AccountService accountService; /** * 统一下单 * * @return String * @throws Exception */ @ApiOperation(value = "提交支付", notes = "易乾支付统一下单") @PostMapping("/toPay") @Transactional public Map toPay(@ModelAttribute @Validated Order order) throws Exception { String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //异步通知地址 String returnUrl = "http://dev.dayaedu.com";//支付后返回页面 String payUrl = "https://qyfapi.95epay.com/api/api/hPay/toPayHtml";//支付跳转页 String payChannels = "{\"fastpayXy\":true,\"weChatPay\":true,\"weChatPayMobile\":false,\"aliPay\":true,\"aliPayMobile\":true,\"balancePay\":false}";//支付方式配置 String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号 order.setOrderNo(orderNo); order.setCreateTime(new Date()); //订单提交时间 order.setStatus(1); //订单状态 orderService.insert(order); //获取支付成功跟支付中的订单数 int branchId = order.getBranchId(); int payOrderNums = orderService.getPayOrderNums(); //分佣账户(1、每10笔,前四笔收入私户 2、私户每个商户最多收款400万) if ((payOrderNums + 1) % 10 <= 4) { //私人账户 branchId = 0; } //获取分佣账户 Account account = accountService.getAccountByBranchId(branchId); String routingMerNo = account.getSellerNo(); Map routingList = new LinkedHashMap(); routingList.put("routingMerNo", routingMerNo);//分佣账户 routingList.put("routingFee", order.getAmount()); //分佣金额 List tempRoutingList = new ArrayList(); tempRoutingList.add(routingList); /*订单信息*/ String orderBody = "大雅乐盟培训课程"; /*订单标题*/ String orderSubject = "培训订单"; Map resultMap = new LinkedHashMap(); resultMap.put("sellerNo", "0021677"); //收款商户号 resultMap.put("payChannels", payChannels); //支付方式 resultMap.put("orderBody", orderBody); //订单信息 resultMap.put("payAmount", order.getAmount()); //支付金额 resultMap.put("apiPayType", "1"); //*API支付类型1-即时支付,2-担保支付,3-预授权支付*/ resultMap.put("tradeType", "0"); //*交易类型1—充值,0—收款* resultMap.put("merMerOrderNo", orderNo); //商户订单号 resultMap.put("orderSubject", orderSubject); //订单标题 resultMap.put("returnUrl", returnUrl); //前台页面地址 resultMap.put("tempRoutingList", JSON.toJSONString(tempRoutingList));//分账设置 Map rqMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); rqMap.put("host", payUrl); return rqMap; } /** * 交易查询 * * @param merOrderNoList 用户订单号 * @return * @throws Exception */ //@PostMapping("/query") //@Scheduled(cron = "0/3 40 11 * * ?") public String query(String merOrderNoList) throws Exception { String notifyUrl = ""; //回调地址 Map resultMap = new LinkedHashMap<>(); resultMap.put("merOrderNoList", merOrderNoList); Map requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); return yqQueryService.orderQuery(requestMap); } /** * 用户信息(商户)查询 * * @param sonMerNo 子商户号 * @return * @throws Exception */ @PostMapping("/queryaccount") public String queryAccount(String sonMerNo) throws Exception { String notifyUrl = ""; //回调地址 Map resultMap = new LinkedHashMap<>(); resultMap.put("merOrderNoList", sonMerNo); Map requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); return yqQueryService.queryAccount(requestMap); } /** * 对账查询(定时任务每天对账) * * @return */ public String queryBill() throws Exception { String notifyUrl = ""; //回调地址 Map resultMap = new LinkedHashMap<>(); resultMap.put("tradeDate", ""); //交易日期 resultMap.put("payState", ""); //订单状态 resultMap.put("tradeType", ""); //交易类型,不填为全部 resultMap.put("channelType", ""); //通道类型,不填为全部 Map requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); return yqQueryService.billQuery(requestMap); } /** * 平台转账 * * @return * @throws Exception */ @PostMapping("/platformtransferacc") public String platformTransferAcc() throws Exception { String notifyUrl = ""; //回调地址 Map resultMap = new LinkedHashMap<>(); resultMap.put("payeeNo", ""); //收款方商户号 resultMap.put("payeeName", ""); //收款方姓名 resultMap.put("amount", ""); //金额 resultMap.put("merOrderNo", ""); //商户订单号 resultMap.put("remarks", ""); //备注 Map requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); return yqPayService.platformTransferAcc(requestMap); } /** * 提现短信 * * @return * @throws Exception */ @PostMapping("/sendsms") public Msg sendSms(@ModelAttribute Intfc intfc) throws Exception { String notifyUrl = ""; //回调地址 String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo(); Map resultMap = new LinkedHashMap<>(); resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号 resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号 resultMap.put("amount", intfc.getAmount()); //提现金额 resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号 // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填) // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填) Map requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); return yqPayService.intfc(requestMap); } /** * 提现 * * @return * @throws Exception */ @PostMapping("/intfc") public Msg intfc(@ModelAttribute Intfc intfc) throws Exception { String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //回调地址 String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo(); Map resultMap = new LinkedHashMap<>(); resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号 resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号 resultMap.put("amount", intfc.getAmount()); //提现金额 resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号 // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填) // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填) resultMap.put("seqNo", ""); //流水号(选填) resultMap.put("smsCode", ""); //验证码(选填) Map requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap(); return yqPayService.intfc(requestMap); } /** * 易乾异步通知接口 * * @param msg * @return String * @throws Exception */ @PostMapping("/notify") public Msg notify(@ModelAttribute Msg msg) throws Exception { log.info(msg.toString()); Map rqMap = new LinkedHashMap(); rqMap.put("code", msg.getCode()); rqMap.put("msg", msg.getMsg()); rqMap.put("responseType", msg.getResponseType()); rqMap.put("responseParameters", msg.getResponseParameters()); rqMap.put("sign", msg.getSign()); boolean rs = YqPayUtil.verify(rqMap); msg.setCode(""); msg.setMsg("fail"); Order order = null; NotifyMsg notifyMsg = null; if (rs) { notifyMsg = JSON.parseObject(msg.getResponseParameters(), NotifyMsg.class); String merMerOrderNo = notifyMsg.getMerMerOrderNo(); //自己系统订单号 order = orderService.getOrderByOrderNo(merMerOrderNo); } //支付中订单存在,更新状态 if (order != null && msg.getResponseType().equals("1")) { //更新订单状态 int status = msg.getCode().equals("88") ? 0 : 2; order.setStatus(status); if (order.getOrderNo().isEmpty()) { order.setPayId(notifyMsg.getOrderNo()); //更新易乾付订单号 } if (status == 0) { order.setPay(notifyMsg.getPayAmount()); order.setPayTime(new Date()); } orderService.update(order); msg.setCode("000000"); msg.setMsg("success"); } return msg; } }