SandPayController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.izouma.nineth.config.GeneralProperties;
  7. import com.izouma.nineth.enums.PayMethod;
  8. import com.izouma.nineth.event.OrderNotifyEvent;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import com.izouma.nineth.service.GiftOrderService;
  11. import com.izouma.nineth.service.MintOrderService;
  12. import com.izouma.nineth.service.OrderService;
  13. import com.izouma.nineth.service.SandPayService;
  14. import com.izouma.nineth.utils.SecurityUtils;
  15. import com.izouma.nineth.utils.SnowflakeIdWorker;
  16. import lombok.AllArgsConstructor;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.codec.binary.Base64;
  19. import org.apache.rocketmq.spring.core.RocketMQTemplate;
  20. import org.springframework.security.access.prepost.PreAuthorize;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.math.BigDecimal;
  25. import java.nio.charset.StandardCharsets;
  26. import java.time.LocalDateTime;
  27. @RestController
  28. @RequestMapping("/sandpay")
  29. @Slf4j
  30. @AllArgsConstructor
  31. public class SandPayController {
  32. private SandPayService sandPayService;
  33. private SnowflakeIdWorker snowflakeIdWorker;
  34. private GeneralProperties generalProperties;
  35. private RocketMQTemplate rocketMQTemplate;
  36. private GiftOrderService giftOrderService;
  37. private MintOrderService mintOrderService;
  38. @GetMapping(value = "/testUnion", produces = "text/html")
  39. public String testUnion() {
  40. JSONObject res = sandPayService.requestUnion(snowflakeIdWorker.nextId() + "", new BigDecimal("0.01"),
  41. "话费充值", "话费充值", 180, "", "https://test.raex.vip/9th/home");
  42. if (res.getJSONObject("head").getString("respCode").equals("000000")) {
  43. return res.getJSONObject("body").getString("credential");
  44. }
  45. throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付。");
  46. }
  47. @PostMapping("/notify")
  48. public Object notifyOrder(HttpServletRequest req, HttpServletResponse resp) {
  49. String data = req.getParameter("data");
  50. String sign = req.getParameter("sign");
  51. // 验证签名
  52. boolean valid;
  53. try {
  54. valid = CryptoUtil.verifyDigitalSign(data.getBytes(StandardCharsets.UTF_8), Base64.decodeBase64(sign),
  55. CertUtil.getPublicKey(), "SHA1WithRSA");
  56. if (!valid) {
  57. log.error("verify sign fail.");
  58. log.error("签名字符串(data)为:" + data);
  59. log.error("签名值(sign)为:" + sign);
  60. } else {
  61. log.info("verify sign success");
  62. JSONObject dataJson = JSONObject.parseObject(data);
  63. if (dataJson != null) {
  64. log.info("通知业务数据为:" + JSONObject.toJSONString(dataJson, true));
  65. if ("000000".equals(dataJson.getJSONObject("head").getString("respCode"))) {
  66. JSONObject body = dataJson.getJSONObject("body");
  67. JSONObject extend = body.getJSONObject("extend");
  68. String type = extend.getString("type");
  69. Long id = extend.getLong("id");
  70. String payOrderCode = body.getString("payOrderCode");
  71. String orderCode = body.getString("orderCode");
  72. String bankserial = body.getString("bankserial");
  73. switch (type) {
  74. case "order":
  75. rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
  76. new OrderNotifyEvent(id, PayMethod.ALIPAY, payOrderCode, System.currentTimeMillis()));
  77. break;
  78. case "gift":
  79. giftOrderService.giftNotify(id, PayMethod.ALIPAY, payOrderCode);
  80. break;
  81. case "mintOrder":
  82. mintOrderService.mintNotify(id, PayMethod.ALIPAY, payOrderCode);
  83. }
  84. }
  85. return "respCode=000000";
  86. } else {
  87. log.error("通知数据异常!!!");
  88. }
  89. }
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }
  93. return null;
  94. }
  95. }