Passed
Push — master ( a738d8...ad4d7e )
by
unknown
03:09
created

Payments::capture()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 8.8571
cc 2
eloc 25
nc 2
nop 3
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\Payment;
8
use CultureKings\Afterpay\Model\PaymentsList;
9
use CultureKings\Afterpay\Traits\AuthorizationTrait;
10
use CultureKings\Afterpay\Traits\ClientTrait;
11
use CultureKings\Afterpay\Traits\SerializerTrait;
12
use GuzzleHttp\Client;
13
use GuzzleHttp\Exception\ClientException;
14
use GuzzleHttp\Query;
15
use JMS\Serializer\SerializerInterface;
16
17
/**
18
 * Class Payments
19
 *
20
 * @package CultureKings\Afterpay\Service
21
 */
22
class Payments
23
{
24
    use ClientTrait;
25
    use AuthorizationTrait;
26
    use SerializerTrait;
27
28
    /**
29
     * Payments constructor.
30
     * @param Client              $client
31
     * @param Authorization       $authorization
32
     * @param SerializerInterface $serializer
33
     */
34
    public function __construct(
35
        Client $client,
36
        Authorization $authorization,
37
        SerializerInterface $serializer
38
    ) {
39
        $this->setClient($client);
40
        $this->setAuthorization($authorization);
41
        $this->setSerializer($serializer);
42
    }
43
44
    /**
45
     * @param array $filters
46
     * @return array|object
47
     *
48
     * I would of liked to call this list() but it's a reserved keyword in < php7
49
     */
50
    public function listPayments(array $filters = [])
51
    {
52
        $query = new Query($filters);
53
        $query->setAggregator($query::duplicateAggregator());
54
55
        $result = $this->getClient()->get(
56
            'payments',
57
            [
58
                'auth' => [
59
                    $this->getAuthorization()->getMerchantId(),
60
                    $this->getAuthorization()->getSecret(),
61
                ],
62
                'query' => $query,
63
            ]
64
        );
65
66
        return $this->getSerializer()->deserialize(
67
            $result->getBody()->getContents(),
68
            PaymentsList::class,
69
            'json'
70
        );
71
    }
72
73
    /**
74
     * @param string $orderToken
75
     * @param string $merchantReference
76
     * @param string $webhookEventUrl
77
     *
78
     * @return Payment|object
79
     */
80
    public function capture($orderToken, $merchantReference = '', $webhookEventUrl = '')
81
    {
82
        $request = [
83
            'token' => $orderToken,
84
            'merchantReference' => $merchantReference,
85
            'webhookEventUrl' => $webhookEventUrl,
86
        ];
87
88
        try {
89
            $result = $this->getClient()->post(
90
                'payments/capture',
91
                [
92
                    'auth' => [
93
                        $this->getAuthorization()->getMerchantId(),
94
                        $this->getAuthorization()->getSecret(),
95
                    ],
96
                    'headers' => [
97
                        'Accept' => 'application/json',
98
                        'Content-Type' => 'application/json',
99
                    ],
100
                    'body' => $this->getSerializer()->serialize($request, 'json'),
101
                ]
102
            );
103
        } catch (ClientException $e) {
104
            throw new ApiException(
105
                $this->getSerializer()->deserialize(
106
                    $e->getResponse()->getBody()->getContents(),
107
                    ErrorResponse::class,
108
                    'json'
109
                )
110
            );
111
        }
112
113
        return $this->getSerializer()->deserialize(
114
            $result->getBody()->getContents(),
115
            Payment::class,
116
            'json'
117
        );
118
    }
119
120
    /**
121
     * @param string $id
122
     * @return Payment|object
123
     */
124
    public function get($id)
125
    {
126
        try {
127
            $result = $this->getClient()->get(
128
                sprintf('payments/%s', $id),
129
                [
130
                    'auth' => [
131
                        $this->getAuthorization()->getMerchantId(),
132
                        $this->getAuthorization()->getSecret(),
133
                    ],
134
                ]
135
            );
136
        } catch (ClientException $e) {
137
            throw new ApiException(
138
                $this->getSerializer()->deserialize(
139
                    $e->getResponse()->getBody()->getContents(),
140
                    ErrorResponse::class,
141
                    'json'
142
                )
143
            );
144
        }
145
146
        return $this->getSerializer()->deserialize(
147
            $result->getBody()->getContents(),
148
            Payment::class,
149
            'json'
150
        );
151
    }
152
153
    /**
154
     * @param string $token
155
     * @return Payment|object
156
     * @throws ApiException
157
     */
158
    public function getByToken($token)
159
    {
160
        try {
161
            $result = $this->getClient()->get(
162
                sprintf('payments/token:%s', $token),
163
                [
164
                    'auth' => [
165
                        $this->getAuthorization()->getMerchantId(),
166
                        $this->getAuthorization()->getSecret(),
167
                    ],
168
                ]
169
            );
170
        } catch (ClientException $e) {
171
            throw new ApiException(
172
                $this->getSerializer()->deserialize(
173
                    $e->getResponse()->getBody()->getContents(),
174
                    ErrorResponse::class,
175
                    'json'
176
                )
177
            );
178
        }
179
180
181
        return $this->getSerializer()->deserialize(
182
            $result->getBody()->getContents(),
183
            Payment::class,
184
            'json'
185
        );
186
    }
187
188
    /**
189
     * @param string $orderToken
190
     * @param string $merchantReference
191
     * @param string $webhookEventUrl
192
     * @return Payment|object
193
     */
194
    public function authorise($orderToken, $merchantReference = '', $webhookEventUrl = '')
195
    {
196
        $request = [
197
            'token' => $orderToken,
198
            'merchantReference' => $merchantReference,
199
            'webhookEventUrl' => $webhookEventUrl,
200
        ];
201
202
        try {
203
            $result = $this->getClient()->post(
204
                'payments',
205
                [
206
                    'auth' => [
207
                        $this->getAuthorization()->getMerchantId(),
208
                        $this->getAuthorization()->getSecret(),
209
                    ],
210
                    'headers' => [
211
                        'Accept' => 'application/json',
212
                        'Content-Type' => 'application/json',
213
                    ],
214
                    'body' => $this->getSerializer()->serialize($request, 'json'),
215
                ]
216
            );
217
        } catch (ClientException $e) {
218
            throw new ApiException(
219
                $this->getSerializer()->deserialize(
220
                    $e->getResponse()->getBody()->getContents(),
221
                    ErrorResponse::class,
222
                    'json'
223
                )
224
            );
225
        }
226
227
        return $this->getSerializer()->deserialize(
228
            $result->getBody()->getContents(),
229
            Payment::class,
230
            'json'
231
        );
232
    }
233
234
    /**
235
     * @param string $paymentId
236
     * @return Payment|object
237
     */
238
    public function void($paymentId)
239
    {
240
        try {
241
            $result = $this->getClient()->post(
242
                sprintf('payments/%s/void', $paymentId),
243
                [
244
                    'auth' => [
245
                        $this->getAuthorization()->getMerchantId(),
246
                        $this->getAuthorization()->getSecret(),
247
                    ],
248
                ]
249
            );
250
        } catch (ClientException $e) {
251
            throw new ApiException(
252
                $this->getSerializer()->deserialize(
253
                    $e->getResponse()->getBody()->getContents(),
254
                    ErrorResponse::class,
255
                    'json'
256
                )
257
            );
258
        }
259
260
        return $this->getSerializer()->deserialize(
261
            $result->getBody()->getContents(),
262
            Payment::class,
263
            'json'
264
        );
265
    }
266
}
267