From 610d4cd2073466206addbf9e7cdd255d02a01781 Mon Sep 17 00:00:00 2001 From: "michal.banczerowski" Date: Tue, 11 Feb 2025 16:03:19 +0100 Subject: [PATCH 1/3] Delete information about IP addres tpay --- examples/CardNotification.php | 5 -- src/Dictionaries/NotificationsIP.php | 15 ---- .../BasicNotificationHandler.php | 5 +- .../BlikAliasNotificationHandler.php | 34 --------- src/Notifications/CardNotificationHandler.php | 5 +- src/Utilities/ObjectsHelper.php | 63 +-------------- src/Utilities/ServerValidator.php | 76 ------------------- src/legacy_classes.php | 1 - tests/LegacyNamespaceTest.php | 1 - 9 files changed, 3 insertions(+), 202 deletions(-) delete mode 100644 src/Dictionaries/NotificationsIP.php delete mode 100644 src/Notifications/BlikAliasNotificationHandler.php delete mode 100644 src/Utilities/ServerValidator.php diff --git a/examples/CardNotification.php b/examples/CardNotification.php index e9dd7f4..05f20e5 100644 --- a/examples/CardNotification.php +++ b/examples/CardNotification.php @@ -31,11 +31,6 @@ public function init() private function getTpayNotification() { - // If you want to disable server IP validation, run this command (not recommended): - $this->disableValidationServerIP(); - // If you use proxy communication and want to check for Tpay server IP at HTTP_X_FORWARDED_FOR, fun this command: - $this->enableForwardedIPValidation(); - // Check Tpay server IP and validate parameters $notification = $this->handleNotification(); // Get order details from your DB $shopOrderData = $this->getOrderDetailsFromDatabase($notification['order_id']); diff --git a/src/Dictionaries/NotificationsIP.php b/src/Dictionaries/NotificationsIP.php deleted file mode 100644 index 543b704..0000000 --- a/src/Dictionaries/NotificationsIP.php +++ /dev/null @@ -1,15 +0,0 @@ -validateServerIP && false === $this->isTpayServer()) { - throw new TException('Request is not from secure server'); - } if (false === $checkMD5) { throw new TException('MD5 checksum is invalid'); } diff --git a/src/Notifications/BlikAliasNotificationHandler.php b/src/Notifications/BlikAliasNotificationHandler.php deleted file mode 100644 index f2c9b85..0000000 --- a/src/Notifications/BlikAliasNotificationHandler.php +++ /dev/null @@ -1,34 +0,0 @@ -getResponse(new PaymentTypeBlikAlias()); - if (true === $this->validateServerIP && false === $this->isTpayServer()) { - throw new TException('Request is not from secure server'); - } - echo 'TRUE'; - - return $res; - } -} diff --git a/src/Notifications/CardNotificationHandler.php b/src/Notifications/CardNotificationHandler.php index 66c033d..5980afd 100644 --- a/src/Notifications/CardNotificationHandler.php +++ b/src/Notifications/CardNotificationHandler.php @@ -13,7 +13,7 @@ class CardNotificationHandler extends PaymentCard { /** * Check cURL request from tpay server after payment. - * This method check server ip, required fields and md5 checksum sent by payment server. + * This method check required fields and md5 checksum sent by payment server. * Display information to prevent sending repeated notifications. * * @throws TException @@ -32,9 +32,6 @@ public function handleNotification() } else { throw new TException('Unknown notification type'); } - if (true === $this->validateServerIP && false === $this->isTpayServer()) { - throw new TException('Request is not from secure server'); - } echo json_encode([CardDictionary::RESULT => '1']); diff --git a/src/Utilities/ObjectsHelper.php b/src/Utilities/ObjectsHelper.php index 781262d..74beb05 100644 --- a/src/Utilities/ObjectsHelper.php +++ b/src/Utilities/ObjectsHelper.php @@ -3,7 +3,6 @@ namespace Tpay\OriginApi\Utilities; use Tpay\OriginApi\Curl\Curl; -use Tpay\OriginApi\Dictionaries\NotificationsIP; use Tpay\OriginApi\Validators\FieldsConfigValidator; class ObjectsHelper @@ -73,17 +72,11 @@ class ObjectsHelper */ protected $cardHashAlg = 'sha1'; - protected $secureIP = NotificationsIP::SECURE_IPS; - protected $validateServerIP = true; - protected $validateForwardedIP = false; - protected $transactionApi; - protected $cardsApi; - protected $basicClient; - protected $validator; protected $curl; /** * @param string $url + * * @param array $params * * @return array @@ -98,58 +91,4 @@ public function requests($url, $params) ->doRequest() ->getResult(); } - - /** - * Disabling validation of payment notification server IP - * Validation of tpay server ip is very important. - * Use this method only in test mode and be sure to enable validation in production. - */ - public function disableValidationServerIP() - { - $this->validateServerIP = false; - - return $this; - } - - /** Enabling validation of payment notification server IP */ - public function enableValidationServerIP() - { - $this->validateServerIP = true; - - return $this; - } - - /** - * CloudFlare protected servers will be validated like all others - * It is default behavior - */ - public function disableForwardedIPValidation() - { - $this->validateForwardedIP = false; - - return $this; - } - - /** Enabling validation for CloudFlare protected servers */ - public function enableForwardedIPValidation() - { - $this->validateForwardedIP = true; - - return $this; - } - - /** - * Check if request is called from secure tpay server - * - * @return bool - */ - public function isTpayServer() - { - return (new ServerValidator( - $this->validateServerIP, - $this->validateForwardedIP, - $this->secureIP - ) - )->isValid(); - } } diff --git a/src/Utilities/ServerValidator.php b/src/Utilities/ServerValidator.php deleted file mode 100644 index e13db7d..0000000 --- a/src/Utilities/ServerValidator.php +++ /dev/null @@ -1,76 +0,0 @@ - */ - private $secureIP; - - public function __construct($validateServerIP, $validateForwardedIP, array $secureIP) - { - $this->validateServerIP = $validateServerIP; - $this->validateForwardedIP = $validateForwardedIP; - $this->secureIP = $secureIP; - } - - /** - * Check if request is called from secure tpay server - * - * @return bool - */ - public function isValid() - { - if (!$this->validateServerIP) { - return true; - } - - $remoteIP = $this->getServerValue(static::REMOTE_ADDRESS); - $forwarderIP = $this->getServerValue(static::FORWARDER_ADDRESS); - - if (is_null($remoteIP) && is_null($forwarderIP)) { - return false; - } - - if ($this->checkIP($remoteIP)) { - return true; - } - - return (bool) ($this->validateForwardedIP && $this->checkIP($forwarderIP)); - } - - /** - * Get value from $_SERVER array if exists - * - * @param string $name - * - * @return null|string - */ - private function getServerValue($name) - { - if (isset($_SERVER[$name])) { - return $_SERVER[$name]; - } - } - - /** - * Validate if $ip is secure - * - * @param string $ip - * - * @return bool - */ - private function checkIP($ip) - { - return in_array($ip, $this->secureIP, true); - } -} diff --git a/src/legacy_classes.php b/src/legacy_classes.php index 5f42477..9f1b8e1 100644 --- a/src/legacy_classes.php +++ b/src/legacy_classes.php @@ -16,7 +16,6 @@ class CurlOptions extends \Tpay\OriginApi\Curl\CurlOptions {} namespace tpayLibs\src\_class_tpay\Notifications { class BasicNotificationHandler extends \Tpay\OriginApi\Notifications\BasicNotificationHandler {} - class BlikAliasNotificationHandler extends \Tpay\OriginApi\Notifications\BlikAliasNotificationHandler {} class CardNotificationHandler extends \Tpay\OriginApi\Notifications\CardNotificationHandler {} } diff --git a/tests/LegacyNamespaceTest.php b/tests/LegacyNamespaceTest.php index 57b01ed..52a7a51 100644 --- a/tests/LegacyNamespaceTest.php +++ b/tests/LegacyNamespaceTest.php @@ -72,7 +72,6 @@ public function legacyClassNamesFromArray() 'tpayLibs\\src\\_class_tpay\\Curl\\CurlOptions', 'tpayLibs\\src\\_class_tpay\\MassPayments', 'tpayLibs\\src\\_class_tpay\\Notifications\\BasicNotificationHandler', - 'tpayLibs\\src\\_class_tpay\\Notifications\\BlikAliasNotificationHandler', 'tpayLibs\\src\\_class_tpay\\Notifications\\CardNotificationHandler', 'tpayLibs\\src\\_class_tpay\\PaymentBlik', 'tpayLibs\\src\\_class_tpay\\PaymentCard', From e82d677dda2b2627f34912007919ab7a56e0eda5 Mon Sep 17 00:00:00 2001 From: "michal.banczerowski" Date: Tue, 11 Feb 2025 16:07:18 +0100 Subject: [PATCH 2/3] fix --- src/legacy_classes.php | 1 - tests/LegacyNamespaceTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/legacy_classes.php b/src/legacy_classes.php index 9f1b8e1..3c7820d 100644 --- a/src/legacy_classes.php +++ b/src/legacy_classes.php @@ -41,7 +41,6 @@ class BasicReports extends \Tpay\OriginApi\Reports\BasicReports {} namespace tpayLibs\src\_class_tpay\Utilities { class Lang extends \Tpay\OriginApi\Utilities\Lang {} class ObjectsHelper extends \Tpay\OriginApi\Utilities\ObjectsHelper {} - class ServerValidator extends \Tpay\OriginApi\Utilities\ServerValidator {} class TException extends \Tpay\OriginApi\Utilities\TException {} class Util extends \Tpay\OriginApi\Utilities\Util {} } diff --git a/tests/LegacyNamespaceTest.php b/tests/LegacyNamespaceTest.php index 52a7a51..5277a17 100644 --- a/tests/LegacyNamespaceTest.php +++ b/tests/LegacyNamespaceTest.php @@ -86,7 +86,6 @@ public function legacyClassNamesFromArray() 'tpayLibs\\src\\_class_tpay\\TransactionApi', 'tpayLibs\\src\\_class_tpay\\Utilities\\Lang', 'tpayLibs\\src\\_class_tpay\\Utilities\\ObjectsHelper', - 'tpayLibs\\src\\_class_tpay\\Utilities\\ServerValidator', 'tpayLibs\\src\\_class_tpay\\Utilities\\TException', 'tpayLibs\\src\\_class_tpay\\Utilities\\Util', 'tpayLibs\\src\\_class_tpay\\Validators\\AccessConfigValidator', From 75be4142a87f024b7bf46a93af9a789675548b55 Mon Sep 17 00:00:00 2001 From: "michal.banczerowski" Date: Tue, 11 Feb 2025 16:10:52 +0100 Subject: [PATCH 3/3] fix2 --- src/legacy_classes.php | 1 - tests/LegacyNamespaceTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/legacy_classes.php b/src/legacy_classes.php index 3c7820d..74eb64b 100644 --- a/src/legacy_classes.php +++ b/src/legacy_classes.php @@ -93,7 +93,6 @@ class CardDictionary extends \Tpay\OriginApi\Dictionaries\CardDictionary {} class FieldsConfigDictionary extends \Tpay\OriginApi\Dictionaries\FieldsConfigDictionary {} class FieldValueFilters extends \Tpay\OriginApi\Dictionaries\FieldValueFilters {} class HttpCodesDictionary extends \Tpay\OriginApi\Dictionaries\HttpCodesDictionary {} - class NotificationsIP extends \Tpay\OriginApi\Dictionaries\NotificationsIP {} class PaymentTypesDictionary extends \Tpay\OriginApi\Dictionaries\PaymentTypesDictionary {} } diff --git a/tests/LegacyNamespaceTest.php b/tests/LegacyNamespaceTest.php index 5277a17..a31565b 100644 --- a/tests/LegacyNamespaceTest.php +++ b/tests/LegacyNamespaceTest.php @@ -58,7 +58,6 @@ public function legacyClassNamesFromArray() 'tpayLibs\\src\\Dictionaries\\ISO_codes\\CountryCodesDictionary', 'tpayLibs\\src\\Dictionaries\\ISO_codes\\CurrencyCodesDictionary', 'tpayLibs\\src\\Dictionaries\\Localization\\CardPaymentLanguagesDictionary', - 'tpayLibs\\src\\Dictionaries\\NotificationsIP', 'tpayLibs\\src\\Dictionaries\\PaymentTypesDictionary', 'tpayLibs\\src\\Dictionaries\\Payments\\BasicFieldsDictionary', 'tpayLibs\\src\\Dictionaries\\Payments\\BlikFieldsDictionary',