PayMethodSwitchController.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.ruoyi.app.pay;
  2. import com.ruoyi.app.pay.dto.PayMethodSwitchRequest;
  3. import com.ruoyi.common.annotation.Anonymous;
  4. import com.ruoyi.common.core.domain.AjaxResult;
  5. import com.ruoyi.common.exception.ServiceException;
  6. import com.ruoyi.common.utils.MessageUtils;
  7. import com.ruoyi.system.utils.Auth;
  8. import com.ruoyi.system.utils.JwtUtil;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestHeader;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. /**
  17. * 待支付订单更换支付方式入口(#661,2026-09-22)。
  18. *
  19. * <p>只服务「换方式」场景:原方式重试仍走 /pay/omg/retry 与 /pay/line/create,行为不变。
  20. * 业务校验在 {@link PayMethodSwitchService},本控制器只做入参兜底与异常翻译。
  21. */
  22. @RestController
  23. @RequestMapping("/pay")
  24. public class PayMethodSwitchController {
  25. private static final Logger logger = LoggerFactory.getLogger(PayMethodSwitchController.class);
  26. private final PayMethodSwitchService switchService;
  27. public PayMethodSwitchController(PayMethodSwitchService switchService) {
  28. this.switchService = switchService;
  29. }
  30. /** 用户对待支付订单更换支付方式:回写 payType 并按新方式拉起支付(到付/现金直接成功)。 */
  31. @Anonymous
  32. @Auth
  33. @PostMapping("/switch")
  34. public AjaxResult switchMethod(@RequestHeader String token,
  35. @RequestBody(required = false) PayMethodSwitchRequest request) {
  36. if (request == null || request.getOrderId() == null || request.getOrderId().trim().isEmpty()) {
  37. return AjaxResult.error(MessageUtils.message("no.pay.switch.order.invalid"));
  38. }
  39. try {
  40. Long userId = Long.valueOf(new JwtUtil().getusid(token));
  41. return switchService.switchMethod(userId, request);
  42. } catch (ServiceException exception) {
  43. // 业务拒绝(订单不可支付/方式非法/闸门限制)携带国际化文案原样返回
  44. return AjaxResult.error(exception.getMessage());
  45. } catch (com.ruoyi.app.omgpay.OmgPaymentBusinessException exception) {
  46. // 渠道业务拒绝(如已有激活尝试)按 OMG 既有约定翻译,不再落通用文案误导排障
  47. logger.warn("pay switch rejected by channel orderId={}, code={}",
  48. request == null ? null : request.getOrderId(), exception.getCode());
  49. return AjaxResult.error(MessageUtils.message(exception.getMessageKey()),
  50. new com.ruoyi.app.omgpay.dto.OmgPaymentErrorResponse(exception.getCode().name()));
  51. } catch (Exception exception) {
  52. // 兜底前必须留痕:真实异常(网关调用/NPE/SQL)只有这里能看到,不打日志等于盲修
  53. logger.error("pay switch failed orderId={}",
  54. request == null ? null : request.getOrderId(), exception);
  55. return AjaxResult.error(MessageUtils.message("no.pay.switch.failed"));
  56. }
  57. }
  58. }