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

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