1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base payment gateway class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Omnipay\Common; |
7
|
|
|
|
8
|
|
|
use Omnipay\Common\Http\Client; |
9
|
|
|
use Omnipay\Common\Http\ClientInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base payment gateway class |
15
|
|
|
* |
16
|
|
|
* This abstract class should be extended by all payment gateways |
17
|
|
|
* throughout the Omnipay system. It enforces implementation of |
18
|
|
|
* the GatewayInterface interface and defines various common attributes |
19
|
|
|
* and methods that all gateways should have. |
20
|
|
|
* |
21
|
|
|
* Example: |
22
|
|
|
* |
23
|
|
|
* <code> |
24
|
|
|
* // Initialise the gateway |
25
|
|
|
* $gateway->initialize(...); |
26
|
|
|
* |
27
|
|
|
* // Get the gateway parameters. |
28
|
|
|
* $parameters = $gateway->getParameters(); |
29
|
|
|
* |
30
|
|
|
* // Create a credit card object |
31
|
|
|
* $card = new CreditCard(...); |
32
|
|
|
* |
33
|
|
|
* // Do an authorisation transaction on the gateway |
34
|
|
|
* if ($gateway->supportsAuthorize()) { |
35
|
|
|
* $gateway->authorize(...); |
36
|
|
|
* } else { |
37
|
|
|
* throw new \Exception('Gateway does not support authorize()'); |
38
|
|
|
* } |
39
|
|
|
* </code> |
40
|
|
|
* |
41
|
|
|
* For further code examples see the *omnipay-example* repository on github. |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
abstract class AbstractGateway implements GatewayInterface |
45
|
|
|
{ |
46
|
|
|
use ParametersTrait { |
47
|
|
|
setParameter as traitSetParameter; |
48
|
|
|
getParameter as traitGetParameter; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ClientInterface |
53
|
|
|
*/ |
54
|
|
|
protected $httpClient; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
58
|
|
|
*/ |
59
|
|
|
protected $httpRequest; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a new gateway instance |
63
|
|
|
* |
64
|
|
|
* @param ClientInterface $httpClient A HTTP client to make API calls with |
65
|
|
|
* @param HttpRequest $httpRequest A Symfony HTTP request object |
66
|
|
|
*/ |
67
|
66 |
|
public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null) |
68
|
|
|
{ |
69
|
66 |
|
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); |
70
|
66 |
|
$this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); |
71
|
66 |
|
$this->initialize(); |
72
|
66 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the short name of the Gateway |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
3 |
|
public function getShortName() |
80
|
|
|
{ |
81
|
3 |
|
return Helper::getGatewayShortName(get_class($this)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Initialize this gateway with default parameters |
86
|
|
|
* |
87
|
|
|
* @param array $parameters |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
66 |
|
public function initialize(array $parameters = array()) |
91
|
|
|
{ |
92
|
66 |
|
$this->parameters = new ParameterBag; |
93
|
|
|
|
94
|
|
|
// set default parameters |
95
|
66 |
|
foreach ($this->getDefaultParameters() as $key => $value) { |
96
|
6 |
|
if (is_array($value)) { |
97
|
3 |
|
$this->parameters->set($key, reset($value)); |
98
|
|
|
} else { |
99
|
6 |
|
$this->parameters->set($key, $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
66 |
|
Helper::initialize($this, $parameters); |
104
|
|
|
|
105
|
66 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
66 |
|
public function getDefaultParameters() |
112
|
|
|
{ |
113
|
66 |
|
return array(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $key |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
9 |
|
public function getParameter($key) |
121
|
|
|
{ |
122
|
9 |
|
return $this->traitGetParameter($key); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $key |
127
|
|
|
* @param mixed $value |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
15 |
|
public function setParameter($key, $value) |
131
|
|
|
{ |
132
|
15 |
|
return $this->traitSetParameter($key, $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
3 |
|
public function getTestMode() |
139
|
|
|
{ |
140
|
3 |
|
return $this->getParameter('testMode'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param boolean $value |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
6 |
|
public function setTestMode($value) |
148
|
|
|
{ |
149
|
6 |
|
return $this->setParameter('testMode', $value); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
3 |
|
public function getCurrency() |
156
|
|
|
{ |
157
|
3 |
|
return strtoupper($this->getParameter('currency')); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $value |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
6 |
|
public function setCurrency($value) |
165
|
|
|
{ |
166
|
6 |
|
return $this->setParameter('currency', $value); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Supports Authorize |
171
|
|
|
* |
172
|
|
|
* @return boolean True if this gateway supports the authorize() method |
173
|
|
|
*/ |
174
|
3 |
|
public function supportsAuthorize() |
175
|
|
|
{ |
176
|
3 |
|
return method_exists($this, 'authorize'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Supports Complete Authorize |
181
|
|
|
* |
182
|
|
|
* @return boolean True if this gateway supports the completeAuthorize() method |
183
|
|
|
*/ |
184
|
3 |
|
public function supportsCompleteAuthorize() |
185
|
|
|
{ |
186
|
3 |
|
return method_exists($this, 'completeAuthorize'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Supports Capture |
191
|
|
|
* |
192
|
|
|
* @return boolean True if this gateway supports the capture() method |
193
|
|
|
*/ |
194
|
3 |
|
public function supportsCapture() |
195
|
|
|
{ |
196
|
3 |
|
return method_exists($this, 'capture'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Supports Purchase |
201
|
|
|
* |
202
|
|
|
* @return boolean True if this gateway supports the purchase() method |
203
|
|
|
*/ |
204
|
3 |
|
public function supportsPurchase() |
205
|
|
|
{ |
206
|
3 |
|
return method_exists($this, 'purchase'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Supports Complete Purchase |
211
|
|
|
* |
212
|
|
|
* @return boolean True if this gateway supports the completePurchase() method |
213
|
|
|
*/ |
214
|
3 |
|
public function supportsCompletePurchase() |
215
|
|
|
{ |
216
|
3 |
|
return method_exists($this, 'completePurchase'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Supports Fetch Transaction |
221
|
|
|
* |
222
|
|
|
* @return boolean True if this gateway supports the fetchTransaction() method |
223
|
|
|
*/ |
224
|
3 |
|
public function supportsFetchTransaction() |
225
|
|
|
{ |
226
|
3 |
|
return method_exists($this, 'fetchTransaction'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Supports Refund |
231
|
|
|
* |
232
|
|
|
* @return boolean True if this gateway supports the refund() method |
233
|
|
|
*/ |
234
|
3 |
|
public function supportsRefund() |
235
|
|
|
{ |
236
|
3 |
|
return method_exists($this, 'refund'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Supports Void |
241
|
|
|
* |
242
|
|
|
* @return boolean True if this gateway supports the void() method |
243
|
|
|
*/ |
244
|
3 |
|
public function supportsVoid() |
245
|
|
|
{ |
246
|
3 |
|
return method_exists($this, 'void'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Supports AcceptNotification |
251
|
|
|
* |
252
|
|
|
* @return boolean True if this gateway supports the acceptNotification() method |
253
|
|
|
*/ |
254
|
3 |
|
public function supportsAcceptNotification() |
255
|
|
|
{ |
256
|
3 |
|
return method_exists($this, 'acceptNotification'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Supports CreateCard |
261
|
|
|
* |
262
|
|
|
* @return boolean True if this gateway supports the create() method |
263
|
|
|
*/ |
264
|
3 |
|
public function supportsCreateCard() |
265
|
|
|
{ |
266
|
3 |
|
return method_exists($this, 'createCard'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Supports DeleteCard |
271
|
|
|
* |
272
|
|
|
* @return boolean True if this gateway supports the delete() method |
273
|
|
|
*/ |
274
|
3 |
|
public function supportsDeleteCard() |
275
|
|
|
{ |
276
|
3 |
|
return method_exists($this, 'deleteCard'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Supports UpdateCard |
281
|
|
|
* |
282
|
|
|
* @return boolean True if this gateway supports the update() method |
283
|
|
|
*/ |
284
|
3 |
|
public function supportsUpdateCard() |
285
|
|
|
{ |
286
|
3 |
|
return method_exists($this, 'updateCard'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Create and initialize a request object |
291
|
|
|
* |
292
|
|
|
* This function is usually used to create objects of type |
293
|
|
|
* Omnipay\Common\Message\AbstractRequest (or a non-abstract subclass of it) |
294
|
|
|
* and initialise them with using existing parameters from this gateway. |
295
|
|
|
* |
296
|
|
|
* Example: |
297
|
|
|
* |
298
|
|
|
* <code> |
299
|
|
|
* class MyRequest extends \Omnipay\Common\Message\AbstractRequest {}; |
300
|
|
|
* |
301
|
|
|
* class MyGateway extends \Omnipay\Common\AbstractGateway { |
302
|
|
|
* function myRequest($parameters) { |
303
|
|
|
* $this->createRequest('MyRequest', $parameters); |
304
|
|
|
* } |
305
|
|
|
* } |
306
|
|
|
* |
307
|
|
|
* // Create the gateway object |
308
|
|
|
* $gw = Omnipay::create('MyGateway'); |
309
|
|
|
* |
310
|
|
|
* // Create the request object |
311
|
|
|
* $myRequest = $gw->myRequest($someParameters); |
312
|
|
|
* </code> |
313
|
|
|
* |
314
|
|
|
* @param string $class The request class name |
315
|
|
|
* @param array $parameters |
316
|
|
|
* @return \Omnipay\Common\Message\AbstractRequest |
317
|
|
|
*/ |
318
|
3 |
|
protected function createRequest($class, array $parameters) |
319
|
|
|
{ |
320
|
3 |
|
$obj = new $class($this->httpClient, $this->httpRequest); |
321
|
|
|
|
322
|
3 |
|
return $obj->initialize(array_replace($this->getParameters(), $parameters)); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get the global default HTTP client. |
327
|
|
|
* |
328
|
|
|
* @return ClientInterface |
329
|
|
|
*/ |
330
|
66 |
|
protected function getDefaultHttpClient() |
331
|
|
|
{ |
332
|
66 |
|
return new Client(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Get the global default HTTP request. |
337
|
|
|
* |
338
|
|
|
* @return HttpRequest |
339
|
|
|
*/ |
340
|
66 |
|
protected function getDefaultHttpRequest() |
341
|
|
|
{ |
342
|
66 |
|
return HttpRequest::createFromGlobals(); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|