OrderNotifyController.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.github.kevinsawicki.http.HttpRequest;
  11. import com.huifu.adapay.core.AdapayCore;
  12. import com.huifu.adapay.core.util.AdapaySign;
  13. import com.izouma.nineth.config.AlipayProperties;
  14. import com.izouma.nineth.config.GeneralProperties;
  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 com.izouma.nineth.utils.SnowflakeIdWorker;
  21. import lombok.AllArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.collections.MapUtils;
  24. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  25. import org.springframework.stereotype.Controller;
  26. import org.springframework.web.bind.annotation.*;
  27. import javax.servlet.http.HttpServletRequest;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
  32. @Slf4j
  33. @Controller
  34. @RequestMapping("/notify")
  35. @AllArgsConstructor
  36. public class OrderNotifyController {
  37. private final AlipayProperties alipayProperties;
  38. private final OrderService orderService;
  39. private final WxPayService wxPayService;
  40. private final AssetService assetService;
  41. private final GiftOrderService giftOrderService;
  42. private final SnowflakeIdWorker snowflakeIdWorker;
  43. private final RocketMQTemplate rocketMQTemplate;
  44. private final GeneralProperties generalProperties;
  45. @PostMapping("/order/alipay")
  46. @ResponseBody
  47. public String notify(HttpServletRequest request) throws AlipayApiException {
  48. Map<String, String> params = new HashMap<>();
  49. Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
  50. for (Map.Entry<String, String[]> entry : entrySet) {
  51. String name = entry.getKey();
  52. String[] values = entry.getValue();
  53. int valLen = values.length;
  54. if (valLen == 1) {
  55. params.put(name, values[0]);
  56. } else if (valLen > 1) {
  57. StringBuilder sb = new StringBuilder();
  58. for (String val : values) {
  59. sb.append(",").append(val);
  60. }
  61. params.put(name, sb.substring(1));
  62. } else {
  63. params.put(name, "");
  64. }
  65. }
  66. log.info("支付宝回调 {}", JSON.toJSONString(params, PrettyFormat));
  67. AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
  68. if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
  69. JSONObject body = JSON.parseObject(params.get("body"));
  70. String action = body.getString("action");
  71. switch (action) {
  72. case "payOrder": {
  73. Long orderId = body.getLong("orderId");
  74. orderService.notifyOrder(orderId, PayMethod.ALIPAY, MapUtils.getString(params, "trade_no"));
  75. break;
  76. }
  77. case "payGiftOrder": {
  78. Long orderId = body.getLong("orderId");
  79. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, MapUtils.getString(params, "trade_no"));
  80. break;
  81. }
  82. }
  83. return "success";
  84. }
  85. return "error";
  86. }
  87. @PostMapping(value = "/order/weixin", produces = "application/xml")
  88. @ResponseBody
  89. public String wxNotify(@RequestBody String xmlData) throws WxPayException {
  90. log.info("微信支付回调: {}", xmlData);
  91. final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
  92. notifyResult.checkResult(wxPayService, "MD5", true);
  93. JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
  94. String action = attach.getString("action");
  95. switch (action) {
  96. case "payOrder": {
  97. Long orderId = attach.getLong("orderId");
  98. orderService.notifyOrder(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  99. break;
  100. }
  101. case "payGiftOrder": {
  102. Long orderId = attach.getLong("orderId");
  103. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  104. break;
  105. }
  106. }
  107. return WxPayNotifyResponse.success("OK");
  108. }
  109. @PostMapping(value = "/order/iap")
  110. @ResponseBody
  111. public String iap(@RequestParam String receiptData, @RequestParam Long orderId) {
  112. String data = "{\"receipt-data\":\"" + receiptData + "\"}";
  113. String body = HttpRequest.post("https://buy.itunes.apple.com/verifyReceipt")
  114. .contentType("application/json")
  115. .send(data)
  116. .body();
  117. JSONObject jsonObject = JSON.parseObject(body);
  118. int status = jsonObject.getInteger("status");
  119. if (status == 21007) {
  120. jsonObject = JSON.parseObject(HttpRequest.post("https://sandbox.itunes.apple.com/verifyReceipt")
  121. .contentType("application/json")
  122. .send(data)
  123. .body());
  124. status = jsonObject.getInteger("status");
  125. }
  126. if (status == 0) {
  127. orderService.notifyOrder(orderId, PayMethod.WEIXIN, snowflakeIdWorker.nextId() + "");
  128. }
  129. return "ok";
  130. }
  131. // @PostMapping("/adapay/order/{orderId}")
  132. // @ResponseBody
  133. // public void adapayNotify(@PathVariable Long orderId, HttpServletRequest request) {
  134. // log.info("adapay notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  135. // try {
  136. // String data = request.getParameter("data");
  137. // String sign = request.getParameter("sign");
  138. // String type = request.getParameter("type");
  139. // if ("payment.succeeded".equals(type)) {
  140. // boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  141. // log.info("checkSign {}", checkSign);
  142. // if (checkSign) {
  143. // JSONObject jsonObject = JSON.parseObject(data);
  144. // String channel = jsonObject.getString("pay_channel");
  145. // String id = jsonObject.getString("id");
  146. //
  147. // rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  148. // new OrderNotifyEvent(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id, LocalDateTime.now()));
  149. //
  150. // orderService.notifyOrder(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
  151. // }
  152. // }
  153. // } catch (Exception e) {
  154. // e.printStackTrace();
  155. // }
  156. // }
  157. @PostMapping("/adapay/order/{orderId}")
  158. @ResponseBody
  159. public void adapayNotify(@PathVariable Long orderId, HttpServletRequest request) throws Exception {
  160. log.info("adapay notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  161. String data = request.getParameter("data");
  162. String sign = request.getParameter("sign");
  163. String type = request.getParameter("type");
  164. if ("payment.succeeded".equals(type)) {
  165. boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  166. log.info("checkSign {}", checkSign);
  167. if (checkSign) {
  168. JSONObject jsonObject = JSON.parseObject(data);
  169. String channel = jsonObject.getString("pay_channel");
  170. String id = jsonObject.getString("id");
  171. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  172. new OrderNotifyEvent(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id, System.currentTimeMillis()));
  173. }
  174. }
  175. }
  176. @PostMapping("/adapay/giftOrder/{orderId}")
  177. @ResponseBody
  178. public void adapayGiftNotify(@PathVariable Long orderId, HttpServletRequest request) {
  179. log.info("adapay gift notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  180. try {
  181. String data = request.getParameter("data");
  182. String sign = request.getParameter("sign");
  183. String type = request.getParameter("type");
  184. if ("payment.succeeded".equals(type)) {
  185. boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  186. log.info("checkSign {}", checkSign);
  187. if (checkSign) {
  188. JSONObject jsonObject = JSON.parseObject(data);
  189. String channel = jsonObject.getString("pay_channel");
  190. String id = jsonObject.getString("id");
  191. giftOrderService.giftNotify(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
  192. }
  193. }
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. }
  197. }
  198. }