ApplicationGateway::requestAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
/*
3
* This file is part of the PayBreak/paybreak-sdk-php 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 2
        } catch (ErrorResponseException $e) {
56 1
            $this->logWarning('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
57 1
            throw new SdkException($e->getMessage());
58 1
        } catch (\Exception $e) {
59 1
            $this->logError('ApplicationGateway::initialiseApplication[' . $e->getCode() . ']: ' . $e->getMessage());
60 1
            throw new SdkException('Problem Initialising Application on Provider API');
61
        }
62
    }
63
64
    /**
65
     * @author EA, EB
66
     * @param ApplicationEntity $application
67
     * @param string $token
68
     * @return ApplicationEntity
69
     * @throws SdkException
70
     */
71 1
    public function initialiseAssistedApplication(ApplicationEntity $application, $token)
72
    {
73
        $api = $this->getApiFactory()->makeApiClient($token);
74
75
        try {
76
            $response = $api->post('/v4/initialize-assisted-application', $application->toArray(true));
77
78
            $application->setId($response['application']);
79
            $application->setResumeUrl($response['url']);
80
            $application->setUser($response['user']);
81
82 1
            return $application;
83
        } catch (ErrorResponseException $e) {
84
            $this->logWarning('ApplicationGateway::initialiseAssistedApplication[' . $e->getCode() . ']: ' . $e->getMessage());
85
            throw new SdkException($e->getMessage());
86
        } catch (\Exception $e) {
87
            $this->logError('ApplicationGateway::initialiseAssistedApplication[' . $e->getCode() . ']: ' . $e->getMessage());
88
            throw new SdkException('Problem Initialising Assisted Application on Provider API');
89
        }
90
    }
91
92
    /**
93
     * @author WN, EB
94
     * @param int $id
95
     * @param string $token
96
     * @param string $reference
97
     * @return bool
98
     * @throws SdkException
99
     */
100 1
    public function fulfilApplication($id, $token, $reference = null)
101
    {
102 1
        return $this->requestAction('/v4/applications/' . $id . '/fulfil', ['reference' => $reference], $token);
103
    }
104
105
    /**
106
     * @param int $id
107
     * @param string $description
108
     * @param string $token
109
     * @return bool
110
     * @throws SdkException
111
     */
112 1
    public function cancelApplication($id, $description, $token)
113
    {
114 1
        return $this->requestAction(
115 1
            '/v4/applications/' . $id . '/request-cancellation',
116 1
            ['description' => $description],
117
            $token
118 1
        );
119
    }
120
121
    /**
122
     * @param $token
123
     * @return array
124
     * @throws SdkException
125
     */
126 1
    public function getPendingCancellations($installationId, $token)
127
    {
128 1
        return $this->fetchDocument(
129 1
            '/v4/installations/' . $installationId . '/applications',
130 1
            $token,
131 1
            'Pending Cancellations',
132 1
            ['pending-cancellations' => true]
133 1
        );
134
    }
135
136
    /**
137
     * Get Merchant Payments. Filter Params can accept
138
     * - since, until : (string - ISO8601 Date Part Only)
139
     * - count, offset : (int)
140
     *
141
     * @author SL
142
     * @param $application
143
     * @param $token
144
     * @param array $filterParams
145
     * @return array
146
     */
147 1
    public function getMerchantPayments($application, $token, array $filterParams = [])
148
    {
149 1
        return $this->fetchDocument(
150 1
            '/v4/applications/' . $application . '/merchant-payments',
151 1
            $token,
152 1
            'Merchant Payments',
153
            $filterParams
154 1
        );
155
    }
156
157
    /**
158
     * Add a Merchant Payment to the given application. Amount should be supplied in pence.
159
     *
160
     * @author SL
161
     * @param string $application
162
     * @param \DateTime $effectiveDate
163
     * @param int $amount
164
     * @param string $token
165
     * @return array
166
     */
167 1
    public function addMerchantPayment($application, \DateTime $effectiveDate, $amount, $token)
168
    {
169 1
        return $this->postDocument(
170 1
            '/v4/applications/' . $application . '/merchant-payments',
171
            [
172 1
                'amount' => $amount,
173 1
                'effective_date' => $effectiveDate->format('Y-m-d'),
174 1
            ],
175 1
            $token,
176
            'Add Merchant Payment'
177 1
        );
178
    }
179
180
    /**
181
     * @author EB
182
     * @param $installationId
183
     * @param $application
184
     * @param $token
185
     * @return array
186
     */
187 1
    public function getApplicationCreditInfo($installationId, $application, $token)
188
    {
189 1
        return $this->postDocument(
190 1
            'v4/installations/' . $installationId . '/applications/' . $application . '/get-credit-information',
191 1
            [],
192 1
            $token,
193
            'Application Credit Information'
194 1
        );
195
    }
196
197
    /**
198
     * Get application history
199
     *
200
     * @author SL
201
     * @param $application
202
     * @param $token
203
     * @return array
204
     */
205 1
    public function getApplicationHistory($application, $token)
206
    {
207 1
        return $this->fetchDocument(
208 1
            '/v4/applications/' . $application . '/status-history',
209 1
            $token,
210
            'Application Status History'
211 1
        );
212
    }
213
214
    /**
215
     * @author WN
216
     * @param string $action
217
     * @param array $data
218
     * @param string $token
219
     * @return bool
220
     * @throws SdkException
221
     */
222 2
    private function requestAction($action, $data, $token)
223
    {
224 2
        $this->postDocument($action, $data, $token, 'Application');
225 2
        return true;
226
    }
227
}
228