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

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