Issues (1092)

Observer/OrderPaymentPlaceEnd.php (3 issues)

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\Observer;
28
29
use Magento\Sales\Model\Order;
0 ignored issues
show
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
use Magento\Framework\Event\ObserverInterface;
0 ignored issues
show
The type Magento\Framework\Event\ObserverInterface 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...
31
use Magento\Framework\Event\Observer;
0 ignored issues
show
The type Magento\Framework\Event\Observer 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...
32
use Payone\Core\Helper\Consumerscore;
33
use Magento\Sales\Model\Order\Payment;
34
use Magento\Checkout\Model\Session;
35
36
/**
37
 * Event class to set the orderstatus to new and pending
38
 */
39
class OrderPaymentPlaceEnd implements ObserverInterface
40
{
41
    /**
42
     * PAYONE payment helper
43
     *
44
     * @var Consumerscore
45
     */
46
    protected $consumerscoreHelper;
47
48
    /**
49
     * Checkout session object
50
     *
51
     * @var Session
52
     */
53
    protected $checkoutSession;
54
55
    /**
56
     * Array of keys to remove from session after payment was successful
57
     *
58
     * @var array
59
     */
60
    protected $aCheckoutSessionClearList = [
61
        'is_payone_redirect_cancellation',
62
        'is_payone_amazon_pay_auth',
63
        'payone_workorder_id',
64
        'amazon_workorder_id',
65
        'amazon_address_token',
66
        'amazon_reference_id',
67
        'order_reference_details_executed',
68
        'trigger_invalid_payment',
69
        'payone_ratepay_device_fingerprint_token',
70
        'payone_is_amazon_pay_express_payment',
71
        'payone_quote_address_hash',
72
        'payone_express_address_response',
73
    ];
74
75
    /**
76
     * Constructor
77
     *
78
     * @param Consumerscore $consumerscoreHelper
79
     * @param Session       $checkoutSession
80
     */
81
    public function __construct(Consumerscore $consumerscoreHelper, Session $checkoutSession)
82
    {
83
        $this->consumerscoreHelper = $consumerscoreHelper;
84
        $this->checkoutSession = $checkoutSession;
85
    }
86
87
    /**
88
     * Handle order status
89
     *
90
     * @param  Observer $observer
91
     * @return void
92
     */
93
    protected function handleOrderStatus(Observer $observer)
94
    {
95
        /** @var Payment $oPayment */
96
        $oPayment = $observer->getEvent()->getPayment();
97
        $oPaymentInstance = $oPayment->getMethodInstance();
98
        if (stripos($oPaymentInstance->getCode(), 'payone') !== false) {
99
            $oOrder = $oPayment->getOrder();
100
            $oOrder->setState(Order::STATE_NEW);
101
            $oOrder->setStatus($oPaymentInstance->getConfigData('order_status'));
102
        }
103
    }
104
105
    /**
106
     * Execute certain tasks after the payment is placed and thus the order is placed
107
     *
108
     * @param  Observer $observer
109
     * @return void
110
     */
111
    public function execute(Observer $observer)
112
    {
113
        // set status to new - pending on new orders
114
        $this->handleOrderStatus($observer);
115
116
        // increment counter for every order, needed for the A/B test feature
117
        $this->consumerscoreHelper->incrementConsumerscoreSampleCounter();
118
119
        $this->clearSession($this->checkoutSession, $this->aCheckoutSessionClearList);
120
    }
121
122
    /**
123
     * Unset given keys from session object
124
     *
125
     * @param  Session $oSession
126
     * @param  array   $aClearParamList
127
     * @return void
128
     */
129
    protected function clearSession($oSession, $aClearParamList)
130
    {
131
        $aSessionData = $oSession->getData();
132
        foreach ($aSessionData as $sKey => $sValue) {
133
            foreach ($aClearParamList as $sDeleteKey) {
134
                if (stripos($sKey, $sDeleteKey) !== false) {
135
                    $oSession->unsetData($sKey);
136
                    break;
137
                }
138
            }
139
        }
140
    }
141
}
142