PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Framework / PaymentGateways / PaymentGatewayRegister.php

PaymentGatewayRegister.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/PaymentGateways/PaymentGatewayRegister.php

131 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 4.17.0 Declare the nullable parameter explicitly.
25 * @since 2.30.0 added $supportedFormVersion param to filter gateways by supported form version
26 * @since 2.18.0
27 */
28 public function getPaymentGateways(?int $supportedFormVersion = null): array
29 {
30 if (!$supportedFormVersion) {
31 return $this->gateways;
32 }
33
34 return array_filter($this->gateways, static function (string $gatewayClass) use ($supportedFormVersion) {
35 /** @var PaymentGateway $gateway */
36 $gateway = give($gatewayClass);
37
38 return in_array($supportedFormVersion, $gateway->supportsFormVersions(), true);
39 });
40 }
41
42 /**
43 * Get Gateway
44 *
45 * @since 2.18.0
46 */
47 public function getPaymentGateway(string $id): PaymentGateway
48 {
49 if (!$this->hasPaymentGateway($id)) {
50 throw new InvalidArgumentException("No gateway exists with the ID {$id}");
51 }
52
53 /** @var PaymentGateway $gateway */
54 $gateway = give($this->gateways[$id]);
55
56 return $gateway;
57 }
58
59 /**
60 * @since 2.18.0
61 */
62 public function hasPaymentGateway(string $id): bool
63 {
64 return isset($this->gateways[$id]);
65 }
66
67 /**
68 * Register Gateway
69 *
70 * @since 2.18.0
71 *
72 * @throws OverflowException|InvalidArgumentException|Exception
73 */
74 public function registerGateway(string $gatewayClass)
75 {
76 if (!is_subclass_of($gatewayClass, PaymentGateway::class)) {
77 throw new InvalidArgumentException(
78 sprintf(
79 '%1$s must extend %2$s',
80 $gatewayClass,
81 PaymentGateway::class
82 )
83 );
84 }
85
86 $gatewayId = $gatewayClass::id();
87
88 if ($this->hasPaymentGateway($gatewayId)) {
89 throw new OverflowException("Cannot register a gateway with an id that already exists: $gatewayId");
90 }
91
92
93 $this->gateways[$gatewayId] = $gatewayClass;
94
95
96 $this->registerGatewayWithServiceContainer($gatewayClass, $gatewayId);
97 }
98
99 /**
100 * Unregister Gateway
101 *
102 * @since 2.18.0
103 *
104 * @param $gatewayId
105 */
106 public function unregisterGateway($gatewayId)
107 {
108 if (isset($this->gateways[$gatewayId])) {
109 unset($this->gateways[$gatewayId]);
110 }
111 }
112
113 /**
114 *
115 * Register Gateway with Service Container as Singleton
116 * with option of adding Subscription Module through filter "give_gateway_{$gatewayId}_subscription_module"
117 *
118 * @since 2.18.0
119 *
120 * @return void
121 */
122 private function registerGatewayWithServiceContainer(string $gatewayClass, string $gatewayId)
123 {
124 give()->singleton($gatewayClass, function (Container $container) use ($gatewayClass, $gatewayId) {
125 $subscriptionModule = apply_filters("givewp_gateway_{$gatewayId}_subscription_module", null);
126
127 return new $gatewayClass($subscriptionModule ? $container->make($subscriptionModule) : null);
128 });
129 }
130 }
131