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 |
View Code Duplication |
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 |
View Code Duplication |
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 |
102
|
|
|
* @param int $id |
103
|
|
|
* @param string $token |
104
|
|
|
* @return bool |
105
|
|
|
* @throws SdkException |
106
|
|
|
*/ |
107
|
2 |
|
public function fulfilApplication($id, $token) |
108
|
|
|
{ |
109
|
2 |
|
return $this->requestAction('/v4/applications/' . $id . '/fulfil', [], $token); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param int $id |
114
|
|
|
* @param string $description |
115
|
|
|
* @param string $token |
116
|
|
|
* @return bool |
117
|
|
|
* @throws SdkException |
118
|
|
|
*/ |
119
|
2 |
|
public function cancelApplication($id, $description, $token) |
120
|
|
|
{ |
121
|
2 |
|
return $this->requestAction( |
122
|
2 |
|
'/v4/applications/' . $id . '/request-cancellation', |
123
|
2 |
|
['description' => $description], |
124
|
|
|
$token |
125
|
2 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $token |
130
|
|
|
* @return array |
131
|
|
|
* @throws SdkException |
132
|
|
|
*/ |
133
|
2 |
|
public function getPendingCancellations($installationId, $token) |
134
|
|
|
{ |
135
|
2 |
|
return $this->fetchDocument( |
136
|
2 |
|
'/v4/installations/' . $installationId . '/applications', |
137
|
2 |
|
$token, |
138
|
2 |
|
'Pending Cancellations', |
139
|
2 |
|
['pending-cancellations' => true] |
140
|
2 |
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get Merchant Payments. Filter Params can accept |
145
|
|
|
* - since, until : (string - ISO8601 Date Part Only) |
146
|
|
|
* - count, offset : (int) |
147
|
|
|
* |
148
|
|
|
* @author SL |
149
|
|
|
* @param $application |
150
|
|
|
* @param $token |
151
|
|
|
* @param array $filterParams |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
2 |
|
public function getMerchantPayments($application, $token, array $filterParams = []) |
155
|
|
|
{ |
156
|
2 |
|
return $this->fetchDocument( |
157
|
2 |
|
'/v4/applications/' . $application . '/merchant-payments', |
158
|
2 |
|
$token, |
159
|
2 |
|
'Merchant Payments', |
160
|
|
|
$filterParams |
161
|
2 |
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Add a Merchant Payment to the given application. Amount should be supplied in pence. |
166
|
|
|
* |
167
|
|
|
* @author SL |
168
|
|
|
* @param string $application |
169
|
|
|
* @param \DateTime $effectiveDate |
170
|
|
|
* @param int $amount |
171
|
|
|
* @param string $token |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
2 |
|
public function addMerchantPayment($application, \DateTime $effectiveDate, $amount, $token) |
175
|
|
|
{ |
176
|
2 |
|
return $this->postDocument( |
177
|
2 |
|
'/v4/applications/' . $application . '/merchant-payments', |
178
|
|
|
[ |
179
|
2 |
|
'amount' => $amount, |
180
|
2 |
|
'effective_date' => $effectiveDate->format('Y-m-d'), |
181
|
2 |
|
], |
182
|
2 |
|
$token, |
183
|
|
|
'Add Merchant Payment' |
184
|
2 |
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @author EB |
189
|
|
|
* @param $installationId |
190
|
|
|
* @param $application |
191
|
|
|
* @param $token |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
2 |
|
public function getApplicationCreditInfo($installationId, $application, $token) |
195
|
|
|
{ |
196
|
2 |
|
return $this->postDocument( |
197
|
2 |
|
'v4/installations/' . $installationId . '/applications/' . $application . '/get-credit-information', |
198
|
2 |
|
[], |
199
|
2 |
|
$token, |
200
|
|
|
'Application Credit Information' |
201
|
2 |
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get application history |
206
|
|
|
* |
207
|
|
|
* @author SL |
208
|
|
|
* @param $application |
209
|
|
|
* @param $token |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
2 |
|
public function getApplicationHistory($application, $token) |
213
|
|
|
{ |
214
|
2 |
|
return $this->fetchDocument( |
215
|
2 |
|
'/v4/applications/' . $application . '/status-history', |
216
|
2 |
|
$token, |
217
|
|
|
'Application Status History' |
218
|
2 |
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @author WN |
223
|
|
|
* @param string $action |
224
|
|
|
* @param array $data |
225
|
|
|
* @param string $token |
226
|
|
|
* @return bool |
227
|
|
|
* @throws SdkException |
228
|
|
|
*/ |
229
|
4 |
|
private function requestAction($action, $data, $token) |
230
|
|
|
{ |
231
|
4 |
|
$this->postDocument($action, $data, $token, 'Application'); |
232
|
4 |
|
return true; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.