<?php
/**
 * BookiFlex Payment Extension
 *
 * @package    BookiFlex
 * @subpackage Payment
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */


declare(strict_types=1);
namespace BookiFlex\Extensions\Payment\Service\Processor;

use BookiFlex\Core\Contract\Payment\PaymentInterface;
use BookiFlex\Core\Contract\Payment\PaymentProcessorInterface;
use Payum\Core\Payum;

defined( 'ABSPATH' ) || exit;

/**
 * Default processor: initiates payments through Payum security tokens.
 *
 * Wraps the original TokenFactory logic one-to-one. It is the catch-all
 * processor, so every gateway not claimed by another processor lands here.
 */
class PayumPaymentProcessor implements PaymentProcessorInterface
{
    public function __construct(
        private readonly Payum $payum,
    ) {
    }

    public function getName(): string
    {
        return 'payum';
    }

    public function supports(string $gatewayName): bool
    {
        // Catch-all: Payum handles any gateway not claimed by another processor.
        return true;
    }

    public function createCaptureUrl(PaymentInterface $payment, string $gatewayName): string
    {
        return $this->payum->getTokenFactory()->createCaptureToken(
            $gatewayName,
            $payment,
            rest_url('bflex/v1/payment/done')
        )->getTargetUrl();
    }

    public function createAuthorizeUrl(PaymentInterface $payment, string $gatewayName): string
    {
        return $this->payum->getTokenFactory()->createAuthorizeToken(
            $gatewayName,
            $payment,
            rest_url('bflex/v1/payment/done')
        )->getTargetUrl();
    }

    public function createCancelUrl(PaymentInterface $payment, string $gatewayName): string
    {
        return $this->payum->getTokenFactory()->createCancelToken(
            $gatewayName,
            $payment,
            rest_url('bflex/v1/payment/done')
        )->getTargetUrl();
    }
}