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
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->getSerializer()->deserialize( |
78
|
|
|
(string) $result->getBody(), |
79
|
|
|
Model\Merchant\PaymentsList::class, |
80
|
|
|
'json' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $orderToken |
86
|
|
|
* @param string $merchantReference |
87
|
|
|
* @param string $webhookEventUrl |
88
|
|
|
* |
89
|
|
|
* @return Model\Merchant\Payment|object |
90
|
|
|
*/ |
91
|
|
|
public function capture($orderToken, $merchantReference = '', $webhookEventUrl = '') |
92
|
|
|
{ |
93
|
|
|
$request = [ |
94
|
|
|
'token' => $orderToken, |
95
|
|
|
'merchantReference' => $merchantReference, |
96
|
|
|
'webhookEventUrl' => $webhookEventUrl, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$requestBody = $this->getSerializer()->serialize($request, 'json'); |
101
|
|
|
$result = $this->getClient()->post( |
102
|
|
|
'payments/capture', |
103
|
|
|
[ |
104
|
|
|
'auth' => [ |
105
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
106
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
107
|
|
|
], |
108
|
|
|
'headers' => [ |
109
|
|
|
'Accept' => 'application/json', |
110
|
|
|
'Content-Type' => 'application/json', |
111
|
|
|
], |
112
|
|
|
'body' => $requestBody, |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} catch (BadResponseException $e) { |
116
|
|
|
throw new ApiException( |
117
|
|
|
$this->getSerializer()->deserialize( |
118
|
|
|
(string) $e->getResponse()->getBody(), |
119
|
|
|
Model\ErrorResponse::class, |
120
|
|
|
'json' |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->getSerializer()->deserialize( |
126
|
|
|
(string) $result->getBody(), |
127
|
|
|
Model\Merchant\Payment::class, |
128
|
|
|
'json' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $id |
134
|
|
|
* @return Model\Merchant\Payment|object |
135
|
|
|
*/ |
136
|
|
|
public function get($id) |
137
|
|
|
{ |
138
|
|
|
try { |
139
|
|
|
$result = $this->getClient()->get( |
140
|
|
|
sprintf('payments/%s', $id), |
141
|
|
|
[ |
142
|
|
|
'auth' => [ |
143
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
144
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
145
|
|
|
], |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
} catch (BadResponseException $e) { |
149
|
|
|
throw new ApiException( |
150
|
|
|
$this->getSerializer()->deserialize( |
151
|
|
|
(string) $e->getResponse()->getBody(), |
152
|
|
|
Model\ErrorResponse::class, |
153
|
|
|
'json' |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->getSerializer()->deserialize( |
159
|
|
|
(string) $result->getBody(), |
160
|
|
|
Model\Merchant\Payment::class, |
161
|
|
|
'json' |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $token |
167
|
|
|
* @return Model\Merchant\Payment|object |
168
|
|
|
* @throws ApiException |
169
|
|
|
*/ |
170
|
|
|
public function getByToken($token) |
171
|
|
|
{ |
172
|
|
|
try { |
173
|
|
|
$result = $this->getClient()->get( |
174
|
|
|
sprintf('payments/token:%s', $token), |
175
|
|
|
[ |
176
|
|
|
'auth' => [ |
177
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
178
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
179
|
|
|
], |
180
|
|
|
] |
181
|
|
|
); |
182
|
|
|
} catch (BadResponseException $e) { |
183
|
|
|
throw new ApiException( |
184
|
|
|
$this->getSerializer()->deserialize( |
185
|
|
|
(string) $e->getResponse()->getBody(), |
186
|
|
|
Model\ErrorResponse::class, |
187
|
|
|
'json' |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
return $this->getSerializer()->deserialize( |
194
|
|
|
(string) $result->getBody(), |
195
|
|
|
Model\Merchant\Payment::class, |
196
|
|
|
'json' |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $orderToken |
202
|
|
|
* @param string $merchantReference |
203
|
|
|
* @param string $webhookEventUrl |
204
|
|
|
* @return Model\Merchant\Payment|object |
205
|
|
|
*/ |
206
|
|
|
public function authorise($orderToken, $merchantReference = '', $webhookEventUrl = '') |
207
|
|
|
{ |
208
|
|
|
$request = [ |
209
|
|
|
'token' => $orderToken, |
210
|
|
|
'merchantReference' => $merchantReference, |
211
|
|
|
'webhookEventUrl' => $webhookEventUrl, |
212
|
|
|
]; |
213
|
|
|
|
214
|
|
|
try { |
215
|
|
|
$result = $this->getClient()->post( |
216
|
|
|
'payments', |
217
|
|
|
[ |
218
|
|
|
'auth' => [ |
219
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
220
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
221
|
|
|
], |
222
|
|
|
'headers' => [ |
223
|
|
|
'Accept' => 'application/json', |
224
|
|
|
'Content-Type' => 'application/json', |
225
|
|
|
], |
226
|
|
|
'body' => $this->getSerializer()->serialize($request, 'json'), |
227
|
|
|
] |
228
|
|
|
); |
229
|
|
|
} catch (BadResponseException $e) { |
230
|
|
|
throw new ApiException( |
231
|
|
|
$this->getSerializer()->deserialize( |
232
|
|
|
(string) $e->getResponse()->getBody(), |
233
|
|
|
Model\ErrorResponse::class, |
234
|
|
|
'json' |
235
|
|
|
) |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->getSerializer()->deserialize( |
240
|
|
|
(string) $result->getBody(), |
241
|
|
|
Model\Merchant\Payment::class, |
242
|
|
|
'json' |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $paymentId |
248
|
|
|
* @return Model\Merchant\Payment|object |
249
|
|
|
*/ |
250
|
|
|
public function void($paymentId) |
251
|
|
|
{ |
252
|
|
|
try { |
253
|
|
|
$result = $this->getClient()->post( |
254
|
|
|
sprintf('payments/%s/void', $paymentId), |
255
|
|
|
[ |
256
|
|
|
'auth' => [ |
257
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
258
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
259
|
|
|
], |
260
|
|
|
] |
261
|
|
|
); |
262
|
|
|
} catch (BadResponseException $e) { |
263
|
|
|
throw new ApiException( |
264
|
|
|
$this->getSerializer()->deserialize( |
265
|
|
|
(string) $e->getResponse()->getBody(), |
266
|
|
|
Model\ErrorResponse::class, |
267
|
|
|
'json' |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $this->getSerializer()->deserialize( |
273
|
|
|
(string) $result->getBody(), |
274
|
|
|
Model\Merchant\Payment::class, |
275
|
|
|
'json' |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $paymentId |
281
|
|
|
* @param Model\Money $amount |
282
|
|
|
* @param string $merchantReference |
283
|
|
|
* @param string $requestId |
284
|
|
|
* |
285
|
|
|
* @return array|\JMS\Serializer\scalar|object |
286
|
|
|
*/ |
287
|
|
|
public function refund($paymentId, Model\Money $amount, $merchantReference = '', $requestId = '') |
288
|
|
|
{ |
289
|
|
|
$request = [ |
290
|
|
|
'amount' => $amount, |
291
|
|
|
'merchantReference' => $merchantReference, |
292
|
|
|
'requestId' => $requestId, |
293
|
|
|
]; |
294
|
|
|
|
295
|
|
|
try { |
296
|
|
|
$result = $this->getClient()->post( |
297
|
|
|
sprintf('payments/%s/refund', $paymentId), |
298
|
|
|
[ |
299
|
|
|
'auth' => [ |
300
|
|
|
$this->getAuthorization()->getMerchantId(), |
|
|
|
|
301
|
|
|
$this->getAuthorization()->getSecret(), |
|
|
|
|
302
|
|
|
], |
303
|
|
|
'headers' => [ |
304
|
|
|
'Accept' => 'application/json', |
305
|
|
|
'Content-Type' => 'application/json', |
306
|
|
|
], |
307
|
|
|
'body' => $this->getSerializer()->serialize($request, 'json'), |
308
|
|
|
] |
309
|
|
|
); |
310
|
|
|
} catch (BadResponseException $e) { |
311
|
|
|
throw new ApiException( |
312
|
|
|
$this->getSerializer()->deserialize( |
313
|
|
|
(string) $e->getResponse()->getBody(), |
314
|
|
|
Model\ErrorResponse::class, |
315
|
|
|
'json' |
316
|
|
|
) |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return $this->getSerializer()->deserialize( |
321
|
|
|
(string) $result->getBody(), |
322
|
|
|
Model\Merchant\Refund::class, |
323
|
|
|
'json' |
324
|
|
|
); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
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: