Total Complexity | 41 |
Total Lines | 544 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like PaymentsApiClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PaymentsApiClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class PaymentsApiClient extends CommonApiClient implements PaymentsApiClientInterface |
||
50 | { |
||
51 | const SUB_URL_CREATE_PAYMENT = 'api/payment'; |
||
52 | const SUB_URL_CREATE_PAYMENT_SUBMIT = 'api/payment/submit'; |
||
53 | const SUB_URL_RETRIEVE_PAYMENT = 'api/payment/%s'; |
||
54 | const SUB_URL_LIST_PAYMENTS = 'api/payment'; |
||
55 | const SUB_URL_REFUND_PAYMENT = 'api/payment/refund/%s'; |
||
56 | const SUB_URL_AUTHORIZE_PAYMENT = 'api/payment/authorize/%s'; |
||
57 | const SUB_URL_REMIND_PAYMENT = 'api/payment/remind/%s'; |
||
58 | const SUB_URL_COLLECT_PAYMENTS = 'api/payment/collect/%s'; |
||
59 | const SUB_URL_LATE_PAYMENTS = 'api/payment/late-payment/%s'; |
||
60 | const SUB_URL_SHIPPING_GOODS_PAYMENT = 'api/payment/shipping-goods/%s'; |
||
61 | const SUB_URL_CANCEL_PAYMENT = 'api/payment/cancel/%s'; |
||
62 | const SUB_URL_RETRIEVE_API_CALL = 'api/%s'; |
||
63 | const SUB_URL_LIST_PAYMENT_OPTIONS = 'api/shop/oauth/%s/payment-options/%s'; |
||
64 | const SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS = 'api/shop/oauth/%s/payment-options/variants/%s'; |
||
65 | const SUB_URL_TRANSACTION = 'api/rest/v1/transactions/%s'; |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | * |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | public function createPaymentRequest(CreatePaymentRequest $createPaymentRequest) |
||
73 | { |
||
74 | $this->configuration->assertLoaded(); |
||
75 | |||
76 | if (!$createPaymentRequest->getChannel()) { |
||
77 | $createPaymentRequest->setChannel( |
||
78 | $this->configuration->getChannelSet() |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | $request = RequestBuilder::post($this->getCreatePaymentURL()) |
||
83 | ->addRawHeader( |
||
84 | $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString() |
||
85 | ) |
||
86 | ->setRequestEntity($createPaymentRequest) |
||
87 | ->setResponseEntity(new CreatePaymentResponse()) |
||
88 | ->build(); |
||
89 | |||
90 | return $this->executeRequest($request, OauthToken::SCOPE_CREATE_PAYMENT); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | * |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | public function submitPaymentRequest(SubmitPaymentRequest $submitPaymentRequest) |
||
99 | { |
||
100 | $this->configuration->assertLoaded(); |
||
101 | |||
102 | if (!$submitPaymentRequest->getChannel()) { |
||
103 | $submitPaymentRequest->setChannel( |
||
104 | $this->configuration->getChannelSet() |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | $request = RequestBuilder::post($this->getSubmitPaymentURL()) |
||
109 | ->addRawHeader( |
||
110 | $this->getToken(OauthToken::SCOPE_CREATE_PAYMENT)->getAuthorizationString() |
||
111 | ) |
||
112 | ->contentTypeIsJson() |
||
113 | ->setRequestEntity($submitPaymentRequest) |
||
114 | ->setResponseEntity(new RetrievePaymentResponse()) |
||
115 | ->build(); |
||
116 | |||
117 | return $this->executeRequest($request, OauthToken::SCOPE_CREATE_PAYMENT); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | * |
||
123 | * @throws \Exception |
||
124 | */ |
||
125 | public function retrievePaymentRequest($paymentId) |
||
126 | { |
||
127 | $this->configuration->assertLoaded(); |
||
128 | |||
129 | $request = RequestBuilder::get($this->getRetrievePaymentURL($paymentId)) |
||
130 | ->addRawHeader( |
||
131 | $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString() |
||
132 | ) |
||
133 | ->setResponseEntity(new RetrievePaymentResponse()) |
||
134 | ->build(); |
||
135 | |||
136 | return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | * |
||
142 | * @throws \Exception |
||
143 | */ |
||
144 | public function listPaymentsRequest(ListPaymentsRequest $listPaymentsRequest) |
||
145 | { |
||
146 | $this->configuration->assertLoaded(); |
||
147 | |||
148 | $request = RequestBuilder::get($this->getListPaymentsURL()) |
||
149 | ->addRawHeader( |
||
150 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
151 | ) |
||
152 | ->setRequestEntity($listPaymentsRequest) |
||
153 | ->setResponseEntity(new ListPaymentsResponse()) |
||
154 | ->build(); |
||
155 | |||
156 | return $this->executeRequest($request); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * |
||
162 | * @throws \Exception |
||
163 | */ |
||
164 | public function refundPaymentRequest($paymentId, $amount) |
||
165 | { |
||
166 | $this->configuration->assertLoaded(); |
||
167 | |||
168 | $request = RequestBuilder::post($this->getRefundPaymentURL($paymentId)) |
||
169 | ->setParams([ |
||
170 | 'amount' => $amount, |
||
171 | ]) |
||
172 | ->addRawHeader( |
||
173 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
174 | ) |
||
175 | ->setRequestEntity(new RefundPaymentRequest()) |
||
176 | ->setResponseEntity(new RefundPaymentResponse()) |
||
177 | ->build(); |
||
178 | |||
179 | return $this->executeRequest($request); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | * |
||
185 | * @throws \Exception |
||
186 | */ |
||
187 | public function refundItemsPaymentRequest($paymentId, $items) |
||
188 | { |
||
189 | $this->configuration->assertLoaded(); |
||
190 | |||
191 | $refundPaymentRequest = new RefundItemsPaymentRequest(); |
||
192 | $refundPaymentRequest->setPaymentItems($items); |
||
193 | |||
194 | $request = RequestBuilder::post($this->getRefundPaymentURL($paymentId)) |
||
195 | ->addRawHeader( |
||
196 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
197 | ) |
||
198 | ->contentTypeIsJson() |
||
199 | ->setRequestEntity($refundPaymentRequest) |
||
200 | ->setResponseEntity(new RefundPaymentResponse()) |
||
201 | ->build(); |
||
202 | |||
203 | return $this->executeRequest($request); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | * |
||
209 | * @throws \Exception |
||
210 | */ |
||
211 | public function authorizePaymentRequest($paymentId, AuthorizePaymentRequest $paymentRequest = null) |
||
212 | { |
||
213 | $this->configuration->assertLoaded(); |
||
214 | |||
215 | $request = RequestBuilder::post($this->getAuthorizePaymentURL($paymentId)) |
||
216 | ->addRawHeader( |
||
217 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
218 | ) |
||
219 | ->setRequestEntity($paymentRequest ?: new AuthorizePaymentRequest()) |
||
220 | ->setResponseEntity(new AuthorizePaymentResponse()) |
||
221 | ->build(); |
||
222 | |||
223 | return $this->executeRequest($request); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | * |
||
229 | * @throws \Exception |
||
230 | * |
||
231 | * @deprecated This request is only available for Santander DE Invoice and not used anywhere |
||
232 | */ |
||
233 | public function remindPaymentRequest($paymentId) |
||
234 | { |
||
235 | $this->configuration->assertLoaded(); |
||
236 | |||
237 | $request = RequestBuilder::post($this->getRemindPaymentURL($paymentId)) |
||
238 | ->addRawHeader( |
||
239 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
240 | ) |
||
241 | ->setResponseEntity(new RemindPaymentResponse()) |
||
242 | ->build(); |
||
243 | |||
244 | return $this->executeRequest($request); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | * |
||
250 | * @throws \Exception |
||
251 | * |
||
252 | * @deprecated This request is only available for Santander DE Invoice and not used anywhere |
||
253 | */ |
||
254 | public function collectPaymentsRequest($paymentId) |
||
255 | { |
||
256 | $this->configuration->assertLoaded(); |
||
257 | |||
258 | $request = RequestBuilder::post($this->getCollectPaymentsURL($paymentId)) |
||
259 | ->addRawHeader( |
||
260 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
261 | ) |
||
262 | ->setResponseEntity(new CollectPaymentsResponse()) |
||
263 | ->build(); |
||
264 | |||
265 | return $this->executeRequest($request); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | * |
||
271 | * @throws \Exception |
||
272 | * |
||
273 | * @deprecated This request is only available for Santander DE Invoice and not used anywhere |
||
274 | */ |
||
275 | public function latePaymentsRequest($paymentId) |
||
276 | { |
||
277 | $this->configuration->assertLoaded(); |
||
278 | |||
279 | $request = RequestBuilder::post($this->getLatePaymentsURL($paymentId)) |
||
280 | ->addRawHeader( |
||
281 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
282 | ) |
||
283 | ->setResponseEntity(new LatePaymentsResponse()) |
||
284 | ->build(); |
||
285 | |||
286 | return $this->executeRequest($request); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | * |
||
292 | * @throws \Exception |
||
293 | */ |
||
294 | public function shippingGoodsPaymentRequest( |
||
295 | $paymentId, |
||
296 | ShippingGoodsPaymentRequest $paymentRequest = null |
||
297 | ) { |
||
298 | $this->configuration->assertLoaded(); |
||
299 | |||
300 | $request = RequestBuilder::post($this->getShippingGoodsPaymentURL($paymentId)) |
||
301 | ->addRawHeader( |
||
302 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
303 | ) |
||
304 | ->contentTypeIsJson() |
||
305 | ->setRequestEntity($paymentRequest ?: new ShippingGoodsPaymentRequest()) |
||
306 | ->setResponseEntity(new ShippingGoodsPaymentResponse()) |
||
307 | ->build(); |
||
308 | |||
309 | return $this->executeRequest($request); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | * |
||
315 | * @throws \Exception |
||
316 | */ |
||
317 | public function cancelPaymentRequest($paymentId, $amount = null) |
||
318 | { |
||
319 | $this->configuration->assertLoaded(); |
||
320 | |||
321 | $request = RequestBuilder::post($this->getCancelPaymentURL($paymentId)) |
||
322 | ->setParams([ |
||
323 | 'amount' => $amount, |
||
324 | ]) |
||
325 | ->addRawHeader( |
||
326 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
327 | ) |
||
328 | ->setRequestEntity(new CancelPaymentRequest()) |
||
329 | ->setResponseEntity(new CancelPaymentResponse()) |
||
330 | ->build(); |
||
331 | |||
332 | return $this->executeRequest($request); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | * |
||
338 | * @throws \Exception |
||
339 | */ |
||
340 | public function retrieveApiCallRequest($callId) |
||
341 | { |
||
342 | $this->configuration->assertLoaded(); |
||
343 | |||
344 | $request = RequestBuilder::get($this->getRetrieveApiCallURL($callId)) |
||
345 | ->addRawHeader( |
||
346 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
347 | ) |
||
348 | ->setResponseEntity(new RetrieveApiCallResponse()) |
||
349 | ->build(); |
||
350 | |||
351 | return $this->executeRequest($request); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | * |
||
357 | * @throws \Exception |
||
358 | */ |
||
359 | public function listPaymentOptionsRequest($params = [], $businessUuid = '', $channel = '') |
||
360 | { |
||
361 | $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid(); |
||
362 | $channel = $channel ?: $this->getConfiguration()->getChannelSet(); |
||
363 | |||
364 | $request = RequestBuilder::get($this->getListPaymentOptionsURL($businessUuid, $channel, $params)) |
||
365 | ->addRawHeader( |
||
366 | $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString() |
||
367 | ) |
||
368 | ->setResponseEntity(new ListPaymentOptionsResponse()) |
||
369 | ->build(); |
||
370 | |||
371 | return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | * |
||
377 | * @throws \Exception |
||
378 | */ |
||
379 | public function listPaymentOptionsWithVariantsRequest($params = [], $businessUuid = '', $channel = '') |
||
380 | { |
||
381 | $businessUuid = $businessUuid ?: $this->getConfiguration()->getBusinessUuid(); |
||
382 | $channel = $channel ?: $this->getConfiguration()->getChannelSet(); |
||
383 | |||
384 | $request = RequestBuilder::get($this->getListPaymentOptionsVariantsURL($businessUuid, $channel, $params)) |
||
385 | ->addRawHeader( |
||
386 | $this->getToken(OauthToken::SCOPE_PAYMENT_INFO)->getAuthorizationString() |
||
387 | ) |
||
388 | ->setResponseEntity(new ListPaymentOptionsWithVariantsResponse()) |
||
389 | ->build(); |
||
390 | |||
391 | return $this->executeRequest($request, OauthToken::SCOPE_PAYMENT_INFO); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | * |
||
397 | * @throws \Exception |
||
398 | */ |
||
399 | public function getTransactionRequest($paymentId) |
||
400 | { |
||
401 | $this->configuration->assertLoaded(); |
||
402 | |||
403 | $request = RequestBuilder::get($this->getTransactionURL($paymentId)) |
||
404 | ->addRawHeader( |
||
405 | $this->getToken(OauthToken::SCOPE_PAYMENT_ACTIONS)->getAuthorizationString() |
||
406 | ) |
||
407 | ->setResponseEntity(new GetTransactionResponse()) |
||
408 | ->build(); |
||
409 | |||
410 | return $this->executeRequest($request); |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns URL for Create Payment path |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | protected function getCreatePaymentURL() |
||
419 | { |
||
420 | return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Returns URL for Submit Payment path |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | protected function getSubmitPaymentURL() |
||
429 | { |
||
430 | return $this->getBaseUrl() . self::SUB_URL_CREATE_PAYMENT_SUBMIT; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Returns URL for Retrieve Payment path |
||
435 | * |
||
436 | * @param string $paymentId |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | protected function getRetrievePaymentURL($paymentId) |
||
441 | { |
||
442 | return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_PAYMENT, $paymentId); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Returns URL for List Payments path |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | protected function getListPaymentsURL() |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Returns URL for Refund Payment path |
||
457 | * |
||
458 | * @param string $paymentId |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | protected function getRefundPaymentURL($paymentId) |
||
463 | { |
||
464 | return $this->getBaseUrl() . sprintf(self::SUB_URL_REFUND_PAYMENT, $paymentId); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Returns URL for Authorize Payment path |
||
469 | * |
||
470 | * @param string $paymentId |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | protected function getAuthorizePaymentURL($paymentId) |
||
475 | { |
||
476 | return $this->getBaseUrl() . sprintf(self::SUB_URL_AUTHORIZE_PAYMENT, $paymentId); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Returns URL for Remind Payment path |
||
481 | * |
||
482 | * @param string $paymentId |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | protected function getRemindPaymentURL($paymentId) |
||
487 | { |
||
488 | return $this->getBaseUrl() . sprintf(self::SUB_URL_REMIND_PAYMENT, $paymentId); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Returns URL for Collect Payment path |
||
493 | * |
||
494 | * @param string $paymentId |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | protected function getCollectPaymentsURL($paymentId) |
||
499 | { |
||
500 | return $this->getBaseUrl() . sprintf(self::SUB_URL_COLLECT_PAYMENTS, $paymentId); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Returns URL for Late Payments path |
||
505 | * |
||
506 | * @param string $paymentId |
||
507 | * |
||
508 | * @return string |
||
509 | */ |
||
510 | protected function getLatePaymentsURL($paymentId) |
||
511 | { |
||
512 | return $this->getBaseUrl() . sprintf(self::SUB_URL_LATE_PAYMENTS, $paymentId); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Returns URL for Shipping Goods Payment path |
||
517 | * |
||
518 | * @param string $paymentId |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | protected function getShippingGoodsPaymentURL($paymentId) |
||
523 | { |
||
524 | return $this->getBaseUrl() . sprintf(self::SUB_URL_SHIPPING_GOODS_PAYMENT, $paymentId); |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Returns URL for Cancel Payment path |
||
529 | * |
||
530 | * @param string $paymentId |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | protected function getCancelPaymentURL($paymentId) |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Returns URL for Retrieve API Call path |
||
541 | * |
||
542 | * @param string $callId |
||
543 | * |
||
544 | * @return string |
||
545 | */ |
||
546 | protected function getRetrieveApiCallURL($callId) |
||
547 | { |
||
548 | return $this->getBaseUrl() . sprintf(self::SUB_URL_RETRIEVE_API_CALL, $callId); |
||
549 | } |
||
550 | |||
551 | /** |
||
552 | * Returns URL for Available Payment Options path |
||
553 | * |
||
554 | * @param string $businessUuid |
||
555 | * @param string $channel |
||
556 | * @param array $params |
||
557 | * |
||
558 | * @return string |
||
559 | */ |
||
560 | protected function getListPaymentOptionsURL($businessUuid, $channel, $params = []) |
||
561 | { |
||
562 | return $this->getBaseUrl() |
||
563 | . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS, $businessUuid, $channel) |
||
564 | . (empty($params) ? '' : '?' . http_build_query($params)); |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * Returns URL for Available Payment Options request |
||
569 | * |
||
570 | * @param string $businessUuid |
||
571 | * @param string $channel |
||
572 | * @param array $params |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | protected function getListPaymentOptionsVariantsURL($businessUuid, $channel, $params = []) |
||
577 | { |
||
578 | return $this->getBaseUrl() |
||
579 | . sprintf(self::SUB_URL_LIST_PAYMENT_OPTIONS_VARIANTS, $businessUuid, $channel) |
||
580 | . (empty($params) ? '' : '?' . http_build_query($params)); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Returns URL to Transaction path |
||
585 | * |
||
586 | * @param int $paymentId |
||
587 | * |
||
588 | * @return string |
||
589 | */ |
||
590 | protected function getTransactionURL($paymentId) |
||
593 | } |
||
594 | } |
||
595 |