PaymentResultManager.java 3.66 KB
package com.diligrp.cashier.trade.manager;

import com.diligrp.cashier.shared.service.ServiceEndpointSupport;
import com.diligrp.cashier.shared.service.ThreadPoolService;
import com.diligrp.cashier.shared.spi.IPaymentEventListener;
import com.diligrp.cashier.shared.util.JsonUtils;
import com.diligrp.cashier.shared.util.ObjectUtils;
import com.diligrp.cashier.trade.domain.OnlineRefundResult;
import com.diligrp.cashier.trade.domain.OnlinePaymentResult;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("paymentResultManager")
public class PaymentResultManager {

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

    @Resource
    private ObjectProvider<IPaymentEventListener> eventListeners;

    /**
     * 通知业务系统在线支付通道处理结果
     */
    public void notifyPaymentResult(String uri, OnlinePaymentResult payload) {
        ThreadPoolService.getIoThreadPoll().submit(() -> {
            List<IPaymentEventListener> lifeCycles = eventListeners.stream().toList();
            for (IPaymentEventListener listener : lifeCycles) {
                try {
                    listener.onEvent(payload);
                } catch (Exception ex) {
                    LOG.error("Failed to notify trade payment result", ex);
                }
            }
        });

        if (ObjectUtils.isEmpty(uri)) {
            return;
        }

        ThreadPoolService.getIoThreadPoll().submit(() -> {
            try {
                String body = JsonUtils.toJsonString(payload);
                LOG.info("Notifying online trade payment result: {}", body);
                ServiceEndpointSupport.HttpResult httpResult = new NotifyHttpClient(uri).send(body);
                if (httpResult.statusCode != 200) {
                    LOG.error("Failed to notify trade payment result");
                }
            } catch (Exception ex) {
                LOG.error("Failed to notify trade payment result", ex);
            }
        });
    }

    /**
     * 通知业务系统退款处理结果
     */
    public void notifyRefundResult(String uri, OnlineRefundResult payload) {
        ThreadPoolService.getIoThreadPoll().submit(() -> {
            List<IPaymentEventListener> lifeCycles = eventListeners.stream().toList();
            for (IPaymentEventListener listener : lifeCycles) {
                try {
                    listener.onEvent(payload);
                } catch (Exception ex) {
                    LOG.error("Failed to notify trade refund result", ex);
                }
            }
        });

        if (ObjectUtils.isEmpty(uri)) {
            return;
        }

        ThreadPoolService.getIoThreadPoll().submit(() -> {
            try {
                String body = JsonUtils.toJsonString(payload);
                LOG.info("Notifying online trade refund result: {}", body);
                ServiceEndpointSupport.HttpResult httpResult = new NotifyHttpClient(uri).send(body);
                if (httpResult.statusCode != 200) {
                    LOG.error("Failed to notify trade refund result");
                }
            } catch (Exception ex) {
                LOG.error("Failed to notify trade refund result", ex);
            }
        });
    }

    private static class NotifyHttpClient extends ServiceEndpointSupport {
        private final String baseUrl;

        public NotifyHttpClient(String baseUrl) {
            this.baseUrl =  baseUrl;
        }

        public HttpResult send(String body) {
            return send(baseUrl, body);
        }
    }
}