| 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§ion=' . $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 minimum amount allowed by PayPlug. |
| 324 |
* |
| 325 |
* This amount is in cents. |
| 326 |
* |
| 327 |
* @return int |
| 328 |
*/ |
| 329 |
public static function get_minimum_amount() { |
| 330 |
return 99; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Get maximum amount allowed by PayPlug. |
| 335 |
* |
| 336 |
* This amount is in cents. |
| 337 |
* |
| 338 |
* @return int |
| 339 |
*/ |
| 340 |
public static function get_maximum_amount() { |
| 341 |
return 2000000; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Convert amount in cents. |
| 346 |
* |
| 347 |
* @param float $amount |
| 348 |
* |
| 349 |
* @return int |
| 350 |
*/ |
| 351 |
public static function get_payplug_amount( $amount ) { |
| 352 |
if ( is_null( $amount ) ) { |
| 353 |
return $amount; |
| 354 |
} |
| 355 |
|
| 356 |
return absint( wc_format_decimal( ( (float) $amount * 100 ), wc_get_price_decimals() ) ); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Extract useful metadata from PayPlug response. |
| 361 |
* |
| 362 |
* @param APIResource $resource |
| 363 |
* |
| 364 |
* @return array |
| 365 |
*/ |
| 366 |
public static function extract_transaction_metadata( $resource ) { |
| 367 |
return [ |
| 368 |
'transaction_id' => sanitize_text_field( $resource->id ), |
| 369 |
'paid' => (bool) $resource->is_paid, |
| 370 |
'refunded' => (bool) $resource->is_refunded, |
| 371 |
'amount' => sanitize_text_field( $resource->amount ), |
| 372 |
'amount_refunded' => sanitize_text_field( $resource->amount_refunded ), |
| 373 |
'3ds' => (bool) $resource->is_3ds, |
| 374 |
'live' => (bool) $resource->is_live, |
| 375 |
'paid_at' => isset( $resource->hosted_payment->paid_at ) ? sanitize_text_field( $resource->hosted_payment->paid_at ) : sanitize_text_field( $resource->created_at ), |
| 376 |
'card_last4' => sanitize_text_field( $resource->card->last4 ), |
| 377 |
'card_exp_month' => sanitize_text_field( $resource->card->exp_month ), |
| 378 |
'card_exp_year' => sanitize_text_field( $resource->card->exp_year ), |
| 379 |
'card_brand' => sanitize_text_field( $resource->card->brand ), |
| 380 |
'card_country' => sanitize_text_field( $resource->card->country ), |
| 381 |
]; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* @param \WC_Order $order |
| 386 |
* |
| 387 |
* @return array|bool |
| 388 |
* @author Clément Boirie |
| 389 |
*/ |
| 390 |
public static function get_transaction_metadata( $order ) { |
| 391 |
|
| 392 |
if ( PayplugWoocommerceHelper::is_pre_30() ) { |
| 393 |
return get_post_meta( $order->id, '_payplug_metadata', true ); |
| 394 |
} else { |
| 395 |
return $order->get_meta( '_payplug_metadata', true ); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Save transaction metadata extracted from PayPlug response. |
| 401 |
* |
| 402 |
* @param \WC_Order $order |
| 403 |
* @param array $metadata |
| 404 |
* |
| 405 |
* @return void |
| 406 |
*/ |
| 407 |
public static function save_transaction_metadata( $order, $metadata ) { |
| 408 |
|
| 409 |
if ( PayplugWoocommerceHelper::is_pre_30() ) { |
| 410 |
update_post_meta( $order->id, '_payplug_metadata', $metadata ); |
| 411 |
} else { |
| 412 |
$order->add_meta_data( '_payplug_metadata', $metadata, true ); |
| 413 |
|
| 414 |
if ( is_callable( [ $order, 'save' ] ) ) { |
| 415 |
$order->save(); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
} |