Issues (1092)

Model/TransactionStatus/Forwarding.php (2 issues)

Labels
Severity
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\TransactionStatus;
28
29
/**
30
 * Class for handling the TransactionStatus forwarding
31
 */
32
class Forwarding
33
{
34
    /**
35
     * PAYONE config helper
36
     *
37
     * @var \Payone\Core\Helper\Config
38
     */
39
    protected $configHelper;
40
41
    /**
42
     * Magento 2 Curl library
43
     *
44
     * @var \Magento\Framework\HTTP\Client\Curl
0 ignored issues
show
The type Magento\Framework\HTTP\Client\Curl was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
     */
46
    protected $curl;
47
48
    /**
49
     * Logger object
50
     *
51
     * @var \Psr\Log\LoggerInterface
0 ignored issues
show
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
     */
53
    protected $logger;
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param \Payone\Core\Helper\Config            $configHelper
59
     * @param \Magento\Framework\HTTP\Client\Curl   $curl
60
     * @param \Psr\Log\LoggerInterface              $logger
61
     */
62
    public function __construct(
63
        \Payone\Core\Helper\Config $configHelper,
64
        \Magento\Framework\HTTP\Client\Curl $curl,
65
        \Psr\Log\LoggerInterface $logger
66
    ) {
67
        $this->configHelper = $configHelper;
68
        $this->curl = $curl;
69
        $this->logger = $logger;
70
    }
71
72
    /**
73
     * Log to log-file if configured
74
     *
75
     * @param  string $sMessage
76
     * @param  array $aPostArray
77
     * @return void
78
     */
79
    protected function log($sMessage, $aPostArray)
80
    {
81
        if ((bool)$this->configHelper->getConfigParam('log_active', 'forwarding', 'payone_misc') === true) {
82
            $sIdent = '';
83
            if (isset($aPostArray['txid'])) {
84
                $sIdent = $aPostArray['txid'].' - ';
85
            }
86
            $this->logger->info($sIdent.$sMessage);
87
        }
88
    }
89
90
    /**
91
     * Execute TransactionStatus forwarding with curl
92
     *
93
     * @param  array  $aPostArray
94
     * @param  string $sUrl
95
     * @param  int    $iTimeout
96
     * @return void
97
     */
98
    public function forwardRequest($aPostArray, $sUrl, $iTimeout)
99
    {
100
        if ($iTimeout == 0) {
101
            $iTimeout = 45;
102
        }
103
104
        $this->log($sUrl.' Forward with timeout of '.$iTimeout.' seconds', $aPostArray);
105
106
        $this->curl->setTimeout($iTimeout);
107
        $this->curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
108
        $this->curl->setOption(CURLOPT_SSL_VERIFYHOST, false);
109
        try {
110
            $this->curl->post($sUrl, $aPostArray);
111
            $this->log($sUrl.' Response: '.$this->curl->getBody(), $aPostArray);
112
        } catch(\Exception $exc) {
113
            $this->log($sUrl.' Exception: '.$exc->getMessage(), $aPostArray);
114
        }
115
    }
116
117
    /**
118
     * Forward a request and dont wait for a response
119
     *
120
     * @param  array  $aPostArray
121
     * @param  string $sUrl
122
     * @return void
123
     */
124
    public function forwardAsyncRequest($aPostArray, $sUrl)
125
    {
126
	// Increased timeout to 5500ms
127
	// See payone-gmbh/magento-2 issue #316
128
        $this->curl->setOption(CURLOPT_TIMEOUT_MS, 5500);
129
        $this->curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
130
        $this->curl->setOption(CURLOPT_SSL_VERIFYHOST, false);
131
	$this->curl->setOption(CURLOPT_FOLLOWLOCATION, true);
132
        try {
133
            $this->curl->post($sUrl, $aPostArray);
134
        } catch (\Exception $exc) {
135
            // Async calls will always throw a timeout exception
136
        }
137
    }
138
139
    /**
140
     * Handle single TransactionStatus forwarding
141
     *
142
     * @param  array  $aPostArray
143
     * @param  array  $aForwardEntry
144
     * @param  string $sStatusAction
145
     * @return void
146
     */
147
    protected function handleSingleForwarding($aPostArray, $aForwardEntry, $sStatusAction)
148
    {
149
        foreach ($aForwardEntry['txaction'] as $sForwardAction) {
150
            if ($sForwardAction == $sStatusAction) {
151
                $this->forwardRequest($aPostArray, $aForwardEntry['url'], (int)$aForwardEntry['timeout']);
152
            }
153
        }
154
    }
155
156
    /**
157
     * Converts post array to a single-line string for output in log
158
     *
159
     * @param  array $aPostArray
160
     * @return string
161
     */
162
    protected function getStatusLogLine($aPostArray)
163
    {
164
        $sLine = '';
165
        foreach ($aPostArray as $sKey => $sValue) {
166
            if (is_array($sValue)) {
167
                $sValue = '['.$this->getStatusLogLine($sValue).']';
168
            }
169
            $sLine .= $sKey.'='.$sValue.';';
170
        }
171
        return $sLine;
172
    }
173
174
    /**
175
     * Handle TransactionStatus forwarding
176
     *
177
     * @param  array $aPostArray
178
     * @return void
179
     */
180
    public function handleForwardings($aPostArray)
181
    {
182
        $this->log('Handle StatusForwarding: '.$this->getStatusLogLine($aPostArray), $aPostArray);
183
        if (isset($aPostArray['txaction'])) {
184
            $aForwarding = $this->configHelper->getForwardingUrls();
185
            $sStatusAction = $aPostArray['txaction'];
186
            foreach ($aForwarding as $aForwardEntry) {
187
                if (isset($aForwardEntry['txaction']) && !empty($aForwardEntry['txaction'])) {
188
                    $this->handleSingleForwarding($aPostArray, $aForwardEntry, $sStatusAction);
189
                }
190
            }
191
        }
192
    }
193
}
194