| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that sets up the gateways. |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Classes/Charitable_Gateways |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2019, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 14 |
|
| 15 |
if ( ! class_exists( 'Charitable_Gateways' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Charitable_Gateways |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Charitable_Gateways { |
| 23 |
|
| 24 |
/** |
| 25 |
* The single instance of this class. |
| 26 |
* |
| 27 |
* @var Charitable_Gateways|null |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* All available payment gateways. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
private $gateways; |
| 37 |
|
| 38 |
/** |
| 39 |
* Set up the class. |
| 40 |
* |
| 41 |
* Note that the only way to instantiate an object is with the charitable_start method, |
| 42 |
* which can only be called during the start phase. In other words, don't try |
| 43 |
* to instantiate this object. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
protected function __construct() { |
| 48 |
add_action( 'init', array( $this, 'register_gateways' ) ); |
| 49 |
add_action( 'charitable_make_default_gateway', array( $this, 'handle_gateway_settings_request' ) ); |
| 50 |
add_action( 'charitable_enable_gateway', array( $this, 'handle_gateway_settings_request' ) ); |
| 51 |
add_action( 'charitable_disable_gateway', array( $this, 'handle_gateway_settings_request' ) ); |
| 52 |
add_filter( 'charitable_settings_fields_gateways_gateway', array( $this, 'register_gateway_settings' ), 10, 2 ); |
| 53 |
|
| 54 |
do_action( 'charitable_gateway_start', $this ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns and/or create the single instance of this class. |
| 59 |
* |
| 60 |
* @since 1.2.0 |
| 61 |
* |
| 62 |
* @return Charitable_Gateways |
| 63 |
*/ |
| 64 |
public static function get_instance() { |
| 65 |
if ( is_null( self::$instance ) ) { |
| 66 |
self::$instance = new self(); |
| 67 |
} |
| 68 |
|
| 69 |
return self::$instance; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Register gateways. |
| 74 |
* |
| 75 |
* To register a new gateway, you need to hook into the `charitable_payment_gateways` |
| 76 |
* hook and give Charitable the name of your gateway class. |
| 77 |
* |
| 78 |
* @since 1.2.0 |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function register_gateways() { |
| 83 |
$this->gateways = apply_filters( 'charitable_payment_gateways', array( |
| 84 |
'offline' => 'Charitable_Gateway_Offline', |
| 85 |
'paypal' => 'Charitable_Gateway_Paypal', |
| 86 |
) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Receives a request to enable or disable a payment gateway and validates it before passing it off. |
| 91 |
* |
| 92 |
* @since 1.0.0 |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function handle_gateway_settings_request() { |
| 97 |
if ( ! wp_verify_nonce( $_REQUEST['_nonce'], 'gateway' ) ) { |
| 98 |
wp_die( __( 'Cheatin\' eh?!', 'charitable' ) ); |
| 99 |
} |
| 100 |
|
| 101 |
$gateway = isset( $_REQUEST['gateway_id'] ) ? $_REQUEST['gateway_id'] : false; |
| 102 |
|
| 103 |
/* Gateway must be set */ |
| 104 |
if ( false === $gateway ) { |
| 105 |
wp_die( __( 'Missing gateway.', 'charitable' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
/* Validate gateway. */ |
| 109 |
if ( ! isset( $this->gateways[ $gateway ] ) ) { |
| 110 |
wp_die( __( 'Invalid gateway.', 'charitable' ) ); |
| 111 |
} |
| 112 |
|
| 113 |
switch ( $_REQUEST['charitable_action'] ) { |
| 114 |
case 'disable_gateway' : |
| 115 |
$this->disable_gateway( $gateway ); |
| 116 |
break; |
| 117 |
case 'enable_gateway' : |
| 118 |
$this->enable_gateway( $gateway ); |
| 119 |
break; |
| 120 |
case 'make_default_gateway' : |
| 121 |
$this->set_default_gateway( $gateway ); |
| 122 |
break; |
| 123 |
default : |
| 124 |
do_action( 'charitable_gateway_settings_request', $_REQUEST['charitable_action'], $gateway ); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns all available payment gateways. |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function get_available_gateways() { |
| 136 |
return $this->gateways; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns the current active gateways. |
| 141 |
* |
| 142 |
* @since 1.0.0 |
| 143 |
* |
| 144 |
* @return string[] |
| 145 |
*/ |
| 146 |
public function get_active_gateways() { |
| 147 |
$active_gateways = charitable_get_option( 'active_gateways', array() ); |
| 148 |
|
| 149 |
foreach ( $active_gateways as $gateway_id => $gateway_class ) { |
| 150 |
if ( ! class_exists( $gateway_class ) ) { |
| 151 |
unset( $active_gateways[ $gateway_id ] ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
uksort( $active_gateways, array( $this, 'sort_by_default' ) ); |
| 156 |
|
| 157 |
return apply_filters( 'charitable_active_gateways', $active_gateways ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns an array of the active gateways, in ID => name format. |
| 162 |
* |
| 163 |
* This is useful for select/radio input fields. |
| 164 |
* |
| 165 |
* @since 1.0.0 |
| 166 |
* |
| 167 |
* @return string[] |
| 168 |
*/ |
| 169 |
public function get_gateway_choices() { |
| 170 |
$gateways = array(); |
| 171 |
|
| 172 |
foreach ( $this->get_active_gateways() as $id => $class ) { |
| 173 |
$gateway = new $class; |
| 174 |
$gateways[ $id ] = $gateway->get_label(); |
| 175 |
} |
| 176 |
|
| 177 |
return $gateways; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns a text description of the active gateways. |
| 182 |
* |
| 183 |
* @since 1.3.0 |
| 184 |
* |
| 185 |
* @return string[] |
| 186 |
*/ |
| 187 |
public function get_active_gateways_names() { |
| 188 |
$gateways = array(); |
| 189 |
|
| 190 |
foreach ( $this->get_active_gateways() as $id => $class ) { |
| 191 |
$gateway = new $class; |
| 192 |
$gateways[] = $gateway->get_name(); |
| 193 |
} |
| 194 |
|
| 195 |
return $gateways; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Return the gateway class name for a given gateway. |
| 200 |
* |
| 201 |
* @since 1.0.0 |
| 202 |
* |
| 203 |
* @param string $gateway Gateway ID. |
| 204 |
* @return string|false |
| 205 |
*/ |
| 206 |
public function get_gateway( $gateway ) { |
| 207 |
return isset( $this->gateways[ $gateway ] ) ? $this->gateways[ $gateway ] : false; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Return the gateway object for a given gateway. |
| 212 |
* |
| 213 |
* @since 1.0.0 |
| 214 |
* |
| 215 |
* @param string $gateway Gateway ID. |
| 216 |
* @return Charitable_Gateway|null |
| 217 |
*/ |
| 218 |
public function get_gateway_object( $gateway ) { |
| 219 |
$class = $this->get_gateway( $gateway ); |
| 220 |
return $class ? new $class : null; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns whether the passed gateway is active. |
| 225 |
* |
| 226 |
* @since 1.0.0 |
| 227 |
* |
| 228 |
* @param string $gateway_id Gateway ID. |
| 229 |
* @return boolean |
| 230 |
*/ |
| 231 |
public function is_active_gateway( $gateway_id ) { |
| 232 |
return array_key_exists( $gateway_id, $this->get_active_gateways() ); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Checks whether the submitted gateway is valid. |
| 237 |
* |
| 238 |
* @since 1.4.3 |
| 239 |
* |
| 240 |
* @param string $gateway Gateway ID. |
| 241 |
* @return boolean |
| 242 |
*/ |
| 243 |
public function is_valid_gateway( $gateway ) { |
| 244 |
return apply_filters( 'charitable_is_valid_gateway', array_key_exists( $gateway, $this->gateways ), $gateway ); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the default gateway. |
| 249 |
* |
| 250 |
* @since 1.0.0 |
| 251 |
* |
| 252 |
* @return string |
| 253 |
*/ |
| 254 |
public function get_default_gateway() { |
| 255 |
return charitable_get_option( 'default_gateway', '' ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Provide default gateway settings fields. |
| 260 |
* |
| 261 |
* @since 1.0.0 |
| 262 |
* |
| 263 |
* @param array $settings Gateway settings. |
| 264 |
* @param Charitable_Gateway $gateway The gateway's helper object. |
| 265 |
* @return array |
| 266 |
*/ |
| 267 |
public function register_gateway_settings( $settings, Charitable_Gateway $gateway ) { |
| 268 |
add_filter( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), array( $gateway, 'default_gateway_settings' ), 5 ); |
| 269 |
add_filter( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), array( $gateway, 'gateway_settings' ), 15 ); |
| 270 |
return apply_filters( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), $settings ); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Returns true if test mode is enabled. |
| 275 |
* |
| 276 |
* @since 1.0.0 |
| 277 |
* |
| 278 |
* @return boolean |
| 279 |
*/ |
| 280 |
public function in_test_mode() { |
| 281 |
$enabled = charitable_get_option( 'test_mode', false ); |
| 282 |
return apply_filters( 'charitable_in_test_mode', $enabled ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Checks whether all of the active gateways support a feature. |
| 287 |
* |
| 288 |
* If ANY gateway doesn't support the feature, this returns false. |
| 289 |
* |
| 290 |
* @since 1.4.0 |
| 291 |
* |
| 292 |
* @param string $feature Feature to search for. |
| 293 |
* @return boolean |
| 294 |
*/ |
| 295 |
public function all_gateways_support( $feature ) { |
| 296 |
foreach ( $this->get_active_gateways() as $gateway_id => $gateway_class ) { |
| 297 |
|
| 298 |
$gateway_object = new $gateway_class; |
| 299 |
|
| 300 |
if ( false === $gateway_object->supports( $feature ) ) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Checks whether any of the active gateways support a feature. |
| 310 |
* |
| 311 |
* If any gateway supports the feature, this returns true. Otherwise false. |
| 312 |
* |
| 313 |
* @since 1.4.0 |
| 314 |
* |
| 315 |
* @param string $feature Feature to check for. |
| 316 |
* @return boolean |
| 317 |
*/ |
| 318 |
public function any_gateway_supports( $feature ) { |
| 319 |
foreach ( $this->get_active_gateways() as $gateway_id => $gateway_class ) { |
| 320 |
|
| 321 |
$gateway_object = new $gateway_class; |
| 322 |
|
| 323 |
if ( true === $gateway_object->supports( $feature ) ) { |
| 324 |
return true; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Checks whether all of the active gateways support AJAX. |
| 333 |
* |
| 334 |
* If ANY gateway doesn't support AJAX, this returns false. |
| 335 |
* |
| 336 |
* @since 1.3.0 |
| 337 |
* |
| 338 |
* @return boolean |
| 339 |
*/ |
| 340 |
public function gateways_support_ajax() { |
| 341 |
return $this->all_gateways_support( '1.3.0' ); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Return an array of recommended gateways for the current site. |
| 346 |
* |
| 347 |
* Note that this will only return gateways that are not already |
| 348 |
* available on the site. i.e. If you have Stripe installed, it |
| 349 |
* will not suggest that. |
| 350 |
* |
| 351 |
* @since 1.5.0 |
| 352 |
* |
| 353 |
* @return array |
| 354 |
*/ |
| 355 |
public function get_recommended_gateways() { |
| 356 |
$available = $this->get_available_gateways(); |
| 357 |
$gateways = array( |
| 358 |
'payfast' => __( 'Payfast', 'charitable' ), |
| 359 |
'payumoney' => __( 'PayUMoney', 'charitable' ), |
| 360 |
'stripe' => __( 'Stripe', 'charitable' ), |
| 361 |
'authorize_net' => __( 'Authorize.Net', 'charitable' ), |
| 362 |
); |
| 363 |
|
| 364 |
/* If the user has already enabled one of these, leave them alone. :) */ |
| 365 |
foreach ( $gateways as $gateway_id => $gateway ) { |
| 366 |
if ( array_key_exists( $gateway_id, $available ) ) { |
| 367 |
return array(); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
$currency = charitable_get_option( 'currency', 'AUD' ); |
| 372 |
$locale = get_locale(); |
| 373 |
|
| 374 |
if ( 'en_ZA' == $locale || 'ZAR' == $currency ) { |
| 375 |
return charitable_array_subset( $gateways, array( 'payfast' ) ); |
| 376 |
} |
| 377 |
|
| 378 |
if ( 'hi_IN' == $locale || 'INR' == $currency ) { |
| 379 |
return charitable_array_subset( $gateways, array( 'payumoney' ) ); |
| 380 |
} |
| 381 |
|
| 382 |
return charitable_array_subset( $gateways, array( 'stripe', 'authorize_net' ) ); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Sets the default gateway. |
| 387 |
* |
| 388 |
* @since 1.0.0 |
| 389 |
* |
| 390 |
* @param string $gateway Gateway ID. |
| 391 |
* @return void |
| 392 |
*/ |
| 393 |
protected function set_default_gateway( $gateway ) { |
| 394 |
$settings = get_option( 'charitable_settings' ); |
| 395 |
$settings['default_gateway'] = $gateway; |
| 396 |
|
| 397 |
update_option( 'charitable_settings', $settings ); |
| 398 |
|
| 399 |
charitable_get_admin_notices()->add_success( __( 'Default Gateway Updated', 'charitable' ) ); |
| 400 |
|
| 401 |
do_action( 'charitable_set_gateway_gateway', $gateway ); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Enable a payment gateway. |
| 406 |
* |
| 407 |
* @since 1.0.0 |
| 408 |
* |
| 409 |
* @param string $gateway Gateway ID. |
| 410 |
* @return void |
| 411 |
*/ |
| 412 |
protected function enable_gateway( $gateway ) { |
| 413 |
$settings = get_option( 'charitable_settings' ); |
| 414 |
|
| 415 |
$active_gateways = isset( $settings['active_gateways'] ) ? $settings['active_gateways'] : array(); |
| 416 |
$active_gateways[ $gateway ] = $this->gateways[ $gateway ]; |
| 417 |
$settings['active_gateways'] = $active_gateways; |
| 418 |
|
| 419 |
/* If this is the only gateway, make it the default gateway */ |
| 420 |
if ( 1 == count( $settings['active_gateways'] ) ) { |
| 421 |
$settings['default_gateway'] = $gateway; |
| 422 |
} |
| 423 |
|
| 424 |
update_option( 'charitable_settings', $settings ); |
| 425 |
|
| 426 |
Charitable_Settings::get_instance()->add_update_message( __( 'Gateway enabled', 'charitable' ), 'success' ); |
| 427 |
|
| 428 |
do_action( 'charitable_gateway_enable', $gateway ); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Disable a payment gateway. |
| 433 |
* |
| 434 |
* @since 1.0.0 |
| 435 |
* |
| 436 |
* @param string $gateway Gateway ID. |
| 437 |
* @return void |
| 438 |
*/ |
| 439 |
protected function disable_gateway( $gateway ) { |
| 440 |
$settings = get_option( 'charitable_settings' ); |
| 441 |
|
| 442 |
if ( ! isset( $settings['active_gateways'][ $gateway ] ) ) { |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
unset( $settings['active_gateways'][ $gateway ] ); |
| 447 |
|
| 448 |
/* Set a new default gateway */ |
| 449 |
if ( $gateway == $this->get_default_gateway() ) { |
| 450 |
|
| 451 |
$settings['default_gateway'] = count( $settings['active_gateways'] ) ? key( $settings['active_gateways'] ) : ''; |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
update_option( 'charitable_settings', $settings ); |
| 456 |
|
| 457 |
Charitable_Settings::get_instance()->add_update_message( __( 'Gateway disabled', 'charitable' ), 'success' ); |
| 458 |
|
| 459 |
do_action( 'charitable_gateway_disable', $gateway ); |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Sort the active gateways, placing the default gateway first. |
| 464 |
* |
| 465 |
* @since 1.4.0 |
| 466 |
* |
| 467 |
* @param string $a Gateway to compare. |
| 468 |
* @param string $b Gateway to compare against. |
| 469 |
* @return int |
| 470 |
*/ |
| 471 |
protected function sort_by_default( $a, $b ) { |
| 472 |
$default = $this->get_default_gateway(); |
| 473 |
|
| 474 |
if ( $a == $default ) { |
| 475 |
return -1; |
| 476 |
} |
| 477 |
|
| 478 |
if ( $b == $default ) { |
| 479 |
return 1; |
| 480 |
} |
| 481 |
|
| 482 |
return 0; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
endif; |
| 487 |
|