Completed
Pull Request — develop (#39)
by Gergely
12:07 queued 10:03
created

initialiseAssistedApplication()   A

Complexity

Conditions 3
Paths 10

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8.8593

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 2
cts 15
cp 0.1333
rs 9.2
c 0
b 0
f 0
cc 3
eloc 13
nc 10
nop 2
crap 8.8593
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 PayBreak\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 1
    public function getApplication($id, $token)
33
    {
34 1
        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 3
    public function initialiseApplication(ApplicationEntity $application, $token)
45
    {
46 3
        $api = $this->getApiFactory()->makeApiClient($token);
47
48
        try {
49 3
            $response = $api->post('/v4/initialize-application', $application->toArray(true));
50
51 1
            $application->setId($response['application']);
52 1
            $application->setResumeUrl($response['url']);
53
54 1
            return $application;
55
56 2
        } catch (ErrorResponseException $e) {
57
58 1
            $this->logWarning('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
59 1
            throw new SdkException($e->getMessage());
60
61 1
        } catch (\Exception $e) {
62
63 1
            $this->logError('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
64 1
            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 1
    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 1
            $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 1
    public function fulfilApplication($id, $token, $reference = null)
109
    {
110 1
        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 1
    public function cancelApplication($id, $description, $token)
121
    {
122 1
        return $this->requestAction(
123 1
            '/v4/applications/' . $id . '/request-cancellation',
124 1
            ['description' => $description],
125
            $token
126 1
        );
127
    }
128
129
    /**
130
     * @param $token
131
     * @return array
132
     * @throws SdkException
133
     */
134 1
    public function getPendingCancellations($installationId, $token)
135
    {
136 1
        return $this->fetchDocument(
137 1
            '/v4/installations/' . $installationId . '/applications',
138 1
            $token,
139 1
            'Pending Cancellations',
140 1
            ['pending-cancellations' => true]
141 1
        );
142
    }
143
144
    /**
145
     * Get Merchant Payments. Filter Params can accept
146
     * - since, until : (string - ISO8601 Date Part Only)
147
     * - count, offset : (int)
148
     *
149
     * @author SL
150
     * @param $application
151
     * @param $token
152
     * @param array $filterParams
153
     * @return array
154
     */
155 1
    public function getMerchantPayments($application, $token, array $filterParams = [])
156
    {
157 1
        return $this->fetchDocument(
158 1
            '/v4/applications/' . $application . '/merchant-payments',
159 1
            $token,
160 1
            'Merchant Payments',
161
            $filterParams
162 1
        );
163
    }
164
165
    /**
166
     * Add a Merchant Payment to the given application. Amount should be supplied in pence.
167
     *
168
     * @author SL
169
     * @param string $application
170
     * @param \DateTime $effectiveDate
171
     * @param int $amount
172
     * @param string $token
173
     * @return array
174
     */
175 1
    public function addMerchantPayment($application, \DateTime $effectiveDate, $amount, $token)
176
    {
177 1
        return $this->postDocument(
178 1
            '/v4/applications/' . $application . '/merchant-payments',
179
            [
180 1
                'amount' => $amount,
181 1
                'effective_date' => $effectiveDate->format('Y-m-d'),
182 1
            ],
183 1
            $token,
184
            'Add Merchant Payment'
185 1
        );
186
    }
187
188
    /**
189
     * @author EB
190
     * @param $installationId
191
     * @param $application
192
     * @param $token
193
     * @return array
194
     */
195 1
    public function getApplicationCreditInfo($installationId, $application, $token)
196
    {
197 1
        return $this->postDocument(
198 1
            'v4/installations/' . $installationId . '/applications/' . $application . '/get-credit-information',
199 1
            [],
200 1
            $token,
201
            'Application Credit Information'
202 1
        );
203
    }
204
205
    /**
206
     * Get application history
207
     *
208
     * @author SL
209
     * @param $application
210
     * @param $token
211
     * @return array
212
     */
213 1
    public function getApplicationHistory($application, $token)
214
    {
215 1
        return $this->fetchDocument(
216 1
            '/v4/applications/' . $application . '/status-history',
217 1
            $token,
218
            'Application Status History'
219 1
        );
220
    }
221
222
    /**
223
     * @author WN
224
     * @param string $action
225
     * @param array $data
226
     * @param string $token
227
     * @return bool
228
     * @throws SdkException
229
     */
230 2
    private function requestAction($action, $data, $token)
231
    {
232 2
        $this->postDocument($action, $data, $token, 'Application');
233 2
        return true;
234
    }
235
}
236