1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DansMaCulotte\Omnipay; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\GatewayFactory; |
6
|
|
|
|
7
|
|
|
class OmnipayGatewayManager |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The application instance. |
11
|
|
|
* |
12
|
|
|
* @var \Illuminate\Foundation\Application |
13
|
|
|
*/ |
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var GatewayFactory |
18
|
|
|
*/ |
19
|
|
|
protected $factory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The default settings, applied to every gateway |
23
|
|
|
*/ |
24
|
|
|
protected $defaults; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The registered gateways |
28
|
|
|
*/ |
29
|
|
|
protected $gateways; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The default gateway override |
33
|
|
|
*/ |
34
|
|
|
protected $gateway; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new Gateway manager instance. |
38
|
|
|
* |
39
|
|
|
* @param \Illuminate\Foundation\Application $app |
40
|
|
|
* @param \Omnipay\Common\GatewayFactory $factory |
41
|
|
|
* @param array |
42
|
|
|
*/ |
43
|
|
|
public function __construct($app, GatewayFactory $factory, $defaults = []) |
44
|
|
|
{ |
45
|
|
|
$this->app = $app; |
46
|
|
|
$this->factory = $factory; |
47
|
|
|
$this->defaults = $defaults; |
48
|
|
|
$this->gateway = null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get a gateway |
53
|
|
|
* |
54
|
|
|
* @return \Omnipay\Common\GatewayInterface |
55
|
|
|
*/ |
56
|
|
|
public function makeGateway() |
57
|
|
|
{ |
58
|
|
|
$class = $this->gateway ?: $this->getDefaultGateway(); |
59
|
|
|
|
60
|
|
|
if (!isset($this->gateways[$class])) { |
61
|
|
|
$gateway = $this->factory->create($class, null, $this->app['request']); |
62
|
|
|
$gateway->initialize($this->getConfig($class)); |
63
|
|
|
$this->gateways[$class] = $gateway; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->gateways[$class]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the configuration, based on the config and the defaults. |
71
|
|
|
* @param string $name |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function getConfig(string $name) |
75
|
|
|
{ |
76
|
|
|
return array_merge( |
77
|
|
|
$this->defaults, |
78
|
|
|
$this->app['config']->get('omnipay.gateways.'.$name, []) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the default gateway name. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getDefaultGateway() |
88
|
|
|
{ |
89
|
|
|
return $this->app['config']['omnipay.default']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the default gateway name. |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function setDefaultGateway(string $name) |
99
|
|
|
{ |
100
|
|
|
$this->app['config']['omnipay.default'] = $name; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $name |
105
|
|
|
* @return OmnipayGatewayManager |
106
|
|
|
*/ |
107
|
|
|
public function withGateway(string $name) |
108
|
|
|
{ |
109
|
|
|
$this->gateway = $name; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Dynamically call the default driver instance. |
116
|
|
|
* |
117
|
|
|
* @param string $method |
118
|
|
|
* @param array $parameters |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function __call($method, $parameters) |
122
|
|
|
{ |
123
|
|
|
return call_user_func_array([$this->makeGateway(), $method], $parameters); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|