package org.dromara.pay.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.pay.event.PaySuccessEvent;
import org.dromara.pay.service.PayNotifyService;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 支付订单事件监听
 *
 * @author pengles
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class PayOrderListener {

    private final PayNotifyService payNotifyService;

    /**
     * 监听支付成功事件，创建回调任务
     */
    @Async
    @EventListener
    public void onPaySuccess(PaySuccessEvent event) {
        log.info("监听到支付成功事件: orderNo={}", event.getOrder().getNo());
        try {
            payNotifyService.createTask(event.getOrder());
        } catch (Exception e) {
            log.error("创建支付回调任务失败", e);
        }
    }
}
