1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 8 |
5
|
|
|
* |
6
|
|
|
* @category Action |
7
|
|
|
* @package Payever\Payments |
8
|
|
|
* @author payever GmbH <[email protected]> |
9
|
|
|
* @copyright 2017-2021 payever GmbH |
10
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
11
|
|
|
* @link https://docs.payever.org/shopsystems/api/getting-started |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Payever\ExternalIntegration\Payments\Action; |
15
|
|
|
|
16
|
|
|
use Payever\ExternalIntegration\Payments\Base\PaymentsApiClientInterface; |
17
|
|
|
use Payever\ExternalIntegration\Payments\Http\MessageEntity\GetTransactionResultEntity; |
18
|
|
|
use Payever\ExternalIntegration\Payments\Http\ResponseEntity\GetTransactionResponse; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class represents payment actions used in Payever API |
22
|
|
|
* @SuppressWarnings(PHPMD.MissingImport) |
23
|
|
|
*/ |
24
|
|
|
class ActionDecider implements ActionDeciderInterface |
25
|
|
|
{ |
26
|
|
|
/** @var PaymentsApiClientInterface */ |
27
|
|
|
protected $api; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AbstractActionDecider constructor. |
31
|
|
|
* |
32
|
|
|
* @param PaymentsApiClientInterface $api |
33
|
|
|
*/ |
34
|
|
|
public function __construct(PaymentsApiClientInterface $api) |
35
|
|
|
{ |
36
|
|
|
$this->api = $api; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
42
|
|
|
*/ |
43
|
|
|
public function isActionAllowed($paymentId, $transactionAction, $throwException = true) |
44
|
|
|
{ |
45
|
|
|
$this->assertArguments($transactionAction, $paymentId); |
46
|
|
|
if (in_array($this->getPolyfillAction($transactionAction), $this->getEnabledActions($paymentId), true)) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->assertFound($throwException, $transactionAction, $paymentId); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check if the cancel action for the transaction is allowed |
55
|
|
|
* |
56
|
|
|
* @param string $paymentId |
57
|
|
|
* @param bool $throwException |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
* |
61
|
|
|
* @throws \Exception when $throwException is true and target action is not allowed |
62
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
63
|
|
|
*/ |
64
|
|
|
public function isCancelAllowed($paymentId, $throwException = true) |
65
|
|
|
{ |
66
|
|
|
return $this->isActionAllowed($paymentId, static::ACTION_CANCEL, $throwException); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if the refund action for the transaction is allowed |
71
|
|
|
* |
72
|
|
|
* @param string $paymentId |
73
|
|
|
* @param bool $throwException |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
* |
77
|
|
|
* @throws \Exception when $throwException is true and target action is not allowed |
78
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
79
|
|
|
*/ |
80
|
|
|
public function isRefundAllowed($paymentId, $throwException = true) |
81
|
|
|
{ |
82
|
|
|
return $this->isActionAllowed($paymentId, static::ACTION_REFUND, $throwException); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check if the shipping goods action for the transaction is allowed |
87
|
|
|
* |
88
|
|
|
* @param string $paymentId |
89
|
|
|
* @param bool $throwException |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
* |
93
|
|
|
* @throws \Exception when $throwException is true and target action is not allowed |
94
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
95
|
|
|
*/ |
96
|
|
|
public function isShippingAllowed($paymentId, $throwException = true) |
97
|
|
|
{ |
98
|
|
|
return $this->isActionAllowed($paymentId, static::ACTION_SHIPPING_GOODS, $throwException); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
104
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
105
|
|
|
*/ |
106
|
|
|
public function isPartialActionAllowed( |
107
|
|
|
$paymentId, |
108
|
|
|
$transactionAction, |
109
|
|
|
$throwException = true |
110
|
|
|
) { |
111
|
|
|
$response = $this->api->getTransactionRequest($paymentId); |
112
|
|
|
|
113
|
|
|
/** @var GetTransactionResponse $getTransactionEntity */ |
114
|
|
|
$getTransactionEntity = $response->getResponseEntity(); |
115
|
|
|
|
116
|
|
|
/** @var GetTransactionResultEntity $getTransactionResult */ |
117
|
|
|
$getTransactionResult = $getTransactionEntity->getResult(); |
118
|
|
|
$actions = $getTransactionResult->getActions(); |
119
|
|
|
|
120
|
|
|
foreach ($actions as $action) { |
121
|
|
|
if (!is_object($action) || !isset($action->action, $action->enabled) || !(bool) $action->enabled) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ( |
126
|
|
|
$transactionAction === $this->getPolyfillAction($action->action) && |
127
|
|
|
property_exists($action, 'partialAllowed') && |
128
|
|
|
$action->partialAllowed |
129
|
|
|
) { |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($throwException) { |
135
|
|
|
throw new ActionNotAllowedException(sprintf( |
136
|
|
|
'Partial action "%s" is not allowed for payment id "%s"', |
137
|
|
|
$transactionAction, |
138
|
|
|
$paymentId |
139
|
|
|
)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if the partial cancel action for the transaction is allowed |
147
|
|
|
* |
148
|
|
|
* @param string $paymentId |
149
|
|
|
* @param bool $throwException |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
* |
153
|
|
|
* @throws \Exception when $throwException is true and target action is not allowed |
154
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
155
|
|
|
*/ |
156
|
|
|
public function isPartialCancelAllowed($paymentId, $throwException = true) |
157
|
|
|
{ |
158
|
|
|
return $this->isPartialActionAllowed( |
159
|
|
|
$paymentId, |
160
|
|
|
static::ACTION_CANCEL, |
161
|
|
|
$throwException |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Check if the partial refund action for the transaction is allowed |
168
|
|
|
* |
169
|
|
|
* @param string $paymentId |
170
|
|
|
* @param bool $throwException |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
* |
174
|
|
|
* @throws \Exception when $throwException is true and target action is not allowed |
175
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
176
|
|
|
*/ |
177
|
|
|
public function isPartialRefundAllowed($paymentId, $throwException = true) |
178
|
|
|
{ |
179
|
|
|
return $this->isPartialActionAllowed( |
180
|
|
|
$paymentId, |
181
|
|
|
static::ACTION_REFUND, |
182
|
|
|
$throwException |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Check if the partial shipping goods action for the transaction is allowed |
188
|
|
|
* |
189
|
|
|
* @param string $paymentId |
190
|
|
|
* @param bool $throwException |
191
|
|
|
* |
192
|
|
|
* @return bool |
193
|
|
|
* |
194
|
|
|
* @throws \Exception when $throwException is true and target action is not allowed |
195
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
196
|
|
|
*/ |
197
|
|
|
public function isPartialShippingAllowed($paymentId, $throwException = true) |
198
|
|
|
{ |
199
|
|
|
return $this->isPartialActionAllowed( |
200
|
|
|
$paymentId, |
201
|
|
|
static::ACTION_SHIPPING_GOODS, |
202
|
|
|
$throwException |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $paymentId |
208
|
|
|
* @return string[] |
209
|
|
|
*/ |
210
|
|
|
protected function getEnabledActions($paymentId) |
211
|
|
|
{ |
212
|
|
|
$response = $this->api->getTransactionRequest($paymentId); |
213
|
|
|
/** @var GetTransactionResponse $getTransactionEntity */ |
214
|
|
|
$getTransactionEntity = $response->getResponseEntity(); |
215
|
|
|
/** @var GetTransactionResultEntity $getTransactionResult */ |
216
|
|
|
$getTransactionResult = $getTransactionEntity->getResult(); |
217
|
|
|
$actions = $getTransactionResult->getActions(); |
218
|
|
|
$enabledActions = []; |
219
|
|
|
foreach ($actions as $action) { |
220
|
|
|
if (!is_object($action) || !isset($action->action, $action->enabled) || !(bool) $action->enabled) { |
221
|
|
|
continue; |
222
|
|
|
} |
223
|
|
|
$enabledActions[] = $this->getPolyfillAction($action->action); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $enabledActions; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $action |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
protected function getPolyfillAction($action) |
234
|
|
|
{ |
235
|
|
|
if ($action === ActionDeciderInterface::ACTION_RETURN) { |
|
|
|
|
236
|
|
|
$action = ActionDeciderInterface::ACTION_REFUND; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $action; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string|null $transactionAction |
244
|
|
|
* @param string|null $paymentId |
245
|
|
|
* @throws \Exception |
246
|
|
|
*/ |
247
|
|
|
protected function assertArguments($transactionAction, $paymentId) |
248
|
|
|
{ |
249
|
|
|
if (empty($transactionAction) || empty($paymentId)) { |
250
|
|
|
throw new \Exception('Wrong arguments.'); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param bool $throwException |
256
|
|
|
* @param string $transactionAction |
257
|
|
|
* @param string $paymentId |
258
|
|
|
* @return bool |
259
|
|
|
* @throws ActionNotAllowedException |
260
|
|
|
*/ |
261
|
|
|
protected function assertFound($throwException, $transactionAction, $paymentId) |
262
|
|
|
{ |
263
|
|
|
if ($throwException) { |
264
|
|
|
$message = sprintf( |
265
|
|
|
'Action "%s" is not allowed for payment id "%s"', |
266
|
|
|
$transactionAction, |
267
|
|
|
$paymentId |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
throw new ActionNotAllowedException($message); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.