1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Gateways; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\GatewayInterface; |
6
|
|
|
use Omnipay\Common\Http\ClientInterface; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class GatewayFactory |
12
|
|
|
* @package ByTIC\Payments\Gateways |
13
|
|
|
*/ |
14
|
|
|
class GatewayFactory |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new gateway instance |
19
|
|
|
* |
20
|
|
|
* @param string $class Gateway name |
21
|
|
|
* @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation |
22
|
|
|
* @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation |
23
|
|
|
* @throws RuntimeException If no such gateway is found |
24
|
|
|
* @return GatewayInterface An object of class $class is created and returned |
25
|
|
|
*/ |
26
|
1 |
|
public function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null) |
27
|
|
|
{ |
28
|
1 |
|
if (!class_exists($class)) { |
29
|
|
|
$class = self::getGatewayClassName($class); |
30
|
1 |
|
if (!class_exists($class)) { |
31
|
|
|
throw new RuntimeException("Class '$class' not found"); |
32
|
|
|
} |
33
|
|
|
} |
34
|
1 |
|
|
35
|
|
|
return new $class($httpClient, $httpRequest); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Resolve a short gateway name to a full namespaced gateway class. |
40
|
|
|
* |
41
|
|
|
* Class names beginning with a namespace marker (\) are left intact. |
42
|
|
|
* Non-namespaced classes are expected to be in the \Omnipay namespace, e.g.: |
43
|
|
|
* |
44
|
|
|
* \Custom\Gateway => \Custom\Gateway |
45
|
|
|
* \Custom_Gateway => \Custom_Gateway |
46
|
|
|
* Stripe => \Omnipay\Stripe\Gateway |
47
|
|
|
* PayPal\Express => \Omnipay\PayPal\ExpressGateway |
48
|
|
|
* PayPal_Express => \Omnipay\PayPal\ExpressGateway |
49
|
|
|
* |
50
|
|
|
* @param string $shortName The short gateway name |
51
|
|
|
* @return string The fully namespaced gateway class name |
52
|
5 |
|
*/ |
53
|
|
|
public static function getGatewayClassName($shortName) |
54
|
5 |
|
{ |
55
|
|
|
if (0 === strpos($shortName, '\\')) { |
56
|
|
|
return $shortName; |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
// replace underscores with namespace marker, PSR-0 style |
60
|
5 |
|
$shortName = str_replace('_', '\\', $shortName); |
61
|
5 |
|
if (false === strpos($shortName, '\\')) { |
62
|
|
|
$shortName .= '\\'; |
63
|
|
|
} |
64
|
5 |
|
|
65
|
|
|
$tries= [ |
66
|
|
|
'\\ByTIC\Payments\\' . $shortName . 'Gateway', |
67
|
|
|
'\\ByTIC\Payments\Gateways\Providers\\' . $shortName . 'Gateway', |
68
|
|
|
'\\Paytic\Payments\\' . $shortName . 'Gateway', |
69
|
|
|
'\\Paytic\Payments\Gateways\Providers\\' . $shortName . 'Gateway' |
70
|
|
|
]; |
71
|
|
|
foreach ($tries as $try) { |
72
|
|
|
if (class_exists($try)) { |
73
|
|
|
return $try; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return '\\ByTIC\Payments\Gateways\Providers\\' . $shortName . 'Gateway'; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|