|
1
|
|
|
<?php |
|
2
|
|
|
namespace CultureKings\Afterpay\Service\Merchant; |
|
3
|
|
|
|
|
4
|
|
|
use CultureKings\Afterpay\Exception\ApiException; |
|
5
|
|
|
use CultureKings\Afterpay\Model; |
|
6
|
|
|
use CultureKings\Afterpay\Traits; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
10
|
|
|
use GuzzleHttp\Psr7; |
|
11
|
|
|
use JMS\Serializer\SerializerInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Payments |
|
15
|
|
|
* |
|
16
|
|
|
* @package CultureKings\Afterpay\Service\Merchant |
|
17
|
|
|
*/ |
|
18
|
|
|
class Payments |
|
19
|
|
|
{ |
|
20
|
|
|
use Traits\ClientTrait; |
|
21
|
|
|
use Traits\AuthorizationTrait; |
|
22
|
|
|
use Traits\SerializerTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Payments constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Client $client |
|
28
|
|
|
* @param Model\Merchant\Authorization $authorization |
|
29
|
|
|
* @param SerializerInterface $serializer |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
Client $client, |
|
33
|
|
|
Model\Merchant\Authorization $authorization, |
|
34
|
|
|
SerializerInterface $serializer |
|
35
|
|
|
) { |
|
36
|
|
|
$this->setClient($client); |
|
37
|
|
|
$this->setAuthorization($authorization); |
|
38
|
|
|
$this->setSerializer($serializer); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $filters |
|
43
|
|
|
* @return array|object |
|
44
|
|
|
* |
|
45
|
|
|
* I would of liked to call this list() but it's a reserved keyword in < php7 |
|
46
|
|
|
* |
|
47
|
|
|
* According to Guzzle GitHub issue #1196 the build_query helper also does |
|
48
|
|
|
* duplicate aggregation. |
|
49
|
|
|
* |
|
50
|
|
|
* @see https://github.com/guzzle/guzzle/issues/1196 |
|
51
|
|
|
*/ |
|
52
|
|
|
public function listPayments(array $filters = [ ]) |
|
53
|
|
|
{ |
|
54
|
|
|
$query = Psr7\build_query($filters); |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
|
|
$result = $this->getClient()->get( |
|
58
|
|
|
'payments', |
|
59
|
|
|
[ |
|
60
|
|
|
'auth' => [ |
|
61
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
62
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
63
|
|
|
], |
|
64
|
|
|
'query' => $query, |
|
65
|
|
|
] |
|
66
|
|
|
); |
|
67
|
|
|
} catch (BadResponseException $e) { |
|
68
|
|
|
throw new ApiException( |
|
69
|
|
|
$this->getSerializer()->deserialize( |
|
70
|
|
|
(string) $e->getResponse()->getBody(), |
|
71
|
|
|
Model\ErrorResponse::class, |
|
72
|
|
|
'json' |
|
73
|
|
|
), |
|
74
|
|
|
$e |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->getSerializer()->deserialize( |
|
79
|
|
|
(string) $result->getBody(), |
|
80
|
|
|
Model\Merchant\PaymentsList::class, |
|
81
|
|
|
'json' |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $orderToken |
|
87
|
|
|
* @param string $merchantReference |
|
88
|
|
|
* @param string $webhookEventUrl |
|
89
|
|
|
* |
|
90
|
|
|
* @return Model\Merchant\Payment|object |
|
91
|
|
|
*/ |
|
92
|
|
|
public function capture($orderToken, $merchantReference = '', $webhookEventUrl = '') |
|
93
|
|
|
{ |
|
94
|
|
|
$request = [ |
|
95
|
|
|
'token' => $orderToken, |
|
96
|
|
|
'merchantReference' => $merchantReference, |
|
97
|
|
|
'webhookEventUrl' => $webhookEventUrl, |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
try { |
|
101
|
|
|
$requestBody = $this->getSerializer()->serialize($request, 'json'); |
|
102
|
|
|
$result = $this->getClient()->post( |
|
103
|
|
|
'payments/capture', |
|
104
|
|
|
[ |
|
105
|
|
|
'auth' => [ |
|
106
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
107
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
108
|
|
|
], |
|
109
|
|
|
'headers' => [ |
|
110
|
|
|
'Accept' => 'application/json', |
|
111
|
|
|
'Content-Type' => 'application/json', |
|
112
|
|
|
], |
|
113
|
|
|
'body' => $requestBody, |
|
114
|
|
|
] |
|
115
|
|
|
); |
|
116
|
|
|
} catch (BadResponseException $e) { |
|
117
|
|
|
throw new ApiException( |
|
118
|
|
|
$this->getSerializer()->deserialize( |
|
119
|
|
|
(string) $e->getResponse()->getBody(), |
|
120
|
|
|
Model\ErrorResponse::class, |
|
121
|
|
|
'json' |
|
122
|
|
|
), |
|
123
|
|
|
$e |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->getSerializer()->deserialize( |
|
128
|
|
|
(string) $result->getBody(), |
|
129
|
|
|
Model\Merchant\Payment::class, |
|
130
|
|
|
'json' |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $id |
|
136
|
|
|
* @return Model\Merchant\Payment|object |
|
137
|
|
|
*/ |
|
138
|
|
|
public function get($id) |
|
139
|
|
|
{ |
|
140
|
|
|
try { |
|
141
|
|
|
$result = $this->getClient()->get( |
|
142
|
|
|
sprintf('payments/%s', $id), |
|
143
|
|
|
[ |
|
144
|
|
|
'auth' => [ |
|
145
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
146
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
147
|
|
|
], |
|
148
|
|
|
] |
|
149
|
|
|
); |
|
150
|
|
|
} catch (BadResponseException $e) { |
|
151
|
|
|
throw new ApiException( |
|
152
|
|
|
$this->getSerializer()->deserialize( |
|
153
|
|
|
(string) $e->getResponse()->getBody(), |
|
154
|
|
|
Model\ErrorResponse::class, |
|
155
|
|
|
'json' |
|
156
|
|
|
), |
|
157
|
|
|
$e |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->getSerializer()->deserialize( |
|
162
|
|
|
(string) $result->getBody(), |
|
163
|
|
|
Model\Merchant\Payment::class, |
|
164
|
|
|
'json' |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $token |
|
170
|
|
|
* @return Model\Merchant\Payment|object |
|
171
|
|
|
* @throws ApiException |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getByToken($token) |
|
174
|
|
|
{ |
|
175
|
|
|
try { |
|
176
|
|
|
$result = $this->getClient()->get( |
|
177
|
|
|
sprintf('payments/token:%s', $token), |
|
178
|
|
|
[ |
|
179
|
|
|
'auth' => [ |
|
180
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
181
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
182
|
|
|
], |
|
183
|
|
|
] |
|
184
|
|
|
); |
|
185
|
|
|
} catch (BadResponseException $e) { |
|
186
|
|
|
throw new ApiException( |
|
187
|
|
|
$this->getSerializer()->deserialize( |
|
188
|
|
|
(string) $e->getResponse()->getBody(), |
|
189
|
|
|
Model\ErrorResponse::class, |
|
190
|
|
|
'json' |
|
191
|
|
|
), |
|
192
|
|
|
$e |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
return $this->getSerializer()->deserialize( |
|
198
|
|
|
(string) $result->getBody(), |
|
199
|
|
|
Model\Merchant\Payment::class, |
|
200
|
|
|
'json' |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param string $orderToken |
|
206
|
|
|
* @param string $merchantReference |
|
207
|
|
|
* @param string $webhookEventUrl |
|
208
|
|
|
* @return Model\Merchant\Payment|object |
|
209
|
|
|
*/ |
|
210
|
|
|
public function authorise($orderToken, $merchantReference = '', $webhookEventUrl = '') |
|
211
|
|
|
{ |
|
212
|
|
|
$request = [ |
|
213
|
|
|
'token' => $orderToken, |
|
214
|
|
|
'merchantReference' => $merchantReference, |
|
215
|
|
|
'webhookEventUrl' => $webhookEventUrl, |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
try { |
|
219
|
|
|
$result = $this->getClient()->post( |
|
220
|
|
|
'payments', |
|
221
|
|
|
[ |
|
222
|
|
|
'auth' => [ |
|
223
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
224
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
225
|
|
|
], |
|
226
|
|
|
'headers' => [ |
|
227
|
|
|
'Accept' => 'application/json', |
|
228
|
|
|
'Content-Type' => 'application/json', |
|
229
|
|
|
], |
|
230
|
|
|
'body' => $this->getSerializer()->serialize($request, 'json'), |
|
231
|
|
|
] |
|
232
|
|
|
); |
|
233
|
|
|
} catch (BadResponseException $e) { |
|
234
|
|
|
throw new ApiException( |
|
235
|
|
|
$this->getSerializer()->deserialize( |
|
236
|
|
|
(string) $e->getResponse()->getBody(), |
|
237
|
|
|
Model\ErrorResponse::class, |
|
238
|
|
|
'json' |
|
239
|
|
|
), |
|
240
|
|
|
$e |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $this->getSerializer()->deserialize( |
|
245
|
|
|
(string) $result->getBody(), |
|
246
|
|
|
Model\Merchant\Payment::class, |
|
247
|
|
|
'json' |
|
248
|
|
|
); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param string $paymentId |
|
253
|
|
|
* @return Model\Merchant\Payment|object |
|
254
|
|
|
*/ |
|
255
|
|
|
public function void($paymentId) |
|
256
|
|
|
{ |
|
257
|
|
|
try { |
|
258
|
|
|
$result = $this->getClient()->post( |
|
259
|
|
|
sprintf('payments/%s/void', $paymentId), |
|
260
|
|
|
[ |
|
261
|
|
|
'auth' => [ |
|
262
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
263
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
264
|
|
|
], |
|
265
|
|
|
] |
|
266
|
|
|
); |
|
267
|
|
|
} catch (BadResponseException $e) { |
|
268
|
|
|
throw new ApiException( |
|
269
|
|
|
$this->getSerializer()->deserialize( |
|
270
|
|
|
(string) $e->getResponse()->getBody(), |
|
271
|
|
|
Model\ErrorResponse::class, |
|
272
|
|
|
'json' |
|
273
|
|
|
), |
|
274
|
|
|
$e |
|
275
|
|
|
); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return $this->getSerializer()->deserialize( |
|
279
|
|
|
(string) $result->getBody(), |
|
280
|
|
|
Model\Merchant\Payment::class, |
|
281
|
|
|
'json' |
|
282
|
|
|
); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param string $paymentId |
|
287
|
|
|
* @param Model\Money $amount |
|
288
|
|
|
* @param string $merchantReference |
|
289
|
|
|
* @param string $requestId |
|
290
|
|
|
* |
|
291
|
|
|
* @return array|\JMS\Serializer\scalar|object |
|
292
|
|
|
*/ |
|
293
|
|
|
public function refund($paymentId, Model\Money $amount, $merchantReference = '', $requestId = '') |
|
294
|
|
|
{ |
|
295
|
|
|
$request = [ |
|
296
|
|
|
'amount' => $amount, |
|
297
|
|
|
'merchantReference' => $merchantReference, |
|
298
|
|
|
'requestId' => $requestId, |
|
299
|
|
|
]; |
|
300
|
|
|
|
|
301
|
|
|
try { |
|
302
|
|
|
$result = $this->getClient()->post( |
|
303
|
|
|
sprintf('payments/%s/refund', $paymentId), |
|
304
|
|
|
[ |
|
305
|
|
|
'auth' => [ |
|
306
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
|
|
307
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
|
|
308
|
|
|
], |
|
309
|
|
|
'headers' => [ |
|
310
|
|
|
'Accept' => 'application/json', |
|
311
|
|
|
'Content-Type' => 'application/json', |
|
312
|
|
|
], |
|
313
|
|
|
'body' => $this->getSerializer()->serialize($request, 'json'), |
|
314
|
|
|
] |
|
315
|
|
|
); |
|
316
|
|
|
} catch (BadResponseException $e) { |
|
317
|
|
|
throw new ApiException( |
|
318
|
|
|
$this->getSerializer()->deserialize( |
|
319
|
|
|
(string) $e->getResponse()->getBody(), |
|
320
|
|
|
Model\ErrorResponse::class, |
|
321
|
|
|
'json' |
|
322
|
|
|
), |
|
323
|
|
|
$e |
|
324
|
|
|
); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
return $this->getSerializer()->deserialize( |
|
328
|
|
|
(string) $result->getBody(), |
|
329
|
|
|
Model\Merchant\Refund::class, |
|
330
|
|
|
'json' |
|
331
|
|
|
); |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: