| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.izouma.nineth.web;
- import cn.com.sandpay.cashier.sdk.CertUtil;
- import cn.com.sandpay.cashier.sdk.CryptoUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.izouma.nineth.config.GeneralProperties;
- import com.izouma.nineth.enums.PayMethod;
- import com.izouma.nineth.event.OrderNotifyEvent;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.service.GiftOrderService;
- import com.izouma.nineth.service.MintOrderService;
- import com.izouma.nineth.service.OrderService;
- import com.izouma.nineth.service.SandPayService;
- import com.izouma.nineth.utils.SecurityUtils;
- import com.izouma.nineth.utils.SnowflakeIdWorker;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.codec.binary.Base64;
- import org.apache.rocketmq.spring.core.RocketMQTemplate;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.math.BigDecimal;
- import java.nio.charset.StandardCharsets;
- import java.time.LocalDateTime;
- @RestController
- @RequestMapping("/sandpay")
- @Slf4j
- @AllArgsConstructor
- public class SandPayController {
- private SandPayService sandPayService;
- private SnowflakeIdWorker snowflakeIdWorker;
- private GeneralProperties generalProperties;
- private RocketMQTemplate rocketMQTemplate;
- private GiftOrderService giftOrderService;
- private MintOrderService mintOrderService;
- @GetMapping(value = "/testUnion", produces = "text/html")
- public String testUnion() {
- JSONObject res = sandPayService.requestUnion(snowflakeIdWorker.nextId() + "", new BigDecimal("0.01"),
- "话费充值", "话费充值", 180, "", "https://test.raex.vip/9th/home");
- if (res.getJSONObject("head").getString("respCode").equals("000000")) {
- return res.getJSONObject("body").getString("credential");
- }
- throw new BusinessException("绿洲宇宙冷却系统已启动,请稍后支付。");
- }
- @PostMapping("/notify")
- public Object notifyOrder(HttpServletRequest req, HttpServletResponse resp) {
- String data = req.getParameter("data");
- String sign = req.getParameter("sign");
- // 验证签名
- boolean valid;
- try {
- valid = CryptoUtil.verifyDigitalSign(data.getBytes(StandardCharsets.UTF_8), Base64.decodeBase64(sign),
- CertUtil.getPublicKey(), "SHA1WithRSA");
- if (!valid) {
- log.error("verify sign fail.");
- log.error("签名字符串(data)为:" + data);
- log.error("签名值(sign)为:" + sign);
- } else {
- log.info("verify sign success");
- JSONObject dataJson = JSONObject.parseObject(data);
- if (dataJson != null) {
- log.info("通知业务数据为:" + JSONObject.toJSONString(dataJson, true));
- if ("000000".equals(dataJson.getJSONObject("head").getString("respCode"))) {
- JSONObject body = dataJson.getJSONObject("body");
- JSONObject extend = body.getJSONObject("extend");
- String type = extend.getString("type");
- Long id = extend.getLong("id");
- String payOrderCode = body.getString("payOrderCode");
- String orderCode = body.getString("orderCode");
- String bankserial = body.getString("bankserial");
- switch (type) {
- case "order":
- rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
- new OrderNotifyEvent(id, PayMethod.ALIPAY, payOrderCode, System.currentTimeMillis()));
- break;
- case "gift":
- giftOrderService.giftNotify(id, PayMethod.ALIPAY, payOrderCode);
- break;
- case "mintOrder":
- mintOrderService.mintNotify(id, PayMethod.ALIPAY, payOrderCode);
- }
- }
- return "respCode=000000";
- } else {
- log.error("通知数据异常!!!");
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
|