Actions
4 years ago
CommandHandlers
3 years ago
Commands
3 years ago
Contracts
3 years ago
DataTransferObjects
3 years ago
Exceptions
4 years ago
Log
4 years ago
Routes
3 years ago
Traits
3 years ago
DonationSummary.php
3 years ago
PaymentGateway.php
3 years ago
PaymentGatewayRegister.php
3 years ago
SubscriptionModule.php
4 years ago
PaymentGatewayRegister.php
115 lines
| 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 | protected $gateways = []; |
| 17 | |
| 18 | /** |
| 19 | * Get Gateways |
| 20 | * |
| 21 | * @since 2.18.0 |
| 22 | */ |
| 23 | public function getPaymentGateways(): array |
| 24 | { |
| 25 | return $this->gateways; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get Gateway |
| 30 | * |
| 31 | * @since 2.18.0 |
| 32 | */ |
| 33 | public function getPaymentGateway(string $id): PaymentGateway |
| 34 | { |
| 35 | if (!$this->hasPaymentGateway($id)) { |
| 36 | throw new InvalidArgumentException("No gateway exists with the ID {$id}"); |
| 37 | } |
| 38 | |
| 39 | /** @var PaymentGateway $gateway */ |
| 40 | $gateway = give($this->gateways[$id]); |
| 41 | |
| 42 | return $gateway; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @since 2.18.0 |
| 47 | */ |
| 48 | public function hasPaymentGateway(string $id): bool |
| 49 | { |
| 50 | return isset($this->gateways[$id]); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Register Gateway |
| 55 | * |
| 56 | * @since 2.18.0 |
| 57 | * |
| 58 | * @throws OverflowException|InvalidArgumentException|Exception |
| 59 | */ |
| 60 | public function registerGateway(string $gatewayClass) |
| 61 | { |
| 62 | if (!is_subclass_of($gatewayClass, PaymentGateway::class)) { |
| 63 | throw new InvalidArgumentException( |
| 64 | sprintf( |
| 65 | '%1$s must extend %2$s', |
| 66 | $gatewayClass, |
| 67 | PaymentGateway::class |
| 68 | ) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | $gatewayId = $gatewayClass::id(); |
| 73 | |
| 74 | if ($this->hasPaymentGateway($gatewayId)) { |
| 75 | throw new OverflowException("Cannot register a gateway with an id that already exists: $gatewayId"); |
| 76 | } |
| 77 | |
| 78 | $this->gateways[$gatewayId] = $gatewayClass; |
| 79 | |
| 80 | $this->registerGatewayWithServiceContainer($gatewayClass, $gatewayId); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Unregister Gateway |
| 85 | * |
| 86 | * @since 2.18.0 |
| 87 | * |
| 88 | * @param $gatewayId |
| 89 | */ |
| 90 | public function unregisterGateway($gatewayId) |
| 91 | { |
| 92 | if (isset($this->gateways[$gatewayId])) { |
| 93 | unset($this->gateways[$gatewayId]); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * |
| 99 | * Register Gateway with Service Container as Singleton |
| 100 | * with option of adding Subscription Module through filter "give_gateway_{$gatewayId}_subscription_module" |
| 101 | * |
| 102 | * @since 2.18.0 |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | private function registerGatewayWithServiceContainer(string $gatewayClass, string $gatewayId) |
| 107 | { |
| 108 | give()->singleton($gatewayClass, function (Container $container) use ($gatewayClass, $gatewayId) { |
| 109 | $subscriptionModule = apply_filters("givewp_gateway_{$gatewayId}_subscription_module", null); |
| 110 | |
| 111 | return new $gatewayClass($subscriptionModule ? $container->make($subscriptionModule) : null); |
| 112 | }); |
| 113 | } |
| 114 | } |
| 115 |