1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larium\Pay\Gateway; |
4
|
|
|
|
5
|
|
|
use Larium\Pay\Response; |
6
|
|
|
use Larium\Pay\ParamsBag; |
7
|
|
|
use Larium\Pay\GatewayException; |
8
|
|
|
use Larium\Pay\Transaction\Query; |
9
|
|
|
use Larium\Pay\Transaction\Cancel; |
10
|
|
|
use Larium\Pay\Transaction\Refund; |
11
|
|
|
use Larium\Pay\Transaction\Capture; |
12
|
|
|
use Larium\Pay\Transaction\Initial; |
13
|
|
|
use Larium\Pay\Transaction\Transfer; |
14
|
|
|
use Larium\Pay\Transaction\Purchase; |
15
|
|
|
use Larium\Pay\Transaction\Retrieve; |
16
|
|
|
use Larium\Pay\Transaction\Authorize; |
17
|
|
|
use Larium\Pay\Transaction\Transaction; |
18
|
|
|
use Larium\Pay\Transaction\ThreedSecureAuthenticate; |
19
|
|
|
|
20
|
|
|
abstract class Gateway |
21
|
|
|
{ |
22
|
|
|
protected $sandbox; |
23
|
|
|
|
24
|
|
|
protected $options; |
25
|
|
|
|
26
|
|
|
private $responseCallback; |
27
|
|
|
|
28
|
|
|
private $transactionToMethod = [ |
29
|
|
|
Query::class => 'query', |
30
|
|
|
Cancel::class => 'cancel', |
31
|
|
|
Refund::class => 'refund', |
32
|
|
|
Capture::class => 'capture', |
33
|
|
|
Initial::class => 'initiate', |
34
|
|
|
Transfer::class => 'transfer', |
35
|
|
|
Purchase::class => 'purchase', |
36
|
|
|
Retrieve::class => 'retrieve', |
37
|
|
|
Authorize::class => 'authorize', |
38
|
|
|
Transaction::class => 'transaction', |
39
|
|
|
ThreedSecureAuthenticate::class => 'threedSecureAuthenticate', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return whether the response is success or not. |
44
|
|
|
* |
45
|
|
|
* $response param contains all the elements of gateway response, |
46
|
|
|
* parsed as associative array, including http status and headers. |
47
|
|
|
* |
48
|
|
|
* @param array $response |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
abstract protected function success(array $response); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the message from gateway response. |
55
|
|
|
* |
56
|
|
|
* $response param contains all the elements of gateway response, |
57
|
|
|
* parsed as associative array, including http status and headers. |
58
|
|
|
* |
59
|
|
|
* @param array $response |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
abstract protected function message(array $response); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns th unique transaction id from gateway response. |
66
|
|
|
* |
67
|
|
|
* $response param contains all the elements of gateway response, |
68
|
|
|
* parsed as associative array, including http status and headers. |
69
|
|
|
* |
70
|
|
|
* @param array $response |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
abstract protected function transactionId(array $response); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns error code from gateway if exists. |
77
|
|
|
* |
78
|
|
|
* $response param contains all the elements of gateway response, |
79
|
|
|
* parsed as associative array, including http status and headers. |
80
|
|
|
* |
81
|
|
|
* @param array $response |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
|
|
abstract protected function errorCode(array $response); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns response code from card processing, if exists. |
88
|
|
|
* @link https://arch.developer.visa.com/vpp/documents/xml/Request_and_Response.html Example of response codes |
89
|
|
|
* |
90
|
|
|
* $response param contains all the elements of gateway response, |
91
|
|
|
* parsed as associative array, including http status and headers. |
92
|
|
|
* |
93
|
|
|
* @param array $response |
94
|
|
|
* @return string|null |
95
|
|
|
*/ |
96
|
|
|
abstract protected function responseCode(array $response); |
97
|
|
|
|
98
|
|
|
final public function __construct(array $options = []) |
99
|
|
|
{ |
100
|
|
|
$this->options = new ParamsBag($options); |
101
|
|
|
|
102
|
|
|
$this->sandbox = $this->options->get('sandbox', false); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function execute( |
106
|
|
|
Transaction $transaction, |
107
|
|
|
callable $responseCallback = null |
108
|
|
|
) { |
109
|
|
|
$transaction->commit(); |
110
|
|
|
|
111
|
|
|
$this->responseCallback = $responseCallback; |
112
|
|
|
|
113
|
|
|
foreach ($this->transactionToMethod as $class => $method) { |
114
|
|
|
if ($transaction instanceof $class) { |
115
|
|
|
return $this->$method($transaction); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
throw new \RuntimeException( |
120
|
|
|
sprintf('Invalid transaction type `%s`', get_class($transaction)) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function purchase(Purchase $transaction) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function authorize(Authorize $transaction) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function capture(Capture $transaction) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function refund(Refund $transaction) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function cancel(Cancel $transaction) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function retrieve(Retrieve $transaction) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function initiate(Initial $transaction) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function query(Query $transaction) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function threedSecureAuthenticate(ThreedSecureAuthenticate $transaction) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function transfer(Transfer $transaction) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
throw GatewayException::notImplemented(__FUNCTION__); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Creates and return the response from gateway. |
176
|
|
|
* |
177
|
|
|
* @param bool $success Whether response was success or not. |
178
|
|
|
* @param string $message The message theat describe response status or |
179
|
|
|
* reason. |
180
|
|
|
* @param string $transactionId The unique identifier from transaction. |
181
|
|
|
* @param string $errorCode The error code if transaction was failed. |
182
|
|
|
* @param string $responsecCode The ISO 8583 response code. Not always |
|
|
|
|
183
|
|
|
* available. |
184
|
|
|
* @param array $payload An associative array of the response from |
185
|
|
|
* gateway including http status and headers. |
186
|
|
|
* @param string $rawResponse The raw response of gateway. |
187
|
|
|
* @param string $rawRequest The raw request to gateway. |
188
|
|
|
* |
189
|
|
|
* @return Larium\Pay\Response|mixed Response may be a user response if $responseCallback |
190
|
|
|
* param is used in execute method |
191
|
|
|
*/ |
192
|
|
|
protected function createResponse( |
193
|
|
|
$success, |
194
|
|
|
$message, |
195
|
|
|
$transactionId, |
196
|
|
|
$errorCode = '0', |
197
|
|
|
$responseCode = null, |
198
|
|
|
array $payload = null, |
199
|
|
|
$rawResponse = null, |
200
|
|
|
$rawRequest = null |
201
|
|
|
) { |
202
|
|
|
$response = new Response( |
203
|
|
|
$success, |
204
|
|
|
$message, |
205
|
|
|
$transactionId, |
206
|
|
|
$errorCode, |
207
|
|
|
$responseCode, |
208
|
|
|
$payload, |
|
|
|
|
209
|
|
|
$rawResponse, |
210
|
|
|
$rawRequest |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
if ($this->responseCallback) { |
214
|
|
|
return call_user_func_array( |
215
|
|
|
$this->responseCallback, |
216
|
|
|
[ |
217
|
|
|
$response, |
218
|
|
|
$payload |
219
|
|
|
] |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $response; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.