PluginProbe
PayPlug for WooCommerce (Official) / 1.4.0
PayPlug for WooCommerce (Official) v1.4.0
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.4.0, at src/PayplugWoocommerceHelper.php

595 lines 11.3 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 * Set flag ipn ( in progress / over ) on order
437 *
438 * @param \WC_Order $order
439 * @param array $metadata
440 * @param boolean $flag
441 *
442 * @return void
443 */
444 public static function set_flag_ipn_order ( $order, $metadata, $flag) {
445 $metadata['transaction_in_progress'] = $flag;
446 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
447 }
448
449 /**
450 * Get transient key from payplug option
451 *
452 * @return string
453 */
454 public static function get_transient_key($options)
455 {
456 $transient_key = PayplugGatewayOney3x::OPTION_NAME . (array_key_exists('mode', $options) && $options['mode'] === 'yes' ? "_live" : "_test");
457 return $transient_key;
458 }
459
460 /**
461 * Set transient data for payplug account
462 *
463 * @return string
464 */
465 public static function set_transient_data($data, $options = null)
466 {
467 $options = $options ? $options : get_option('woocommerce_payplug_settings', []);
468 $transient_key = PayplugWoocommerceHelper::get_transient_key($options);
469 set_transient($transient_key, isset($data['httpResponse']) ? $data['httpResponse'] : []);
470 }
471
472 /**
473 * Get current option from payplug settings
474 *
475 * @return array
476 */
477 public static function get_account_data_from_options()
478 {
479 $options = get_option('woocommerce_payplug_settings', []);
480 $transient_key = self::get_transient_key($options);
481 $account = get_transient($transient_key);
482 if (is_array($account)) {
483 $account['oneyEnabled'] = (isset($options['oney']) && !empty($options['oney'])) ? $options['oney'] : '';
484 }
485 return $account;
486 }
487
488 /**
489 * Set current option from payplug settings and api call
490 *
491 * @return void
492 */
493 public static function set_account_data_from_options()
494 {
495 $options = get_option('woocommerce_payplug_settings', []);
496 $payplug_test_key = !empty($options['payplug_test_key']) ? $options['payplug_test_key'] : '';
497 $payplug_live_key = !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : '';
498 if (empty($payplug_test_key) && empty($payplug_live_key)) {
499 return array();
500 }
501
502 try {
503 $parameters_account = Authentication::getAccount(new Payplug($options['mode'] === 'yes' ? $payplug_live_key : $payplug_test_key));
504 self::set_transient_data($parameters_account, $options);
505 } catch (\Payplug\Exception\UnauthorizedException $e) {
506 } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
507 }
508
509 }
510
511 /**
512 * Get min and max for oney payment
513 *
514 * @return array
515 */
516 public static function get_min_max_oney() {
517 $account = self::get_account_data_from_options();
518 if (!$account) {
519 return array();
520 }
521 return [
522 'min' => floatval($account['configuration']['oney']['min_amounts']['EUR'])/100,
523 'max' => floatval($account['configuration']['oney']['max_amounts']['EUR'])/100
524 ];
525 }
526
527 /**
528 * Check if oney is available with current settings
529 *
530 * @return boolean
531 */
532 public static function is_oney_available() {
533 $account = self::get_account_data_from_options();
534 if (!$account) {
535 return false;
536 }
537 return ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == "1" && $account['oneyEnabled'] === "yes");
538 }
539
540 /**
541 * Hide popup for if country_code != payplug country
542 * https://payplug-prod.atlassian.net/browse/WOOC-249
543 *
544 * @return bool
545 */
546 public static function show_oney_popup()
547 {
548 preg_match( '([a-z-]+)', get_locale(), $country );
549 $country = strtoupper($country[0]);
550
551 $account = self::get_account_data_from_options();
552 if( $account && $account['permissions'][PayplugPermissions::USE_ONEY] == true && $account["country"] == $country ){
553 return true;
554 }
555
556 return false;
557 }
558
559 /**
560 * Load translations from plugin languages folder.
561 *
562 * @param string $plugin_rel_path
563 *
564 * @return bool
565 */
566 public static function load_plugin_textdomain( $plugin_rel_path ) {
567
568 $domain = 'payplug';
569
570 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
571
572 $mofile = $domain . '-' . $locale . '.mo';
573
574 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
575
576 return load_textdomain( $domain, $path . '/' . $mofile );
577 }
578
579 /**
580 * Check and update value for oney simulation
581 *
582 * @return void
583 */
584 public static function oney_simulation_values ($keys_array, &$array) {
585 foreach($keys_array as $key) {
586 if (array_key_exists($key, $array)) {
587 $array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100;
588 foreach ($array[$key]['installments'] as $k => $value) {
589 $array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100;
590 }
591 }
592 }
593 }
594 }
595