Skip to content

Commit 3b6d3ac

Browse files
committed
MAG2-59 - Added proxy mode to TransactionStatus script
Fixes PAYONE-GmbH#143
1 parent 9e6ce89 commit 3b6d3ac

File tree

6 files changed

+149
-6
lines changed

6 files changed

+149
-6
lines changed

Helper/Environment.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@
3131
*/
3232
class Environment extends \Payone\Core\Helper\Base
3333
{
34+
/**
35+
* Extended remote address object
36+
*
37+
* @var \Payone\Core\Model\Environment\RemoteAddress
38+
*/
39+
protected $remoteAddress;
40+
41+
/**
42+
* Constructor
43+
*
44+
* @param \Magento\Framework\App\Helper\Context $context
45+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
46+
* @param \Payone\Core\Helper\Shop $shopHelper
47+
* @param \Payone\Core\Model\Environment\RemoteAddress $remoteAddress
48+
*/
49+
public function __construct(
50+
\Magento\Framework\App\Helper\Context $context,
51+
\Magento\Store\Model\StoreManagerInterface $storeManager,
52+
\Payone\Core\Helper\Shop $shopHelper,
53+
\Payone\Core\Model\Environment\RemoteAddress $remoteAddress
54+
) {
55+
parent::__construct($context, $storeManager, $shopHelper);
56+
$this->remoteAddress = $remoteAddress;
57+
}
58+
3459
/**
3560
* Get encoding
3661
*
@@ -44,11 +69,16 @@ public function getEncoding()
4469
/**
4570
* Get the IP of the requesting client
4671
*
72+
* @param bool $blUseHttpXForwarded
4773
* @return string
4874
*/
49-
public function getRemoteIp()
75+
public function getRemoteIp($blUseHttpXForwarded = false)
5076
{
51-
return $this->_remoteAddress->getRemoteAddress();
77+
$blProxyMode = (bool)$this->getConfigParam('proxy_mode', 'processing', 'payone_misc');
78+
if ($blUseHttpXForwarded === true && $blProxyMode === true) {
79+
$this->remoteAddress->useHttpXForwarded();
80+
}
81+
return $this->remoteAddress->getRemoteAddress();
5282
}
5383

5484
/**
@@ -58,7 +88,7 @@ public function getRemoteIp()
5888
*/
5989
public function isRemoteIpValid()
6090
{
61-
$sRemoteIp = $this->getRemoteIp();
91+
$sRemoteIp = $this->getRemoteIp(true);
6292
$sValidIps = $this->getConfigParam('valid_ips', 'processing', 'payone_misc');
6393
$aWhitelist = explode("\n", $sValidIps);
6494
$aWhitelist = array_filter(array_map('trim', $aWhitelist));

Model/Environment/RemoteAddress.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* PHP version 5
18+
*
19+
* @category Payone
20+
* @package Payone_Magento2_Plugin
21+
* @author FATCHIP GmbH <[email protected]>
22+
* @copyright 2003 - 2016 Payone GmbH
23+
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
24+
* @link http://www.payone.de
25+
*/
26+
27+
namespace Payone\Core\Model\Environment;
28+
29+
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress as CoreRemoteAddress;
30+
31+
/**
32+
* Extended RemoteAddress class
33+
*/
34+
class RemoteAddress extends CoreRemoteAddress
35+
{
36+
/**
37+
* Add X-Forwarded-For header to IP address determination
38+
*
39+
* @return void
40+
*/
41+
public function useHttpXForwarded()
42+
{
43+
if (array_key_exists('x-forwarded-for', $this->alternativeHeaders) === false) {
44+
$this->alternativeHeaders['x-forwarded-for'] = 'HTTP_X_FORWARDED_FOR';
45+
}
46+
}
47+
}

Test/Unit/Helper/EnvironmentTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
use Magento\Framework\App\Helper\Context;
3434
use Magento\Store\Model\ScopeInterface;
3535
use Magento\Framework\App\Config\ScopeConfigInterface;
36-
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
36+
use Payone\Core\Model\Environment\RemoteAddress;
3737
use Payone\Core\Test\Unit\BaseTestCase;
3838
use Payone\Core\Test\Unit\PayoneObjectManager;
3939

@@ -72,7 +72,8 @@ protected function setUp()
7272

7373
$this->environment = $this->objectManager->getObject(Environment::class, [
7474
'context' => $context,
75-
'storeManager' => $storeManager
75+
'storeManager' => $storeManager,
76+
'remoteAddress' => $remoteAddress
7677
]);
7778
}
7879

@@ -99,7 +100,8 @@ public function testIsRemoteIpValid()
99100
->method('getValue')
100101
->willReturnMap(
101102
[
102-
['payone_misc/processing/valid_ips', ScopeInterface::SCOPE_STORE, null, $sWhitelist]
103+
['payone_misc/processing/valid_ips', ScopeInterface::SCOPE_STORE, null, $sWhitelist],
104+
['payone_misc/processing/proxy_mode', ScopeInterface::SCOPE_STORE, null, 1]
103105
]
104106
);
105107

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* PHP version 5
18+
*
19+
* @category Payone
20+
* @package Payone_Magento2_Plugin
21+
* @author FATCHIP GmbH <[email protected]>
22+
* @copyright 2003 - 2017 Payone GmbH
23+
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
24+
* @link http://www.payone.de
25+
*/
26+
27+
namespace Payone\Core\Test\Unit\Model\Methods;
28+
29+
use Payone\Core\Model\Environment\RemoteAddress as ClassToTest;
30+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
31+
use Payone\Core\Test\Unit\BaseTestCase;
32+
use Payone\Core\Test\Unit\PayoneObjectManager;
33+
34+
class RemoteAddressTest extends BaseTestCase
35+
{
36+
/**
37+
* @var ClassToTest
38+
*/
39+
private $classToTest;
40+
41+
/**
42+
* @var ObjectManager|PayoneObjectManager
43+
*/
44+
private $objectManager;
45+
46+
protected function setUp()
47+
{
48+
$this->objectManager = $this->getObjectManager();
49+
50+
$this->classToTest = $this->objectManager->getObject(ClassToTest::class);
51+
}
52+
53+
public function testUseHttpXForwardedTest()
54+
{
55+
$result = $this->classToTest->useHttpXForwarded();
56+
$this->assertNull($result);
57+
}
58+
}

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@
426426
<label>Valid PAYONE IPs</label>
427427
<comment>Enter valid PAYONE IPs (Format xxx.xxx.xxx.xxx). As a Wildcard You can use * </comment>
428428
</field>
429+
<field id="proxy_mode" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
430+
<label>Proxy-Mode</label>
431+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
432+
<comment>If your shop is behind a Proxy. Enable this option to transmit the HTTP_X_FORWARDED_FOR</comment>
433+
</field>
429434
</group>
430435
<group id="forwarding" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
431436
<label>Transaction-Status Forwarding</label>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@
385385
<payone_misc>
386386
<processing>
387387
<valid_ips><![CDATA[185.60.20.*]]></valid_ips>
388+
<proxy_mode>0</proxy_mode>
388389
</processing>
389390
<costs>
390391
<sku>delivery</sku>

0 commit comments

Comments
 (0)