OrderNotifyController.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.izouma.nineth.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alipay.api.AlipayApiException;
  5. import com.alipay.api.internal.util.AlipaySignature;
  6. import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
  7. import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
  8. import com.github.binarywang.wxpay.exception.WxPayException;
  9. import com.github.binarywang.wxpay.service.WxPayService;
  10. import com.huifu.adapay.core.AdapayCore;
  11. import com.huifu.adapay.core.util.AdapaySign;
  12. import com.izouma.nineth.config.AlipayProperties;
  13. import com.izouma.nineth.config.GeneralProperties;
  14. import com.izouma.nineth.config.RedisKeys;
  15. import com.izouma.nineth.enums.PayMethod;
  16. import com.izouma.nineth.event.OrderNotifyEvent;
  17. import com.izouma.nineth.service.AssetService;
  18. import com.izouma.nineth.service.GiftOrderService;
  19. import com.izouma.nineth.service.OrderService;
  20. import lombok.AllArgsConstructor;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.collections.MapUtils;
  23. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  24. import org.springframework.data.redis.core.BoundSetOperations;
  25. import org.springframework.data.redis.core.RedisTemplate;
  26. import org.springframework.stereotype.Controller;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.servlet.http.HttpServletRequest;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import java.util.concurrent.TimeUnit;
  33. import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
  34. @Slf4j
  35. @Controller
  36. @RequestMapping("/notify")
  37. @AllArgsConstructor
  38. public class OrderNotifyController {
  39. private final AlipayProperties alipayProperties;
  40. private final OrderService orderService;
  41. private final WxPayService wxPayService;
  42. private final AssetService assetService;
  43. private final GiftOrderService giftOrderService;
  44. private final RedisTemplate<String, Object> redisTemplate;
  45. private final RocketMQTemplate rocketMQTemplate;
  46. private final GeneralProperties generalProperties;
  47. @PostMapping("/order/alipay")
  48. @ResponseBody
  49. public String notify(HttpServletRequest request) throws AlipayApiException {
  50. Map<String, String> params = new HashMap<>();
  51. Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
  52. for (Map.Entry<String, String[]> entry : entrySet) {
  53. String name = entry.getKey();
  54. String[] values = entry.getValue();
  55. int valLen = values.length;
  56. if (valLen == 1) {
  57. params.put(name, values[0]);
  58. } else if (valLen > 1) {
  59. StringBuilder sb = new StringBuilder();
  60. for (String val : values) {
  61. sb.append(",").append(val);
  62. }
  63. params.put(name, sb.substring(1));
  64. } else {
  65. params.put(name, "");
  66. }
  67. }
  68. log.info("支付宝回调 {}", JSON.toJSONString(params, PrettyFormat));
  69. AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
  70. if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
  71. JSONObject body = JSON.parseObject(params.get("body"));
  72. String action = body.getString("action");
  73. switch (action) {
  74. case "payOrder": {
  75. Long orderId = body.getLong("orderId");
  76. orderService.notifyOrder(orderId, PayMethod.ALIPAY, MapUtils.getString(params, "trade_no"));
  77. break;
  78. }
  79. case "payGiftOrder": {
  80. Long orderId = body.getLong("orderId");
  81. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, MapUtils.getString(params, "trade_no"));
  82. break;
  83. }
  84. }
  85. return "success";
  86. }
  87. return "error";
  88. }
  89. @PostMapping(value = "/order/weixin", produces = "application/xml")
  90. @ResponseBody
  91. public String wxNotify(@RequestBody String xmlData) throws WxPayException {
  92. log.info("微信支付回调: {}", xmlData);
  93. final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
  94. notifyResult.checkResult(wxPayService, "MD5", true);
  95. JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
  96. String action = attach.getString("action");
  97. switch (action) {
  98. case "payOrder": {
  99. Long orderId = attach.getLong("orderId");
  100. orderService.notifyOrder(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  101. break;
  102. }
  103. case "payGiftOrder": {
  104. Long orderId = attach.getLong("orderId");
  105. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  106. break;
  107. }
  108. }
  109. return WxPayNotifyResponse.success("OK");
  110. }
  111. @PostMapping("/adapay/order/{orderId}")
  112. @ResponseBody
  113. public void adapayNotify(@PathVariable Long orderId, HttpServletRequest request) {
  114. log.info("adapay notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  115. try {
  116. String data = request.getParameter("data");
  117. String sign = request.getParameter("sign");
  118. String type = request.getParameter("type");
  119. if ("payment.succeeded".equals(type)) {
  120. boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  121. log.info("checkSign {}", checkSign);
  122. if (checkSign) {
  123. JSONObject jsonObject = JSON.parseObject(data);
  124. String channel = jsonObject.getString("pay_channel");
  125. String id = jsonObject.getString("id");
  126. PayMethod payMethod = channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY;
  127. BoundSetOperations<String, Object> listOps = redisTemplate.boundSetOps(RedisKeys.PAY_RECORD + orderId);
  128. listOps.add(id);
  129. listOps.expire(7, TimeUnit.DAYS);
  130. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  131. new OrderNotifyEvent(orderId, payMethod, id, System.currentTimeMillis()));
  132. }
  133. }
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. @PostMapping("/adapay/giftOrder/{orderId}")
  139. @ResponseBody
  140. public void adapayGiftNotify(@PathVariable Long orderId, HttpServletRequest request) {
  141. log.info("adapay gift notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  142. try {
  143. String data = request.getParameter("data");
  144. String sign = request.getParameter("sign");
  145. boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  146. log.info("checkSign {}", checkSign);
  147. if (checkSign) {
  148. JSONObject jsonObject = JSON.parseObject(data);
  149. String channel = jsonObject.getString("pay_channel");
  150. String id = jsonObject.getString("id");
  151. giftOrderService.giftNotify(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
  152. }
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. }