AlipayNotifyController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.izouma.nineth.config.AlipayProperties;
  7. import com.izouma.nineth.config.GeneralProperties;
  8. import com.izouma.nineth.enums.PayMethod;
  9. import com.izouma.nineth.event.OrderNotifyEvent;
  10. import lombok.AllArgsConstructor;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.collections.MapUtils;
  13. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.ResponseBody;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import javax.servlet.http.HttpServletRequest;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.Set;
  21. @RestController
  22. @Slf4j
  23. @AllArgsConstructor
  24. public class AlipayNotifyController {
  25. private AlipayProperties alipayProperties;
  26. private GeneralProperties generalProperties;
  27. private RocketMQTemplate rocketMQTemplate;
  28. @PostMapping("/alipay/notify")
  29. @ResponseBody
  30. public String notify(HttpServletRequest request) throws AlipayApiException {
  31. Map<String, String> params = new HashMap<>();
  32. Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
  33. for (Map.Entry<String, String[]> entry : entrySet) {
  34. String name = entry.getKey();
  35. String[] values = entry.getValue();
  36. int valLen = values.length;
  37. if (valLen == 1) {
  38. params.put(name, values[0]);
  39. } else if (valLen > 1) {
  40. StringBuilder sb = new StringBuilder();
  41. for (String val : values) {
  42. sb.append(",").append(val);
  43. }
  44. params.put(name, sb.toString().substring(1));
  45. } else {
  46. params.put(name, "");
  47. }
  48. }
  49. log.info("支付宝回调 {}", JSON.toJSONString(params, true));
  50. AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
  51. if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
  52. JSONObject body = JSON.parseObject(params.get("body"));
  53. String type = body.getString("type");
  54. Long orderId = body.getLong("orderId");
  55. String tradeNo = params.get("trade_no");
  56. switch (type) {
  57. case "order":
  58. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  59. new OrderNotifyEvent(orderId, PayMethod.ALIPAY,
  60. tradeNo, System.currentTimeMillis()));
  61. break;
  62. case "gift":
  63. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  64. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  65. System.currentTimeMillis(), OrderNotifyEvent.TYPE_GIFT_ORDER));
  66. break;
  67. case "mintOrder":
  68. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  69. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  70. System.currentTimeMillis(), OrderNotifyEvent.TYPE_MINT_ORDER));
  71. break;
  72. case "recharge":
  73. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  74. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  75. System.currentTimeMillis(), OrderNotifyEvent.TYPE_RECHARGE));
  76. break;
  77. case "picOrder":
  78. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  79. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  80. System.currentTimeMillis(), OrderNotifyEvent.TYPE_PIC));
  81. break;
  82. case "domain":
  83. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  84. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  85. System.currentTimeMillis(), OrderNotifyEvent.DOMAIN));
  86. break;
  87. case "ask":
  88. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  89. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  90. System.currentTimeMillis(), OrderNotifyEvent.TYPE_ASK));
  91. break;
  92. case "auctionOrder":
  93. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  94. new OrderNotifyEvent(orderId, PayMethod.ALIPAY, tradeNo,
  95. System.currentTimeMillis(), OrderNotifyEvent.TYPE_AUCTION_ORDER));
  96. break;
  97. }
  98. }
  99. return "success";
  100. }
  101. }