1
|
|
|
<?php |
2
|
|
|
namespace CultureKings\Afterpay\Service; |
3
|
|
|
|
4
|
|
|
use CultureKings\Afterpay\Exception\ApiException; |
5
|
|
|
use CultureKings\Afterpay\Model\Authorization; |
6
|
|
|
use CultureKings\Afterpay\Model\ErrorResponse; |
7
|
|
|
use CultureKings\Afterpay\Model\Money; |
8
|
|
|
use CultureKings\Afterpay\Model\Payment; |
9
|
|
|
use CultureKings\Afterpay\Model\PaymentsList; |
10
|
|
|
use CultureKings\Afterpay\Model\Refund; |
11
|
|
|
use CultureKings\Afterpay\Traits\AuthorizationTrait; |
12
|
|
|
use CultureKings\Afterpay\Traits\ClientTrait; |
13
|
|
|
use CultureKings\Afterpay\Traits\SerializerTrait; |
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\Exception\ClientException; |
16
|
|
|
use GuzzleHttp\Query; |
17
|
|
|
use JMS\Serializer\SerializerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Payments |
21
|
|
|
* |
22
|
|
|
* @package CultureKings\Afterpay\Service |
23
|
|
|
*/ |
24
|
|
|
class Payments |
25
|
|
|
{ |
26
|
|
|
use ClientTrait; |
27
|
|
|
use AuthorizationTrait; |
28
|
|
|
use SerializerTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Payments constructor. |
32
|
|
|
* @param Client $client |
33
|
|
|
* @param Authorization $authorization |
34
|
|
|
* @param SerializerInterface $serializer |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
Client $client, |
38
|
|
|
Authorization $authorization, |
39
|
|
|
SerializerInterface $serializer |
40
|
|
|
) { |
41
|
|
|
$this->setClient($client); |
42
|
|
|
$this->setAuthorization($authorization); |
43
|
|
|
$this->setSerializer($serializer); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $filters |
48
|
|
|
* @return array|object |
49
|
|
|
* |
50
|
|
|
* I would of liked to call this list() but it's a reserved keyword in < php7 |
51
|
|
|
*/ |
52
|
|
|
public function listPayments(array $filters = []) |
53
|
|
|
{ |
54
|
|
|
$query = new Query($filters); |
55
|
|
|
$query->setAggregator($query::duplicateAggregator()); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
$result = $this->getClient()->get( |
59
|
|
|
'payments', |
60
|
|
|
[ |
61
|
|
|
'auth' => [ |
62
|
|
|
$this->getAuthorization()->getMerchantId(), |
63
|
|
|
$this->getAuthorization()->getSecret(), |
64
|
|
|
], |
65
|
|
|
'query' => $query, |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
} catch (ClientException $e) { |
69
|
|
|
throw new ApiException( |
70
|
|
|
$this->getSerializer()->deserialize( |
71
|
|
|
$e->getResponse()->getBody()->getContents(), |
72
|
|
|
ErrorResponse::class, |
73
|
|
|
'json' |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->getSerializer()->deserialize( |
79
|
|
|
$result->getBody()->getContents(), |
80
|
|
|
PaymentsList::class, |
81
|
|
|
'json' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $orderToken |
87
|
|
|
* @param string $merchantReference |
88
|
|
|
* @param string $webhookEventUrl |
89
|
|
|
* |
90
|
|
|
* @return 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
|
|
|
$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' => $this->getSerializer()->serialize($request, 'json'), |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} catch (ClientException $e) { |
116
|
|
|
throw new ApiException( |
117
|
|
|
$this->getSerializer()->deserialize( |
118
|
|
|
$e->getResponse()->getBody()->getContents(), |
119
|
|
|
ErrorResponse::class, |
120
|
|
|
'json' |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->getSerializer()->deserialize( |
126
|
|
|
$result->getBody()->getContents(), |
127
|
|
|
Payment::class, |
128
|
|
|
'json' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $id |
134
|
|
|
* @return 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 (ClientException $e) { |
149
|
|
|
throw new ApiException( |
150
|
|
|
$this->getSerializer()->deserialize( |
151
|
|
|
$e->getResponse()->getBody()->getContents(), |
152
|
|
|
ErrorResponse::class, |
153
|
|
|
'json' |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->getSerializer()->deserialize( |
159
|
|
|
$result->getBody()->getContents(), |
160
|
|
|
Payment::class, |
161
|
|
|
'json' |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $token |
167
|
|
|
* @return 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 (ClientException $e) { |
183
|
|
|
throw new ApiException( |
184
|
|
|
$this->getSerializer()->deserialize( |
185
|
|
|
$e->getResponse()->getBody()->getContents(), |
186
|
|
|
ErrorResponse::class, |
187
|
|
|
'json' |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
return $this->getSerializer()->deserialize( |
194
|
|
|
$result->getBody()->getContents(), |
195
|
|
|
Payment::class, |
196
|
|
|
'json' |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $orderToken |
202
|
|
|
* @param string $merchantReference |
203
|
|
|
* @param string $webhookEventUrl |
204
|
|
|
* @return 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 (ClientException $e) { |
230
|
|
|
throw new ApiException( |
231
|
|
|
$this->getSerializer()->deserialize( |
232
|
|
|
$e->getResponse()->getBody()->getContents(), |
233
|
|
|
ErrorResponse::class, |
234
|
|
|
'json' |
235
|
|
|
) |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->getSerializer()->deserialize( |
240
|
|
|
$result->getBody()->getContents(), |
241
|
|
|
Payment::class, |
242
|
|
|
'json' |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $paymentId |
248
|
|
|
* @return 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 (ClientException $e) { |
263
|
|
|
throw new ApiException( |
264
|
|
|
$this->getSerializer()->deserialize( |
265
|
|
|
$e->getResponse()->getBody()->getContents(), |
266
|
|
|
ErrorResponse::class, |
267
|
|
|
'json' |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $this->getSerializer()->deserialize( |
273
|
|
|
$result->getBody()->getContents(), |
274
|
|
|
Payment::class, |
275
|
|
|
'json' |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $paymentId |
281
|
|
|
* @param Money $amount |
282
|
|
|
* @param string $merchantReference |
283
|
|
|
* @return array|\JMS\Serializer\scalar|object |
284
|
|
|
*/ |
285
|
|
|
public function refund($paymentId, Money $amount, $merchantReference = '') |
286
|
|
|
{ |
287
|
|
|
$request = [ |
288
|
|
|
'amount' => $amount, |
289
|
|
|
'merchantReference' => $merchantReference, |
290
|
|
|
]; |
291
|
|
|
|
292
|
|
|
try { |
293
|
|
|
$result = $this->getClient()->post( |
294
|
|
|
sprintf('payments/%s/refund', $paymentId), |
295
|
|
|
[ |
296
|
|
|
'auth' => [ |
297
|
|
|
$this->getAuthorization()->getMerchantId(), |
298
|
|
|
$this->getAuthorization()->getSecret(), |
299
|
|
|
], |
300
|
|
|
'headers' => [ |
301
|
|
|
'Accept' => 'application/json', |
302
|
|
|
'Content-Type' => 'application/json', |
303
|
|
|
], |
304
|
|
|
'body' => $this->getSerializer()->serialize($request, 'json'), |
305
|
|
|
] |
306
|
|
|
); |
307
|
|
|
} catch (ClientException $e) { |
308
|
|
|
throw new ApiException( |
309
|
|
|
$this->getSerializer()->deserialize( |
310
|
|
|
$e->getResponse()->getBody()->getContents(), |
311
|
|
|
ErrorResponse::class, |
312
|
|
|
'json' |
313
|
|
|
) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return $this->getSerializer()->deserialize( |
318
|
|
|
$result->getBody()->getContents(), |
319
|
|
|
Refund::class, |
320
|
|
|
'json' |
321
|
|
|
); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|