-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalleeHelper.php
260 lines (224 loc) · 8.12 KB
/
WalleeHelper.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php declare(strict_types=1);
namespace Plugin\jtl_wallee;
use JTL\Checkout\Nummern;
use JTL\Plugin\Data\Localization;
use JTL\Plugin\Helper;
use JTL\Plugin\Helper as PluginHelper;
use JTL\Shop;
use Wallee\Sdk\ApiClient;
/**
* Class WalleeHelper
* @package Plugin\jtl_wallee
*/
class WalleeHelper extends Helper
{
const EN_ISO3 = 'eng';
const DE_ISO3 = 'ger';
const IT_ISO3 = 'ita';
const FR_ISO3 = 'fre';
const PAYMENT_METHOD_PREFIX = 'wallee_payment';
const USER_ID = 'jtl_wallee_user_id';
const SPACE_ID = 'jtl_wallee_space_id';
const APPLICATION_KEY = 'jtl_wallee_application_key';
const SPACE_VIEW_ID = 'jtl_wallee_space_view_id';
const SEND_AUTHORIZATION_EMAIL = 'jtl_wallee_send_authorization_email';
const SEND_FULFILL_EMAIL = 'jtl_wallee_send_fulfill_email';
const SEND_CONFIRMATION_EMAIL = 'jtl_wallee_send_confirmation_email';
const PREVENT_FROM_DUPLICATED_ORDERS = 'jtl_wallee_prevent_from_duplicated_orders';
const INTEGRATION_TYPE = 'jtl_wallee_integration_type';
const INTEGRATION_TYPE_PAYMENT_PAGE = 'payment_page';
const PAYMENT_METHOD_CONFIGURATION = 'PaymentMethodConfiguration';
const REFUND = 'Refund';
const TRANSACTION = 'Transaction';
const TRANSACTION_INVOICE = 'TransactionInvoice';
const PLUGIN_CUSTOM_PAGES = [
'thank-you-page' => [
self::DE_ISO3 => 'wallee-danke-seite',
self::EN_ISO3 => 'wallee-thank-you-page',
self::IT_ISO3 => 'wallee-pagina-di-ringraziamento',
self::FR_ISO3 => 'wallee-page-de-remerciement',
],
'payment-page' => [
self::DE_ISO3 => 'wallee-zahlungsseite',
self::EN_ISO3 => 'wallee-payment-page',
self::IT_ISO3 => 'wallee-pagina-di-pagamento',
self::FR_ISO3 => 'wallee-page-de-paiement',
],
'fail-page' => [
self::DE_ISO3 => 'wallee-bezahlung-fehlgeschlagen',
self::EN_ISO3 => 'wallee-failed-payment',
self::IT_ISO3 => 'wallee-pagamento-fallito',
self::FR_ISO3 => 'wallee-paiement-echoue',
],
];
/**
* @param string $text
* @param string $divider
* @return string
*/
public static function slugify(string $text, string $divider = '_'): string
{
// replace non letter or digits by divider
$text = preg_replace('~[^\pL\d]+~u', $divider, $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, $divider);
// remove duplicate divider
$text = preg_replace('~-+~', $divider, $text);
// lowercase
$text = strtolower($text);
return $text;
}
/**
* @return string
*/
public static function getLanguageString(): string
{
switch ($_SESSION['currentLanguage']->iso) {
case self::DE_ISO3:
return 'de_DE';
case self::FR_ISO3:
return 'fr_FR';
case self::IT_ISO3:
return 'it_IT';
default:
return 'en_GB';
}
}
/**
* @param $isAdmin
* @return string
*/
public static function getLanguageIso($isAdmin = true): string
{
if (!$isAdmin) {
return $_SESSION['cISOSprache'];
}
$gettext = Shop::Container()->getGetText();
$langTag = $_SESSION['AdminAccount']->language ?? $gettext->getLanguage();
switch (substr($langTag, 0, 2)) {
case 'de':
return self::DE_ISO3;
case 'en':
return self::EN_ISO3;
case 'fr':
return self::FR_ISO3;
case 'it':
return self::IT_ISO3;
}
}
/**
* @param Localization $localization
* @param array $keys
* @param $isAdmin
* @return array
*/
public static function getTranslations(Localization $localization, array $keys, $isAdmin = true): array
{
$translations = [];
foreach ($keys as $key) {
$translations[$key] = $localization->getTranslation($key, self::getLanguageIso($isAdmin));
}
return $translations;
}
/**
* @param Localization $localization
* @return array
*/
public static function getPaymentStatusWithTransations(Localization $localization): array
{
$translations = WalleeHelper::getTranslations($localization, [
'jtl_wallee_order_status_cancelled',
'jtl_wallee_order_status_open',
'jtl_wallee_order_status_in_processing',
'jtl_wallee_order_status_paid',
'jtl_wallee_order_status_shipped',
'jtl_wallee_order_status_partially_shipped',
]);
return [
'-1' => $translations['jtl_wallee_order_status_cancelled'],
'1' => $translations['jtl_wallee_order_status_open'],
'2' => $translations['jtl_wallee_order_status_in_processing'],
'3' => $translations['jtl_wallee_order_status_paid'],
'4' => $translations['jtl_wallee_order_status_shipped'],
'5' => $translations['jtl_wallee_order_status_partially_shipped'],
];
}
/**
* @param int $pluginId
* @return ApiClient|null
*/
public static function getApiClient(int $pluginId): ?ApiClient
{
if (class_exists('Wallee\Sdk\ApiClient')) {
$apiClient = new WalleeApiClient($pluginId);
return $apiClient->getApiClient();
} else {
if (isset($_POST['Setting'])) {
$plugin = PluginHelper::getLoaderByPluginID($pluginId)->init($pluginId);
$translations = WalleeHelper::getTranslations($plugin->getLocalization(), [
'jtl_wallee_need_to_install_sdk',
]);
Shop::Container()->getAlertService()->addDanger(
$translations['jtl_wallee_need_to_install_sdk'],
'getApiClient'
);
}
return null;
}
}
/**
* @param $update
* @param $lastOrderNo
* @return array
*
* This is edited JTL native function.
*/
public static function createOrderNo($update = true, $lastOrderNo = 0): array
{
$conf = Shop::getSettingSection(\CONF_KAUFABWICKLUNG);
$number = new Nummern(\JTL_GENNUMBER_ORDERNUMBER);
$orderNo = 1;
$increment = (int)($conf['bestellabschluss_bestellnummer_anfangsnummer'] ?? 1);
if ($number) {
$orderNo = $number->getNummer() + $increment;
$number->setNummer($number->getNummer() + 1);
if ($update === true) {
// This part is used when setting Prevent From Duplicated Order No === 'NO'. We increase order number only once (even multiple orders with same nr are completed
// Example: setting is set to No, we accept duplicated order no. Two customers are making order with no Order 1. If both finishes the payment, next order will be Order 2, not Order 2 and Order 3
if ($lastOrderNo === 0 || $orderNo - $lastOrderNo === 0) {
$number->update();
}
}
}
/*
* %Y = -aktuelles Jahr
* %m = -aktueller Monat
* %d = -aktueller Tag
* %W = -aktuelle KW
*/
$prefix = \str_replace(
['%Y', '%m', '%d', '%W'],
[\date('Y'), \date('m'), \date('d'), \date('W')],
$conf['bestellabschluss_bestellnummer_praefix']
);
$suffix = \str_replace(
['%Y', '%m', '%d', '%W'],
[\date('Y'), \date('m'), \date('d'), \date('W')],
$conf['bestellabschluss_bestellnummer_suffix']
);
return [$prefix . $orderNo . $suffix, $orderNo];
}
/**
* @param int $pluginId
* @return string
*/
public static function getIntegrationType(int $pluginId): string
{
$config = self::getConfigByID($pluginId);
return $config[self::INTEGRATION_TYPE] ?? 'payment_page';
}
}