CashierDeskController.java 4 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.service.ICashierDeskService;
import com.diligrp.cashier.boss.service.IMerchantService;
import com.diligrp.cashier.boss.util.CashierOrderConverter;
import com.diligrp.cashier.pipeline.domain.OnlinePaymentStatus;
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;

    @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");
        AssertUtils.notEmpty(request.getRedirectUrl(), "redirectUrl 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<?> orderPayment(@RequestParam("paymentId") String paymentId) {
        cashierDeskService.closePrepayOrder(paymentId);
        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);
    }

    @RequestMapping(value = "/orderRefund.do")
    public Message<?> requestRefund(@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);
    }
}