SandPayController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.izouma.nineth.web;
  2. import cn.com.sandpay.cashier.sdk.CertUtil;
  3. import cn.com.sandpay.cashier.sdk.CryptoUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.izouma.nineth.config.GeneralProperties;
  6. import com.izouma.nineth.enums.PayMethod;
  7. import com.izouma.nineth.event.OrderNotifyEvent;
  8. import lombok.AllArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.commons.codec.binary.Base64;
  11. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.nio.charset.StandardCharsets;
  18. @RestController
  19. @RequestMapping("/sandpay")
  20. @Slf4j
  21. @AllArgsConstructor
  22. public class SandPayController {
  23. private GeneralProperties generalProperties;
  24. private RocketMQTemplate rocketMQTemplate;
  25. @PostMapping("/notify")
  26. public Object notifyOrder(HttpServletRequest req, HttpServletResponse resp) {
  27. String data = req.getParameter("data");
  28. String sign = req.getParameter("sign");
  29. // 验证签名
  30. boolean valid;
  31. try {
  32. valid = CryptoUtil.verifyDigitalSign(data.getBytes(StandardCharsets.UTF_8), Base64.decodeBase64(sign),
  33. CertUtil.getPublicKey(), "SHA1WithRSA");
  34. if (!valid) {
  35. log.error("verify sign fail.");
  36. log.error("签名字符串(data)为:" + data);
  37. log.error("签名值(sign)为:" + sign);
  38. } else {
  39. log.info("verify sign success");
  40. JSONObject dataJson = JSONObject.parseObject(data);
  41. if (dataJson != null) {
  42. log.info("通知业务数据为:" + JSONObject.toJSONString(dataJson, true));
  43. if ("000000".equals(dataJson.getJSONObject("head").getString("respCode"))) {
  44. JSONObject body = dataJson.getJSONObject("body");
  45. JSONObject extend = body.getJSONObject("extend");
  46. String type = extend.getString("type");
  47. Long id = extend.getLong("id");
  48. String payOrderCode = body.getString("payOrderCode");
  49. String orderCode = body.getString("orderCode");
  50. String bankserial = body.getString("bankserial");
  51. switch (type) {
  52. case "order":
  53. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  54. new OrderNotifyEvent(id, PayMethod.SANDPAY,
  55. payOrderCode, System.currentTimeMillis()));
  56. break;
  57. case "gift":
  58. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  59. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  60. System.currentTimeMillis(), OrderNotifyEvent.TYPE_GIFT_ORDER));
  61. break;
  62. case "mintOrder":
  63. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  64. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  65. System.currentTimeMillis(), OrderNotifyEvent.TYPE_MINT_ORDER));
  66. break;
  67. case "recharge":
  68. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  69. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  70. System.currentTimeMillis(), OrderNotifyEvent.TYPE_RECHARGE));
  71. break;
  72. case "auctionOrder":
  73. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  74. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  75. System.currentTimeMillis(), OrderNotifyEvent.TYPE_AUCTION_ORDER));
  76. case "picOrder":
  77. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  78. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  79. System.currentTimeMillis(), OrderNotifyEvent.TYPE_PIC));
  80. break;
  81. case "domain":
  82. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  83. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  84. System.currentTimeMillis(), OrderNotifyEvent.DOMAIN));
  85. break;
  86. case "ask":
  87. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  88. new OrderNotifyEvent(id, PayMethod.SANDPAY, payOrderCode,
  89. System.currentTimeMillis(), OrderNotifyEvent.TYPE_ASK));
  90. break;
  91. }
  92. }
  93. return "respCode=000000";
  94. } else {
  95. log.error("通知数据异常!!!");
  96. }
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. return null;
  102. }
  103. }