|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Gateways\Manager\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Payments\Gateways\GatewaysCollection; |
|
6
|
|
|
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait; |
|
7
|
|
|
use Nip\Container\Container; |
|
8
|
|
|
use Omnipay\Common\AbstractGateway; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait HasGatewaysCollectionTrait |
|
12
|
|
|
* @package ByTIC\Payments\Gateways\Manager\Traits |
|
13
|
|
|
*/ |
|
14
|
|
|
trait HasGatewaysCollectionTrait |
|
15
|
|
|
{ |
|
16
|
|
|
protected $supportedGateways = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Internal factory storage |
|
20
|
|
|
* |
|
21
|
|
|
* @var GatewaysCollection|GatewayTrait[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $collection; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param $name |
|
27
|
|
|
* @return AbstractGateway |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function gateway($name): AbstractGateway |
|
30
|
|
|
{ |
|
31
|
|
|
return self::instance()->getCollection()->get($name); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return GatewaysCollection |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getAll(): GatewaysCollection |
|
39
|
|
|
{ |
|
40
|
|
|
return self::instance()->getCollection(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the gateway factory |
|
45
|
|
|
* |
|
46
|
|
|
* Creates a new empty GatewayFactory if none has been set previously. |
|
47
|
|
|
* |
|
48
|
|
|
* @return GatewaysCollection A GatewayFactory instance |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getCollection(): GatewaysCollection |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_null($this->collection)) { |
|
|
|
|
|
|
53
|
|
|
$this->collection = new GatewaysCollection; |
|
54
|
|
|
$this->initCollection(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->collection; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function initCollection() |
|
61
|
|
|
{ |
|
62
|
|
|
$gatewayNames = self::getSupportedGateways(); |
|
|
|
|
|
|
63
|
|
|
$collection = self::getCollection(); |
|
|
|
|
|
|
64
|
|
|
foreach ($gatewayNames as $gatewayClass) { |
|
65
|
|
|
$gateway = $this->getFactory()->create($gatewayClass); |
|
|
|
|
|
|
66
|
|
|
$collection->offsetSet($gateway->getName(), $gateway); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get a list of supported gateways which may be available |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getSupportedGateways(): array |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->supportedGateways === null) { |
|
78
|
|
|
$this->supportedGateways = $this->generateSupportedGateways(); |
|
79
|
|
|
} |
|
80
|
|
|
return $this->supportedGateways; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function generateSupportedGateways(): array |
|
84
|
|
|
{ |
|
85
|
|
|
if (!function_exists('config') || !function_exists('app')) { |
|
86
|
|
|
return $this->generateSupportedGatewaysGeneric(); |
|
87
|
|
|
} |
|
88
|
|
|
$container = function_exists('app') ? app() : Container::getInstance(); |
|
89
|
|
|
if (!$container->has('config')) { |
|
90
|
|
|
return $this->generateSupportedGatewaysGeneric(); |
|
91
|
|
|
} |
|
92
|
|
|
$config = $container->get('config'); |
|
93
|
|
|
|
|
94
|
|
|
if ($config->has('payments.gateways')) { |
|
95
|
|
|
return $config->get('payments.gateways')->toArray(); |
|
96
|
|
|
} |
|
97
|
|
|
return $this->generateSupportedGatewaysGeneric(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function generateSupportedGatewaysGeneric(): array |
|
101
|
|
|
{ |
|
102
|
|
|
return [ |
|
103
|
|
|
'Payu', |
|
104
|
|
|
'Mobilpay', |
|
105
|
|
|
'Euplatesc', |
|
106
|
|
|
'Librapay', |
|
107
|
|
|
'Romcard', |
|
108
|
|
|
'Twispay', |
|
109
|
|
|
'Paylike', |
|
110
|
|
|
'PlatiOnline' |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|