OrderNotifyController.java 6.4 KB

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