1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nxmad\Larapay\Abstracts; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use Illuminate\Config\Repository; |
7
|
|
|
use Nxmad\Larapay\Models\Transaction; |
|
|
|
|
8
|
|
|
use Nxmad\Larapay\Contracts\Gateway as GatewayContract; |
9
|
|
|
|
10
|
|
|
abstract class Gateway implements GatewayContract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The list of aliases for payment gateway. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $aliases = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The list of required request attributes. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $required = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The general settings for payment processor, like secret key or merchant id. |
28
|
|
|
* |
29
|
|
|
* @var Repository |
30
|
|
|
*/ |
31
|
|
|
protected $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The temporary settings for payment processor, actual only in for this request, like payment description, etc. |
35
|
|
|
* |
36
|
|
|
* @var Repository |
37
|
|
|
*/ |
38
|
|
|
protected $custom; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Payment process method. |
42
|
|
|
* Possible values are below. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $method = self::LARAPAY_GET_REDIRECT; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Classic redirection with GET parameters. |
50
|
|
|
*/ |
51
|
|
|
const LARAPAY_GET_REDIRECT = 'GET'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Redirect with POST data for old gateways (using hack with form). |
55
|
|
|
*/ |
56
|
|
|
const LARAPAY_POST_REDIRECT = 'POST'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* No redirect method (e.g. for card payments) |
60
|
|
|
*/ |
61
|
|
|
const LARAPAY_NO_REDIRECT = '_'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sign outcome request (insert request signature in request parameters). |
65
|
|
|
* |
66
|
|
|
* @param array $data |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
abstract public function sign(array $data): string; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gateway constructor. |
74
|
|
|
* |
75
|
|
|
* @param array $config |
76
|
|
|
*/ |
77
|
|
|
public function __construct(array $config = []) |
78
|
|
|
{ |
79
|
|
|
$this->custom = new Repository; |
80
|
|
|
$this->config = new Repository($config); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Process payment. |
85
|
|
|
* |
86
|
|
|
* @param Transaction $transaction |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function interact(Transaction $transaction) |
91
|
|
|
{ |
92
|
|
|
$this->prepare($transaction); |
93
|
|
|
$this->fill([ |
94
|
|
|
'amount' => $transaction->getAmount(), |
95
|
|
|
'description' => $transaction->getDescription(), |
96
|
|
|
'id' => $transaction->getPrimaryValue(), |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$this->signature = $this->sign($this->custom->all()); |
|
|
|
|
100
|
|
|
|
101
|
|
|
foreach ($this->required as $field) { |
102
|
|
|
if (! $this->custom->has($this->getAlias($field))) { |
103
|
|
|
throw new RuntimeException("Required field [{$field}] is not presented."); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->method == self::LARAPAY_NO_REDIRECT) { |
108
|
|
|
return $this->customBehavior(); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->method == self::LARAPAY_GET_REDIRECT) { |
112
|
|
|
return redirect()->away($this->getInteractionUrl() . '?' . http_build_query($this->custom->all())); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return view('larapay::form', [ |
116
|
|
|
'method' => 'POST', |
117
|
|
|
'data' => $this->custom->all(), |
118
|
|
|
'action' => $this->getInteractionUrl(), |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Custom gateway logic instead redirect. |
124
|
|
|
* You can override this method in children class. |
125
|
|
|
* |
126
|
|
|
* @param Transaction $transaction |
127
|
|
|
*/ |
128
|
|
|
public function customBehavior(Transaction $transaction) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Prepare Transaction. |
134
|
|
|
* You can override this method in children class. |
135
|
|
|
* |
136
|
|
|
* @param Transaction $transaction |
137
|
|
|
*/ |
138
|
|
|
public function prepare(Transaction $transaction) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Determine if this gateway needs redirect. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function needRedirect() |
148
|
|
|
{ |
149
|
|
|
return $this->method != self::LARAPAY_NO_REDIRECT; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Fill custom parameters. |
154
|
|
|
* |
155
|
|
|
* @param array $parameters |
156
|
|
|
* |
157
|
|
|
* @return Gateway |
158
|
|
|
*/ |
159
|
|
|
public function fill(array $parameters): self |
160
|
|
|
{ |
161
|
|
|
foreach ($parameters as $key => $value) { |
162
|
|
|
$this->set($key, $value); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set custom field value respecting aliases. |
170
|
|
|
* |
171
|
|
|
* @param $field |
172
|
|
|
* @param $value |
173
|
|
|
* |
174
|
|
|
* @return self |
175
|
|
|
*/ |
176
|
|
|
public function set($field, $value = null): self |
177
|
|
|
{ |
178
|
|
|
if (is_array($field)) { |
179
|
|
|
return $this->fill($field); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->custom->set($this->getAlias($field), $value); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Magic set method. |
189
|
|
|
* |
190
|
|
|
* @param $name |
191
|
|
|
* @param $value |
192
|
|
|
* |
193
|
|
|
* @return Gateway |
194
|
|
|
*/ |
195
|
|
|
public function __set($name, $value) |
196
|
|
|
{ |
197
|
|
|
return self::set(...func_get_args()); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get custom field value respecting aliases. |
202
|
|
|
* |
203
|
|
|
* @param string $field |
204
|
|
|
* @param mixed $default |
205
|
|
|
* |
206
|
|
|
* @return mixed |
207
|
|
|
*/ |
208
|
|
|
public function get(string $field, $default = null) |
209
|
|
|
{ |
210
|
|
|
return $this->custom->get($this->getAlias($field), $default); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Magic get method. |
215
|
|
|
* |
216
|
|
|
* @param $name |
217
|
|
|
* |
218
|
|
|
* @return mixed |
219
|
|
|
*/ |
220
|
|
|
public function __get($name) |
221
|
|
|
{ |
222
|
|
|
return self::get(...func_get_args()); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get slug of gateway based on class name. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getSlug(): string |
231
|
|
|
{ |
232
|
|
|
if (isset($this->slug)) { |
|
|
|
|
233
|
|
|
return $this->slug; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return str_slug(array_last(explode('\\', get_class($this)))); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Determine if field has alias for payment gateway. |
241
|
|
|
* E.g., some gateways can accept description in $_GET parameter vendorPrefix_desc, |
242
|
|
|
* so we need conversion: 'description' => 'vendorPrefix_desc'. |
243
|
|
|
* |
244
|
|
|
* @param string $field |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
protected function getAlias(string $field): string |
249
|
|
|
{ |
250
|
|
|
if (isset($this->aliases[$field])) { |
251
|
|
|
return $this->aliases[$field]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $field; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths