1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the colinodell\omnipay-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ColinODell\OmnipayBundle\Service; |
13
|
|
|
|
14
|
|
|
use Omnipay\Common\Http\Client; |
15
|
|
|
use Omnipay\Common\GatewayFactory; |
16
|
|
|
use Omnipay\Common\GatewayInterface; |
17
|
|
|
use Omnipay\Common\Helper; |
18
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
19
|
|
|
|
20
|
|
|
class Omnipay |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var GatewayFactory |
24
|
|
|
*/ |
25
|
|
|
protected $gatewayFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var GatewayInterface[] |
34
|
|
|
*/ |
35
|
|
|
protected $cache; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var GatewayInterface[] |
39
|
|
|
*/ |
40
|
|
|
protected $registeredGateways = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string[] |
44
|
|
|
*/ |
45
|
|
|
protected $disabledGateways = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $defaultGatewayName; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $initializeOnRegistration = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param GatewayFactory $gatewayFactory |
59
|
|
|
* @param array $config |
60
|
|
|
*/ |
61
|
|
|
public function __construct(GatewayFactory $gatewayFactory, array $config = array()) |
62
|
|
|
{ |
63
|
|
|
$this->gatewayFactory = $gatewayFactory; |
64
|
|
|
$this->config = $config; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $gatewayName |
69
|
|
|
* |
70
|
|
|
* @return GatewayInterface |
71
|
|
|
*/ |
72
|
|
|
public function get($gatewayName) |
73
|
|
|
{ |
74
|
|
|
if (!isset($this->cache[$gatewayName])) { |
75
|
|
|
$gateway = $this->createGateway($gatewayName); |
76
|
|
|
$this->cache[$gatewayName] = $gateway; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->cache[$gatewayName]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param GatewayInterface $gatewayInstance |
84
|
|
|
* @param string|null $alias |
85
|
|
|
*/ |
86
|
|
|
public function registerGateway(GatewayInterface $gatewayInstance, $alias = null) |
87
|
|
|
{ |
88
|
|
|
$name = $alias ?: Helper::getGatewayShortName(get_class($gatewayInstance)); |
89
|
|
|
|
90
|
|
|
if (in_array($name, $this->disabledGateways)) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->registeredGateways[$name] = $gatewayInstance; |
95
|
|
|
|
96
|
|
|
if ($this->initializeOnRegistration) { |
97
|
|
|
$gatewayInstance->initialize($this->getGatewayConfig($name)); |
98
|
|
|
$this->cache[$name] = $gatewayInstance; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string[] $disabledGateways |
104
|
|
|
*/ |
105
|
|
|
public function setDisabledGateways(array $disabledGateways) |
106
|
|
|
{ |
107
|
|
|
$this->disabledGateways = $disabledGateways; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return GatewayInterface |
112
|
|
|
*/ |
113
|
|
|
public function getDefaultGateway() |
114
|
|
|
{ |
115
|
|
|
if (null === $this->defaultGatewayName) { |
116
|
|
|
throw new InvalidConfigurationException('Default gateway is not configured'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->get($this->defaultGatewayName); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $defaultGatewayName |
124
|
|
|
*/ |
125
|
|
|
public function setDefaultGatewayName($defaultGatewayName) |
126
|
|
|
{ |
127
|
|
|
$this->defaultGatewayName = $defaultGatewayName; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param boolean $initializeOnRegistration |
132
|
|
|
*/ |
133
|
|
|
public function initializeOnRegistration($initializeOnRegistration) |
134
|
|
|
{ |
135
|
|
|
$this->initializeOnRegistration = $initializeOnRegistration; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $gatewayName |
140
|
|
|
* @return GatewayInterface |
141
|
|
|
*/ |
142
|
|
|
protected function createGateway($gatewayName) |
143
|
|
|
{ |
144
|
|
|
$httpClient = new Client(); |
145
|
|
|
|
146
|
|
|
if (isset($this->registeredGateways[$gatewayName])) { |
147
|
|
|
$gateway = $this->registeredGateways[$gatewayName]; |
148
|
|
|
} else { |
149
|
|
|
/** @var GatewayInterface $gateway */ |
150
|
|
|
$gateway = $this->gatewayFactory->create($gatewayName, $httpClient); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$gateway->initialize($this->getGatewayConfig($gatewayName)); |
154
|
|
|
|
155
|
|
|
return $gateway; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $gatewayName |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
protected function getGatewayConfig($gatewayName) |
163
|
|
|
{ |
164
|
|
|
return isset($this->config[$gatewayName]) ? $this->config[$gatewayName] : []; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|