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; |
30
|
|
|
use Magento\Framework\Exception\LocalizedException; |
31
|
|
|
use Magento\Quote\Model\Quote; |
32
|
|
|
use Magento\Sales\Model\Order as SalesOrder; |
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 |
57
|
|
|
*/ |
58
|
|
|
protected $orderFactory; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Totals collector object |
62
|
|
|
* |
63
|
|
|
* @var \Magento\Quote\Model\Quote\TotalsCollector |
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 \Payone\Core\Helper\Database $databaseHelper |
74
|
|
|
* @param \Payone\Core\Helper\Customer $customerHelper |
75
|
|
|
* @param \Magento\Sales\Model\OrderFactory $orderFactory |
76
|
|
|
* @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
\Magento\Framework\App\Helper\Context $context, |
80
|
|
|
\Magento\Store\Model\StoreManagerInterface $storeManager, |
81
|
|
|
\Payone\Core\Helper\Shop $shopHelper, |
82
|
|
|
\Payone\Core\Helper\Database $databaseHelper, |
83
|
|
|
\Payone\Core\Helper\Customer $customerHelper, |
84
|
|
|
\Magento\Sales\Model\OrderFactory $orderFactory, |
85
|
|
|
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector |
86
|
|
|
) { |
87
|
|
|
parent::__construct($context, $storeManager, $shopHelper); |
88
|
|
|
$this->databaseHelper = $databaseHelper; |
89
|
|
|
$this->customerHelper = $customerHelper; |
90
|
|
|
$this->orderFactory = $orderFactory; |
91
|
|
|
$this->totalsCollector = $totalsCollector; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the order related to the given TransactionStatus |
96
|
|
|
* |
97
|
|
|
* @param string $sTxid |
98
|
|
|
* @return SalesOrder|null |
99
|
|
|
*/ |
100
|
|
|
public function getOrderByTxid($sTxid) |
101
|
|
|
{ |
102
|
|
|
$sIncrementId = $this->databaseHelper->getOrderIncrementIdByTxid($sTxid); |
103
|
|
|
$oOrder = $this->orderFactory->create()->loadByIncrementId($sIncrementId); |
104
|
|
|
if ($oOrder && $oOrder->getId()) { |
105
|
|
|
return $oOrder; |
106
|
|
|
} |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine the cheapest available shipping method |
112
|
|
|
* |
113
|
|
|
* @param Quote $oQuote |
114
|
|
|
* @param Address $oShippingAddress |
115
|
|
|
* @return string|bool |
116
|
|
|
*/ |
117
|
|
|
public function getShippingMethod(Quote $oQuote, Address $oShippingAddress) |
118
|
|
|
{ |
119
|
|
|
$aRates = []; |
120
|
|
|
|
121
|
|
|
// Needed for getGroupedAllShippingRates, otherwise sometimes empty output |
122
|
|
|
$this->totalsCollector->collectAddressTotals($oQuote, $oShippingAddress); |
123
|
|
|
$oShippingRates = $oShippingAddress->getGroupedAllShippingRates(); |
124
|
|
|
|
125
|
|
|
foreach ($oShippingRates as $oCarrierRates) { |
126
|
|
|
foreach ($oCarrierRates as $oRate) { |
127
|
|
|
$aRates[$oRate->getPrice()] = $oRate->getCode(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!empty($aRates)) { // more than one shipping method existing? |
132
|
|
|
ksort($aRates); // sort by price ascending |
133
|
|
|
return array_shift($aRates); // return the cheapest shipping-method |
134
|
|
|
} |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get Shipping method and add it to the shipping-address object |
140
|
|
|
* |
141
|
|
|
* @param Address $oAddress |
142
|
|
|
* @param Quote $oQuote |
143
|
|
|
* @return Address |
144
|
|
|
* @throws LocalizedException |
145
|
|
|
*/ |
146
|
|
|
public function setShippingMethod(Address $oAddress, Quote $oQuote) |
147
|
|
|
{ |
148
|
|
|
$oAddress->setCollectShippingRates(true); |
149
|
|
|
|
150
|
|
|
$sShippingMethod = $this->getShippingMethod($oQuote, $oAddress); |
151
|
|
|
if (!$sShippingMethod) { |
152
|
|
|
throw new LocalizedException(__("No shipping method available for your address!")); |
153
|
|
|
} |
154
|
|
|
$oAddress->setShippingMethod($sShippingMethod); |
155
|
|
|
|
156
|
|
|
return $oAddress; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Fill billing and shipping addresses with the needed information from the response |
161
|
|
|
* |
162
|
|
|
* @param Address $oAddress |
163
|
|
|
* @param string $sFirstname |
164
|
|
|
* @param string $sLastname |
165
|
|
|
* @param string $sStreet |
166
|
|
|
* @param string $sCity |
167
|
|
|
* @param string $sZip |
168
|
|
|
* @param string $sCountry |
169
|
|
|
* @param string $sState |
170
|
|
|
* @return Address |
171
|
|
|
*/ |
172
|
|
|
public function fillSingleAddress(Address $oAddress, $sFirstname, $sLastname, $sStreet, $sCity, $sZip, $sCountry, $sState) |
173
|
|
|
{ |
174
|
|
|
$oAddress->setFirstname($sFirstname); |
175
|
|
|
$oAddress->setLastname($sLastname); |
176
|
|
|
$oAddress->setStreet($sStreet); |
177
|
|
|
$oAddress->setCity($sCity); |
178
|
|
|
$oAddress->setPostcode($sZip); |
179
|
|
|
$oAddress->setCountryId($sCountry); |
180
|
|
|
|
181
|
|
|
$oRegion = $this->customerHelper->getRegion($sCountry, $sState); |
182
|
|
|
if ($oRegion) { |
183
|
|
|
$oAddress->setRegionId($oRegion->getId()); |
184
|
|
|
$oAddress->setRegionCode($sState); |
185
|
|
|
} |
186
|
|
|
return $oAddress; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|