1 | <?php |
||
2 | |||
3 | namespace ByTIC\Payments\Gateways\Traits; |
||
4 | |||
5 | use ByTIC\Payments\Gateways\Manager; |
||
6 | use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait; |
||
7 | use Omnipay\Common\AbstractGateway; |
||
8 | use Omnipay\Common\GatewayInterface; |
||
9 | |||
10 | /** |
||
11 | * Class HasGatewaysTrait |
||
12 | * @package ByTIC\Payments\Traits |
||
13 | */ |
||
14 | trait HasGatewaysTrait |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var null|Manager |
||
19 | */ |
||
20 | protected $gatewaysManager = null; |
||
21 | |||
22 | /** |
||
23 | * @var null|GatewayTrait|GatewayInterface |
||
24 | */ |
||
25 | protected $gateway = null; |
||
26 | |||
27 | /** |
||
28 | * @return GatewayTrait|GatewayInterface|null |
||
29 | * @throws \Exception |
||
30 | */ |
||
31 | 18 | public function getGateway() |
|
32 | { |
||
33 | 18 | if ($this->gateway === null) { |
|
34 | 18 | $this->initGateway(); |
|
35 | } |
||
36 | |||
37 | 18 | return $this->gateway; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param GatewayTrait|GatewayInterface|null $gateway |
||
42 | */ |
||
43 | 18 | public function setGateway($gateway) |
|
44 | { |
||
45 | 18 | $this->gateway = $gateway; |
|
46 | 18 | } |
|
47 | |||
48 | /** |
||
49 | * @throws \Exception |
||
50 | */ |
||
51 | 18 | protected function initGateway() |
|
52 | { |
||
53 | 18 | $gateway = $this->newGateway($this->getGatewayName()); |
|
54 | 18 | $this->setGateway($gateway); |
|
55 | 18 | } |
|
56 | |||
57 | /** |
||
58 | * @param $name |
||
59 | * @return null|GatewayTrait|GatewayInterface |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | 18 | protected function newGateway($name) |
|
63 | { |
||
64 | 18 | if (empty($name)) { |
|
65 | throw new \Exception("No name in newGateway for ".get_class($this)); |
||
66 | } |
||
67 | |||
68 | 18 | $gateway = clone $this->getGatewaysManager()::gateway($name); |
|
69 | 18 | if (!($gateway instanceof GatewayInterface)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
70 | throw new \Exception("Invalid gateway name ['.$name.'] in ".get_class($this)); |
||
71 | } |
||
72 | 18 | $gatewayParams = $this->getGatewayOptions(); |
|
73 | 18 | $gateway->initialize($gatewayParams); |
|
74 | |||
75 | 18 | return $gateway; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return Manager|null |
||
80 | */ |
||
81 | 18 | public function getGatewaysManager() |
|
82 | { |
||
83 | 18 | if ($this->gatewaysManager == null) { |
|
84 | 18 | $this->initGatewaysManager(); |
|
85 | } |
||
86 | |||
87 | 18 | return $this->gatewaysManager; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param Manager|null $gatewaysManager |
||
92 | */ |
||
93 | 18 | public function setGatewaysManager($gatewaysManager) |
|
94 | { |
||
95 | 18 | $this->gatewaysManager = $gatewaysManager; |
|
96 | 18 | } |
|
97 | |||
98 | 18 | protected function initGatewaysManager() |
|
99 | { |
||
100 | 18 | $this->setGatewaysManager($this->generateGatewaysManager()); |
|
101 | 18 | } |
|
102 | |||
103 | protected function generateGatewaysManager(): Manager |
||
104 | { |
||
105 | return payments_gateways(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | abstract public function getGatewayName(); |
||
112 | |||
113 | /** |
||
114 | * @return mixed |
||
115 | */ |
||
116 | abstract protected function getGatewayOptions(); |
||
117 | } |
||
118 |