| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\PaymentGateways; |
| 4 |
|
| 5 |
use Give\Container\Container; |
| 6 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 7 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 8 |
use Give\Framework\PaymentGateways\Contracts\PaymentGatewaysIterator; |
| 9 |
use Give\Framework\PaymentGateways\Exceptions\OverflowException; |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 2.18.0 |
| 13 |
*/ |
| 14 |
class PaymentGatewayRegister extends PaymentGatewaysIterator |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var string[] |
| 18 |
*/ |
| 19 |
protected $gateways = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Get Gateways |
| 23 |
* |
| 24 |
* @since 2.30.0 added $supportedFormVersion param to filter gateways by supported form version |
| 25 |
* @since 2.18.0 |
| 26 |
*/ |
| 27 |
public function getPaymentGateways(int $supportedFormVersion = null): array |
| 28 |
{ |
| 29 |
if (!$supportedFormVersion) { |
| 30 |
return $this->gateways; |
| 31 |
} |
| 32 |
|
| 33 |
return array_filter($this->gateways, static function (string $gatewayClass) use ($supportedFormVersion) { |
| 34 |
/** @var PaymentGateway $gateway */ |
| 35 |
$gateway = give($gatewayClass); |
| 36 |
|
| 37 |
return in_array($supportedFormVersion, $gateway->supportsFormVersions(), true); |
| 38 |
}); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get Gateway |
| 43 |
* |
| 44 |
* @since 2.18.0 |
| 45 |
*/ |
| 46 |
public function getPaymentGateway(string $id): PaymentGateway |
| 47 |
{ |
| 48 |
if (!$this->hasPaymentGateway($id)) { |
| 49 |
throw new InvalidArgumentException("No gateway exists with the ID {$id}"); |
| 50 |
} |
| 51 |
|
| 52 |
/** @var PaymentGateway $gateway */ |
| 53 |
$gateway = give($this->gateways[$id]); |
| 54 |
|
| 55 |
return $gateway; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @since 2.18.0 |
| 60 |
*/ |
| 61 |
public function hasPaymentGateway(string $id): bool |
| 62 |
{ |
| 63 |
return isset($this->gateways[$id]); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register Gateway |
| 68 |
* |
| 69 |
* @since 2.18.0 |
| 70 |
* |
| 71 |
* @throws OverflowException|InvalidArgumentException|Exception |
| 72 |
*/ |
| 73 |
public function registerGateway(string $gatewayClass) |
| 74 |
{ |
| 75 |
if (!is_subclass_of($gatewayClass, PaymentGateway::class)) { |
| 76 |
throw new InvalidArgumentException( |
| 77 |
sprintf( |
| 78 |
'%1$s must extend %2$s', |
| 79 |
$gatewayClass, |
| 80 |
PaymentGateway::class |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
$gatewayId = $gatewayClass::id(); |
| 86 |
|
| 87 |
if ($this->hasPaymentGateway($gatewayId)) { |
| 88 |
throw new OverflowException("Cannot register a gateway with an id that already exists: $gatewayId"); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
$this->gateways[$gatewayId] = $gatewayClass; |
| 93 |
|
| 94 |
|
| 95 |
$this->registerGatewayWithServiceContainer($gatewayClass, $gatewayId); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Unregister Gateway |
| 100 |
* |
| 101 |
* @since 2.18.0 |
| 102 |
* |
| 103 |
* @param $gatewayId |
| 104 |
*/ |
| 105 |
public function unregisterGateway($gatewayId) |
| 106 |
{ |
| 107 |
if (isset($this->gateways[$gatewayId])) { |
| 108 |
unset($this->gateways[$gatewayId]); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* |
| 114 |
* Register Gateway with Service Container as Singleton |
| 115 |
* with option of adding Subscription Module through filter "give_gateway_{$gatewayId}_subscription_module" |
| 116 |
* |
| 117 |
* @since 2.18.0 |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
private function registerGatewayWithServiceContainer(string $gatewayClass, string $gatewayId) |
| 122 |
{ |
| 123 |
give()->singleton($gatewayClass, function (Container $container) use ($gatewayClass, $gatewayId) { |
| 124 |
$subscriptionModule = apply_filters("givewp_gateway_{$gatewayId}_subscription_module", null); |
| 125 |
|
| 126 |
return new $gatewayClass($subscriptionModule ? $container->make($subscriptionModule) : null); |
| 127 |
}); |
| 128 |
} |
| 129 |
} |
| 130 |
|