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