Completed
Pull Request — develop (#32)
by
unknown
10:19
created

ApplicationGateway::getMerchantPayments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
/*
3
* This file is part of the PayBreak/basket package.
4
*
5
* (c) PayBreak <[email protected]>
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace PayBreak\Sdk\Gateways;
12
13
use PayBreak\Sdk\Entities\ApplicationEntity;
14
use PayBreak\Sdk\SdkException;
15
use WNowicki\Generic\ApiClient\ErrorResponseException;
16
17
/**
18
 * Application Gateway
19
 *
20
 * @author WN
21
 * @package PayBreak\Sdk\Gateways
22
 */
23
class ApplicationGateway extends AbstractGateway
24
{
25
    /**
26
     * @author WN
27
     * @param int $id
28
     * @param string $token
29
     * @return ApplicationEntity
30
     * @throws SdkException
31
     */
32 2
    public function getApplication($id, $token)
33
    {
34 2
        return ApplicationEntity::make($this->fetchDocument('/v4/applications/' . $id, $token, 'Application'));
35
    }
36
37
    /**
38
     * @author WN
39
     * @param ApplicationEntity $application
40
     * @param string $token
41
     * @return ApplicationEntity
42
     * @throws SdkException
43
     */
44 6
    public function initialiseApplication(ApplicationEntity $application, $token)
45
    {
46 6
        $api = $this->getApiFactory()->makeApiClient($token);
47
48
        try {
49 6
            $response = $api->post('/v4/initialize-application', $application->toArray(true));
50
51 2
            $application->setId($response['application']);
52 2
            $application->setResumeUrl($response['url']);
53
54 2
            return $application;
55
56 4
        } catch (ErrorResponseException $e) {
57
58 2
            $this->logWarning('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
59 2
            throw new SdkException($e->getMessage());
60
61 2
        } catch (\Exception $e) {
62
63 2
            $this->logError('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
64 2
            throw new SdkException('Problem Initialising Application on Provider API');
65
        }
66
    }
67
68
    /**
69
     * @author EA, EB
70
     * @param ApplicationEntity $application
71
     * @param string $token
72
     * @return ApplicationEntity
73
     * @throws SdkException
74
     */
75 2
    public function initialiseAssistedApplication(ApplicationEntity $application, $token)
76
    {
77
        $api = $this->getApiFactory()->makeApiClient($token);
78
79
        try {
80
            $response = $api->post('/v4/initialize-assisted-application', $application->toArray(true));
81
82 2
            $application->setId($response['application']);
83
            $application->setResumeUrl($response['url']);
84
            $application->setUser($response['user']);
85
86
            return $application;
87
88
        } catch (ErrorResponseException $e) {
89
90
            $this->logWarning('ApplicationGateway::initialiseAssistedApplication[' . $e->getCode() . ']: ' . $e->getMessage());
91
            throw new SdkException($e->getMessage());
92
93
        } catch (\Exception $e) {
94
95
            $this->logError('ApplicationGateway::initialiseAssistedApplication[' . $e->getCode() . ']: ' . $e->getMessage());
96
            throw new SdkException('Problem Initialising Assisted Application on Provider API');
97
        }
98
    }
99
100
    /**
101
     * @author WN, EB
102
     * @param int $id
103
     * @param string $token
104
     * @param string $reference
105
     * @return bool
106
     * @throws SdkException
107
     */
108 2
    public function fulfilApplication($id, $token, $reference = null)
109
    {
110 2
        return $this->requestAction('/v4/applications/' . $id . '/fulfil', ['reference' => $reference], $token);
111
    }
112
113
    /**
114
     * @param int $id
115
     * @param string $description
116
     * @param string $token
117
     * @return bool
118
     * @throws SdkException
119
     */
120 2
    public function cancelApplication($id, $description, $token)
121
    {
122 2
        return $this->requestAction(
123 2
            '/v4/applications/' . $id . '/request-cancellation',
124 2
            ['description' => $description],
125
            $token
126 2
        );
127
    }
128
129
    /**
130
     * @author JH
131
     * @param int $installationId
132
     * @param int $application
133
     * @param int $amount
134
     * @param string $description
135
     * @param string $token
136
     * @return array
137
     */
138
    public function amendOrder($installationId, $application, $amount, $description, $token)
139
    {
140
        return $this->postDocument(
141
            '/v4/installations/' . $installationId . '/applications/' . $application . '/amend',
142
            [
143
                'amount' => $amount,
144
                'description' => $description,
145
            ],
146
            $token,
147
            'Amend Order'
148
        );
149
    }
150
151
    /**
152
     * @param $token
153
     * @return array
154
     * @throws SdkException
155
     */
156 2
    public function getPendingCancellations($installationId, $token)
157
    {
158 2
        return $this->fetchDocument(
159 2
            '/v4/installations/' . $installationId . '/applications',
160 2
            $token,
161 2
            'Pending Cancellations',
162 2
            ['pending-cancellations' => true]
163 2
        );
164
    }
165
166
    /**
167
     * Get Merchant Payments. Filter Params can accept
168
     * - since, until : (string - ISO8601 Date Part Only)
169
     * - count, offset : (int)
170
     *
171
     * @author SL
172
     * @param $application
173
     * @param $token
174
     * @param array $filterParams
175
     * @return array
176
     */
177 2
    public function getMerchantPayments($application, $token, array $filterParams = [])
178
    {
179 2
        return $this->fetchDocument(
180 2
            '/v4/applications/' . $application . '/merchant-payments',
181 2
            $token,
182 2
            'Merchant Payments',
183
            $filterParams
184 2
        );
185
    }
186
187
    /**
188
     * Add a Merchant Payment to the given application. Amount should be supplied in pence.
189
     *
190
     * @author SL
191
     * @param string $application
192
     * @param \DateTime $effectiveDate
193
     * @param int $amount
194
     * @param string $token
195
     * @return array
196
     */
197 2
    public function addMerchantPayment($application, \DateTime $effectiveDate, $amount, $token)
198
    {
199 2
        return $this->postDocument(
200 2
            '/v4/applications/' . $application . '/merchant-payments',
201
            [
202 2
                'amount' => $amount,
203 2
                'effective_date' => $effectiveDate->format('Y-m-d'),
204 2
            ],
205 2
            $token,
206
            'Add Merchant Payment'
207 2
        );
208
    }
209
210
    /**
211
     * @author EB
212
     * @param $installationId
213
     * @param $application
214
     * @param $token
215
     * @return array
216
     */
217 2
    public function getApplicationCreditInfo($installationId, $application, $token)
218
    {
219 2
        return $this->postDocument(
220 2
            'v4/installations/' . $installationId . '/applications/' . $application . '/get-credit-information',
221 2
            [],
222 2
            $token,
223
            'Application Credit Information'
224 2
        );
225
    }
226
227
    /**
228
     * Get application history
229
     *
230
     * @author SL
231
     * @param $application
232
     * @param $token
233
     * @return array
234
     */
235 2
    public function getApplicationHistory($application, $token)
236
    {
237 2
        return $this->fetchDocument(
238 2
            '/v4/applications/' . $application . '/status-history',
239 2
            $token,
240
            'Application Status History'
241 2
        );
242
    }
243
244
    /**
245
     * @author WN
246
     * @param string $action
247
     * @param array $data
248
     * @param string $token
249
     * @return bool
250
     * @throws SdkException
251
     */
252 4
    private function requestAction($action, $data, $token)
253
    {
254 4
        $this->postDocument($action, $data, $token, 'Application');
255 4
        return true;
256
    }
257
}
258