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