YqPayController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package com.ym.mec.collectfee.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ym.mec.collectfee.common.web.BaseController;
  4. import com.ym.mec.collectfee.entity.Account;
  5. import com.ym.mec.collectfee.entity.Order;
  6. import com.ym.mec.collectfee.service.AccountService;
  7. import com.ym.mec.collectfee.service.OrderService;
  8. import com.ym.mec.collectfee.service.YqPayService;
  9. import com.ym.mec.collectfee.service.YqQueryService;
  10. import com.ym.mec.collectfee.utils.GenerateNum;
  11. import com.ym.mec.collectfee.utils.yqpay.*;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.context.annotation.Lazy;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import org.springframework.validation.annotation.Validated;
  20. import org.springframework.web.bind.annotation.ModelAttribute;
  21. import org.springframework.web.bind.annotation.PostMapping;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import springfox.documentation.annotations.ApiIgnore;
  25. import java.math.BigDecimal;
  26. import java.util.*;
  27. @Api("支付")
  28. @Slf4j
  29. @RestController
  30. @RequestMapping("yqpay")
  31. public class YqPayController extends BaseController {
  32. @Autowired
  33. @Lazy
  34. private YqPayService yqPayService;
  35. @Autowired
  36. @Lazy
  37. private YqQueryService yqQueryService;
  38. @Autowired
  39. private OrderService orderService;
  40. @Autowired
  41. private AccountService accountService;
  42. /**
  43. * 统一下单
  44. *
  45. * @return String
  46. * @throws Exception
  47. */
  48. @ApiOperation(value = "提交支付", notes = "易乾支付统一下单")
  49. @PostMapping("/toPay")
  50. @Transactional
  51. public Map toPay(@ModelAttribute @Validated Order order) throws Exception {
  52. String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //异步通知地址
  53. String returnUrl = "http://dev.dayaedu.com";//支付后返回页面
  54. String payUrl = "https://qyfapi.95epay.com/api/api/hPay/toPayHtml";//支付跳转页
  55. String payChannels = "{\"fastpayXy\":true,\"weChatPay\":true,\"weChatPayMobile\":false,\"aliPay\":true,\"aliPayMobile\":true,\"balancePay\":false}";//支付方式配置
  56. String orderNo = GenerateNum.getInstance().GenerateOrderNo(); //自己系统订单号
  57. order.setOrderNo(orderNo);
  58. order.setCreateTime(new Date()); //订单提交时间
  59. order.setStatus(1); //订单状态
  60. orderService.insert(order);
  61. //获取支付成功跟支付中的订单数
  62. int branchId = order.getBranchId();
  63. int payOrderNums = orderService.getPayOrderNums();
  64. //分佣账户(1、每10笔,前四笔收入私户 2、私户每个商户最多收款400万)
  65. if ((payOrderNums + 1) % 10 <= 4) { //私人账户
  66. branchId = 0;
  67. }
  68. //获取分佣账户
  69. Account account = accountService.getAccountByBranchId(branchId);
  70. String routingMerNo = account.getSellerNo();
  71. Map<String, Object> routingList = new LinkedHashMap<String, Object>();
  72. routingList.put("routingMerNo", routingMerNo);//分佣账户
  73. routingList.put("routingFee", order.getAmount()); //分佣金额
  74. List<Map> tempRoutingList = new ArrayList();
  75. tempRoutingList.add(routingList);
  76. /*订单信息*/
  77. String orderBody = "大雅乐盟培训课程";
  78. /*订单标题*/
  79. String orderSubject = "培训订单";
  80. Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
  81. resultMap.put("sellerNo", "0021677"); //收款商户号
  82. resultMap.put("payChannels", payChannels); //支付方式
  83. resultMap.put("orderBody", orderBody); //订单信息
  84. resultMap.put("payAmount", order.getAmount()); //支付金额
  85. resultMap.put("apiPayType", "1"); //*API支付类型1-即时支付,2-担保支付,3-预授权支付*/
  86. resultMap.put("tradeType", "0"); //*交易类型1—充值,0—收款*
  87. resultMap.put("merMerOrderNo", orderNo); //商户订单号
  88. resultMap.put("orderSubject", orderSubject); //订单标题
  89. resultMap.put("returnUrl", returnUrl); //前台页面地址
  90. resultMap.put("tempRoutingList", JSON.toJSONString(tempRoutingList));//分账设置
  91. Map rqMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  92. rqMap.put("host", payUrl);
  93. return rqMap;
  94. }
  95. /**
  96. * 交易查询
  97. *
  98. * @param merOrderNoList 用户订单号
  99. * @return
  100. * @throws Exception
  101. */
  102. //@PostMapping("/query")
  103. //@Scheduled(cron = "0/3 40 11 * * ?")
  104. public String query(String merOrderNoList) throws Exception {
  105. String notifyUrl = ""; //回调地址
  106. Map<String, Object> resultMap = new LinkedHashMap<>();
  107. resultMap.put("merOrderNoList", merOrderNoList);
  108. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  109. return yqQueryService.orderQuery(requestMap);
  110. }
  111. /**
  112. * 用户信息(商户)查询
  113. *
  114. * @param sonMerNo 子商户号
  115. * @return
  116. * @throws Exception
  117. */
  118. @PostMapping("/queryaccount")
  119. public String queryAccount(String sonMerNo) throws Exception {
  120. String notifyUrl = ""; //回调地址
  121. Map<String, Object> resultMap = new LinkedHashMap<>();
  122. resultMap.put("merOrderNoList", sonMerNo);
  123. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  124. return yqQueryService.queryAccount(requestMap);
  125. }
  126. /**
  127. * 对账查询(定时任务每天对账)
  128. *
  129. * @return
  130. */
  131. public String queryBill() throws Exception {
  132. String notifyUrl = ""; //回调地址
  133. Map<String, Object> resultMap = new LinkedHashMap<>();
  134. resultMap.put("tradeDate", ""); //交易日期
  135. resultMap.put("payState", ""); //订单状态
  136. resultMap.put("tradeType", ""); //交易类型,不填为全部
  137. resultMap.put("channelType", ""); //通道类型,不填为全部
  138. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  139. return yqQueryService.billQuery(requestMap);
  140. }
  141. /**
  142. * 平台转账
  143. *
  144. * @return
  145. * @throws Exception
  146. */
  147. @PostMapping("/platformtransferacc")
  148. public String platformTransferAcc() throws Exception {
  149. String notifyUrl = ""; //回调地址
  150. Map<String, Object> resultMap = new LinkedHashMap<>();
  151. resultMap.put("payeeNo", ""); //收款方商户号
  152. resultMap.put("payeeName", ""); //收款方姓名
  153. resultMap.put("amount", ""); //金额
  154. resultMap.put("merOrderNo", ""); //商户订单号
  155. resultMap.put("remarks", ""); //备注
  156. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  157. return yqPayService.platformTransferAcc(requestMap);
  158. }
  159. /**
  160. * 提现短信
  161. *
  162. * @return
  163. * @throws Exception
  164. */
  165. @PostMapping("/sendsms")
  166. public Msg sendSms(@ModelAttribute Intfc intfc) throws Exception {
  167. String notifyUrl = ""; //回调地址
  168. String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo();
  169. Map<String, Object> resultMap = new LinkedHashMap<>();
  170. resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号
  171. resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号
  172. resultMap.put("amount", intfc.getAmount()); //提现金额
  173. resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号
  174. // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填)
  175. // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填)
  176. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  177. return yqPayService.intfc(requestMap);
  178. }
  179. /**
  180. * 提现
  181. *
  182. * @return
  183. * @throws Exception
  184. */
  185. @PostMapping("/intfc")
  186. public Msg intfc(@ModelAttribute Intfc intfc) throws Exception {
  187. String notifyUrl = "http://47.99.212.176:9000/yqpay/notify"; //回调地址
  188. String merMerOrderNo = GenerateNum.getInstance().GenerateOrderNo();
  189. Map<String, Object> resultMap = new LinkedHashMap<>();
  190. resultMap.put("wdMerNo", intfc.getWdMerNo()); //提现商户号
  191. resultMap.put("merMerOrderNo", merMerOrderNo); //商户订单号
  192. resultMap.put("amount", intfc.getAmount()); //提现金额
  193. resultMap.put("cardNo", intfc.getCardNo()); //提现银行卡号
  194. // resultMap.put("phone", intfc.getPhone()); //预留手机号(选填)
  195. // resultMap.put("cardType", intfc.getCardType()); //卡类型 0-个人银行卡,1-企业银行卡。(选填)
  196. resultMap.put("seqNo", ""); //流水号(选填)
  197. resultMap.put("smsCode", ""); //验证码(选填)
  198. Map<String, Object> requestMap = new YqPayUtil(notifyUrl, resultMap).getRequestMap();
  199. return yqPayService.intfc(requestMap);
  200. }
  201. /**
  202. * 易乾异步通知接口
  203. *
  204. * @param msg
  205. * @return String
  206. * @throws Exception
  207. */
  208. @PostMapping("/notify")
  209. public Msg notify(@ModelAttribute Msg msg) throws Exception {
  210. log.info(msg.toString());
  211. Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
  212. rqMap.put("code", msg.getCode());
  213. rqMap.put("msg", msg.getMsg());
  214. rqMap.put("responseType", msg.getResponseType());
  215. rqMap.put("responseParameters", msg.getResponseParameters());
  216. rqMap.put("sign", msg.getSign());
  217. boolean rs = YqPayUtil.verify(rqMap);
  218. msg.setCode("");
  219. msg.setMsg("fail");
  220. Order order = null;
  221. NotifyMsg notifyMsg = null;
  222. if (rs) {
  223. notifyMsg = JSON.parseObject(msg.getResponseParameters(), NotifyMsg.class);
  224. String merMerOrderNo = notifyMsg.getMerMerOrderNo(); //自己系统订单号
  225. order = orderService.getOrderByOrderNo(merMerOrderNo);
  226. }
  227. //支付中订单存在,更新状态
  228. if (order != null && msg.getResponseType().equals("1")) {
  229. //更新订单状态
  230. int status = msg.getCode().equals("88") ? 0 : 2;
  231. order.setStatus(status);
  232. if (order.getOrderNo().isEmpty()) {
  233. order.setPayId(notifyMsg.getOrderNo()); //更新易乾付订单号
  234. }
  235. if (status == 0) {
  236. order.setPay(notifyMsg.getPayAmount());
  237. order.setPayTime(new Date());
  238. }
  239. orderService.update(order);
  240. msg.setCode("000000");
  241. msg.setMsg("success");
  242. }
  243. return msg;
  244. }
  245. }