1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gregoriohc\Moneta\Common; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Gregoriohc\Moneta\Common\Concerns\Parametrizable; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method bool supportsAuthorize() |
12
|
|
|
* @method bool supportsCapture() |
13
|
|
|
* @method bool supportsPurchase() |
14
|
|
|
* @method bool supportsRefund() |
15
|
|
|
* @method bool supportsVoid() |
16
|
|
|
* @method bool supportsCreatePaymentMethod() |
17
|
|
|
* @method bool supportsDeletePaymentMethod() |
18
|
|
|
* @method bool supportsUpdatePaymentMethod() |
19
|
|
|
* @method bool supportsAcceptNotification() |
20
|
|
|
* @property bool test_mode |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractGateway implements GatewayInterface, ArrayAccess, IteratorAggregate, JsonSerializable |
23
|
|
|
{ |
24
|
|
|
use Parametrizable; |
25
|
|
|
|
26
|
|
|
private $requestMethods = [ |
27
|
|
|
'authorize', |
28
|
|
|
'capture', |
29
|
|
|
'purchase', |
30
|
|
|
'refund', |
31
|
|
|
'void', |
32
|
|
|
'createPaymentMethod', |
33
|
|
|
'deletePaymentMethod', |
34
|
|
|
'updatePaymentMethod', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
private $webhookMethods = [ |
38
|
|
|
'acceptNotification', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
protected $client; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new gateway instance |
48
|
|
|
* |
49
|
|
|
* @param array $parameters |
50
|
|
|
*/ |
51
|
39 |
|
public function __construct($parameters = []) |
52
|
|
|
{ |
53
|
39 |
|
$this->bootParameters($parameters); |
54
|
39 |
|
$this->bootClient(); |
55
|
39 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
39 |
|
public function defaultParameters() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
39 |
|
'test_mode' => false, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
27 |
|
public function parametersValidationRules() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
27 |
|
'test_mode' => 'required', |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the short name of the Gateway |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
3 |
|
public function shortName() |
83
|
|
|
{ |
84
|
3 |
|
return class_basename($this); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
3 |
|
public function isInTestMode() |
91
|
|
|
{ |
92
|
3 |
|
return $this->test_mode; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
3 |
|
public function client() |
99
|
|
|
{ |
100
|
3 |
|
return $this->client; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create and initialize a request object |
105
|
|
|
* |
106
|
|
|
* @param string $class The request class name |
107
|
|
|
* @param array $parameters |
108
|
|
|
* @return \Gregoriohc\Moneta\Common\Messages\AbstractRequest |
109
|
|
|
*/ |
110
|
27 |
|
protected function createRequest($class, $parameters = []) |
111
|
|
|
{ |
112
|
27 |
|
$this->validateParameters(); |
113
|
|
|
|
114
|
24 |
|
return new $class($this, $parameters); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
30 |
|
private function requestClass($name) |
122
|
|
|
{ |
123
|
30 |
|
return substr(get_class($this), 0, -strlen(class_basename($this))) . 'Messages\\' . ucfirst($name) . 'Request'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @param $arguments |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
33 |
|
public function __call($name, $arguments) |
132
|
|
|
{ |
133
|
33 |
|
if (strpos($name, 'supports') === 0) { |
134
|
9 |
|
$name = lcfirst(substr($name, 8)); |
135
|
9 |
|
if (in_array($name, $this->requestMethods)) { |
136
|
9 |
|
return method_exists($this, $name) || class_exists($this->requestClass($name)); |
137
|
3 |
|
} elseif (in_array($name, $this->webhookMethods)) { |
138
|
3 |
|
return method_exists($this, $name); |
139
|
|
|
} |
140
|
30 |
|
} elseif (in_array($name, $this->requestMethods)) { |
141
|
27 |
|
$requestClass = $this->requestClass($name); |
142
|
27 |
|
if (class_exists($requestClass)) { |
143
|
24 |
|
$parameters = array_key_exists(0, $arguments) ? $arguments[0] : []; |
144
|
24 |
|
return $this->createRequest($requestClass, $parameters); |
145
|
|
|
} else { |
146
|
3 |
|
$class = class_basename($this); |
147
|
3 |
|
throw new \BadMethodCallException("Gateway '$class' does not support '$name' method"); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
$class = get_class($this); |
152
|
3 |
|
throw new \BadMethodCallException("Method '$class::$name' does not exists"); |
153
|
|
|
} |
154
|
|
|
} |