-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHelperGateway.php
289 lines (249 loc) · 7.19 KB
/
HelperGateway.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* NOTICE OF LICENSE
* Title : Conekta Card Payment Gateway for Prestashop
* Author : Conekta.io
* URL : https://www.conekta.io/es/docs/plugins/prestashop.
* PHP Version 7.0.0
* Conekta File Doc Comment
*
* @author Conekta <support@conekta.io>
* @copyright 2012-2023 Conekta
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* @category Conekta
*
* @version GIT: @3.0.0@
*
* @see https://conekta.com/
*/
require_once dirname(__FILE__) . '/model/Database.php';
/**
* HelperGateway Class Doc Comment
*
* @author Conekta <support@conekta.io>
*
* @category Class
*
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* @see https://conekta.com/
*/
class HelperGateway
{
/**
* Add tax lines
*
* @param array|null $taxlines Tax of the Order
*
* @return array
*/
public static function addTaxLines(array $taxlines = null): array
{
$tax_lines = [
'tax_lines' => [],
];
if (isset($taxlines)) {
foreach ($taxlines as $tax) {
$tax_lines['tax_lines'][] = [
'description' => HelperGateway::removeSpecialCharacter($tax['description']),
'amount' => $tax['amount'],
];
}
}
return $tax_lines['tax_lines'];
}
/**
* Add Shipping lines
*
* @param array $shippingLines Shipping of the Order
*
* @return array
*/
public static function addShippingLines($shippingLines = null): array
{
$shippingLinesArray = [
'shipping_lines' => [],
];
if (isset($shippingLines)) {
foreach ($shippingLines as $shipping) {
$shippingLinesArray['shipping_lines'][] = [
'amount' => $shipping['amount'],
'tracking_number' => HelperGateway::removeSpecialCharacter($shipping['tracking_number']),
'carrier' => HelperGateway::removeSpecialCharacter($shipping['carrier']),
'method' => HelperGateway::removeSpecialCharacter($shipping['method']),
];
}
}
return $shippingLinesArray['shipping_lines'];
}
/**
* Calculate amount total of the Order
*
* @param array $order_details Order details
*
* @return int
*/
public static function calculateAmountTotal($order_details = null)
{
$amount = 0;
if (isset($order_details['shipping_lines'])) {
foreach ($order_details['shipping_lines'] as $shipping) {
$amount = $amount + $shipping['amount'];
}
}
if (isset($order_details['tax_lines'])) {
foreach ($order_details['tax_lines'] as $tax) {
$amount = $amount + $tax['amount'];
}
}
foreach ($order_details['line_items'] as $item) {
$amount = $amount + ($item['quantity'] * $item['unit_price']);
}
if (isset($order_details['discount_lines'])) {
foreach ($order_details['discount_lines'] as $discount) {
$amount = $amount - $discount['amount'];
}
}
return $amount;
}
/**
* Validates if the product is subscrition
*
* @param array $items items in the order
*
* @return bool
*/
public static function validateSubscrition($items)
{
if (count($items) > 1) {
return false;
} elseif ($items[0]['cart_quantity'] == 1) {
return Database::isProductSubscription($items[0]['id_product']);
}
return false;
}
/**
* Validates if the items were entered correctly
*
* @param array $items items in the order
*
* @return bool
*/
public static function validateItems($items)
{
if (count($items) > 1) {
return HelperGateway::validateItemsProduct($items);
} elseif ($items[0]['cart_quantity'] > 1) {
if (Database::isProductSubscription($items[0]['id_product'])) {
return false;
}
}
return true;
}
/**
* Validate if items are unsubscribed
*
* @param array $items items in the order
*
* @return bool
*/
public static function validateItemsProduct($items)
{
$i = 0;
$non_subscription = true;
while (count($items) > $i && $non_subscription) {
if (Database::isProductSubscription($items[$i]['id_product'])) {
$non_subscription = false;
}
++$i;
}
return $non_subscription;
}
/**
* Remove special character
*
* @param string $param character string
*
* @return string
*/
public static function removeSpecialCharacter($param)
{
$param = str_replace(['#', '-', '_', '.', '/', '(', ')', '[', ']', '!', '¡', '%'], ' ', $param);
return $param;
}
/**
* Generate Checkout
*
* @param array $items Items of the order
* @param array $payment_options Payment options
*
* @return array
*/
public static function generateCheckout($items, $payment_options)
{
$retorno = HelperGateway::validateSubscrition($items);
if ($retorno) {
return [
'allowed_payment_methods' => ['card'],
'plan_id' => Database::getIdPlan($items[0]['id_product']),
];
} elseif (HelperGateway::validateItemsProduct($items)) {
return [
'allowed_payment_methods' => $payment_options,
];
}
}
/**
* Show inverval of the subscription
*
* @param string $option Interval selected for the subscription
* @param int $frecuency Frecuency of the subscription
*
* @return string
*/
public static function getInterval($option, $frecuency)
{
$interval = '';
switch ($option) {
case 'minute':
($frecuency > 1) ? $interval = 'Minutos' : $interval = 'Minuto';
break;
case 'week':
($frecuency > 1) ? $interval = 'Semanas' : $interval = 'Semana';
break;
case 'half_month':
($frecuency > 1) ? $interval = 'Quincenas' : $interval = 'Quincena';
break;
case 'month':
($frecuency > 1) ? $interval = 'Meses' : $interval = 'Mes';
break;
case 'year':
($frecuency > 1) ? $interval = 'Años' : $interval = 'Año';
break;
}
return $interval;
}
/**
* Check if the product is digital
*
* @param array $items Products
*
* @return bool
*/
public static function isDigital($items)
{
$all_digital = true;
if (empty($items)) {
return false;
}
$i = 0;
do {
if (!$items[$i]['is_virtual']) {
$all_digital = false;
}
++$i;
} while ($i < count($items) && $all_digital);
return $all_digital;
}
}