StudentOrderController.java 13 KB

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