Issues (1092)

Helper/Order.php (10 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\Helper;
28
29
use Magento\Quote\Model\Quote\Address;
0 ignored issues
show
The type Magento\Quote\Model\Quote\Address 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\Exception\LocalizedException;
0 ignored issues
show
The type Magento\Framework\Exception\LocalizedException 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\Quote\Model\Quote;
0 ignored issues
show
The type Magento\Quote\Model\Quote 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 Magento\Sales\Model\Order as SalesOrder;
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...
33
34
/**
35
 * Helper class for everything that has to do with orders
36
 */
37
class Order extends \Payone\Core\Helper\Base
38
{
39
    /**
40
     * PAYONE database helper
41
     *
42
     * @var \Payone\Core\Helper\Database
43
     */
44
    protected $databaseHelper;
45
46
    /**
47
     * PAYONE customer helper
48
     *
49
     * @var \Payone\Core\Helper\Customer
50
     */
51
    protected $customerHelper;
52
53
    /**
54
     * Order factory
55
     *
56
     * @var \Magento\Sales\Model\OrderFactory
0 ignored issues
show
The type Magento\Sales\Model\OrderFactory 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...
57
     */
58
    protected $orderFactory;
59
60
    /**
61
     * Totals collector object
62
     *
63
     * @var \Magento\Quote\Model\Quote\TotalsCollector
0 ignored issues
show
The type Magento\Quote\Model\Quote\TotalsCollector 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...
64
     */
65
    protected $totalsCollector;
66
67
    /**
68
     * Constructor
69
     *
70
     * @param \Magento\Framework\App\Helper\Context      $context
71
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
72
     * @param \Payone\Core\Helper\Shop                   $shopHelper
73
     * @param \Magento\Framework\App\State               $state
74
     * @param \Payone\Core\Helper\Database               $databaseHelper
75
     * @param \Payone\Core\Helper\Customer               $customerHelper
76
     * @param \Magento\Sales\Model\OrderFactory          $orderFactory
77
     * @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
78
     */
79
    public function __construct(
80
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
The type Magento\Framework\App\Helper\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...
81
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
The type Magento\Store\Model\StoreManagerInterface 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...
82
        \Payone\Core\Helper\Shop $shopHelper,
83
        \Magento\Framework\App\State $state,
0 ignored issues
show
The type Magento\Framework\App\State 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...
84
        \Payone\Core\Helper\Database $databaseHelper,
85
        \Payone\Core\Helper\Customer $customerHelper,
86
        \Magento\Sales\Model\OrderFactory $orderFactory,
87
        \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
88
    ) {
89
        parent::__construct($context, $storeManager, $shopHelper, $state);
90
        $this->databaseHelper = $databaseHelper;
91
        $this->customerHelper = $customerHelper;
92
        $this->orderFactory = $orderFactory;
93
        $this->totalsCollector = $totalsCollector;
94
    }
95
96
    /**
97
     * Return the order related to the given TransactionStatus
98
     *
99
     * @param  string $sTxid
100
     * @return SalesOrder|null
101
     */
102
    public function getOrderByTxid($sTxid)
103
    {
104
        $sIncrementId = $this->databaseHelper->getOrderIncrementIdByTxid($sTxid);
105
        $oOrder = $this->orderFactory->create()->loadByIncrementId($sIncrementId);
106
        if ($oOrder && $oOrder->getId()) {
107
            return $oOrder;
108
        }
109
        return null;
110
    }
111
112
    /**
113
     * Fill billing and shipping addresses with the needed information from the response
114
     *
115
     * @param  Address $oAddress
116
     * @param  string  $sFirstname
117
     * @param  string  $sLastname
118
     * @param  string  $sStreet
119
     * @param  string  $sCity
120
     * @param  string  $sZip
121
     * @param  string  $sCountry
122
     * @param  string  $sState
123
     * @return Address
124
     */
125
    public function fillSingleAddress(Address $oAddress, $sFirstname, $sLastname, $sStreet, $sCity, $sZip, $sCountry, $sState)
126
    {
127
        $oAddress->setFirstname($sFirstname);
128
        $oAddress->setLastname($sLastname);
129
        $oAddress->setStreet($sStreet);
130
        $oAddress->setCity($sCity);
131
        $oAddress->setPostcode($sZip);
132
        $oAddress->setCountryId($sCountry);
133
134
        $oRegion = $this->customerHelper->getRegion($sCountry, $sState);
135
        if ($oRegion) {
136
            $oAddress->setRegionId($oRegion->getId());
137
            $oAddress->setRegionCode($sState);
138
        } else {
139
            $oAddress->setRegionId(null);
140
            $oAddress->setRegionCode(null);
141
        }
142
        return $oAddress;
143
    }
144
145
    /**
146
     * Map response parameters to distinct parameters
147
     *
148
     * @param  Address $oAddress
149
     * @param  array   $aResponse
150
     * @param  bool    $blIsBilling
151
     * @return Address
152
     */
153
    public function fillSingleAddressByResponse($oAddress, $aResponse, $blIsBilling = false)
154
    {
155
        $sPrefix = 'shipping';
156
        if ($blIsBilling === true) {
157
            $sPrefix = 'billing';
158
        }
159
        return $this->fillSingleAddress(
160
            $oAddress,
161
            $aResponse['add_paydata['.$sPrefix.'_firstname]'],
162
            $aResponse['add_paydata['.$sPrefix.'_lastname]'],
163
            $aResponse['add_paydata['.$sPrefix.'_street]'],
164
            $aResponse['add_paydata['.$sPrefix.'_city]'],
165
            $aResponse['add_paydata['.$sPrefix.'_zip]'],
166
            $aResponse['add_paydata['.$sPrefix.'_country]'],
167
            $aResponse['add_paydata['.$sPrefix.'_country]']
168
        );
169
    }
170
171
    /**
172
     * Address-handling for billing- and shipping address
173
     *
174
     * @param  Quote $oQuote
175
     * @param  array $aResponse
176
     * @param  bool $blUseBilling
177
     * @return Quote
178
     */
179
    public function updateAddresses(Quote $oQuote, $aResponse, $blUseBilling = false)
180
    {
181
        $oBillingAddress = $this->fillSingleAddressByResponse($oQuote->getBillingAddress(), $aResponse, $blUseBilling);
182
        $oBillingAddress->setEmail($aResponse['add_paydata[email]']);
183
184
        $oQuote->setBillingAddress($oBillingAddress);
185
        $oQuote->getBillingAddress()->setShouldIgnoreValidation(true);
186
187
        if (!$oQuote->getIsVirtual()) {
188
            $oShippingAddress = $this->fillSingleAddressByResponse($oQuote->getShippingAddress(), $aResponse);
189
            #$oShippingAddress->setCollectShippingRates(true);
190
            $this->setShippingMethod($oShippingAddress, $oQuote);
191
            $oQuote->setShippingAddress($oShippingAddress);
192
            $oQuote->getShippingAddress()->setShouldIgnoreValidation(true);
193
        }
194
195
        return $oQuote;
196
    }
197
198
    /**
199
     * Determine the cheapest available shipping method
200
     *
201
     * @param  Quote   $oQuote
202
     * @param  Address $oShippingAddress
203
     * @return string|bool
204
     */
205
    public function getShippingMethod(Quote $oQuote, Address $oShippingAddress)
206
    {
207
        $aRates = [];
208
209
        // Needed for getGroupedAllShippingRates, otherwise sometimes empty output
210
        $this->totalsCollector->collectAddressTotals($oQuote, $oShippingAddress);
211
        $oShippingRates = $oShippingAddress->getGroupedAllShippingRates();
212
213
        foreach ($oShippingRates as $oCarrierRates) {
214
            foreach ($oCarrierRates as $oRate) {
215
                $aRates[(string)$oRate->getPrice()] = $oRate->getCode();
216
            }
217
        }
218
219
        if (!empty($aRates)) { // more than one shipping method existing?
220
            ksort($aRates); // sort by price ascending
221
            return array_shift($aRates); // return the cheapest shipping-method
222
        }
223
        return false;
224
    }
225
226
    /**
227
     * Get Shipping method and add it to the shipping-address object
228
     *
229
     * @param  Address $oAddress
230
     * @param  Quote   $oQuote
231
     * @return Address
232
     * @throws LocalizedException
233
     */
234
    public function setShippingMethod(Address $oAddress, Quote $oQuote)
235
    {
236
        $oAddress->setCollectShippingRates(true);
237
238
        $sShippingMethod = $this->getShippingMethod($oQuote, $oAddress);
239
        if (!$sShippingMethod) {
240
            throw new LocalizedException(__("No shipping method available for your address!"));
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
            throw new LocalizedException(/** @scrutinizer ignore-call */ __("No shipping method available for your address!"));
Loading history...
241
        }
242
        $oAddress->setShippingMethod($sShippingMethod);
243
244
        return $oAddress;
245
    }
246
}
247