PluginProbe
imoje / 4.2.0
imoje v4.2.0
4.16.1 4.16.0 trunk 3.1.1 3.2.0 3.2.1 3.2.3 3.2.4 3.2.6 3.2.7 3.2.8 3.3.0 3.3.1 3.3.2 3.3.3 4.0.0 4.1.0 4.1.1 4.10.1 4.11.0 4.12.0 4.14.0 4.15.0 4.15.1 4.15.2 All 39 releases
imoje / includes / libs / payment-core / src / Api.php

Api.php in imoje 4.2.0, at includes/libs/payment-core/src/Api.php

872 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Imoje\Payment;
4
5 use Exception;
6
7 /**
8 * Class Api
9 *
10 * @package Imoje\Payment
11 */
12 class Api {
13
14 const TRANSACTION = 'transaction';
15 const PAYMENT = 'payment';
16 const PROFILE = 'profile';
17 const MERCHANT = 'merchant';
18 const SERVICE = 'service';
19 const TRANSACTION_TYPE_SALE = 'sale';
20 const TRANSACTION_TYPE_REFUND = 'refund';
21
22 /**
23 * @var array
24 */
25 private static $serviceUrls = [
26 Util::ENVIRONMENT_PRODUCTION => 'https://api.imoje.pl/v1',
27 Util::ENVIRONMENT_SANDBOX => 'https://sandbox.api.imoje.pl/v1',
28 ];
29
30 /**
31 * @var string
32 */
33 private $authorizationToken;
34
35 /**
36 * @var string
37 */
38 private $merchantId;
39
40 /**
41 * @var string
42 */
43 private $serviceId;
44
45 /**
46 * @var string
47 */
48 private $environment;
49
50 /**
51 * Constructor
52 *
53 * @param string $authorizationToken
54 * @param string $merchantId
55 * @param string $serviceId
56 * @param string $environment
57 */
58 public function __construct(
59 $authorizationToken,
60 $merchantId,
61 $serviceId,
62 $environment = ''
63 ) {
64
65 $this->authorizationToken = $authorizationToken;
66 $this->merchantId = $merchantId;
67 $this->serviceId = $serviceId;
68
69 $this->environment = $environment
70 ?: Util::ENVIRONMENT_PRODUCTION;
71 }
72
73 /**
74 * @param string $firstName
75 * @param string $lastName
76 * @param string $street
77 * @param string $city
78 * @param string $region
79 * @param string $postalCode
80 * @param string $countryCodeAlpha2
81 *
82 * @return array
83 */
84 public static function prepareAddressData(
85 $firstName,
86 $lastName,
87 $street,
88 $city,
89 $region,
90 $postalCode,
91 $countryCodeAlpha2
92 ) {
93
94 $array = [
95 'firstName' => $firstName,
96 'lastName' => $lastName,
97 'street' => $street,
98 'city' => $city,
99 'postalCode' => $postalCode,
100 'countryCodeAlpha2' => $countryCodeAlpha2,
101 ];
102
103 if ( $region ) {
104 $array['region'] = $region;
105 }
106
107 return $array;
108 }
109
110 /**
111 * @param string $blikProfileId
112 * @param int $amount
113 * @param string $currency
114 * @param string $orderId
115 * @param string $title
116 * @param string $clientIp
117 * @param string $blikKey
118 * @param string $blikCode
119 *
120 * @return string
121 */
122 public function prepareBlikOneclickData(
123 $blikProfileId,
124 $amount,
125 $currency,
126 $orderId,
127 $title,
128 $clientIp,
129 $blikKey = '',
130 $blikCode = ''
131 ) {
132
133 $array = [
134 'serviceId' => $this->serviceId,
135 'blikProfileId' => $blikProfileId,
136 'amount' => Util::convertAmountToFractional( $amount ),
137 'currency' => $currency,
138 'orderId' => $orderId,
139 'title' => $title,
140 'clientIp' => $clientIp,
141 ];
142
143 if ( $blikKey ) {
144 $array['blikKey'] = $blikKey;
145 }
146
147 if ( $blikCode ) {
148 $array['blikCode'] = $blikCode;
149 }
150
151 return json_encode( $array );
152 }
153
154 /**
155 * @param string $id
156 *
157 * @return array
158 */
159 public function getTransaction( $id ) {
160
161 return $this->call(
162 $this->getTransactionUrl( $id ),
163 Util::METHOD_REQUEST_GET
164 );
165 }
166
167 /**
168 * @param string $url
169 * @param string $methodRequest
170 * @param string $body
171 *
172 * @return array
173 */
174 private function call( $url, $methodRequest, $body = '' ) {
175
176 $curl = curl_init( $url );
177 curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $methodRequest );
178 curl_setopt( $curl, CURLOPT_POSTFIELDS, $body );
179 curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
180 curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 10 );
181 curl_setopt( $curl, CURLOPT_TIMEOUT, 10 );
182 curl_setopt( $curl, CURLOPT_HTTPHEADER, [
183 'Content-Type: application/json',
184 'Authorization: Bearer ' . $this->authorizationToken,
185 ] );
186
187 $resultCurl = json_decode( curl_exec( $curl ), true );
188
189 $httpCode = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
190
191 if ( ( $httpCode !== 200 ) || ! $resultCurl ) {
192
193 $array = [
194 'success' => false,
195 'data' => [
196 'httpCode' => $httpCode,
197 'error' => curl_error( $curl ),
198 'body' => '',
199 'errors' => [],
200 ],
201 ];
202
203 if ( isset( $resultCurl['apiErrorResponse']['message'] ) && $resultCurl['apiErrorResponse']['message'] ) {
204 $array['data']['body'] = $resultCurl['apiErrorResponse']['message'];
205 }
206
207 if ( isset( $resultCurl['apiErrorResponse']['errors'] ) && $resultCurl['apiErrorResponse']['errors'] ) {
208 $array['data']['errors'] = $resultCurl['apiErrorResponse']['errors'];
209 }
210
211 return $array;
212 }
213
214 if ( isset( $resultCurl['transaction']['statusCode'] ) && $resultCurl['transaction']['statusCode'] ) {
215
216 if ( $this->check_retype_code( $resultCurl['transaction']['statusCode'] ) ) {
217 $resultCurl['code'] = 1;
218 }
219
220 if ( $this->check_new_param_alias( $resultCurl['transaction']['statusCode'] ) ) {
221 $resultCurl['newParamAlias'] = 1;
222 }
223 }
224
225 return [
226 'success' => true,
227 'body' => $resultCurl,
228 ];
229 }
230
231 /**
232 * Verify that error code needs to retype BLIK code in frontend
233 *
234 * @param string $code
235 *
236 * @return bool
237 */
238 private function check_retype_code( $code ) {
239 $array = [
240 'BLK-ERROR-210002',
241 'BLK-ERROR-210003',
242 'BLK-ERROR-210004',
243 'BLK-ERROR-210005',
244 'BLK-ERROR-210006',
245 'BLK-ERROR-210007',
246 'BLK-ERROR-210008',
247 'BLK-ERROR-210009',
248 'BLK-ERROR-210010',
249 'BLK-ERROR-210011',
250 'BLK-ERROR-210012',
251 'BLK-ERROR-210013',
252 'BLK-ERROR-210014',
253 'BLK-ERROR-210015',
254 'BLK-ERROR-210016',
255 'BLK-ERROR-210017',
256 'BLK-ERROR-210018',
257 'BLK-ERROR-210019',
258 'BLK-ERROR-210020',
259 'BLK-ERROR-210021',
260 'BLK-ERROR-210022',
261
262 ];
263
264 return in_array( $code, $array );
265 }
266
267 /**
268 * Verify that error code needs to create new paramAlias
269 *
270 * @param string $code
271 *
272 * @return bool
273 */
274 private function check_new_param_alias( $code ) {
275
276 $array = [
277 'BLK-ERROR-210002',
278 'BLK-ERROR-210003',
279 'BLK-ERROR-210004',
280 ];
281
282 return in_array( $code, $array );
283 }
284 // endregion
285
286 /**
287 * @return string
288 */
289 private function getTransactionUrl( $id ) {
290
291 $baseUrl = self::getServiceUrl();
292
293 if ( $baseUrl ) {
294 return $baseUrl
295 . '/'
296 . self::MERCHANT
297 . '/'
298 . $this->merchantId
299 . '/'
300 . self::TRANSACTION
301 . '/'
302 . $id;
303 }
304
305 return '';
306 }
307
308 /**
309 * @return string
310 */
311 private function getServiceUrl() {
312
313 if ( isset( self::$serviceUrls[ $this->environment ] ) ) {
314 return self::$serviceUrls[ $this->environment ];
315 }
316
317 return '';
318 }
319
320 /**
321 * @param int $amount
322 * @param string $currency
323 * @param string $orderId
324 * @param string $paymentMethod
325 * @param string $paymentMethodCode
326 * @param string $successReturnUrl
327 * @param string $failureReturnUrl
328 * @param string $customerFirstName
329 * @param string $customerLastName
330 * @param string $customerEmail
331 * @param string $type
332 * @param string $clientIp
333 * @param string $blikCode
334 * @param array $address
335 * @param string $cid
336 *
337 * @return string
338 */
339 public function prepareData(
340 $amount,
341 $currency,
342 $orderId,
343 $paymentMethod,
344 $paymentMethodCode,
345 $successReturnUrl,
346 $failureReturnUrl,
347 $customerFirstName,
348 $customerLastName,
349 $customerEmail,
350 $type = 'sale',
351 $clientIp = '',
352 $blikCode = '',
353 $address = [],
354 $cid = '',
355 $invoice = []
356 ) {
357
358 if ( ! $clientIp ) {
359 $clientIp = $_SERVER['REMOTE_ADDR'];
360 }
361
362 $array = [
363 'type' => $type,
364 'serviceId' => $this->serviceId,
365 'amount' => Util::convertAmountToFractional( $amount ),
366 'currency' => $currency,
367 'orderId' => (string) $orderId,
368 'title' => (string) $orderId,
369 'paymentMethod' => $paymentMethod,
370 'paymentMethodCode' => $paymentMethodCode,
371 'successReturnUrl' => $successReturnUrl,
372 'failureReturnUrl' => $failureReturnUrl,
373 'clientIp' => $clientIp,
374 'customer' => [
375 'firstName' => $customerFirstName,
376 'lastName' => $customerLastName,
377 'email' => $customerEmail,
378 ],
379 ];
380
381 if ( $blikCode ) {
382 $array['blikCode'] = $blikCode;
383 }
384
385 if ( isset( $address['billing'] ) && $address['billing'] ) {
386 $array['billing'] = $address['billing'];
387 }
388
389 if ( isset( $address['shipping'] ) && $address['shipping'] ) {
390 $array['shipping'] = $address['shipping'];
391 }
392
393 if ( $cid ) {
394 $array['customer']['cid'] = (string) $cid;
395 }
396
397 if ( $invoice ) {
398 $array['invoice'] = $invoice;
399 }
400
401 return json_encode( $array );
402 }
403
404 /**
405 * @param int $amount
406 * @param string $currency
407 * @param string $orderId
408 * @param string $returnUrl
409 * @param string $successReturnUrl
410 * @param string $failureReturnUrl
411 * @param string $customerFirstName
412 * @param string $customerLastName
413 * @param string $customerEmail
414 * @param string $phone
415 * @param string $visibleMethod
416 * @param array $cart
417 * @param array $invoice
418 *
419 * @return string
420 */
421 public function prepareDataPaymentLink(
422 $amount,
423 $currency,
424 $orderId,
425 $returnUrl,
426 $successReturnUrl,
427 $failureReturnUrl,
428 $customerFirstName,
429 $customerLastName,
430 $customerEmail,
431 $phone = '',
432 $visibleMethod = [],
433 $cart = [],
434 $invoice = [],
435 $preselectMethodCode = ''
436 ) {
437
438 $array = [
439 'serviceId' => $this->serviceId,
440 'amount' => Util::convertAmountToFractional( $amount ),
441 'currency' => $currency,
442 'orderId' => (string) $orderId,
443 'title' => (string) $orderId,
444 'returnUrl' => $returnUrl,
445 'successReturnUrl' => $successReturnUrl,
446 'failureReturnUrl' => $failureReturnUrl,
447 'customer' => [
448 'firstName' => $customerFirstName,
449 'lastName' => $customerLastName,
450 'email' => $customerEmail,
451 'phone' => $phone,
452 ],
453 ];
454
455 if ( $phone ) {
456 $array['customer']['phone'] = $phone;
457 }
458
459 if ( $visibleMethod ) {
460 $array['visibleMethod'] = $visibleMethod;
461 }
462
463 if ( $cart ) {
464 $array['cart'] = $cart;
465 }
466
467 if ( $invoice ) {
468 $array['invoice'] = $invoice;
469 }
470
471 if ( $preselectMethodCode ) {
472 $array['preselectMethodCode'] = $preselectMethodCode;
473 }
474
475 return json_encode( $array );
476 }
477
478 /**
479 * @param int $amount
480 *
481 * @return string
482 */
483 public function prepareRefundData(
484 $amount
485 ) {
486
487 return json_encode( [
488 'amount' => $amount,
489 'serviceId' => $this->serviceId,
490 'type' => self::TRANSACTION_TYPE_REFUND,
491 ] );
492 }
493
494 /**
495 * @param string $body
496 *
497 * @return array
498 */
499 public function createTransaction( $body, $url = '', $method = '' ) {
500 if ( ! $url ) {
501 $url = $this->getTransactionCreateUrl();
502 }
503 if ( ! $method ) {
504 $method = Util::METHOD_REQUEST_POST;
505 }
506
507 return $this->call(
508 $url,
509 $method,
510 $body
511 );
512 }
513
514 /**
515 * @param string $body
516 *
517 * @return array
518 */
519 public function createPaymentLink( $body, $url = '', $method = '' ) {
520 if ( ! $url ) {
521 $url = $this->getPaymentLinkCreateUrl();
522 }
523 if ( ! $method ) {
524 $method = Util::METHOD_REQUEST_POST;
525 }
526
527 return $this->call(
528 $url,
529 $method,
530 $body
531 );
532 }
533
534 /**
535 * @return string
536 */
537 private function getTransactionCreateUrl() {
538
539 $baseUrl = self::getServiceUrl();
540
541 if ( $baseUrl ) {
542 return $baseUrl
543 . '/'
544 . self::MERCHANT
545 . '/'
546 . $this->merchantId
547 . '/'
548 . self::TRANSACTION;
549 }
550
551 return '';
552 }
553
554 /**
555 * @return string
556 */
557 private function getPaymentLinkCreateUrl() {
558
559 $baseUrl = self::getServiceUrl();
560
561 if ( $baseUrl ) {
562 return $baseUrl
563 . '/'
564 . self::MERCHANT
565 . '/'
566 . $this->merchantId
567 . '/'
568 . self::PAYMENT;
569 }
570
571 return '';
572 }
573
574 /**
575 * @return array
576 */
577 public function getServiceInfo() {
578 return $this->call(
579 $this->getServiceInfoUrl(),
580 Util::METHOD_REQUEST_GET
581 );
582 }
583
584 /**
585 * @return string
586 */
587 private function getServiceInfoUrl() {
588
589 $baseUrl = self::getServiceUrl();
590
591 if ( $baseUrl ) {
592 return $baseUrl
593 . '/'
594 . self::MERCHANT
595 . '/'
596 . $this->merchantId
597 . '/'
598 . self::SERVICE
599 . '/'
600 . $this->serviceId;
601 }
602
603 return '';
604 }
605
606 /**
607 * @param string $cid
608 *
609 * @return array
610 */
611 public function getBlikProfileList( $cid ) {
612
613 return $this->call(
614 $this->getProfileBlikUrl( $cid ),
615 Util::METHOD_REQUEST_GET
616 );
617 }
618
619 /**
620 * @param string $cid
621 *
622 * @return string
623 */
624 private function getProfileBlikUrl( $cid ) {
625
626 $baseUrl = $this->getServiceUrl();
627
628 if ( $baseUrl ) {
629 return $baseUrl
630 . '/'
631 . self::MERCHANT
632 . '/'
633 . $this->merchantId
634 . '/'
635 . self::PROFILE
636 . '/'
637 . self::SERVICE
638 . '/'
639 . $this->serviceId
640 . '/cid/'
641 . $cid
642 . '/blik';
643 }
644
645 return '';
646 }
647
648 /**
649 * @param string $body
650 *
651 * @return array
652 */
653 public function createRefund( $body, $transactionUuid ) {
654
655 return $this->call(
656 $this->getRefundCreateUrl( $transactionUuid ),
657 Util::METHOD_REQUEST_POST,
658 $body
659 );
660 }
661
662 /**
663 * @param string $transactionUuid
664 *
665 * @return string
666 */
667 private function getRefundCreateUrl( $transactionUuid ) {
668
669 $baseUrl = $this->getServiceUrl();
670
671 if ( $baseUrl ) {
672 return $baseUrl
673 . '/'
674 . self::MERCHANT
675 . '/'
676 . $this->merchantId
677 . '/'
678 . self::TRANSACTION
679 . '/'
680 . $transactionUuid
681 . '/refund';
682 }
683
684 return '';
685 }
686
687 /**
688 * Creates full form with order data.
689 *
690 * @param array $transaction
691 * @param string $submitValue
692 * @param string $submitClass
693 * @param string $submitStyle
694 *
695 * @return string
696 * @throws Exception
697 */
698 public function buildOrderForm( $transaction, $submitValue = '', $submitClass = '', $submitStyle = '' ) {
699
700 if ( ! isset( $transaction['body']['action'] ) ) {
701
702 return false;
703 }
704
705 return Util::createOrderForm(
706 self::parseStringToArray( $transaction['body'] ),
707 $transaction['body']['action']['url'],
708 $transaction['body']['action']['method'],
709 $submitValue,
710 $submitClass,
711 $submitStyle
712 );
713 }
714
715 /**
716 * @param array $transaction
717 *
718 * @return array
719 */
720 public static function parseStringToArray( $transaction ) {
721
722 $array = [];
723
724 if ( $transaction['action']['method'] === Util::METHOD_REQUEST_POST ) {
725 parse_str( $transaction['action']['contentBodyRaw'], $array );
726 }
727
728 if ( $transaction['action']['method'] === Util::METHOD_REQUEST_GET ) {
729 $urlParsed = parse_url( $transaction['action']['url'] );
730
731 if ( isset( $urlParsed['query'] ) && $urlParsed['query'] ) {
732 parse_str( $urlParsed['query'], $array );
733
734 return $array;
735 }
736
737 if ( isset( $transaction['action']['contentBodyRaw'] ) && $transaction['action']['contentBodyRaw'] ) {
738 parse_str( $transaction['action']['contentBodyRaw'], $array );
739 }
740 }
741
742 return $array;
743 }
744
745 /**
746 * @return string
747 */
748 public function getDebitBlikProfileUrl() {
749
750 $baseUrl = self::getServiceUrl();
751
752 if ( $baseUrl ) {
753 return $baseUrl
754 . '/'
755 . self::MERCHANT
756 . '/'
757 . $this->merchantId
758 . '/'
759 . self::TRANSACTION
760 . '/'
761 . self::PROFILE
762 . '/blik';
763 }
764
765 return '';
766 }
767
768 /**
769 * @return string
770 */
771 public function getDeactivateBlikProfileUrl() {
772
773 $baseUrl = self::getServiceUrl();
774
775 if ( $baseUrl ) {
776 return $baseUrl
777 . '/'
778 . self::MERCHANT
779 . '/'
780 . $this->merchantId
781 . '/'
782 . self::PROFILE
783 . '/deactivate/blik';
784 }
785
786 return '';
787 }
788
789 /**
790 * @param array $paymentMethods
791 * @param string $paymentMethod
792 *
793 * @return array
794 */
795 public function getPaymentChannelInServiceAndVerify( $paymentMethods, $paymentMethod, $paymentMethodCode ) {
796
797 if ( ! is_array( $paymentMethod ) ) {
798 return [];
799 }
800
801 foreach ( $paymentMethods as $pm ) {
802 if ( $pm['paymentMethod'] === $paymentMethod
803 && $pm['paymentMethodCode'] === $paymentMethodCode
804 && $pm['isActive']
805 && $pm['isOnline']
806 && ( isset( $pm['transactionLimits'] ) && $pm['transactionLimits'] ) ) {
807
808 return $pm;
809 }
810 }
811
812 return [];
813 }
814
815 /**
816 * @param array $imojeService
817 * @param number $total
818 *
819 * @return bool
820 */
821 public function getPaymentMethodAvailable( $imojeService, $pm, $total ) {
822
823 if ( empty( $imojeService['paymentMethods'] ) ) {
824 return false;
825 }
826
827 foreach ( $imojeService['paymentMethods'] as $payment_method ) {
828 if ( $payment_method['paymentMethod'] === $pm
829 && $payment_method['isActive']
830 && $payment_method['isOnline']
831 && ( isset( $payment_method['transactionLimits'] ) && $payment_method['transactionLimits'] )
832 && $this->verifyTransactionLimits( $payment_method['transactionLimits'], $total ) ) {
833
834 return true;
835 }
836 }
837
838 return false;
839 }
840
841 /**
842 * @param array $transactionLimits
843 * @param number $total
844 *
845 * @return bool
846 */
847 public function verifyTransactionLimits( $transactionLimits, $total ) {
848
849 $isMaxValue = isset( $transactionLimits['maxTransaction']['value'] ) && $transactionLimits['maxTransaction']['value'];
850 $isMinValue = isset( $transactionLimits['minTransaction']['value'] ) && $transactionLimits['minTransaction']['value'];
851
852 if ( $isMaxValue || $isMinValue ) {
853 $total = Util::convertAmountToFractional( $total );
854
855 // leave this condition - in 99% of cases, it is going to be true, so there's no need to check another scenario
856 if ( $isMaxValue && $isMinValue ) {
857 return $total >= $transactionLimits['minTransaction']['value'] && $total <= $transactionLimits['maxTransaction']['value'];
858 }
859
860 if ( $isMaxValue ) {
861 return $total <= $transactionLimits['maxTransaction']['value'];
862 }
863
864 if ( $isMinValue ) {
865 return $total >= $transactionLimits['minTransaction']['value'];
866 }
867 }
868
869 return false;
870 }
871 }
872