| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager; |
| 6 |
use FluentCart\App\Modules\PaymentMethods\PromoGateways\Pro\AuthorizeNetPromo; |
| 7 |
use FluentCart\App\Modules\PaymentMethods\PromoGateways\Pro\MolliePromo; |
| 8 |
use FluentCart\App\Modules\PaymentMethods\PromoGateways\Pro\PaddlePromo; |
| 9 |
|
| 10 |
class PromoGatewaysHandler |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Default promo gateways to register |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
protected $defaultGateways = [ |
| 17 |
'paddle' => PaddlePromo::class, |
| 18 |
'mollie' => MolliePromo::class, |
| 19 |
'authorize_dot_net' => AuthorizeNetPromo::class, |
| 20 |
]; |
| 21 |
|
| 22 |
public function register() |
| 23 |
{ |
| 24 |
add_action('fluent_cart/register_payment_methods', [$this, 'registerPromoGateways'], 20); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register promo gateways |
| 29 |
* Only registers if Fluent Cart Pro is not active |
| 30 |
*/ |
| 31 |
public function registerPromoGateways() |
| 32 |
{ |
| 33 |
$isProActive = defined('FLUENTCART_PRO_PLUGIN_VERSION'); |
| 34 |
|
| 35 |
// Allow filtering of promo gateways |
| 36 |
$gateways = apply_filters('fluent_cart/promo_gateways', $this->defaultGateways); |
| 37 |
|
| 38 |
foreach ($gateways as $slug => $promoClass) { |
| 39 |
// Skip if class doesn't exist |
| 40 |
if (!class_exists($promoClass)) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
|
| 44 |
$isGatewayRegistered = GatewayManager::has($slug); |
| 45 |
if (!$isGatewayRegistered && !$isProActive) { |
| 46 |
$gateway = GatewayManager::getInstance(); |
| 47 |
try { |
| 48 |
$gateway->register($slug, new $promoClass()); |
| 49 |
} catch (\Exception $e) { |
| 50 |
// Log error but continue with other gateways |
| 51 |
error_log(sprintf( |
| 52 |
'Failed to register promo gateway %s: %s', |
| 53 |
$slug, |
| 54 |
$e->getMessage() |
| 55 |
)); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Add a new promo gateway to the default list |
| 63 |
* |
| 64 |
* @param string $slug |
| 65 |
* @param string $className |
| 66 |
*/ |
| 67 |
public function addGateway($slug, $className) |
| 68 |
{ |
| 69 |
$this->defaultGateways[$slug] = $className; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Remove a promo gateway from the default list |
| 74 |
* |
| 75 |
* @param string $slug |
| 76 |
*/ |
| 77 |
public function removeGateway($slug) |
| 78 |
{ |
| 79 |
unset($this->defaultGateways[$slug]); |
| 80 |
} |
| 81 |
} |
| 82 |
|