| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\Core; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
use FluentCart\Framework\Support\Collection; |
| 8 |
|
| 9 |
/** |
| 10 |
* Payment Gateway Manager |
| 11 |
* |
| 12 |
* Manages payment gateway instances using the Singleton pattern. |
| 13 |
* Supports both traditional and direct gateway access patterns: |
| 14 |
* |
| 15 |
* Traditional: GatewayManager::getInstance()->get('stripe') |
| 16 |
* Direct: GatewayManager::getInstance('stripe') |
| 17 |
* Static: GatewayManager::gateway('stripe') |
| 18 |
*/ |
| 19 |
class GatewayManager |
| 20 |
{ |
| 21 |
protected static ?GatewayManager $instance = null; |
| 22 |
protected array $gateways = []; |
| 23 |
public static ?StoreSettings $storeSettings = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the singleton instance or a specific gateway |
| 27 |
* |
| 28 |
* @param string|null $gatewayName Optional gateway name to retrieve directly |
| 29 |
* @return GatewayManager|PaymentGatewayInterface|null |
| 30 |
*/ |
| 31 |
public static function getInstance(?string $gatewayName = null) |
| 32 |
{ |
| 33 |
if (!self::$instance) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
// If gateway name is provided, return the specific gateway |
| 38 |
if ($gatewayName !== null) { |
| 39 |
return self::$instance->get($gatewayName); |
| 40 |
} |
| 41 |
|
| 42 |
// Otherwise return the manager instance |
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
public static function storeSettings(): StoreSettings |
| 46 |
{ |
| 47 |
if (!self::$storeSettings) { |
| 48 |
self::$storeSettings = new StoreSettings(); |
| 49 |
} |
| 50 |
return self::$storeSettings; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Static convenience method to get a gateway directly |
| 55 |
* |
| 56 |
* @param string $gatewayName |
| 57 |
* @return PaymentGatewayInterface|null |
| 58 |
*/ |
| 59 |
public static function gateway(string $gatewayName): ?PaymentGatewayInterface |
| 60 |
{ |
| 61 |
return self::getInstance($gatewayName); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if a gateway is registered |
| 66 |
* |
| 67 |
* @param string $gatewayName |
| 68 |
* @return bool |
| 69 |
*/ |
| 70 |
public static function has($gatewayName): bool |
| 71 |
{ |
| 72 |
if (!$gatewayName) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
return self::getInstance()->get($gatewayName) !== null; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register a payment gateway |
| 80 |
* |
| 81 |
* @param string $name Gateway identifier |
| 82 |
* @param PaymentGatewayInterface $gateway Gateway instance |
| 83 |
*/ |
| 84 |
public function register(string $name, PaymentGatewayInterface $gateway) |
| 85 |
{ |
| 86 |
// Call boot method to allow each gateway to hook AJAX/IPN/webhooks |
| 87 |
if (method_exists($gateway, 'boot')) { |
| 88 |
$gateway->boot(); |
| 89 |
} |
| 90 |
|
| 91 |
if (method_exists($gateway, 'setStoreSettings')) { |
| 92 |
$gateway->setStoreSettings(self::storeSettings()); |
| 93 |
} |
| 94 |
|
| 95 |
$this->gateways[$name] = $gateway; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get a specific payment gateway |
| 100 |
* |
| 101 |
* @param string $name Gateway identifier |
| 102 |
* @return PaymentGatewayInterface|null |
| 103 |
*/ |
| 104 |
public function get(string $name): ?PaymentGatewayInterface |
| 105 |
{ |
| 106 |
return $this->gateways[$name] ?? null; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get all registered gateways |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function all(): array |
| 115 |
{ |
| 116 |
return $this->gateways; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get all enabled gateways |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function enabled(): array |
| 125 |
{ |
| 126 |
return array_filter($this->gateways, function($gateway) { |
| 127 |
return method_exists($gateway, 'isEnabled') ? $gateway->isEnabled() : true; |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get gateway names |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function names(): array |
| 137 |
{ |
| 138 |
return array_keys($this->gateways); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Remove a gateway |
| 143 |
* |
| 144 |
* @param string $name Gateway identifier |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function remove(string $name): bool |
| 148 |
{ |
| 149 |
if (isset($this->gateways[$name])) { |
| 150 |
unset($this->gateways[$name]); |
| 151 |
return true; |
| 152 |
} |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
public function getAllMeta(): array |
| 157 |
{ |
| 158 |
$meta = []; |
| 159 |
$requiredKeys = [ |
| 160 |
'brand_color', 'description', 'icon', 'logo', |
| 161 |
'route', 'status', 'title' |
| 162 |
]; |
| 163 |
|
| 164 |
foreach ($this->gateways as $key => $gateway) { |
| 165 |
$data = $gateway->getMeta(); |
| 166 |
foreach ($requiredKeys as $rKey) { |
| 167 |
if (!array_key_exists($rKey, $data)) { |
| 168 |
throw new \Exception( |
| 169 |
sprintf( |
| 170 |
/* translators: %1$s is the required meta key, %2$s is the gateway identifier */ |
| 171 |
esc_html__('Missing required meta key "%1$s" for gateway: %2$s', 'fluent-cart'), |
| 172 |
esc_html($rKey), |
| 173 |
esc_html($key) |
| 174 |
) |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
$meta[] = $data; |
| 180 |
} |
| 181 |
|
| 182 |
return $meta; |
| 183 |
} |
| 184 |
|
| 185 |
public function getRoutes() |
| 186 |
{ |
| 187 |
$routes = []; |
| 188 |
foreach ($this->gateways as $key => $gateway) { |
| 189 |
$routes[] = [ |
| 190 |
'path' => $gateway->getMeta('route'), |
| 191 |
'name' => $gateway->getMeta('route'), |
| 192 |
'upcoming' => $gateway->getMeta('upcoming'), |
| 193 |
'meta' => [ |
| 194 |
'title' => $gateway->getMeta('title'), |
| 195 |
'label' => $gateway->getMeta('label'), |
| 196 |
'admin_title' => $gateway->getMeta('admin_title') |
| 197 |
] |
| 198 |
]; |
| 199 |
} |
| 200 |
return $routes; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
} |
| 205 |
|