StudentOrderController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package com.ym.mec.student.controller;
  2. import com.ym.mec.biz.dal.dao.OrganizationDao;
  3. import com.ym.mec.biz.dal.dao.SysConfigDao;
  4. import com.ym.mec.biz.dal.dto.LuckStatisDto;
  5. import com.ym.mec.biz.dal.dto.OrderStatisDto;
  6. import com.ym.mec.biz.dal.entity.*;
  7. import com.ym.mec.biz.service.*;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiImplicitParam;
  10. import io.swagger.annotations.ApiImplicitParams;
  11. import io.swagger.annotations.ApiOperation;
  12. import java.io.IOException;
  13. import java.math.BigDecimal;
  14. import java.net.URLEncoder;
  15. import java.util.Arrays;
  16. import java.util.HashMap;
  17. import java.util.LinkedHashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Objects;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.scheduling.annotation.EnableScheduling;
  26. import org.springframework.web.bind.annotation.GetMapping;
  27. import org.springframework.web.bind.annotation.ModelAttribute;
  28. import org.springframework.web.bind.annotation.PostMapping;
  29. import org.springframework.web.bind.annotation.RequestMapping;
  30. import org.springframework.web.bind.annotation.RequestParam;
  31. import org.springframework.web.bind.annotation.RestController;
  32. import com.alibaba.fastjson.JSON;
  33. import com.huifu.adapay.model.payment.PayChannelEnum;
  34. import com.huifu.adapay.model.payment.Payment;
  35. import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
  36. import com.ym.mec.biz.dal.dto.VipBuyResultDto;
  37. import com.ym.mec.biz.dal.enums.DealStatusEnum;
  38. import com.ym.mec.biz.dal.enums.GroupType;
  39. import com.ym.mec.common.controller.BaseController;
  40. import com.ym.mec.common.entity.HttpResponseResult;
  41. import com.ym.mec.thirdparty.adapay.NotifyEvent;
  42. import com.ym.mec.thirdparty.adapay.Pay;
  43. import com.ym.mec.thirdparty.yqpay.Msg;
  44. import com.ym.mec.thirdparty.yqpay.RsqMsg;
  45. import com.ym.mec.thirdparty.yqpay.YqPayFeignService;
  46. import com.ym.mec.thirdparty.yqpay.YqPayUtil;
  47. import com.ym.mec.util.http.HttpUtil;
  48. import javax.servlet.http.HttpServletResponse;
  49. @RequestMapping("studentOrder")
  50. @Api(tags = "订单回调")
  51. @RestController
  52. @EnableScheduling
  53. public class StudentOrderController extends BaseController {
  54. private static final Logger logger = LoggerFactory.getLogger(StudentOrderController.class);
  55. @Autowired
  56. private StudentPaymentOrderService studentPaymentOrderService;
  57. @Autowired
  58. private StudentRegistrationService studentRegistrationService;
  59. @Autowired
  60. private VipGroupService vipGroupService;
  61. @Autowired
  62. private MusicGroupService musicGroupService;
  63. @Autowired
  64. private StudentPaymentOrderDetailService studentPaymentOrderDetailService;
  65. @Autowired
  66. private YqPayFeignService yqPayFeignService;
  67. @Autowired
  68. private StudentPaymentOrderDao studentPaymentOrderDao;
  69. @Autowired
  70. private SysConfigDao sysConfigDao;
  71. @Autowired
  72. private SporadicChargeInfoService sporadicChargeInfoService;
  73. @Autowired
  74. private OrganizationDao organizationDao;
  75. @PostMapping("/notify")
  76. public Msg notify(@ModelAttribute Msg msg) throws Exception {
  77. logger.info(msg.toString());
  78. Map<String, Object> rqMap = new LinkedHashMap<String, Object>();
  79. rqMap.put("code", msg.getCode());
  80. rqMap.put("msg", msg.getMsg());
  81. rqMap.put("responseType", msg.getResponseType());
  82. rqMap.put("responseParameters", msg.getResponseParameters());
  83. rqMap.put("sign", msg.getSign());
  84. //boolean rs = YqPayUtil.verify(rqMap);
  85. msg.setMsg("fail");
  86. Map<String, String> notifyMap = new HashMap<>();
  87. //if (rs) {
  88. notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
  89. //}
  90. //支付中订单存在,更新状态
  91. if (msg.getResponseType().equals("1") && notifyMap.size() > 0) {
  92. String tradeState = msg.getCode().equals("88") ? "1" : "0";
  93. String channelType = notifyMap.get("channelType").equals("1") ? "WXPay" : (notifyMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
  94. notifyMap.put("tradeState", tradeState);
  95. notifyMap.put("totalMoney", notifyMap.get("payAmount"));
  96. notifyMap.put("merOrderNo", notifyMap.get("merMerOrderNo"));
  97. notifyMap.put("channelType", channelType);
  98. studentPaymentOrderService.updateOrder(notifyMap);
  99. msg.setCode("000000");
  100. msg.setMsg("success");
  101. }
  102. return msg;
  103. }
  104. @ApiOperation(value = "查询订单状态")
  105. @PostMapping("/checkOrderStatus")
  106. public Object checkOrderStatus(String orderNo) {
  107. if (StringUtils.isBlank(orderNo)) {
  108. return failed("请指定订单");
  109. }
  110. StudentPaymentOrder orderByOrderNo = studentPaymentOrderService.findOrderByOrderNo(orderNo);
  111. if (Objects.isNull(orderByOrderNo)) {
  112. return failed("未找到指定订单");
  113. }
  114. HashMap<String, Object> orderDetail = new HashMap<>();
  115. orderDetail.put("order", orderByOrderNo);
  116. orderDetail.put("groupType", orderByOrderNo.getGroupType());
  117. if (orderByOrderNo.getGroupType().equals(GroupType.MUSIC)) {
  118. MusicGroup musicGroup = musicGroupService.get(orderByOrderNo.getMusicGroupId());
  119. List<Goods> goodsList = studentPaymentOrderDetailService.findApplyOrderGoods(orderByOrderNo.getId());
  120. orderDetail.put("goods", goodsList);
  121. orderDetail.put("course", musicGroup.getCourseForm());
  122. } else if (orderByOrderNo.getGroupType().equals(GroupType.VIP)) {
  123. VipBuyResultDto vipBuyResultInfo = vipGroupService.findVipBuyResultInfo(Integer.valueOf(orderByOrderNo.getMusicGroupId()));
  124. orderDetail.put("detail", vipBuyResultInfo);
  125. } else if (orderByOrderNo.getGroupType().equals(GroupType.SPORADIC)) {
  126. SporadicChargeInfo info = sporadicChargeInfoService.get(Integer.valueOf(orderByOrderNo.getMusicGroupId()));
  127. orderDetail.put("detail", info);
  128. }
  129. return succeed(orderDetail);
  130. }
  131. @ApiOperation(value = "台牌支付")
  132. @PostMapping("/executePayment")
  133. @ApiImplicitParams({
  134. @ApiImplicitParam(name = "amount", value = "支付金额", required = true, dataType = "BigDecimal"),
  135. @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String"),
  136. @ApiImplicitParam(name = "payChannel", value = "支付方式(alipay-支付宝app支付)", required = true, dataType = "String"),
  137. })
  138. public Object executePayment(BigDecimal amount, String orderNo, String payChannel, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sign, String code) throws Exception {
  139. String appId = "wxcf8e8b33a9477845";
  140. String appSecret = "1286452b9c68b13325dece7cdf892645";
  141. String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
  142. payChannel = PayChannelEnum.ALIPAY_QR.getCode();
  143. // if (!new Pay().verifySign(amount, orderNo, notifyUrl, orderSubject, orderBody,sign)) {
  144. // return failed("签名验证失败");
  145. // }
  146. String openid = "";
  147. if (payChannel == "wx_pub") {
  148. if (code == null || code.isEmpty()) {
  149. return failed("微信支付请先授权");
  150. }
  151. wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
  152. Map<String, String> weChatRes = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
  153. if (!weChatRes.containsKey("openid")) {
  154. return failed("授权失败,请重新授权");
  155. }
  156. openid = weChatRes.get("openid");
  157. }
  158. StudentPaymentOrder order = studentPaymentOrderService.findOrderByOrderNo(orderNo);
  159. if (order == null) {
  160. return failed("订单不存在");
  161. }
  162. Payment payment = new Pay().executePayment(amount, orderNo, payChannel, orderSubject, orderBody, code);
  163. order.setTransNo(payment.getId());
  164. studentPaymentOrderService.update(order);
  165. return succeed(payment);
  166. }
  167. @PostMapping("/adaNotify")
  168. public void adaNotify(@ModelAttribute NotifyEvent notifyEvent) throws Exception {
  169. logger.info(notifyEvent.toString());
  170. if (notifyEvent.getType().equals("payment.success") && notifyEvent.getType().equals("payment.failed")) {
  171. return;
  172. }
  173. Map<String, String> notifyMap = JSON.parseObject(notifyEvent.getData(), Map.class);
  174. //支付中订单存在,更新状态
  175. if (notifyMap.size() > 0) {
  176. String tradeState = notifyEvent.getType().equals("payment.success") ? "1" : "0";
  177. notifyMap.put("tradeState", tradeState);
  178. notifyMap.put("totalMoney", notifyMap.get("pay_amt"));
  179. notifyMap.put("merOrderNo", notifyMap.get("order_no"));
  180. notifyMap.put("merOrderNo", notifyMap.get("order_no"));
  181. notifyMap.put("remarks", notifyMap.get("description"));
  182. studentPaymentOrderService.updateOrder(notifyMap);
  183. }
  184. }
  185. @GetMapping("/authorize")
  186. public String authorize(@RequestParam("returnUrl") String returnUrl) {
  187. String appId = "wxcf8e8b33a9477845";
  188. String url = URLEncoder.encode("http://wxwechat.utools.club/studentOrder/userInfo");
  189. String redirectURL = String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect",
  190. appId, url);
  191. return "redirect:" + redirectURL;
  192. }
  193. @GetMapping("/userInfo")
  194. public Object userInfo(@RequestParam("code") String code,
  195. @RequestParam("state") String returnUrl) throws Exception {
  196. String appId = "wxcf8e8b33a9477845";
  197. String appSecret = "1286452b9c68b13325dece7cdf892645";
  198. String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
  199. wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
  200. Map<String, String> map = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
  201. if (!map.containsKey("openid")) {
  202. return failed("授权失败,请重新授权");
  203. }
  204. return map.get("openid");
  205. }
  206. // @Scheduled(cron = "0/30 * * * * ?")
  207. @GetMapping("/setSuccessStatus")
  208. public void setSuccessStatus() throws Exception {
  209. List<StudentPaymentOrder> payingOrders = studentPaymentOrderService.findOrdersByStatus(DealStatusEnum.ING, "YQPAY");
  210. String[] statusArr = {"0", "1", "7"};
  211. for (StudentPaymentOrder payingOrder : payingOrders) {
  212. Map<String, String> rpMap = new HashMap<>();
  213. rpMap.put("tradeState", "1");
  214. rpMap.put("remarks", "模拟支付成功");
  215. rpMap.put("merOrderNo", payingOrder.getOrderNo());
  216. rpMap.put("orderNo", payingOrder.getOrderNo());
  217. rpMap.put("channelType", "1");
  218. String channelType = rpMap.get("channelType").equals("1") ? "WXPay" : (rpMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
  219. rpMap.put("channelType", channelType);
  220. if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
  221. try {
  222. studentPaymentOrderService.updateOrder(rpMap); //更新订单
  223. } catch (Exception e) {
  224. e.printStackTrace();
  225. continue;
  226. }
  227. }
  228. }
  229. }
  230. @GetMapping("/fixOrder")
  231. private HttpResponseResult fixOrder(String orderNo) throws Exception {
  232. if (orderNo == null || orderNo.isEmpty()) {
  233. return failed("订单号必须填");
  234. }
  235. String notifyUrl = ""; //回调地址
  236. Map<String, Object> resultMap = new LinkedHashMap<>();
  237. resultMap.put("merOrderNoList", orderNo);
  238. Map<String, Object> requestMap = YqPayUtil.getRequestMap(notifyUrl, resultMap);
  239. RsqMsg rsqMsg = new RsqMsg(requestMap);
  240. Msg queryRs = yqPayFeignService.orderQuery(rsqMsg);
  241. if (queryRs.getCode().equals("88")) {
  242. String responseParameters = queryRs.getResponseParameters();
  243. List<Map<String, Object>> responseList = JSON.parseObject(responseParameters, List.class);
  244. return succeed(responseList);
  245. }
  246. return succeed("订单不存在");
  247. }
  248. @RequestMapping("paymentResult")
  249. public void paymentResult(HttpServletResponse response,String orderNo) {
  250. try {
  251. String baseApiUrl = sysConfigDao.findConfigValue("base_api_url");
  252. response.sendRedirect(baseApiUrl + "/#/paymentresult?orderNo=" + orderNo);
  253. } catch (IOException e) {
  254. e.printStackTrace();
  255. }
  256. }
  257. @GetMapping("/getLuckStatis")
  258. public HttpResponseResult getLuckStatis() {
  259. List<Organization> organs = organizationDao.findAllOrgans();
  260. List<OrderStatisDto> orders = studentPaymentOrderDao.getLuckStatis();
  261. BigDecimal totalMoney = BigDecimal.ZERO;
  262. Integer totalNum = 0;
  263. for (Organization organ : organs) {
  264. boolean flag = false;
  265. for (OrderStatisDto order : orders) {
  266. totalMoney = totalMoney.add(order.getMoney());
  267. totalNum += order.getNums();
  268. if (organ.getName().equals(order.getOrganName())) {
  269. flag = true;
  270. }
  271. }
  272. if (!flag) {
  273. OrderStatisDto orderStatisDto = new OrderStatisDto();
  274. orderStatisDto.setOrganName(organ.getName());
  275. orderStatisDto.setMoney(BigDecimal.ZERO);
  276. orderStatisDto.setNums(0);
  277. orders.add(orderStatisDto);
  278. }
  279. }
  280. LuckStatisDto luckStatisDto = new LuckStatisDto();
  281. luckStatisDto.setOrderStatisDtoList(orders);
  282. luckStatisDto.setTotalNum(totalNum);
  283. luckStatisDto.setTotalMoney(totalMoney);
  284. return succeed(luckStatisDto);
  285. }
  286. }