forked from PAYONE-GmbH/magento-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolkit.php
220 lines (205 loc) · 7.12 KB
/
Toolkit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @category Payone
* @package Payone_Magento2_Plugin
* @author FATCHIP GmbH <[email protected]>
* @copyright 2003 - 2016 Payone GmbH
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
* @link http://www.payone.de
*/
namespace Payone\Core\Helper;
use Magento\Framework\DataObject;
use Magento\Sales\Model\Order as SalesOrder;
use Payone\Core\Model\Methods\PayoneMethod;
/**
* Toolkit class for methods that dont fit in a certain drawer
*/
class Toolkit extends \Payone\Core\Helper\Base
{
/**
* PAYONE payment helper
*
* @var \Payone\Core\Helper\Payment
*/
protected $paymentHelper;
/**
* PAYONE shop helper
*
* @var \Payone\Core\Helper\Shop
*/
protected $shopHelper;
/**
* Constructor
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Payone\Core\Helper\Payment $paymentHelper
* @param \Payone\Core\Helper\Shop $shopHelper
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Payone\Core\Helper\Payment $paymentHelper,
\Payone\Core\Helper\Shop $shopHelper
) {
parent::__construct($context, $storeManager);
$this->paymentHelper = $paymentHelper;
$this->shopHelper = $shopHelper;
}
/**
* Get security keys for all payment types for the given store code
*
* @param string $sStoreCode
* @return array
*/
protected function getAllPayoneSecurityKeysByStoreCode($sStoreCode)
{
$aKeys = [];
foreach ($this->paymentHelper->getAvailablePaymentTypes() as $sPaymentCode) {
$iUseGlobal = $this->getConfigParam('use_global', $sPaymentCode, 'payone_payment', $sStoreCode);
if ($iUseGlobal == '0') {
$aKeys[] = $this->getConfigParam('key', $sPaymentCode, 'payone_payment', $sStoreCode);
}
}
return $aKeys;
}
/**
* Get the configured security keys for all available stores
* and payment types - since every payment-type can have its own
*
* @return array
*/
public function getAllPayoneSecurityKeys()
{
$aKeys = $this->getConfigParamAllStores('key');
$aShopIds = $this->storeManager->getStores(false, true);
foreach ($aShopIds as $sStoreCode => $oStore) {
$aKeys = array_merge($aKeys, $this->getAllPayoneSecurityKeysByStoreCode($sStoreCode));
}
return array_unique($aKeys);
}
/**
* Check wheither the given key is configured in the shop and thus valid
*
* @param string $sKey
* @return bool
*/
public function isKeyValid($sKey)
{
$aKeyValues = $this->getAllPayoneSecurityKeys();
foreach ($aKeyValues as $sConfigKey) {
if (md5($sConfigKey) == $sKey) {
return true;
}
}
return false;
}
/**
* Replace substitutes in a given text with the given replacements
*
* @param string $sText
* @param string $aSubstitutionArray
* @param int|bool $iMaxLength
* @return string
*/
public function handleSubstituteReplacement($sText, $aSubstitutionArray, $iMaxLength = false)
{
if (!empty($sText)) {
$sText = str_replace(array_keys($aSubstitutionArray), array_values($aSubstitutionArray), $sText);
if ($iMaxLength !== false && strlen($sText) > $iMaxLength) {
$sText = substr($sText, 0, $iMaxLength); // shorten text if too long
}
return $sText;
}
return '';
}
/**
* Get substituted invoice appendix text
*
* @param SalesOrder $oOrder
* @return string
*/
public function getInvoiceAppendix(SalesOrder $oOrder)
{
$sText = $this->getConfigParam('invoice_appendix', 'invoicing'); // get invoice appendix from config
$aSubstitutionArray = [
'{{order_increment_id}}' => $oOrder->getIncrementId(),
'{{customer_id}}' => $oOrder->getCustomerId(),
];
$sInvoiceAppendix = $this->handleSubstituteReplacement($sText, $aSubstitutionArray, 255);
return $sInvoiceAppendix;
}
/**
* Returns narrative text for authorization request
*
* @param SalesOrder $oOrder
* @param PayoneMethod $oPayment
* @return string
*/
public function getNarrativeText(SalesOrder $oOrder, PayoneMethod $oPayment)
{
$sText = $this->getConfigParam('narrative_text', $oPayment->getCode(), 'payone_payment'); // get narrative text for payment from config
$aSubstitutionArray = [
'{{order_increment_id}}' => $oOrder->getIncrementId(),
];
$sNarrativeText = $this->handleSubstituteReplacement($sText, $aSubstitutionArray, $oPayment->getNarrativeTextMaxLength());
return $sNarrativeText;
}
/**
* Format a price to the XX.YY format
*
* @param double $dPrice price of any sort
* @param int $iDecimals number of digits behind the decimal point
* @return string
*/
public function formatNumber($dPrice, $iDecimals = 2)
{
return number_format($dPrice, $iDecimals, '.', '');
}
/**
* Checks if given string is utf8 encoded
*
* @param string $sString
* @return bool
*/
public function isUTF8($sString)
{
return $sString === mb_convert_encoding(mb_convert_encoding($sString, "UTF-32", "UTF-8"), "UTF-8", "UTF-32");
}
/**
* Return data from data-object
* Needed because of different ways to read from it for different magento versions
*
* @param DataObject $oData
* @param string $sKey
* @return string|null
*/
public function getAdditionalDataEntry(DataObject $oData, $sKey)
{
// The way to read the form-parameters changed with version 2.0.6
if (version_compare($this->shopHelper->getMagentoVersion(), '2.0.6', '>=')) { // Magento 2.0.6 and above
$aAdditionalData = $oData->getAdditionalData();
if (isset($aAdditionalData[$sKey])) {
return $aAdditionalData[$sKey];
}
return null;
}
// everything below 2.0.6
return $oData->getData($sKey);
}
}