| 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\Addons\PaystackAddon; |
| 7 |
use FluentCart\App\Modules\PaymentMethods\PromoGateways\Addons\RazorpayAddon; |
| 8 |
use FluentCart\App\Modules\PaymentMethods\PromoGateways\Addons\MercadoPagoAddon; |
| 9 |
use FluentCart\App\Modules\PaymentMethods\PromoGateways\Addons\FlutterwaveAddon; |
| 10 |
|
| 11 |
class AddonGatewaysHandler |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Default addon gateways to register |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
protected $defaultGateways = [ |
| 18 |
'paystack' => PaystackAddon::class, |
| 19 |
'razorpay' => RazorpayAddon::class, |
| 20 |
'mercado_pago' => MercadoPagoAddon::class, |
| 21 |
'flutterwave' => FlutterwaveAddon::class, |
| 22 |
]; |
| 23 |
|
| 24 |
public function register() |
| 25 |
{ |
| 26 |
add_action('fluent_cart/register_payment_methods', [$this, 'registerPromoGateways'], 20); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Register addon gateways |
| 31 |
* Can be filtered to add or remove gateways |
| 32 |
*/ |
| 33 |
public function registerPromoGateways() |
| 34 |
{ |
| 35 |
// Allow filtering of addon gateways |
| 36 |
$gateways = apply_filters('fluent_cart/addon_gateways', $this->defaultGateways); |
| 37 |
|
| 38 |
foreach ($gateways as $slug => $addonClass) { |
| 39 |
// Skip if class doesn't exist |
| 40 |
if (!class_exists($addonClass)) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
|
| 44 |
$isGatewayRegistered = GatewayManager::has($slug); |
| 45 |
if (!$isGatewayRegistered) { |
| 46 |
$gateway = GatewayManager::getInstance(); |
| 47 |
try { |
| 48 |
$gateway->register($slug, new $addonClass()); |
| 49 |
} catch (\Exception $e) { |
| 50 |
// Log error but continue with other gateways |
| 51 |
error_log(sprintf( |
| 52 |
'Failed to register addon gateway %s: %s', |
| 53 |
$slug, |
| 54 |
$e->getMessage() |
| 55 |
)); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Add a new addon 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 an addon 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 |
|