| 1 |
<?php |
| 2 |
namespace EasyInvoice; |
| 3 |
|
| 4 |
use EasyInvoice\Interfaces\PaymentGatewayInterface; |
| 5 |
|
| 6 |
class PaymentGatewayManager { |
| 7 |
/** |
| 8 |
* Registered payment gateways |
| 9 |
* |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
private $gateways = []; |
| 13 |
|
| 14 |
/** |
| 15 |
* Register a payment gateway |
| 16 |
* |
| 17 |
* @param PaymentGatewayInterface $gateway |
| 18 |
*/ |
| 19 |
public function registerGateway(PaymentGatewayInterface $gateway): void { |
| 20 |
$this->gateways[$gateway->getName()] = $gateway; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get all registered payment gateways |
| 25 |
* |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
public function getGateways(): array { |
| 29 |
return $this->gateways; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get gateway display name (custom or default) |
| 34 |
* |
| 35 |
* @param string $gateway_id |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function getGatewayDisplayName(string $gateway_id): string { |
| 39 |
// Check for custom display name first |
| 40 |
$custom_name = get_option('easy_invoice_gateway_display_name_' . $gateway_id, ''); |
| 41 |
if (!empty($custom_name)) { |
| 42 |
return $custom_name; |
| 43 |
} |
| 44 |
|
| 45 |
// Fall back to gateway's default title |
| 46 |
if (isset($this->gateways[$gateway_id])) { |
| 47 |
$gateway = $this->gateways[$gateway_id]; |
| 48 |
if (method_exists($gateway, 'getTitle')) { |
| 49 |
return $gateway->getTitle(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Final fallback |
| 54 |
return ucfirst(str_replace('_', ' ', $gateway_id)); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get enabled payment gateways |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function getEnabledGateways(): array { |
| 63 |
$enabled_gateways_unsorted = array_filter($this->gateways, function($gateway) { |
| 64 |
/** @var PaymentGatewayInterface $gateway */ |
| 65 |
return $gateway->isEnabled(); |
| 66 |
}); |
| 67 |
|
| 68 |
$gateway_order = get_option('easy_invoice_payment_gateway_order', array()); |
| 69 |
|
| 70 |
$sorted_gateways = array(); |
| 71 |
|
| 72 |
if (!empty($gateway_order)) { |
| 73 |
foreach ($gateway_order as $gateway_id) { |
| 74 |
if (isset($enabled_gateways_unsorted[$gateway_id])) { |
| 75 |
$sorted_gateways[$gateway_id] = $enabled_gateways_unsorted[$gateway_id]; |
| 76 |
unset($enabled_gateways_unsorted[$gateway_id]); // Remove from unsorted list |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Append any remaining enabled gateways that were not in the order array |
| 82 |
// (e.g., newly activated gateways not yet ordered) |
| 83 |
// These will be appended in their original registration order relative to each other. |
| 84 |
$sorted_gateways = array_merge($sorted_gateways, $enabled_gateways_unsorted); |
| 85 |
|
| 86 |
return $sorted_gateways; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get a specific payment gateway |
| 91 |
* |
| 92 |
* @param string $name |
| 93 |
* @return PaymentGatewayInterface|null |
| 94 |
*/ |
| 95 |
public function getGateway(string $name): ?PaymentGatewayInterface { |
| 96 |
return $this->gateways[$name] ?? null; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Initialize all payment gateways |
| 101 |
*/ |
| 102 |
public function initGateways(): void { |
| 103 |
foreach ($this->gateways as $gateway) { |
| 104 |
$gateway->init(); |
| 105 |
} |
| 106 |
} |
| 107 |
} |