1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nxmad\Larapay; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Nxmad\Larapay\Abstracts\Gateway; |
10
|
|
|
use Illuminate\Contracts\Config\Repository; |
11
|
|
|
|
12
|
|
|
class GatewayManager implements Contracts\Payments |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The global GatewayManager config. |
16
|
|
|
* |
17
|
|
|
* @var Repository |
18
|
|
|
*/ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The list of default implementations. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $gateways = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The list of created drivers. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $created = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new manager instance. |
37
|
|
|
* |
38
|
|
|
* @param Repository $config |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Repository $config) |
41
|
|
|
{ |
42
|
|
|
$this->config = $config; |
43
|
|
|
|
44
|
|
|
foreach ($this->config->get('larapay.gateways') as $slug => $implementation) { |
45
|
|
|
$this->extend($slug, $implementation); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get gateway module instance. |
51
|
|
|
* |
52
|
|
|
* @param string $gateway |
53
|
|
|
* |
54
|
|
|
* @return Gateway |
55
|
|
|
*/ |
56
|
|
|
public function gateway(string $gateway): Gateway |
57
|
|
|
{ |
58
|
|
|
$gateway = mb_strtolower($gateway); |
59
|
|
|
|
60
|
|
|
if (! isset($this->created[$gateway])) { |
61
|
|
|
$this->created[$gateway] = $this->createGateway($gateway); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->created[$gateway]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Extend basic implementations list. |
69
|
|
|
* |
70
|
|
|
* @param string $driver |
71
|
|
|
* @param string $implementation |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
|
|
public function extend(string $driver, string $implementation): self |
76
|
|
|
{ |
77
|
|
|
$this->gateways[$driver] = $implementation; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create and cache driver instance to fast access in future. |
84
|
|
|
* |
85
|
|
|
* @param string $gateway |
86
|
|
|
* |
87
|
|
|
* @return Gateway |
88
|
|
|
* |
89
|
|
|
* @throws InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
protected function createGateway(string $gateway): Gateway |
92
|
|
|
{ |
93
|
|
|
if (! isset($this->gateways[$gateway])) { |
94
|
|
|
throw new InvalidArgumentException("Gateway [{$gateway}] was not found."); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$implementation = $this->gateways[$gateway]; |
98
|
|
|
|
99
|
|
|
if (! class_exists($implementation)) { |
100
|
|
|
throw new RuntimeException("Class [{$implementation}] was not found."); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return new $implementation($this->config->get("services.{$gateway}", [])); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|