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