HmPayController.java 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.izouma.nineth.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alipay.api.AlipayApiException;
  4. import com.alipay.api.internal.util.AlipaySignature;
  5. import com.izouma.nineth.config.GeneralProperties;
  6. import com.izouma.nineth.config.HmPayProperties;
  7. import com.izouma.nineth.enums.PayMethod;
  8. import com.izouma.nineth.event.OrderNotifyEvent;
  9. import lombok.AllArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  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.stream.Collectors;
  21. @RestController
  22. @RequestMapping("/hmpay")
  23. @Slf4j
  24. @AllArgsConstructor
  25. public class HmPayController extends BaseController {
  26. private final HmPayProperties hmPayProperties;
  27. private final RocketMQTemplate rocketMQTemplate;
  28. private final GeneralProperties generalProperties;
  29. @GetMapping("/notify/{type}/{id}")
  30. public String orderNotify(@PathVariable String type, @PathVariable Long id, HttpServletRequest req) throws AlipayApiException {
  31. log.info("回调type={}, orderId={}, 参数={}", type, id, JSON.toJSONString(req.getParameterMap(), true));
  32. Map<String, String> params = new HashMap<>();
  33. req.getParameterMap().forEach((k, v) -> {
  34. if (v != null && v.length > 0 && StringUtils.isNotEmpty(v[0])) {
  35. params.put(k, v[0]);
  36. }
  37. });
  38. String sign = params.get("sign");
  39. params.remove("sign");
  40. String signStr = params.entrySet().stream().sorted(Map.Entry.comparingByKey())
  41. .map(e -> e.getKey() + "=" + e.getValue())
  42. .collect(Collectors.joining("&"));
  43. boolean verify = AlipaySignature.verify(signStr, sign, hmPayProperties.getHmPublicKey(), "UTF-8", "RSA");
  44. log.info("签名校验: {}", verify);
  45. if ("SUCCESS".equals(params.get("trade_status"))) {
  46. String plat_trx_no = params.get("plat_trx_no");
  47. switch (type) {
  48. case "order":
  49. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  50. new OrderNotifyEvent(id, PayMethod.HMPAY, plat_trx_no, System.currentTimeMillis()));
  51. break;
  52. case "gift":
  53. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  54. new OrderNotifyEvent(id, PayMethod.SANDPAY, plat_trx_no,
  55. System.currentTimeMillis(), OrderNotifyEvent.TYPE_GIFT_ORDER));
  56. break;
  57. case "mintOrder":
  58. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  59. new OrderNotifyEvent(id, PayMethod.SANDPAY, plat_trx_no,
  60. System.currentTimeMillis(), OrderNotifyEvent.TYPE_MINT_ORDER));
  61. break;
  62. case "recharge":
  63. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  64. new OrderNotifyEvent(id, PayMethod.SANDPAY, plat_trx_no,
  65. System.currentTimeMillis(), OrderNotifyEvent.TYPE_RECHARGE));
  66. break;
  67. case "auctionOrder":
  68. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  69. new OrderNotifyEvent(id, PayMethod.SANDPAY, plat_trx_no,
  70. System.currentTimeMillis(), OrderNotifyEvent.TYPE_AUCTION_ORDER));
  71. break;
  72. }
  73. }
  74. return "SUCCESS";
  75. }
  76. }