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