Passed
Pull Request — master (#600)
by
unknown
09:40
created

Index::getPostArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Controller\Transactionstatus;
28
29
use Magento\Sales\Model\Order;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order 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...
30
31
/**
32
 * TransactionStatus receiver
33
 */
34
class Index extends \Payone\Core\Controller\ExternalAction
35
{
36
    /**
37
     * TransactionStatus model
38
     *
39
     * @var \Payone\Core\Model\ResourceModel\TransactionStatus
40
     */
41
    protected $transactionStatus;
42
43
    /**
44
     * PAYONE toolkit helper
45
     *
46
     * @var \Payone\Core\Helper\Toolkit
47
     */
48
    protected $toolkitHelper;
49
50
    /**
51
     * PAYONE environment helper
52
     *
53
     * @var \Payone\Core\Helper\Environment
54
     */
55
    protected $environmentHelper;
56
57
    /**
58
     * PAYONE order helper
59
     *
60
     * @var \Payone\Core\Helper\Order
61
     */
62
    protected $orderHelper;
63
64
    /**
65
     * TransactionStatus handler
66
     *
67
     * @var \Payone\Core\Model\Handler\TransactionStatus
68
     */
69
    protected $transactionStatusHandler;
70
71
    /**
72
     * Result factory for file-download
73
     *
74
     * @var \Magento\Framework\Controller\Result\RawFactory
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\RawFactory 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...
75
     */
76
    protected $resultRawFactory;
77
78
    /**
79
     * PAYONE substitute order handler
80
     *
81
     * @var \Payone\Core\Model\Handler\SubstituteOrder
82
     */
83
    protected $substituteOrder;
84
85
    /**
86
     * CacheInterface object
87
     *
88
     * @var \Magento\Framework\App\CacheInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CacheInterface 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...
89
     */
90
    protected $cache;
91
92
    /**
93
     * Constructor
94
     *
95
     * @param \Magento\Framework\App\Action\Context              $context
96
     * @param \Magento\Framework\Data\Form\FormKey               $formKey
97
     * @param \Payone\Core\Model\ResourceModel\TransactionStatus $transactionStatus
98
     * @param \Payone\Core\Helper\Toolkit                        $toolkitHelper
99
     * @param \Payone\Core\Helper\Environment                    $environmentHelper
100
     * @param \Payone\Core\Helper\Order                          $orderHelper
101
     * @param \Payone\Core\Model\Handler\TransactionStatus       $transactionStatusHandler,
102
     * @param \Magento\Framework\Controller\Result\RawFactory    $resultRawFactory
103
     * @param \Payone\Core\Model\Handler\SubstituteOrder         $substituteOrder
104
     * @param \Magento\Framework\App\CacheInterface              $cache
105
     */
106
    public function __construct(
107
        \Magento\Framework\App\Action\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Context 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...
108
        \Magento\Framework\Data\Form\FormKey $formKey,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Data\Form\FormKey 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...
109
        \Payone\Core\Model\ResourceModel\TransactionStatus $transactionStatus,
110
        \Payone\Core\Helper\Toolkit $toolkitHelper,
111
        \Payone\Core\Helper\Environment $environmentHelper,
112
        \Payone\Core\Helper\Order $orderHelper,
113
        \Payone\Core\Model\Handler\TransactionStatus $transactionStatusHandler,
114
        \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
115
        \Payone\Core\Model\Handler\SubstituteOrder $substituteOrder,
116
        \Magento\Framework\App\CacheInterface $cache
117
    ) {
118
        parent::__construct($context, $formKey);
119
        $this->transactionStatus = $transactionStatus;
120
        $this->toolkitHelper = $toolkitHelper;
121
        $this->environmentHelper = $environmentHelper;
122
        $this->orderHelper = $orderHelper;
123
        $this->transactionStatusHandler = $transactionStatusHandler;
124
        $this->resultRawFactory = $resultRawFactory;
125
        $this->substituteOrder = $substituteOrder;
126
        $this->cache = $cache;
127
    }
128
129
    /**
130
     * Return request parameter value
131
     *
132
     * @param  string $sKey
133
     * @return string
134
     */
135
    protected function getParam($sKey)
136
    {
137
        return $this->getRequest()->getParam($sKey, '');
138
    }
139
140
    /**
141
     * Return Post array
142
     *
143
     * @return array
144
     */
145
    protected function getPostArray()
146
    {
147
        return $this->getRequest()->getPostValue();
148
    }
149
150
    /**
151
     * Write the TransactionStatus to the database
152
     *
153
     * @param  Order $oOrder
154
     * @param  array $aRequest
155
     * @param  bool  $blWillBeHandled
156
     * @return void
157
     */
158
    protected function logTransactionStatus(Order $oOrder, $aRequest, $blWillBeHandled)
159
    {
160
        $this->transactionStatus->addTransactionLogEntry($aRequest, $oOrder, $blWillBeHandled);
161
    }
162
163
    /**
164
     * Main method for executing all needed actions for the incoming TransactionStatus
165
     *
166
     * @return string
167
     */
168
    protected function handleTransactionStatus()
169
    {
170
        if (!$this->environmentHelper->isRemoteIpValid()) {
171
            return 'Access denied';
172
        } elseif (!$this->toolkitHelper->isKeyValid($this->getParam('key'))) {
173
            return 'Key wrong or missing!';
174
        }
175
176
        $oOrder = $this->orderHelper->getOrderByTxid($this->getParam('txid'));
177
        if (!$oOrder) {
178
            return 'Order not found';
179
        }
180
181
        if ($this->getParam('txaction') != 'appointed' && empty($this->transactionStatus->getAppointedIdByTxid($this->getParam('txid')))) {
182
            return 'Appointed status has to be handled first!';
183
        }
184
185
        $lockKey = 'payone_order_status_lock_' . $oOrder->getId();
186
        if ($this->cache->load($lockKey)) {
187
            return 'Another status is being handled at the moment.';
188
        }
189
190
        // Lock transaction status handling for this order for 50 seconds to not handle multiple statuses at the same time
191
        $this->cache->save('1', $lockKey, [], 50);
192
193
        try {
194
            if ($this->getParam('txaction') == 'appointed' && $oOrder->getStatus() == 'canceled') {
195
                // order was canceled in checkout, probably due to browser-back-button usage -> create a new order for incoming payment
196
                $oOrder = $this->substituteOrder->createSubstituteOrder($oOrder, false);
197
            }
198
199
            $this->logTransactionStatus($oOrder, $this->getPostArray(), true);
200
201
            $this->transactionStatusHandler->handle($oOrder, $this->getPostArray());
202
        } catch (\Exception $e) {
203
            $this->cache->remove($lockKey);
204
            throw $e;
205
        }
206
207
        return 'TSOK';
208
    }
209
210
    /**
211
     * Executing TransactionStatus handling
212
     *
213
     * @return \Magento\Framework\Controller\Result\Raw
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\Raw 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...
214
     */
215
    public function execute()
216
    {
217
        $sOutput = $this->handleTransactionStatus();
218
219
        $oResultRaw = $this->resultRawFactory->create();
220
        $oResultRaw->setContents($sOutput);
221
        
222
        return $oResultRaw;
223
    }
224
}
225