CashierDeskController.java 4.98 KB
package com.diligrp.cashier.boss.controller;

import com.diligrp.cashier.boss.domain.CashierOrderDTO;
import com.diligrp.cashier.boss.domain.CashierPaymentUrl;
import com.diligrp.cashier.boss.domain.ZrPaymentResult;
import com.diligrp.cashier.boss.service.ICashierDeskService;
import com.diligrp.cashier.boss.service.IMerchantService;
import com.diligrp.cashier.boss.util.CashierOrderConverter;
import com.diligrp.cashier.mall.service.biz.PayNotifyService;
import com.diligrp.cashier.pipeline.domain.OnlinePaymentStatus;
import com.diligrp.cashier.pipeline.type.PaymentState;
import com.diligrp.cashier.shared.domain.Message;
import com.diligrp.cashier.shared.util.AssertUtils;
import com.diligrp.cashier.trade.domain.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/payment/cashier")
public class CashierDeskController {

    @Resource
    private IMerchantService merchantService;

    @Resource
    private ICashierDeskService cashierDeskService;

    @Resource
    private PayNotifyService payNotifyService;

    @RequestMapping("/submitOrder")
    public Message<?> submitOrder(@RequestBody CashierOrderDTO request) {
        // 基本参数校验
        AssertUtils.notNull(request.getMchId(), "mchId missed");
        AssertUtils.notEmpty(request.getUserId(), "userId missed");
        AssertUtils.notNull(request.getCashierType(), "cashierType missed");
        AssertUtils.notEmpty(request.getGoods(), "goods missed");
        AssertUtils.notNull(request.getAmount(), "amount missed");
        AssertUtils.isTrue(request.getAmount() > 0, "Invalid amount");
        AssertUtils.notEmpty(request.getOutTradeNo(), "outTradeNo missed");

        CashierOrder cashierOrder = CashierOrderConverter.INSTANCE.convert(request);
        Merchant merchant = merchantService.loadCashierMerchant(request.getMchId());
        CashierPaymentUrl paymentUrl = cashierDeskService.doSubmit(merchant, cashierOrder);
        return Message.success(paymentUrl);
    }

    @RequestMapping("/orderInfo")
    public Message<?> orderInfo(@RequestParam("token") String token) {
        return Message.success(cashierDeskService.getCashierOrderByToken(token));
    }

    @RequestMapping("/orderPayment")
    public Message<?> orderPayment(@RequestBody CashierPayment request) {
        // 基本参数校验
        AssertUtils.notEmpty(request.getTradeId(), "tradeId missed");
        AssertUtils.notNull(request.getPipelineId(), "pipelineId missed");

        OnlinePaymentStatus paymentStatus = cashierDeskService.doPayment(request);
        return Message.success(paymentStatus);
    }

    @RequestMapping("/orderClose")
    public Message<?> orderClose(@RequestParam("tradeId") String tradeId) {
        cashierDeskService.closeTradeOrder(tradeId);
        return Message.success();
    }

    @RequestMapping(value = "/paymentState")
    public Message<?> paymentState(@RequestParam("paymentId") String paymentId,
                                   @RequestParam(name = "mode", required = false) String mode) {

        OnlinePaymentResult response = cashierDeskService.queryPaymentState(paymentId, mode);
        return Message.success(response);
    }

    /**
     * 中瑞对接大润发查支付状态接口
     * 大润发支付成功页面验签功能会限制有效期3分钟, 只能在查询状态时返回大润发成功页面(生成签名), 否则有超时风险
     * 为了保证收银台后端的统一性, 新增单独接口查询状态
     */
    @RequestMapping(value = "/zrPaymentState")
    public Message<?> zrPaymentState(@RequestParam("paymentId") String paymentId,
                                   @RequestParam(name = "mode", required = false) String mode) {

        OnlinePaymentResult response = cashierDeskService.queryPaymentState(paymentId, mode);
        String redirectUrl = PaymentState.SUCCESS.equalTo(response.getState()) ?
            payNotifyService.redirectUrl(response.getTradeId()) : null;

        return Message.success(new ZrPaymentResult(response, redirectUrl));
    }

    @RequestMapping(value = "/orderRefund")
    public Message<?> orderRefund(@RequestBody OnlineRefundDTO request) {
        AssertUtils.notEmpty(request.getTradeId(), "tradeId missed");
        AssertUtils.notNull(request.getAmount(), "amount missed");
        AssertUtils.isTrue(request.getAmount() > 0, "Invalid amount");

        OnlineRefundResult response = cashierDeskService.sendRefundRequest(request);
        return Message.success(response);
    }

    @RequestMapping(value = "/refundState")
    public Message<?> refundState(@RequestParam("refundId") String refundId,
                                  @RequestParam(name = "mode", required = false) String mode) {

        OnlineRefundResult response = cashierDeskService.queryRefundState(refundId, mode);
        return Message.success(response);
    }
}