PluginProbe
PayPlug for WooCommerce (Official) / 1.2.3
PayPlug for WooCommerce (Official) v1.2.3
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / PayplugWoocommerceHelper.php

PayplugWoocommerceHelper.php in PayPlug for WooCommerce (Official) 1.2.3, at src/PayplugWoocommerceHelper.php

532 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce;
4
5 // Exit if accessed directly
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 use Payplug\Resource\APIResource;
11 use Payplug\Payplug;
12 use Payplug\Authentication;
13 use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x;
14 use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions;
15
16 /**
17 * Helper class.
18 *
19 * @package Payplug\PayplugWoocommerce
20 */
21 class PayplugWoocommerceHelper {
22
23 /**
24 * Check if current WooCommerce version is below 3.0.0
25 *
26 * @return bool
27 */
28 public static function is_pre_30() {
29 $wc = function_exists( 'WC' ) ? WC() : $GLOBALS['woocommerce'];
30
31 return version_compare( $wc->version, '3.0.0', '<' );
32 }
33
34 /**
35 * Get a URL to the PayPlug gateway settings page.
36 *
37 * @return string
38 */
39 public static function get_setting_link() {
40 $use_id_as_section = function_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false;
41 $section_slug = $use_id_as_section ? 'payplug' : strtolower( 'PayplugGateway' );
42
43 return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $section_slug );
44 }
45
46 /**
47 * Get all country code supported by PayPlug.
48 *
49 * Those are ISO 3166-1 alpha-2. You can find more information on https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
50 *
51 * @return array
52 */
53 public static function get_supported_countries() {
54 return [
55 'AD',
56 'AO',
57 'AX',
58 'BG',
59 'BO',
60 'BY',
61 'CH',
62 'CR',
63 'DE',
64 'EE',
65 'FI',
66 'GB',
67 'GL',
68 'GT',
69 'HR',
70 'IN',
71 'JM',
72 'KM',
73 'KZ',
74 'LS',
75 'MD',
76 'MM',
77 'MT',
78 'NA',
79 'NO',
80 'PE',
81 'PN',
82 'RE',
83 'SC',
84 'SK',
85 'ST',
86 'TF',
87 'TN',
88 'UA',
89 'VC',
90 'WS',
91 'AE',
92 'AQ',
93 'AZ',
94 'BH',
95 'BQ',
96 'BZ',
97 'CI',
98 'CU',
99 'DJ',
100 'EG',
101 'FJ',
102 'GD',
103 'GM',
104 'GU',
105 'HT',
106 'IO',
107 'JO',
108 'KN',
109 'LA',
110 'LT',
111 'ME',
112 'MN',
113 'MU',
114 'NC',
115 'NP',
116 'PF',
117 'PR',
118 'RO',
119 'SD',
120 'SL',
121 'SV',
122 'TG',
123 'TO',
124 'UG',
125 'VE',
126 'YE',
127 'AM',
128 'AW',
129 'BF',
130 'BN',
131 'BW',
132 'CG',
133 'CO',
134 'AF',
135 'CZ',
136 'EC',
137 'EU',
138 'GA',
139 'GI',
140 'GS',
141 'HN',
142 'IM',
143 'JE',
144 'KI',
145 'KY',
146 'LR',
147 'MC',
148 'ML',
149 'MS',
150 'MZ',
151 'NL',
152 'PA',
153 'PM',
154 'QA',
155 'SB',
156 'SJ',
157 'SS',
158 'TD',
159 'TM',
160 'TZ',
161 'VA',
162 'WF',
163 'AR',
164 'BA',
165 'BI',
166 'BR',
167 'CA',
168 'CK',
169 'CV',
170 'DK',
171 'EH',
172 'FK',
173 'GE',
174 'GN',
175 'GW',
176 'HU',
177 'IQ',
178 'JP',
179 'KP',
180 'LB',
181 'LU',
182 'MF',
183 'MO',
184 'MV',
185 'NE',
186 'NR',
187 'PG',
188 'PS',
189 'RS',
190 'SE',
191 'SM',
192 'SX',
193 'TH',
194 'TR',
195 'UM',
196 'VG',
197 'YT',
198 'AL',
199 'AU',
200 'BE',
201 'BM',
202 'BV',
203 'CF',
204 'CN',
205 'CY',
206 'DZ',
207 'ET',
208 'FR',
209 'GH',
210 'GR',
211 'HM',
212 'IL',
213 'IT',
214 'KH',
215 'KW',
216 'LK',
217 'MA',
218 'MK',
219 'MR',
220 'MY',
221 'NI',
222 'OM',
223 'PL',
224 'PY',
225 'SA',
226 'SI',
227 'SR',
228 'TC',
229 'TL',
230 'TW',
231 'UZ',
232 'VU',
233 'ZW',
234 'AI',
235 'AT',
236 'BD',
237 'BL',
238 'BT',
239 'CD',
240 'CM',
241 'CX',
242 'DO',
243 'ES',
244 'FO',
245 'GG',
246 'GQ',
247 'HK',
248 'IE',
249 'IS',
250 'KG',
251 'KS',
252 'LI',
253 'LY',
254 'MH',
255 'MQ',
256 'MX',
257 'NG',
258 'NZ',
259 'PK',
260 'PW',
261 'RW',
262 'SH',
263 'SO',
264 'SZ',
265 'TK',
266 'TV',
267 'UY',
268 'VN',
269 'ZM',
270 'AG',
271 'AS',
272 'BB',
273 'BJ',
274 'BS',
275 'CC',
276 'CL',
277 'CW',
278 'DM',
279 'ER',
280 'FM',
281 'GF',
282 'GP',
283 'GY',
284 'ID',
285 'IR',
286 'KE',
287 'KR',
288 'LC',
289 'LV',
290 'MG',
291 'MP',
292 'MW',
293 'NF',
294 'NU',
295 'PH',
296 'PT',
297 'RU',
298 'SG',
299 'SN',
300 'SY',
301 'TJ',
302 'TT',
303 'US',
304 'VI',
305 'ZA'
306 ];
307 }
308
309 /**
310 * Ensure country code is supported by PayPlug.
311 *
312 * @param string $country The ISO 3166-1 alpha-2 code for the country
313 *
314 * @return bool
315 * @author Clément Boirie
316 */
317 public static function is_country_supported( $country ) {
318 $country = trim( $country );
319 if ( empty( $country ) ) {
320 return false;
321 }
322
323 return in_array( strtoupper( $country ), self::get_supported_countries() );
324 }
325
326 /**
327 * Get default country.
328 *
329 * @return string
330 */
331 public static function get_default_country() {
332 $country = \WC()->countries->get_base_country();
333
334 return ( self::is_country_supported( $country ) ) ? strtoupper( $country ) : 'FR';
335 }
336
337 /**
338 * Get minimum amount allowed by PayPlug.
339 *
340 * This amount is in cents.
341 *
342 * @return int
343 */
344 public static function get_minimum_amount() {
345 return 99;
346 }
347
348 /**
349 * Get maximum amount allowed by PayPlug.
350 *
351 * This amount is in cents.
352 *
353 * @return int
354 */
355 public static function get_maximum_amount() {
356 return 2000000;
357 }
358
359 /**
360 * Convert amount in cents.
361 *
362 * @param float $amount
363 *
364 * @return int
365 */
366 public static function get_payplug_amount( $amount ) {
367 if ( is_null( $amount ) ) {
368 return $amount;
369 }
370
371 return absint( wc_format_decimal( ( (float) $amount * 100 ), wc_get_price_decimals() ) );
372 }
373
374 /**
375 * Extract useful metadata from PayPlug response.
376 *
377 * @param APIResource $resource
378 *
379 * @return array
380 */
381 public static function extract_transaction_metadata( $resource ) {
382 return [
383 'transaction_id' => sanitize_text_field( $resource->id ),
384 'paid' => (bool) $resource->is_paid,
385 'refunded' => (bool) $resource->is_refunded,
386 'amount' => sanitize_text_field( $resource->amount ),
387 'amount_refunded' => sanitize_text_field( $resource->amount_refunded ),
388 '3ds' => (bool) $resource->is_3ds,
389 'live' => (bool) $resource->is_live,
390 'paid_at' => isset( $resource->hosted_payment->paid_at ) ? sanitize_text_field( $resource->hosted_payment->paid_at ) : sanitize_text_field( $resource->created_at ),
391 'card_last4' => sanitize_text_field( $resource->card->last4 ),
392 'card_exp_month' => sanitize_text_field( $resource->card->exp_month ),
393 'card_exp_year' => sanitize_text_field( $resource->card->exp_year ),
394 'card_brand' => sanitize_text_field( $resource->card->brand ),
395 'card_country' => sanitize_text_field( $resource->card->country ),
396 ];
397 }
398
399 /**
400 * @param \WC_Order $order
401 *
402 * @return array|bool
403 * @author Clément Boirie
404 */
405 public static function get_transaction_metadata( $order ) {
406
407 if ( PayplugWoocommerceHelper::is_pre_30() ) {
408 return get_post_meta( $order->id, '_payplug_metadata', true );
409 } else {
410 return $order->get_meta( '_payplug_metadata', true );
411 }
412 }
413
414 /**
415 * Save transaction metadata extracted from PayPlug response.
416 *
417 * @param \WC_Order $order
418 * @param array $metadata
419 *
420 * @return void
421 */
422 public static function save_transaction_metadata( $order, $metadata ) {
423
424 if ( PayplugWoocommerceHelper::is_pre_30() ) {
425 update_post_meta( $order->id, '_payplug_metadata', $metadata );
426 } else {
427 $order->add_meta_data( '_payplug_metadata', $metadata, true );
428
429 if ( is_callable( [ $order, 'save' ] ) ) {
430 $order->save();
431 }
432 }
433 }
434
435 /**
436 * Get current option from payplug settings
437 *
438 * @return array
439 */
440 public static function get_account_data_from_options($force_update = false) {
441 $options = get_option('woocommerce_payplug_settings', []);
442 $payplug_test_key = !empty($options['payplug_test_key']) ? $options['payplug_test_key'] : '';
443 $payplug_live_key = !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : '';
444 if (empty($payplug_test_key) && empty($payplug_live_key)) {
445 return array();
446 }
447
448 $transient_key = PayplugGatewayOney3x::OPTION_NAME . ($options['mode'] === 'yes' ? "_live" : "_test");
449 $payplug_oney_config = get_transient($transient_key);
450 if ($payplug_oney_config && !$force_update) {
451 $account = $payplug_oney_config;
452 } else {
453 try {
454 $parameters_account = Authentication::getAccount(new Payplug($options['mode'] === 'yes' ? $payplug_live_key : $payplug_test_key));
455 set_transient($transient_key, $parameters_account['httpResponse']);
456 $account = get_transient($transient_key);
457 } catch (\Payplug\Exception\UnauthorizedException $e) {
458 } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
459 }
460 }
461 $account['oneyEnabled'] = $options['oney'];
462 $account['oneyCgvEnabled'] = $options['oneycgv'];
463 return $account;
464
465 }
466
467 /**
468 * Get min and max for oney payment
469 *
470 * @return array
471 */
472 public static function get_min_max_oney() {
473 $account = self::get_account_data_from_options();
474 if (empty($account)) {
475 return array();
476 }
477 return [
478 'min' => floatval($account['configuration']['oney']['min_amounts']['EUR'])/100,
479 'max' => floatval($account['configuration']['oney']['max_amounts']['EUR'])/100
480 ];
481 }
482
483 /**
484 * Check if oney is available with current settings
485 *
486 * @return boolean
487 */
488 public static function is_oney_available() {
489 $account = self::get_account_data_from_options();
490 if (empty($account)) {
491 return false;
492 }
493 return ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == "1" && $account['oneyEnabled'] === "yes" && $account['oneyCgvEnabled'] === "yes");
494 }
495
496 /**
497 * Load translations from plugin languages folder.
498 *
499 * @param string $plugin_rel_path
500 *
501 * @return bool
502 */
503 public static function load_plugin_textdomain( $plugin_rel_path ) {
504
505 $domain = 'payplug';
506
507 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
508
509 $mofile = $domain . '-' . $locale . '.mo';
510
511 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
512
513 return load_textdomain( $domain, $path . '/' . $mofile );
514 }
515
516 /**
517 * Check and update value for oney simulation
518 *
519 * @return void
520 */
521 public static function oney_simulation_values ($keys_array, &$array) {
522 foreach($keys_array as $key) {
523 if (array_key_exists($key, $array)) {
524 $array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100;
525 foreach ($array[$key]['installments'] as $k => $value) {
526 $array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100;
527 }
528 }
529 }
530 }
531 }
532