Skip to content

Commit

Permalink
Added support for PHP 8.0+
Browse files Browse the repository at this point in the history
Added support for PHP 8.0+
  • Loading branch information
alexanderzaiets-paysera authored Apr 11, 2022
1 parent 1d68474 commit 4ca6755
Show file tree
Hide file tree
Showing 21 changed files with 66 additions and 104 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Version history
===============

Version 1.7 - 2022-04-11

* added support for PHP 8.0+

Version 1.6 - 2012-04-12

* refactored WebToPay class to separate non-static classes
Expand Down
21 changes: 5 additions & 16 deletions WebToPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ public static function checkResponse($query, $userData = array()) {
}

if ($logFile) {
self::log('OK', http_build_query($data, null, '&'), $logFile);
self::log('OK', http_build_query($data, '', '&'), $logFile);
}
return $data;

} catch (WebToPayException $exception) {
if ($logFile && $exception->getCode() != WebToPayException::E_DEPRECATED_USAGE) {
self::log('ERR', $exception . "\nQuery: " . http_build_query($query, null, '&'), $logFile);
self::log('ERR', $exception . "\nQuery: " . http_build_query($query, '', '&'), $logFile);
}
throw $exception;
}
Expand Down Expand Up @@ -734,7 +734,7 @@ public function buildForPublicKey() {
* @return string
*/
protected function createUrlFromRequestAndLanguage($request) {
$url = $this->getPaymentUrl() . '?' . http_build_query($request, null, '&');
$url = $this->getPaymentUrl() . '?' . http_build_query($request, '', '&');
return preg_replace('/[\r\n]+/is', '', $url);
}

Expand Down Expand Up @@ -1154,7 +1154,7 @@ protected function validateRequest($data, $specs) {
* @return array
*/
protected function createRequest(array $request) {
$data = $this->util->encodeSafeUrlBase64(http_build_query($request, null, '&'));
$data = $this->util->encodeSafeUrlBase64(http_build_query($request, '', '&'));
return array(
'data' => $data,
'sign' => md5($data . $this->projectPassword),
Expand Down Expand Up @@ -2253,7 +2253,7 @@ class WebToPay_WebClient {
public function get($uri, array $queryData = array()) {
if (count($queryData) > 0) {
$uri .= strpos($uri, '?') === false ? '?' : '&';
$uri .= http_build_query($queryData, null, '&');
$uri .= http_build_query($queryData, '', '&');
}
$url = parse_url($uri);
if ('https' == $url['scheme']) {
Expand Down Expand Up @@ -2330,20 +2330,9 @@ public function encodeSafeUrlBase64($text) {
public function parseHttpQuery($query) {
$params = array();
parse_str($query, $params);
if ($this->checkMagicQuotesOption()) {
$params = $this->stripSlashesRecursively($params);
}
return $params;
}

private function checkMagicQuotesOption() {
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
return false;
} else {
return get_magic_quotes_gpc();
}
}

/**
* Strips slashes recursively, so this method can be used on arrays with more than one level
*
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"classmap": ["src/"]
},
"require": {
"php": "^5.5 || ^7.0"
"php": "^5.5 || ^7.0 || ^8.0"
}
}
21 changes: 0 additions & 21 deletions demo_shop/includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ function h($var) {
return htmlspecialchars($var, ENT_QUOTES, 'UTF-8');
}

function removeQuotes($post) {
if (checkMagicQuotesOption()) {
foreach ($post as &$var) {
if (is_array($var)) {
$var = removeQuotes($var);
} else {
$var = stripslashes($var);
}
}
}
return $post;
}

function checkMagicQuotesOption() {
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
return false;
} else {
return get_magic_quotes_gpc();
}
}

function get_address($scriptName = '') {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $protocol.'://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/' . $scriptName;
Expand Down
2 changes: 1 addition & 1 deletion demo_shop/paymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_once 'includes/config.php';
require_once '../src/includes.php';

$post = removeQuotes($_POST);
$post = $_POST;
$id = $post['id'];
if (!isset($shopItems[$id])) {
redirect_to(get_address());
Expand Down
2 changes: 1 addition & 1 deletion demo_shop/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_once 'includes/config.php';
require_once '../src/includes.php';

$post = removeQuotes($_POST);
$post = $_POST;
$id = $post['id'];
if (!isset($shopItems[$id])) {
redirect_to(get_address());
Expand Down
2 changes: 1 addition & 1 deletion demo_shop/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_once 'includes/config.php';
require_once '../src/includes.php';

$get = removeQuotes($_GET);
$get = $_GET;
$answer = isset($get['answer']) ? $get['answer'] : 'cancel';

if ('callback' == $answer) {
Expand Down
2 changes: 1 addition & 1 deletion demo_shop/sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_once 'includes/config.php';
require_once '../src/includes.php';

$get = removeQuotes($_GET);
$get = $_GET;

try {
$parsedData = WebToPay::validateAndParseData($get, $config['projectid'], $config['sign_password']);
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "src/includes.php">

<testsuites>
Expand Down
4 changes: 2 additions & 2 deletions src/WebToPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,13 @@ public static function checkResponse($query, $userData = array()) {
}

if ($logFile) {
self::log('OK', http_build_query($data, null, '&'), $logFile);
self::log('OK', http_build_query($data, '', '&'), $logFile);
}
return $data;

} catch (WebToPayException $exception) {
if ($logFile && $exception->getCode() != WebToPayException::E_DEPRECATED_USAGE) {
self::log('ERR', $exception . "\nQuery: " . http_build_query($query, null, '&'), $logFile);
self::log('ERR', $exception . "\nQuery: " . http_build_query($query, '', '&'), $logFile);
}
throw $exception;
}
Expand Down
2 changes: 1 addition & 1 deletion src/WebToPay/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function validateRequest($data, $specs) {
* @return array
*/
protected function createRequest(array $request) {
$data = $this->util->encodeSafeUrlBase64(http_build_query($request, null, '&'));
$data = $this->util->encodeSafeUrlBase64(http_build_query($request, '', '&'));
return array(
'data' => $data,
'sign' => md5($data . $this->projectPassword),
Expand Down
2 changes: 1 addition & 1 deletion src/WebToPay/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function buildForPublicKey() {
* @return string
*/
protected function createUrlFromRequestAndLanguage($request) {
$url = $this->getPaymentUrl() . '?' . http_build_query($request, null, '&');
$url = $this->getPaymentUrl() . '?' . http_build_query($request, '', '&');
return preg_replace('/[\r\n]+/is', '', $url);
}

Expand Down
11 changes: 0 additions & 11 deletions src/WebToPay/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,9 @@ public function encodeSafeUrlBase64($text) {
public function parseHttpQuery($query) {
$params = array();
parse_str($query, $params);
if ($this->checkMagicQuotesOption()) {
$params = $this->stripSlashesRecursively($params);
}
return $params;
}

private function checkMagicQuotesOption() {
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
return false;
} else {
return get_magic_quotes_gpc();
}
}

/**
* Strips slashes recursively, so this method can be used on arrays with more than one level
*
Expand Down
2 changes: 1 addition & 1 deletion src/WebToPay/WebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WebToPay_WebClient {
public function get($uri, array $queryData = array()) {
if (count($queryData) > 0) {
$uri .= strpos($uri, '?') === false ? '?' : '&';
$uri .= http_build_query($queryData, null, '&');
$uri .= http_build_query($queryData, '', '&');
}
$url = parse_url($uri);
if ('https' == $url['scheme']) {
Expand Down
24 changes: 12 additions & 12 deletions tests/WebToPay/CallbackValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* Test for class WebToPay_CallbackValidator
*/
class WebToPay_CallbackValidatorTest extends PHPUnit_Framework_TestCase {
class WebToPay_CallbackValidatorTest extends TestCase {

/**
* @var WebToPay_Sign_SignCheckerInterface
Expand All @@ -23,18 +25,17 @@ class WebToPay_CallbackValidatorTest extends PHPUnit_Framework_TestCase {
/**
* Sets up this test
*/
public function setUp() {
$this->signer = $this->getMock('WebToPay_Sign_SignCheckerInterface');
$this->util = $this->getMock('WebToPay_Util', array('decodeSafeUrlBase64', 'parseHttpQuery'));
public function setUp(): void {
$this->signer = $this->createMock('WebToPay_Sign_SignCheckerInterface');
$this->util = $this->createMock('WebToPay_Util', array('decodeSafeUrlBase64', 'parseHttpQuery'));
$this->validator = new WebToPay_CallbackValidator(123, $this->signer, $this->util);
}

/**
* Exception should be thrown on invalid sign
*
* @expectedException WebToPay_Exception_Callback
*/
public function testValidateAndParseDataWithInvalidSign() {
$this->expectException(WebToPay_Exception_Callback::class);
$request = array('data' => 'abcdef', 'sign' => 'qwerty');

$this->signer->expects($this->once())->method('checkSign')->with($request)->will($this->returnValue(false));
Expand All @@ -45,16 +46,15 @@ public function testValidateAndParseDataWithInvalidSign() {

/**
* Exception should be thrown if project ID does not match expected one
*
* @expectedException WebToPay_Exception_Callback
*/
public function testValidateAndParseDataWithInvalidProject() {
$this->expectException(WebToPay_Exception_Callback::class);
$request = array('data' => 'abcdef', 'sign' => 'qwerty');
$parsed = array('projectid' => 456);

$this->signer->expects($this->once())->method('checkSign')->with($request)->will($this->returnValue(true));
$this->util->expects($this->at(0))->method('decodeSafeUrlBase64')->with('abcdef')->will($this->returnValue('zxc'));
$this->util->expects($this->at(1))->method('parseHttpQuery')->with('zxc')->will($this->returnValue($parsed));
$this->util->expects($this->once())->method('decodeSafeUrlBase64')->with('abcdef')->will($this->returnValue('zxc'));
$this->util->expects($this->once())->method('parseHttpQuery')->with('zxc')->will($this->returnValue($parsed));

$this->validator->validateAndParseData($request);
}
Expand All @@ -67,8 +67,8 @@ public function testValidateAndParseData() {
$parsed = array('projectid' => 123, 'someparam' => 'qwerty123', 'type' => 'micro');

$this->signer->expects($this->once())->method('checkSign')->with($request)->will($this->returnValue(true));
$this->util->expects($this->at(0))->method('decodeSafeUrlBase64')->with('abcdef')->will($this->returnValue('zxc'));
$this->util->expects($this->at(1))->method('parseHttpQuery')->with('zxc')->will($this->returnValue($parsed));
$this->util->expects($this->once())->method('decodeSafeUrlBase64')->with('abcdef')->will($this->returnValue('zxc'));
$this->util->expects($this->once())->method('parseHttpQuery')->with('zxc')->will($this->returnValue($parsed));

$this->assertEquals($parsed, $this->validator->validateAndParseData($request));
}
Expand Down
18 changes: 8 additions & 10 deletions tests/WebToPay/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* Test for class WebToPay_Factory
*/
class WebToPay_FactoryTest extends PHPUnit_Framework_TestCase {
class WebToPay_FactoryTest extends TestCase {

/**
* @var WebToPay_Factory
Expand All @@ -18,7 +20,7 @@ class WebToPay_FactoryTest extends PHPUnit_Framework_TestCase {
/**
* Sets up this test
*/
public function setUp() {
public function setUp(): void {
$this->factory = new WebToPay_Factory(array(
'projectId' => '123',
'password' => 'abc',
Expand Down Expand Up @@ -64,37 +66,33 @@ public function testGetPaymentMethodListProvider() {

/**
* Tests exception
*
* @expectedException WebToPay_Exception_Configuration
*/
public function testGetCallbackValidatorWithoutConfiguration() {
$this->expectException(WebToPay_Exception_Configuration::class);
$this->factoryWithoutConfiguration->getCallbackValidator();
}

/**
* Tests exception
*
* @expectedException WebToPay_Exception_Configuration
*/
public function testGetRequestBuilderWithoutConfiguration() {
$this->expectException(WebToPay_Exception_Configuration::class);
$this->factoryWithoutConfiguration->getRequestBuilder();
}

/**
* Tests exception
*
* @expectedException WebToPay_Exception_Configuration
*/
public function testGetSmsAnswerSenderWithoutConfiguration() {
$this->expectException(WebToPay_Exception_Configuration::class);
$this->factoryWithoutConfiguration->getSmsAnswerSender();
}

/**
* Tests exception
*
* @expectedException WebToPay_Exception_Configuration
*/
public function testGetPaymentMethodListProviderWithoutConfiguration() {
$this->expectException(WebToPay_Exception_Configuration::class);
$this->factoryWithoutConfiguration->getPaymentMethodListProvider();
}
}
14 changes: 7 additions & 7 deletions tests/WebToPay/RequestBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* Test for class WebToPay_RequestBuilder
*/
class WebToPay_RequestBuilderTest extends PHPUnit_Framework_TestCase {
class WebToPay_RequestBuilderTest extends TestCase {
/**
* @var WebToPay_UrlBuilder
*/
Expand All @@ -22,8 +24,8 @@ class WebToPay_RequestBuilderTest extends PHPUnit_Framework_TestCase {
/**
* Sets up this test
*/
public function setUp() {
$this->util = $this->getMock('WebToPay_Util', array('encodeSafeUrlBase64'));
public function setUp():void {
$this->util = $this->createMock('WebToPay_Util', array('encodeSafeUrlBase64'));
$this->urlBuilder = $this->getMockBuilder('WebToPay_UrlBuilder')
->disableOriginalConstructor()
->getMock();
Expand All @@ -33,10 +35,9 @@ public function setUp() {

/**
* Test build request when no orderid is passed
*
* @expectedException WebToPay_Exception_Validation
*/
public function testBuildRequestWithNoOrderId() {
$this->expectException(WebToPay_Exception_Validation::class);
$this->builder->buildRequest(array(
'accepturl' => 'http://local.test/',
'cancelurl' => 'http://local.test/',
Expand All @@ -46,10 +47,9 @@ public function testBuildRequestWithNoOrderId() {

/**
* Test build request when invalid currency is passed
*
* @expectedException WebToPay_Exception_Validation
*/
public function testBuildRequestWithInvalidCurrency() {
$this->expectException(WebToPay_Exception_Validation::class);
$this->builder->buildRequest(array(
'orderid' => 123,
'accepturl' => 'http://local.test/',
Expand Down
Loading

0 comments on commit 4ca6755

Please sign in to comment.