PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.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 All 102 releases
← All changes | src/PayplugWoocommerceHelper.php +879 -491 1.2.11 → 3.1.0 View file →
@@ -2,543 +2,931 @@
2 2
3 3 namespace Payplug\PayplugWoocommerce;
4 4
5 5 // Exit if accessed directly
6 -if ( ! defined( 'ABSPATH' ) ) {
7 - exit;
6 +if (!defined('ABSPATH')) {
7 + exit;
8 8 }
9 9
10 -use Payplug\Resource\APIResource;
10 +use Payplug\Authentication;
11 +use Payplug\Exception\ForbiddenException;
11 12 use Payplug\Payplug;
12 -use Payplug\Authentication;
13 +use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
13 14 use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x;
14 15 use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions;
16 +use Payplug\PayplugWoocommerce\Traits\ServiceGetter;
17 +use Payplug\Resource\APIResource;
18 +use WC_Blocks_Utils;
19 +use WC_Subscriptions;
15 20
16 21 /**
17 22 * Helper class.
18 - *
19 - * @package Payplug\PayplugWoocommerce
20 23 */
21 -class PayplugWoocommerceHelper {
24 +class PayplugWoocommerceHelper
25 +{
26 + use ServiceGetter;
22 27
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'];
28 + /**
29 + * Check if current WooCommerce version is below 3.0.0
30 + *
31 + * @return bool
32 + */
33 + public static function is_pre_30()
34 + {
35 + $wc = function_exists('WC') ? WC() : $GLOBALS['woocommerce'];
30 36
31 - return version_compare( $wc->version, '3.0.0', '<' );
32 - }
37 + return version_compare($wc->version, '3.0.0', '<');
38 + }
33 39
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' );
40 + /**
41 + * Get a URL to the PayPlug gateway settings page.
42 + *
43 + * @return string
44 + */
45 + public static function get_setting_link()
46 + {
47 + $use_id_as_section = function_exists('WC') ? version_compare(WC()->version, '2.6', '>=') : false;
48 + $section_slug = $use_id_as_section ? 'payplug' : strtolower('PayplugGateway');
42 49
43 - return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $section_slug );
44 - }
50 + return admin_url('admin.php?page=wc-settings&tab=checkout&section=' . $section_slug);
51 + }
45 52
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 - }
53 + /**
54 + * Get all country code supported by PayPlug.
55 + *
56 + * Those are ISO 3166-1 alpha-2. You can find more information on https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
57 + *
58 + * @return array
59 + */
60 + public static function get_supported_countries()
61 + {
62 + return [
63 + 'AD',
64 + 'AO',
65 + 'AX',
66 + 'BG',
67 + 'BO',
68 + 'BY',
69 + 'CH',
70 + 'CR',
71 + 'DE',
72 + 'EE',
73 + 'FI',
74 + 'GB',
75 + 'GL',
76 + 'GT',
77 + 'HR',
78 + 'IN',
79 + 'JM',
80 + 'KM',
81 + 'KZ',
82 + 'LS',
83 + 'MD',
84 + 'MM',
85 + 'MT',
86 + 'NA',
87 + 'NO',
88 + 'PE',
89 + 'PN',
90 + 'RE',
91 + 'SC',
92 + 'SK',
93 + 'ST',
94 + 'TF',
95 + 'TN',
96 + 'UA',
97 + 'VC',
98 + 'WS',
99 + 'AE',
100 + 'AQ',
101 + 'AZ',
102 + 'BH',
103 + 'BQ',
104 + 'BZ',
105 + 'CI',
106 + 'CU',
107 + 'DJ',
108 + 'EG',
109 + 'FJ',
110 + 'GD',
111 + 'GM',
112 + 'GU',
113 + 'HT',
114 + 'IO',
115 + 'JO',
116 + 'KN',
117 + 'LA',
118 + 'LT',
119 + 'ME',
120 + 'MN',
121 + 'MU',
122 + 'NC',
123 + 'NP',
124 + 'PF',
125 + 'PR',
126 + 'RO',
127 + 'SD',
128 + 'SL',
129 + 'SV',
130 + 'TG',
131 + 'TO',
132 + 'UG',
133 + 'VE',
134 + 'YE',
135 + 'AM',
136 + 'AW',
137 + 'BF',
138 + 'BN',
139 + 'BW',
140 + 'CG',
141 + 'CO',
142 + 'AF',
143 + 'CZ',
144 + 'EC',
145 + 'EU',
146 + 'GA',
147 + 'GI',
148 + 'GS',
149 + 'HN',
150 + 'IM',
151 + 'JE',
152 + 'KI',
153 + 'KY',
154 + 'LR',
155 + 'MC',
156 + 'ML',
157 + 'MS',
158 + 'MZ',
159 + 'NL',
160 + 'PA',
161 + 'PM',
162 + 'QA',
163 + 'SB',
164 + 'SJ',
165 + 'SS',
166 + 'TD',
167 + 'TM',
168 + 'TZ',
169 + 'VA',
170 + 'WF',
171 + 'AR',
172 + 'BA',
173 + 'BI',
174 + 'BR',
175 + 'CA',
176 + 'CK',
177 + 'CV',
178 + 'DK',
179 + 'EH',
180 + 'FK',
181 + 'GE',
182 + 'GN',
183 + 'GW',
184 + 'HU',
185 + 'IQ',
186 + 'JP',
187 + 'KP',
188 + 'LB',
189 + 'LU',
190 + 'MF',
191 + 'MO',
192 + 'MV',
193 + 'NE',
194 + 'NR',
195 + 'PG',
196 + 'PS',
197 + 'RS',
198 + 'SE',
199 + 'SM',
200 + 'SX',
201 + 'TH',
202 + 'TR',
203 + 'UM',
204 + 'VG',
205 + 'YT',
206 + 'AL',
207 + 'AU',
208 + 'BE',
209 + 'BM',
210 + 'BV',
211 + 'CF',
212 + 'CN',
213 + 'CY',
214 + 'DZ',
215 + 'ET',
216 + 'FR',
217 + 'GH',
218 + 'GR',
219 + 'HM',
220 + 'IL',
221 + 'IT',
222 + 'KH',
223 + 'KW',
224 + 'LK',
225 + 'MA',
226 + 'MK',
227 + 'MR',
228 + 'MY',
229 + 'NI',
230 + 'OM',
231 + 'PL',
232 + 'PY',
233 + 'SA',
234 + 'SI',
235 + 'SR',
236 + 'TC',
237 + 'TL',
238 + 'TW',
239 + 'UZ',
240 + 'VU',
241 + 'ZW',
242 + 'AI',
243 + 'AT',
244 + 'BD',
245 + 'BL',
246 + 'BT',
247 + 'CD',
248 + 'CM',
249 + 'CX',
250 + 'DO',
251 + 'ES',
252 + 'FO',
253 + 'GG',
254 + 'GQ',
255 + 'HK',
256 + 'IE',
257 + 'IS',
258 + 'KG',
259 + 'KS',
260 + 'LI',
261 + 'LY',
262 + 'MH',
263 + 'MQ',
264 + 'MX',
265 + 'NG',
266 + 'NZ',
267 + 'PK',
268 + 'PW',
269 + 'RW',
270 + 'SH',
271 + 'SO',
272 + 'SZ',
273 + 'TK',
274 + 'TV',
275 + 'UY',
276 + 'VN',
277 + 'ZM',
278 + 'AG',
279 + 'AS',
280 + 'BB',
281 + 'BJ',
282 + 'BS',
283 + 'CC',
284 + 'CL',
285 + 'CW',
286 + 'DM',
287 + 'ER',
288 + 'FM',
289 + 'GF',
290 + 'GP',
291 + 'GY',
292 + 'ID',
293 + 'IR',
294 + 'KE',
295 + 'KR',
296 + 'LC',
297 + 'LV',
298 + 'MG',
299 + 'MP',
300 + 'MW',
301 + 'NF',
302 + 'NU',
303 + 'PH',
304 + 'PT',
305 + 'RU',
306 + 'SG',
307 + 'SN',
308 + 'SY',
309 + 'TJ',
310 + 'TT',
311 + 'US',
312 + 'VI',
313 + 'ZA',
314 + ];
315 + }
308 316
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 - }
317 + /**
318 + * Ensure country code is supported by PayPlug.
319 + *
320 + * @param string $country The ISO 3166-1 alpha-2 code for the country
321 + *
322 + * @return bool
323 + *
324 + * @author Clément Boirie
325 + */
326 + public static function is_country_supported($country)
327 + {
328 + $country = trim($country);
329 + if (empty($country)) {
330 + return false;
331 + }
322 332
323 - return in_array( strtoupper( $country ), self::get_supported_countries() );
324 - }
333 + return in_array(strtoupper($country), self::get_supported_countries());
334 + }
325 335
326 - /**
327 - * Get default country.
328 - *
329 - * @return string
330 - */
331 - public static function get_default_country() {
332 - $country = \WC()->countries->get_base_country();
336 + /**
337 + * Get default country.
338 + *
339 + * @return string
340 + */
341 + public static function get_default_country()
342 + {
343 + $country = \WC()->countries->get_base_country();
333 344
334 - return ( self::is_country_supported( $country ) ) ? strtoupper( $country ) : 'FR';
335 - }
345 + return (self::is_country_supported($country)) ? strtoupper($country) : 'FR';
346 + }
336 347
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 - }
348 + /**
349 + * Get minimum amount allowed by PayPlug.
350 + *
351 + * This amount is in cents.
352 + *
353 + * @return int
354 + */
355 + public static function get_minimum_amount()
356 + {
357 + return 99;
358 + }
347 359
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 - }
360 + /**
361 + * Get maximum amount allowed by PayPlug.
362 + *
363 + * This amount is in cents.
364 + *
365 + * @return int
366 + */
367 + public static function get_maximum_amount()
368 + {
369 + return 2000000;
370 + }
358 371
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 - }
372 + /**
373 + * Convert amount in cents.
374 + *
375 + * @param float $amount
376 + *
377 + * @return int
378 + */
379 + public static function get_payplug_amount($amount)
380 + {
381 + if (is_null($amount)) {
382 + return $amount;
383 + }
370 384
371 - return absint( wc_format_decimal( ( (float) $amount * 100 ), wc_get_price_decimals() ) );
372 - }
385 + return absint(wc_format_decimal(((float) $amount * 100), wc_get_price_decimals()));
386 + }
373 387
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 - }
388 + /**
389 + * Extract useful metadata from PayPlug response.
390 + *
391 + * @param APIResource $resource
392 + *
393 + * @return array
394 + */
395 + public static function extract_transaction_metadata($resource)
396 + {
397 + // For non-card payment methods (e.g. Oney), the API returns "card": null.
398 + // Accessing null->property throws \Error in PHP 8+, which is not caught by catch(\Exception).
399 + $card = (isset($resource->card) && $resource->card !== null) ? $resource->card : null;
398 400
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 ) {
401 + return [
402 + 'transaction_id' => sanitize_text_field($resource->id),
403 + 'paid' => (bool) $resource->is_paid,
404 + 'refunded' => (bool) $resource->is_refunded,
405 + 'amount' => sanitize_text_field($resource->amount),
406 + 'amount_refunded' => sanitize_text_field($resource->amount_refunded),
407 + '3ds' => (bool) $resource->is_3ds,
408 + 'live' => (bool) $resource->is_live,
409 + 'paid_at' => isset($resource->hosted_payment->paid_at) ? sanitize_text_field($resource->hosted_payment->paid_at) : sanitize_text_field($resource->created_at),
410 + 'card_last4' => $card !== null ? sanitize_text_field($card->last4) : '',
411 + 'card_exp_month' => $card !== null ? sanitize_text_field($card->exp_month) : '',
412 + 'card_exp_year' => $card !== null ? sanitize_text_field($card->exp_year) : '',
413 + 'card_brand' => $card !== null ? sanitize_text_field($card->brand) : '',
414 + 'card_country' => $card !== null ? sanitize_text_field($card->country) : '',
415 + ];
416 + }
406 417
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 - }
418 + /**
419 + * @param \WC_Order $order
420 + *
421 + * @return array|bool
422 + *
423 + * @author Clément Boirie
424 + */
425 + public static function get_transaction_metadata($order)
426 + {
427 + if (self::is_pre_30()) {
428 + return get_post_meta($order->id, '_payplug_metadata', true);
429 + } else {
430 + return $order->get_meta('_payplug_metadata', true);
431 + }
432 + }
413 433
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 ) {
434 + /**
435 + * Save transaction metadata extracted from PayPlug response.
436 + *
437 + * @param \WC_Order $order
438 + * @param array $metadata
439 + *
440 + * @return void
441 + */
442 + public static function save_transaction_metadata($order, $metadata): void
443 + {
444 + if (self::is_pre_30()) {
445 + update_post_meta($order->id, '_payplug_metadata', $metadata);
446 + } else {
447 + $order->add_meta_data('_payplug_metadata', $metadata, true);
448 + $order->save_meta_data();
449 + }
450 + }
423 451
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 );
452 + /**
453 + * Set flag ipn ( in progress / over ) on order
454 + *
455 + * @param \WC_Order $order
456 + * @param array $metadata
457 + * @param bool $flag
458 + *
459 + * @return void
460 + */
461 + public static function set_flag_ipn_order($order, $metadata, $flag): void
462 + {
463 + $metadata['transaction_in_progress'] = $flag;
464 + self::save_transaction_metadata($order, $metadata);
465 + }
428 466
429 - if ( is_callable( [ $order, 'save' ] ) ) {
430 - $order->save();
431 - }
432 - }
433 - }
467 + /**
468 + * Get transient key from payplug option
469 + *
470 + * @return string
471 + */
472 + public static function get_transient_key($options)
473 + {
474 + $transient_key = PayplugGateway::OPTION_NAME . (array_key_exists('mode', $options) && (bool) $options['mode'] ? '_live' : '_test');
434 475
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 - }
476 + return $transient_key;
477 + }
448 478
449 - /**
450 - * Get current option from payplug settings
451 - *
452 - * @return array
453 - */
454 - public static function get_account_data_from_options($force_update = false) {
455 - $options = get_option('woocommerce_payplug_settings', []);
456 - $payplug_test_key = !empty($options['payplug_test_key']) ? $options['payplug_test_key'] : '';
457 - $payplug_live_key = !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : '';
458 - if (empty($payplug_test_key) && empty($payplug_live_key)) {
459 - return array();
479 + /**
480 + * Get transient live key from payplug option
481 + *
482 + * @return string
483 + */
484 + public static function get_live_transient_key()
485 + {
486 + return PayplugGateway::OPTION_NAME . '_live';
487 + }
488 +
489 + /**
490 + * Set transient data for payplug account
491 + *
492 + * @return string
493 + */
494 + public static function set_transient_data($data, $options = null)
495 + {
496 + $options = $options ? $options : self::get_payplug_options();
497 + $transient_key = self::get_transient_key($options);
498 + set_transient($transient_key, isset($data['httpResponse']) ? $data['httpResponse'] : []);
499 + }
500 +
501 + /**
502 + * Get current option from payplug settings
503 + *
504 + * @return array
505 + */
506 + public static function get_account_data_from_options()
507 + {
508 + $options = self::get_payplug_options();
509 + $transient_key = self::get_transient_key($options);
510 + $account = get_transient($transient_key);
511 +
512 + if (empty($account) || !is_array($account)) {
513 + self::set_account_data_from_options();
514 + $account = get_transient($transient_key);
460 515 }
461 516
462 - $transient_key = PayplugGatewayOney3x::OPTION_NAME . ($options['mode'] === 'yes' ? "_live" : "_test");
463 - $payplug_oney_config = get_transient($transient_key);
464 - if ($payplug_oney_config && !$force_update) {
465 - $account = $payplug_oney_config;
466 - } else {
467 - try {
468 - $parameters_account = Authentication::getAccount(new Payplug($options['mode'] === 'yes' ? $payplug_live_key : $payplug_test_key));
469 - set_transient($transient_key, $parameters_account['httpResponse']);
470 - $account = get_transient($transient_key);
471 - } catch (\Payplug\Exception\UnauthorizedException $e) {
472 - } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
517 + return $account;
518 + }
519 +
520 + /**
521 + * Get current option from payplug settings
522 + *this should replace get_account_data_from_options
523 + *
524 + * @return array
525 + */
526 + public static function generic_get_account_data_from_options($gateway_id)
527 + {
528 + $options = self::get_payplug_options();
529 + $transient_key = self::get_transient_key($options);
530 + $account = get_transient($transient_key);
531 +
532 + //if transient is empty, it goes and get the permissions for the customer to populate it
533 + if (empty($account) || !is_array($account)) {
534 + self::set_account_data_from_options();
535 + $account = get_transient($transient_key);
536 + }
537 +
538 + if (!empty($account)) {
539 + $helper = new self();
540 + $configuration = $helper->get_service('configuration');
541 + $account['permissions']['payplug'] = $configuration->get_option('payment_methods.configuration.payplug.active');
542 + }
543 +
544 + return $account;
545 + }
546 +
547 + /**
548 + * Set current option from payplug settings and api call
549 + *
550 + * @return void
551 + */
552 + public static function set_account_data_from_options()
553 + {
554 + $helper = new self();
555 + $configuration = $helper->get_service('configuration');
556 + $options = $configuration->get_options();
557 +
558 + if (empty($options) || !isset($options['api_key']) || !isset($options['jwt'])) {
559 + return [];
560 + }
561 +
562 + // get_bearer_token() refreshes the JWT if it's close to expiry (OAuth2 accounts).
563 + $mode = (bool) $options['mode'] ? 'live' : 'test';
564 + $key = (string) $helper->get_api()->get_bearer_token($mode);
565 +
566 + if (empty($key)) {
567 + return [];
568 + }
569 +
570 + try {
571 + $parameters_account = Authentication::getAccount(new Payplug($key));
572 + self::set_transient_data($parameters_account, $options);
573 + } catch (\Payplug\Exception\UnauthorizedException $e) {
574 + self::exception_handler_400_logout($e->getCode(), __('payplug_enable_feature', 'payplug'), sprintf('Account request error from PayPlug API : %s <br><b> ' . __('Successfully logged out.', 'payplug') . '</b>', wc_print_r($e->getMessage(), true)));
575 + } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
576 + } catch (\Payplug\Exception\ForbiddenException $e) {
577 + } catch (\Payplug\Exception\ForbiddenException $e) {
578 + return [];
579 + }
580 + }
581 +
582 + /**
583 + * Check if oney is available with current settings
584 + *
585 + * @return bool
586 + */
587 + public static function is_oney_available()
588 + {
589 + $account = self::get_account_data_from_options();
590 + if (!$account) {
591 + return false;
592 + }
593 +
594 + $options = self::get_payplug_options();
595 + $oney_active = (bool) $options['payment_methods']['configuration']['oney']['active'];
596 +
597 + return $account && $account['permissions'][PayplugPermissions::USE_ONEY] == '1' && $oney_active;
598 + }
599 +
600 + /**
601 + * Hide popup for if country_code != payplug country
602 + * https://payplug-prod.atlassian.net/browse/WOOC-249
603 + *
604 + * @return bool
605 + */
606 + public static function show_oney_popup()
607 + {
608 + $account = self::get_account_data_from_options();
609 + if ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == true && $account['country'] == self::getISOCountryCode()) {
610 + return true;
611 + }
612 +
613 + return false;
614 + }
615 +
616 + /**
617 + * @return bool
618 + */
619 + public static function check_order_max_amount($order_total)
620 + {
621 + if ($order_total < PayplugGatewayOney3x::MIN_AMOUNT || $order_total > PayplugGatewayOney3x::MAX_AMOUNT) {
622 + return false;
623 + }
624 +
625 + return true;
626 + }
627 +
628 + /**
629 + * Load translations from plugin languages folder.
630 + *
631 + * @param string $plugin_rel_path
632 + *
633 + * @return bool
634 + */
635 + public static function load_plugin_textdomain($plugin_rel_path)
636 + {
637 + $domain = 'payplug';
638 +
639 + $locale = apply_filters('plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain);
640 +
641 + $mofile = $domain . '-' . $locale . '.mo';
642 +
643 + $path = WP_PLUGIN_DIR . '/' . trim($plugin_rel_path, '/');
644 +
645 + return load_textdomain($domain, $path . '/' . $mofile);
646 + }
647 +
648 + public static function getISOCountryCode()
649 + {
650 + preg_match('([a-z-]+)', get_locale(), $country);
651 +
652 + return strtoupper($country[0]);
653 + }
654 +
655 + /**
656 + * Get country of the payplug merchant account and save it to the database
657 + *
658 + * @return string payplug merchant account country
659 + */
660 + public static function get_payplug_merchant_country()
661 + {
662 + $data = self::get_payplug_options();
663 +
664 + //in order to reduce the getAccount calls
665 + if (isset($data['payplug_live_key'])) {
666 + if (!isset($data['payplug_merchant_country'])) {
667 + return self::UpdateCountryOption($data);
473 668 }
669 +
670 + return $data['payplug_merchant_country'];
474 671 }
475 - $account['oneyEnabled'] = (isset($options['oney']) && !empty($options['oney'])) ? $options['oney'] : '';
476 - return $account;
477 672
478 - }
673 + $country = wc_get_base_location();
479 674
480 - /**
481 - * Get min and max for oney payment
482 - *
483 - * @return array
484 - */
485 - public static function get_min_max_oney() {
486 - $account = self::get_account_data_from_options();
487 - if (empty($account)) {
488 - return array();
489 - }
490 - return [
491 - 'min' => floatval($account['configuration']['oney']['min_amounts']['EUR'])/100,
492 - 'max' => floatval($account['configuration']['oney']['max_amounts']['EUR'])/100
493 - ];
494 - }
675 + return $country['country'];
676 + }
495 677
496 - /**
497 - * Check if oney is available with current settings
498 - *
499 - * @return boolean
500 - */
501 - public static function is_oney_available() {
502 - $account = self::get_account_data_from_options();
503 - if (empty($account)) {
504 - return false;
505 - }
506 - return ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == "1" && $account['oneyEnabled'] === "yes");
507 - }
678 + /**
679 + * Update payplug options and return the country
680 + *
681 + * @param $options
682 + *
683 + * @throws \Payplug\Exception\ConfigurationException
684 + *
685 + * @return string
686 + */
687 + public static function UpdateCountryOption($options)
688 + {
689 + if (!isset($options['api_key'])) {
690 + $options['company_iso'] = 'FR';
508 691
509 - /**
510 - * Load translations from plugin languages folder.
511 - *
512 - * @param string $plugin_rel_path
513 - *
514 - * @return bool
515 - */
516 - public static function load_plugin_textdomain( $plugin_rel_path ) {
692 + return $options['company_iso'];
693 + }
517 694
518 - $domain = 'payplug';
695 + try {
696 + $api_key = json_decode($options['api_key'], true);
519 697
520 - $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
698 + //fail safe for non activated account
699 + if ((isset($options['mode'])) && $api_key['test']) {
700 + $key = $options['mode'] && !empty($api_key['live']) ? $api_key['live'] : $api_key['test'];
701 + }
521 702
522 - $mofile = $domain . '-' . $locale . '.mo';
703 + if (empty($api_key['live'])) {
704 + $options['mode'] = false;
705 + }
523 706
524 - $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
707 + if (isset($key) && !empty($key)) {
708 + $response = self::get_account_data_from_options();
709 + }
525 710
526 - return load_textdomain( $domain, $path . '/' . $mofile );
527 - }
528 -
529 - /**
530 - * Check and update value for oney simulation
531 - *
532 - * @return void
533 - */
534 - public static function oney_simulation_values ($keys_array, &$array) {
535 - foreach($keys_array as $key) {
536 - if (array_key_exists($key, $array)) {
537 - $array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100;
538 - foreach ($array[$key]['installments'] as $k => $value) {
539 - $array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100;
711 + if (isset($response['httpResponse']['country'])) {
712 + $options['company_iso'] = $response['httpResponse']['country'];
713 + update_option('woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options));
714 + } else {
715 +
716 + //default value for merchant country
717 + $options['company_iso'] = 'FR';
718 + }
719 + } catch (ForbiddenException $e) {
720 + PayplugGateway::log('Error while getting account : ' . $e->getMessage(), 'error');
721 + \WC_Admin_Settings::add_error($e->getMessage());
722 + $options['company_iso'] = 'FR';
723 + }
724 +
725 + return $options['company_iso'];
726 + }
727 +
728 + public static function get_live_key()
729 + {
730 + // get_bearer_token() refreshes the JWT if it's close to expiry (OAuth2 accounts).
731 + return (new self())->get_api()->get_bearer_token('live');
732 + }
733 +
734 + public static function get_test_key()
735 + {
736 + return (new self())->get_api()->get_bearer_token('test');
737 + }
738 +
739 + public static function check_mode()
740 + {
741 + return self::get_payplug_options()['mode'];
742 + }
743 +
744 + /**
745 + * Domain the Integrated Payment SDK should submit card-tokenization requests to. Depends
746 + * on whether the connected merchant account is QA or production, which is a build-time
747 + * distinction (see SECURE_DOMAIN in payplug-config.php), not something this can derive
748 + * from the Test/Live mode toggle at runtime.
749 + *
750 + * @return string
751 + */
752 + public static function get_secure_domain(): string
753 + {
754 + return SECURE_DOMAIN;
755 + }
756 +
757 + /**
758 + * URL of the official Oney widget loader script. Depends on whether the connected
759 + * merchant account is QA or production, which is a build-time distinction (see
760 + * ONEY_LOADER_URL in payplug-config.php), not something this can derive from the
761 + * Test/Live mode toggle at runtime.
762 + *
763 + * @return string
764 + */
765 + public static function get_oney_loader_url(): string
766 + {
767 + return ONEY_LOADER_URL;
768 + }
769 +
770 + public static function payplug_logout(): void
771 + {
772 + $helper = new self();
773 + $helper->get_service('configuration')->clean_option();
774 + set_transient(self::get_transient_key(self::get_payplug_options()), null);
775 + }
776 +
777 + public static function plugin_deactivation(): void
778 + {
779 + $option_name = 'woocommerce_payplug_settings';
780 + delete_option($option_name);
781 + // for site options in Multisite
782 + delete_site_option($option_name);
783 + \Payplug\PayplugWoocommerce\Model\Lock::delete_lock_table();
784 + }
785 +
786 + public static function available_shipping_methods($carriers = [])
787 + {
788 + // Build enabled shipping methods based on active Zone
789 + $enabled_method_types = [];
790 +
791 + if (class_exists('\\WC_Shipping_Zones')) {
792 + // Regular zones
793 + $zones = \WC_Shipping_Zones::get_zones();
794 + foreach ($zones as $zone) {
795 + $methods = isset($zone['shipping_methods']) ? $zone['shipping_methods'] : [];
796 + foreach ($methods as $m) {
797 + if (!empty($m->enabled)) {
798 + $enabled_method_types[$m->id] = true;
799 + }
540 800 }
541 801 }
802 +
803 + // Locations not covered by zones (zone 0)
804 + $defaultZone = new \WC_Shipping_Zone(0);
805 + foreach ($defaultZone->get_shipping_methods(true) as $m) {
806 + if (!empty($m->enabled)) {
807 + $enabled_method_types[$m->id] = true;
808 + }
809 + }
542 810 }
811 +
812 + $shippings = WC()->shipping()->get_shipping_methods();
813 + $shippings_methods = [];
814 +
815 + foreach ($shippings as $shipping) {
816 + // Only keep shipping method types that are currently enabled in at least one zone
817 + if (!isset($enabled_method_types[$shipping->id])) {
818 + continue;
819 + }
820 + $shippings_methods[] = [
821 + 'id_carrier' => $shipping->id,
822 + 'name' => $shipping->method_title,
823 + 'checked' => in_array($shipping->id, $carriers, true),
824 + ];
825 + }
826 +
827 + return $shippings_methods;
828 + }
829 +
830 + public static function get_payplug_options()
831 + {
832 + $helper = new self();
833 +
834 + return $helper->get_service('configuration')->get_options();
835 + }
836 +
837 + public static function get_applepay_options()
838 + {
839 + $options = self::get_payplug_options();
840 + $applepay_configuration = $options['payment_methods']['configuration']['apple_pay'];
841 + $applepay_display = json_decode($applepay_configuration['display'], true);
842 + $applepay = [
843 + 'enabled' => (bool) $applepay_configuration['active'],
844 + 'checkout' => (bool) $applepay_display['checkout'],
845 + 'cart' => (bool) $applepay_display['cart'],
846 + 'carriers' => json_decode($applepay_configuration['carriers'], true),
847 + ];
848 +
849 + return $applepay;
850 + }
851 +
852 + public static function is_checkout_block()
853 + {
854 + return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('checkout'), 'woocommerce/checkout');
855 + }
856 +
857 + public static function is_cart_block()
858 + {
859 + return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('cart'), 'woocommerce/cart');
860 + }
861 +
862 + public static function is_product_block()
863 + {
864 + return WC_Blocks_Utils::has_block_in_page(wc_get_page_id('product'), 'woocommerce/product');
865 + }
866 +
867 + /**
868 + * Checks if subscriptions are enabled on the site.
869 + *
870 + * @return bool Whether subscriptions is enabled or not.
871 + *
872 + * @since 5.6.0
873 + */
874 + public static function is_subscriptions_enabled()
875 + {
876 + return class_exists('WC_Subscriptions') && class_exists('WC_Subscription') && version_compare(WC_Subscriptions::$version, '2.2.0', '>=');
877 + }
878 +
879 + /**
880 + * Cart has any subscriptions
881 + *
882 + * @return bool
883 + */
884 + public static function is_subscription()
885 + {
886 + if (is_null(WC()->cart)) {
887 + wc_load_cart();
888 + WC()->cart->get_cart_from_session();
889 + }
890 +
891 + if (empty(WC()->cart) || empty(WC()->cart->get_cart())) {
892 + return false;
893 + }
894 +
895 + foreach (WC()->cart->get_cart() as $prod) {
896 + if (empty($prod['product_id'])) {
897 + return false;
898 + }
899 +
900 + $pid = $prod['product_id'];
901 + $product = wc_get_product($pid);
902 +
903 + if ($product->is_type('subscription')) {
904 + return true;
905 + }
906 + }
907 +
908 + return false;
909 + }
910 +
911 + /**
912 + * Handling 400 errors from API (get_account, get_permissions) and return JSON rest response
913 + *
914 + * @param $title
915 + * @param $msg
916 + *
917 + * @return false
918 + */
919 + public static function exception_handler_400_logout($error_code, $title, $msg)
920 + {
921 + if (!empty($error_code) && ($error_code === 401 || $error_code === 403)) {
922 + self::payplug_logout();
923 + wp_send_json_error([
924 + 'title' => $title,
925 + 'msg' => $msg,
926 + 'close' => __('payplug_ok', 'payplug'),
927 + ]);
928 + }
929 +
930 + return false;
543 931 }
544 932 }