RcbPaymentController.java 4.32 KB
package com.diligrp.cashier.boss.controller;

import com.diligrp.cashier.boss.exception.BossServiceException;
import com.diligrp.cashier.pipeline.core.RcbOnlinePipeline;
import com.diligrp.cashier.pipeline.domain.OnlinePaymentResponse;
import com.diligrp.cashier.pipeline.service.IPaymentPipelineManager;
import com.diligrp.cashier.pipeline.type.OutPaymentType;
import com.diligrp.cashier.pipeline.type.PaymentState;
import com.diligrp.cashier.pipeline.util.RcbSignatureUtils;
import com.diligrp.cashier.pipeline.util.RcbStateUtils;
import com.diligrp.cashier.shared.ErrorCode;
import com.diligrp.cashier.shared.util.JsonUtils;
import com.diligrp.cashier.shared.util.ObjectUtils;
import com.diligrp.cashier.trade.model.OnlinePayment;
import com.diligrp.cashier.trade.service.ICashierPaymentService;
import com.diligrp.cashier.trade.service.ITradeAssistantService;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;

@RestController
@RequestMapping(value = "/rcb")
public class RcbPaymentController {

    private static final Logger LOG = LoggerFactory.getLogger(RcbPaymentController.class);

    @Resource
    private ITradeAssistantService tradeAssistantService;

    @Resource
    private ICashierPaymentService cashierPaymentService;

    @Resource
    private IPaymentPipelineManager paymentPipelineManager;

    /**
     * 支付结果通知
     */
    @RequestMapping(value = "/payment/{paymentId}/notify.do")
    public ResponseEntity<?> paymentNotify(HttpServletRequest request, @PathVariable("paymentId") String paymentId) {
        String payload = request.getParameter("order");
        LOG.info("Receiving rcb payment pipeline result: {}\n{}", paymentId, payload);

        try {
            LocalDateTime when = LocalDateTime.now();
            Map<String, String> params = JsonUtils.fromJsonString(payload, new TypeReference<>(){});
            String sign = params.remove("sign");
            String source = RcbSignatureUtils.map2String(params);

            OnlinePayment payment = tradeAssistantService.findByPaymentId(paymentId);
            RcbOnlinePipeline pipeline = paymentPipelineManager.findPipelineById(payment.getPipelineId(), RcbOnlinePipeline.class);
            RcbOnlinePipeline.RcbParams config = pipeline.params();
            if (!RcbSignatureUtils.verify(source, config.getKey(), sign)) {
                LOG.error("Rcb pipeline data sign verify failed");
                throw new BossServiceException(ErrorCode.UNAUTHORIZED_ACCESS_ERROR, "Data sign verify failed");
            }

//            String paymentId = params.get("outTradeNo");
            PaymentState paymentState = RcbStateUtils.paymentState(params.get("orderStatus"));
            String outTradeNo = params.get("cposOrderId");
            String paidTime = params.get("paidTime");
            if (ObjectUtils.isNotEmpty(paidTime)) {
                long timestamp = Long.parseLong(paidTime); // ⽀付完成时间戳
                Instant instant = Instant.ofEpochMilli(timestamp);
                when = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            }
            OutPaymentType outPayType = RcbStateUtils.outPayType(params.get("tradeChannel"));

            String payerId = params.get("payUserInfo");
            String errorDesc = params.get("errorDesc");
            // String outOrderId = params.get("chnOrderId"); // 第三方支付通道的订单号

            OnlinePaymentResponse paymentResponse = new OnlinePaymentResponse(paymentId, outTradeNo, outPayType,
                payerId, when, paymentState, errorDesc);
            cashierPaymentService.notifyPaymentResponse(paymentResponse);
            return ResponseEntity.ok("SUCCESS");
        } catch (Exception ex) {
            LOG.error("Process rcb payment result exception", ex);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED");
        }
    }
}