PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / gateways / class-charitable-gateways.php

class-charitable-gateways.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0, at includes/gateways/class-charitable-gateways.php

935 lines 26.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class that sets up the gateways.
4 *
5 * @package Charitable/Classes/Charitable_Gateways
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.6.57
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Gateways' ) ) :
19
20 /**
21 * Charitable_Gateways
22 *
23 * @since 1.0.0
24 */
25 class Charitable_Gateways {
26
27 /**
28 * The single instance of this class.
29 *
30 * @since 1.0.0
31 *
32 * @var Charitable_Gateways|null
33 */
34 private static $instance = null;
35
36 /**
37 * All available payment gateways.
38 *
39 * @since 1.0.0
40 *
41 * @var array
42 */
43 private $gateways;
44
45 /**
46 * Set up the class.
47 *
48 * Note that the only way to instantiate an object is with the charitable_start method,
49 * which can only be called during the start phase. In other words, don't try
50 * to instantiate this object.
51 *
52 * @since 1.0.0
53 */
54 protected function __construct() {
55 add_action( 'init', array( $this, 'check_settings' ) );
56 add_action( 'init', array( $this, 'register_gateways' ) );
57 add_action( 'init', array( $this, 'connect_gateways' ), 99 );
58 add_action( 'init', array( $this, 'disconnect_gateways' ), 99 );
59 add_action( 'charitable_make_default_gateway', array( $this, 'handle_gateway_settings_request' ) );
60 add_action( 'charitable_enable_gateway', array( $this, 'handle_gateway_settings_request' ) );
61 add_action( 'charitable_disable_gateway', array( $this, 'handle_gateway_settings_request' ) );
62 add_filter( 'charitable_settings_fields_gateways_gateway', array( $this, 'register_gateway_settings' ), 10, 2 );
63
64 add_action( 'charitable_disable_gateway', array( $this, 'handle_gateway_settings_request' ) );
65
66 do_action( 'charitable_gateway_start', $this );
67
68 add_action( 'wp_ajax_charitable-show-stripe-keys', array( $this, 'show_stripe_manual_keys_ajax'), 10 );
69
70 }
71
72 /**
73 * Returns and/or create the single instance of this class.
74 *
75 * @since 1.2.0
76 *
77 * @return Charitable_Gateways
78 */
79 public static function get_instance() {
80 if ( is_null( self::$instance ) ) {
81 self::$instance = new self();
82 }
83
84 return self::$instance;
85 }
86
87 /**
88 * Register gateways.
89 *
90 * To register a new gateway, you need to hook into the `charitable_payment_gateways`
91 * hook and give Charitable the name of your gateway class.
92 *
93 * @since 1.2.0
94 *
95 * @return void
96 */
97 public function register_gateways() {
98 /**
99 * Filter the list of payment gateways.
100 *
101 * @since 1.2.0
102 *
103 * @param array $gateways The list of gateways in gateway ID => gateway class format.
104 */
105 $this->gateways = apply_filters(
106 'charitable_payment_gateways',
107 array(
108 'stripe' => 'Charitable_Gateway_Stripe_AM',
109 'paypal' => 'Charitable_Gateway_Paypal',
110 'offline' => 'Charitable_Gateway_Offline',
111 )
112 );
113 $this->gateways['stripe'] = 'Charitable_Gateway_Stripe_AM';
114
115 }
116
117 /**
118 * Receives a request to enable or disable a payment gateway and validates it before passing it off.
119 *
120 * @since 1.0.0
121 *
122 * @return void
123 */
124 public function handle_gateway_settings_request() {
125 if ( ! wp_verify_nonce( $_REQUEST['_nonce'], 'gateway' ) ) {
126 wp_die( __( 'Cheatin\' eh?!', 'charitable' ) );
127 }
128
129 $gateway = isset( $_REQUEST['gateway_id'] ) ? $_REQUEST['gateway_id'] : false;
130
131 /* Gateway must be set */
132 if ( false === $gateway ) {
133 wp_die( __( 'Missing gateway.', 'charitable' ) );
134 }
135
136 /* Validate gateway. */
137 if ( ! isset( $this->gateways[ $gateway ] ) ) {
138 wp_die( __( 'Invalid gateway.', 'charitable' ) );
139 }
140
141 switch ( $_REQUEST['charitable_action'] ) {
142 case 'disable_gateway':
143 $this->disable_gateway( $gateway );
144 break;
145 case 'enable_gateway':
146 $this->enable_gateway( $gateway );
147 $this->redirect_gateway_settings( $gateway );
148 break;
149 case 'make_default_gateway':
150 $this->set_default_gateway( $gateway );
151 break;
152 default:
153 /**
154 * Do something when a gateway settings request takes place.
155 *
156 * @since 1.0.0
157 *
158 * @param string $action The action taking place.
159 * @param string $gateway_id The gateway ID.
160 */
161 do_action( 'charitable_gateway_settings_request', $_REQUEST['charitable_action'], $gateway );
162 }
163
164 }
165
166 /**
167 * Returns all available payment gateways.
168 *
169 * @since 1.0.0
170 *
171 * @return array
172 */
173 public function get_available_gateways() {
174 if ( isset( $this->gateways['stripe'] ) ) {
175 $this->gateways['stripe'] = charitable()->is_stripe_connect_addon() ? 'Charitable_Gateway_Stripe_AM' : $this->gateways['stripe'];
176 }
177 return $this->gateways;
178 }
179
180 /**
181 * Returns the current active gateways.
182 *
183 * @since 1.0.0
184 *
185 * @return string[]
186 */
187 public function get_active_gateways() {
188 $active_gateways = charitable_get_option( 'active_gateways', array() );
189
190 foreach ( $active_gateways as $gateway_id => $gateway_class ) {
191 if ( ! class_exists( $gateway_class ) ) {
192 unset( $active_gateways[ $gateway_id ] );
193 }
194 }
195
196 uksort( $active_gateways, array( $this, 'sort_by_default' ) );
197
198 // force the strip active gateway normally for stripe addon to point to the stripe built into core.
199 $active_gateways['stripe'] = 'Charitable_Gateway_Stripe_AM';
200
201 /**
202 * Filter the list of active gateways.
203 *
204 * @since 1.0.0
205 *
206 * @param array $gateways Active gateways.
207 */
208 return apply_filters( 'charitable_active_gateways', $active_gateways );
209 }
210
211 /**
212 * Returns an array of the active gateways, in ID => name format.
213 *
214 * This is useful for select/radio input fields.
215 *
216 * @since 1.0.0
217 *
218 * @return string[]
219 */
220 public function get_gateway_choices() {
221 $gateways = array();
222
223 foreach ( $this->get_active_gateways() as $id => $class ) {
224 $gateway = new $class;
225 $gateways[ $id ] = $gateway->get_label();
226 }
227
228 return $gateways;
229 }
230
231 /**
232 * Returns a text description of the active gateways.
233 *
234 * @since 1.3.0
235 *
236 * @return string[]
237 */
238 public function get_active_gateways_names() {
239 $gateways = array();
240
241 foreach ( $this->get_active_gateways() as $id => $class ) {
242 $gateway = new $class;
243 $gateways[] = $gateway->get_name();
244 }
245
246 return $gateways;
247 }
248
249 /**
250 * Return the gateway class name for a given gateway.
251 *
252 * @since 1.0.0
253 *
254 * @param string $gateway Gateway ID.
255 * @return string|false
256 */
257 public function get_gateway( $gateway ) {
258 return isset( $this->gateways[ $gateway ] ) ? $this->gateways[ $gateway ] : false;
259 }
260
261 /**
262 * Return the gateway object for a given gateway.
263 *
264 * @since 1.0.0
265 *
266 * @param string $gateway Gateway ID.
267 * @return Charitable_Gateway|null
268 */
269 public function get_gateway_object( $gateway ) {
270 $class = $this->get_gateway( $gateway );
271 $object = $class ? new $class : null;
272
273 /**
274 * Filter the gateway object.
275 *
276 * @since 1.6.30
277 *
278 * @param Charitable_Gateway|null $object The gateway object.
279 */
280 return apply_filters( 'charitable_gateway_object_' . $gateway, $object, $gateway );
281 }
282
283 /**
284 * Returns whether the passed gateway is active.
285 *
286 * @since 1.0.0
287 *
288 * @param string $gateway_id Gateway ID.
289 * @return boolean
290 */
291 public function is_active_gateway( $gateway_id ) {
292 return array_key_exists( $gateway_id, $this->get_active_gateways() );
293 }
294
295 /**
296 * Checks whether the submitted gateway is valid.
297 *
298 * @since 1.4.3
299 *
300 * @param string $gateway Gateway ID.
301 * @return boolean
302 */
303 public function is_valid_gateway( $gateway ) {
304 /**
305 * Validate a particular gatewya.
306 *
307 * @since 1.4.3
308 *
309 * @param boolean $valid Whether a gateway is valid.
310 * @param string $gateway The gateway ID.
311 */
312
313 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) {
314 error_log( 'is_valid_gateway' );
315 error_log( print_r( $gateway, true ) );
316 error_log( print_r( $this->gateways, true ) );
317 }
318
319 return apply_filters( 'charitable_is_valid_gateway', array_key_exists( $gateway, $this->gateways ), $gateway );
320 }
321
322 /**
323 * Returns the default gateway.
324 *
325 * @since 1.0.0
326 *
327 * @return string
328 */
329 public function get_default_gateway() {
330 return charitable_get_option( 'default_gateway', '' );
331 }
332
333 /**
334 * Provide default gateway settings fields.
335 *
336 * @since 1.0.0
337 *
338 * @param array $settings Gateway settings.
339 * @param Charitable_Gateway $gateway The gateway's helper object.
340 * @return array
341 */
342 public function register_gateway_settings( $settings, Charitable_Gateway $gateway ) {
343 add_filter( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), array( $gateway, 'default_gateway_settings' ), 5 );
344 add_filter( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), array( $gateway, 'gateway_settings' ), 15 );
345
346 /**
347 * Filter the settings to show for a particular gateway.
348 *
349 * @since 1.0.0
350 *
351 * @param array $settings Gateway settings.
352 */
353 return apply_filters( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), $settings );
354 }
355
356 /**
357 * Returns true if test mode is enabled.
358 *
359 * @since 1.0.0
360 *
361 * @return boolean
362 */
363 public function in_test_mode() {
364 $enabled = charitable_get_option( 'test_mode', false );
365
366 /**
367 * Return whether Charitable is in test mode.
368 *
369 * @since 1.0.0
370 *
371 * @param boolean $enabled Whether test mode is on.
372 */
373 return apply_filters( 'charitable_in_test_mode', $enabled );
374 }
375
376 /**
377 * Checks whether all of the active gateways support a feature.
378 *
379 * If ANY gateway doesn't support the feature, this returns false.
380 *
381 * @since 1.4.0
382 *
383 * @param string $feature Feature to search for.
384 * @return boolean
385 */
386 public function all_gateways_support( $feature ) {
387 foreach ( $this->get_active_gateways() as $gateway_id => $gateway_class ) {
388
389 $gateway_object = new $gateway_class;
390
391 if ( false === $gateway_object->supports( $feature ) ) {
392 return false;
393 }
394 }
395
396 return true;
397 }
398
399 /**
400 * Checks whether any of the active gateways support a feature.
401 *
402 * If any gateway supports the feature, this returns true. Otherwise false.
403 *
404 * @since 1.4.0
405 *
406 * @param string $feature Feature to check for.
407 * @return boolean
408 */
409 public function any_gateway_supports( $feature ) {
410 foreach ( $this->get_active_gateways() as $gateway_id => $gateway_class ) {
411
412 $gateway_object = new $gateway_class;
413
414 if ( true === $gateway_object->supports( $feature ) ) {
415 return true;
416 }
417 }
418
419 return false;
420 }
421
422 /**
423 * Checks whether all of the active gateways support AJAX.
424 *
425 * If ANY gateway doesn't support AJAX, this returns false.
426 *
427 * @since 1.3.0
428 *
429 * @return boolean
430 */
431 public function gateways_support_ajax() {
432 return $this->all_gateways_support( '1.3.0' );
433 }
434
435 /**
436 * Return an array of recommended gateways for the current site.
437 *
438 * Note that this will only return gateways that are not already
439 * available on the site. i.e. If you have Stripe installed, it
440 * will not suggest that.
441 *
442 * @since 1.5.0
443 *
444 * @return array
445 */
446 public function get_recommended_gateways() {
447 $available = $this->get_available_gateways();
448 $gateways = array(
449 'payfast' => __( 'Payfast', 'charitable' ),
450 'paystack' => __( 'Paystack', 'charitable' ),
451 'stripe' => __( 'Stripe', 'charitable' ),
452 'authorize_net' => __( 'Authorize.Net', 'charitable' ),
453 'windcave' => __( 'Windcave', 'charitable' ),
454 'braintree' => __( 'Braintree', 'charitable' ),
455 'mollie' => __( 'Mollie', 'charitable' ),
456 'gocardless' => __( 'GoCardless', 'charitable' ),
457 'payrexx' => __( 'Payrexx', 'charitable' ),
458 );
459
460 /* If the user has already enabled one of these, leave them alone. :) */
461 foreach ( $gateways as $gateway_id => $gateway ) {
462 if ( array_key_exists( $gateway_id, $available ) ) {
463 return array();
464 }
465 }
466
467 $currency = charitable_get_default_currency();
468 $locale = get_locale();
469
470 if ( 'en_ZA' == $locale || 'ZAR' == $currency ) {
471 return charitable_array_subset( $gateways, array( 'payfast', 'paystack' ) );
472 }
473
474 if ( in_array( $currency, array( 'NGN', 'GHS' ) ) ) {
475 return charitable_array_subset( $gateways, array( 'paystack' ) );
476 }
477
478 if ( in_array( $locale, array( 'en_NZ', 'en_AU', 'en_GB' ) ) || in_array( $currency, array( 'NZD', 'AUD', 'GBP' ) ) ) {
479 return charitable_array_subset( $gateways, array( 'stripe', 'gocardless' ) );
480 }
481
482 if ( in_array( $locale, array( 'ms_MY', 'ja', 'zh_HK' ) ) || in_array( $currency, array( 'MYR', 'JPY', 'HKD' ) ) ) {
483 return charitable_array_subset( $gateways, array( 'stripe', 'windcave' ) );
484 }
485
486 if ( in_array( $locale, array( 'th' ) ) || in_array( $currency, array( 'BND', 'FJD', 'KWD', 'PGK', 'SBD', 'THB', 'TOP', 'VUV', 'WST' ) ) ) {
487 return charitable_array_subset( $gateways, array( 'windcave' ) );
488 }
489
490 if ( in_array( $currency, array( 'EUR' ) ) ) {
491 return charitable_array_subset( $gateways, array( 'stripe', 'mollie' ) );
492 }
493
494 if ( in_array( $currency, array( 'CHF', 'DKK' ) ) ) {
495 return charitable_array_subset( $gateways, array( 'stripe', 'payrexx' ) );
496 }
497
498 return charitable_array_subset( $gateways, array( 'stripe', 'braintree' ) );
499 }
500
501 /**
502 * Sets the default gateway.
503 *
504 * @since 1.0.0
505 *
506 * @param string $gateway Gateway ID.
507 * @return void
508 */
509 protected function set_default_gateway( $gateway ) {
510 $settings = get_option( 'charitable_settings' );
511
512 $settings['default_gateway'] = $gateway;
513
514 update_option( 'charitable_settings', $settings );
515
516 charitable_get_admin_notices()->add_success( __( 'Default Gateway Updated', 'charitable' ) );
517
518 do_action( 'charitable_set_gateway_gateway', $gateway );
519 }
520
521 /**
522 * Enable a payment gateway.
523 *
524 * @since 1.0.0
525 *
526 * @param string $gateway Gateway ID.
527 * @return void
528 */
529 protected function enable_gateway( $gateway ) {
530 $settings = get_option( 'charitable_settings' );
531
532 $active_gateways = isset( $settings['active_gateways'] ) ? $settings['active_gateways'] : array();
533 $active_gateways[ $gateway ] = $this->gateways[ $gateway ];
534 $settings['active_gateways'] = $active_gateways;
535
536 /* If this is the only gateway, make it the default gateway */
537 if ( 1 == count( $settings['active_gateways'] ) ) {
538 $settings['default_gateway'] = $gateway;
539 }
540
541 update_option( 'charitable_settings', $settings );
542
543 Charitable_Settings::get_instance()->add_update_message( __( 'Gateway enabled', 'charitable' ), 'success' );
544
545 /**
546 * Do something when a payment gateway is enabled.
547 *
548 * @since 1.0.0
549 *
550 * @param string $gateway The payment gateway.
551 */
552 do_action( 'charitable_gateway_enable', $gateway );
553 }
554
555 /**
556 * Disable a payment gateway.
557 *
558 * @since 1.0.0
559 *
560 * @param string $gateway Gateway ID.
561 * @return void
562 */
563 protected function disable_gateway( $gateway ) {
564 $settings = get_option( 'charitable_settings' );
565
566 if ( ! isset( $settings['active_gateways'][ $gateway ] ) ) {
567 return;
568 }
569
570 unset( $settings['active_gateways'][ $gateway ] );
571
572 /* Set a new default gateway */
573 if ( $gateway == $this->get_default_gateway() ) {
574
575 $settings['default_gateway'] = count( $settings['active_gateways'] ) ? key( $settings['active_gateways'] ) : '';
576
577 }
578
579 update_option( 'charitable_settings', $settings );
580
581 Charitable_Settings::get_instance()->add_update_message( __( 'Gateway disabled', 'charitable' ), 'success' );
582
583 /**
584 * Do something when a payment gateway is disabled.
585 *
586 * @since 1.0.0
587 *
588 * @param string $gateway The payment gateway.
589 */
590 do_action( 'charitable_gateway_disable', $gateway );
591 }
592
593 /**
594 * Connects to Stripe by saving account information passed back to the plugin.
595 *
596 * @since 4.2.2
597 *
598 * @return void
599 */
600 public function connect_gateways() {
601 // Current user cannot handle this request.
602 if ( ! current_user_can( 'manage_options' ) ) {
603 return;
604 }
605
606 // Do not need to handle this request, bail.
607 if (
608 ! isset( $_GET['wpcharitable_gateway_connect_completion'] ) ||
609 'stripe_connect' !== $_GET['wpcharitable_gateway_connect_completion'] ||
610 ! isset( $_GET['state'] )
611 ) {
612 return;
613 }
614
615 // Unable to redirect, bail.
616 if ( headers_sent() ) {
617 return;
618 }
619
620 if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
621 $current_url = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
622 } else {
623 $current_url = '';
624 }
625
626 $customer_site_url = remove_query_arg(
627 array(
628 'state',
629 'wpcharitable_gateway_connect_completion'
630 ),
631 $current_url
632 );
633
634 $wpcharitable_credentials_url = add_query_arg(
635 array(
636 'live_mode' => (int) ! charitable_get_option( 'test_mode' ), // (int) ! $this->is_test_mode(),
637 'state' => sanitize_text_field( $_GET['state'] ),
638 'customer_site_url' => urlencode( $customer_site_url ),
639 ),
640 'https://wpcharitable.com/stripe-connect/?wpcharitable_gateway_connect_credentials=stripe_connect' // todo: filter market site url
641 );
642
643 $response = wp_remote_get( esc_url_raw( $wpcharitable_credentials_url ) ); // add add_filter('https_ssl_verify', '__return_false'); here if testing local
644
645 if (
646 is_wp_error( $response ) ||
647 200 !== wp_remote_retrieve_response_code( $response )
648 ) {
649
650 $stripe_account_settings_url = add_query_arg(
651 array(
652 'tab' => 'gateways',
653 'page' => 'charitable-settings',
654 'group' => 'gateways_stripe',
655 ),
656 admin_url( 'admin.php' )
657 );
658
659 $message = wpautop(
660 sprintf(
661 /* translators: %1$s Opening anchor tag, do not translate. %2$s Closing anchor tag, do not translate. */
662 __(
663 'There was an error getting your Stripe credentials. Please %1$stry again%2$s. If you continue to have this problem, please contact support.',
664 'stripe'
665 ),
666 '<a href="' . esc_url( $stripe_account_settings_url ) . '">',
667 '</a>'
668 )
669 );
670
671 wp_die( $message );
672 }
673
674 $body = wp_remote_retrieve_body( $response );
675
676 /** @var string $body */
677 $body = json_decode( $body, true );
678
679 /** @var array<array<string>> $body */
680 $account_data = $body['data'];
681
682 $this->save_account_information( $account_data );
683
684 // update the option to track that we are using stripe connect.
685 update_option( 'charitable_using_stripe_connect', time() );
686
687 /**
688 * Allow further processing after connecting a Stripe account.
689 *
690 * @since 3.6.0
691 *
692 * @param array $data Stripe response data.
693 */
694 do_action( 'wpcharitable_stripe_account_connected', $account_data );
695
696 // If you configure Stripe and the offline gateway is the default gateway, after you have connected successfully with Stripe, it should automatically be set as the default gateway.
697 if ( false === $this->get_default_gateway() || 'offline' === $this->get_default_gateway() ) {
698 $this->set_default_gateway( 'stripe' );
699 }
700
701 wp_redirect( esc_url_raw( $customer_site_url ) );
702 exit;
703 }
704
705 /**
706 * Saves the account information sent back from Stripe, alongside other, to identify the connected account.
707 *
708 * @since 4.4.2
709 *
710 * @param array<string> $data Stripe oAuth account data.
711 * @return void
712 */
713 private function save_account_information( $data, $gateway_id = 'stripe' ) {
714
715 $prefix = $this->in_test_mode()
716 ? 'test'
717 : 'live';
718
719 $settings = get_option( 'charitable_settings' );
720
721 $settings['gateways_' . $gateway_id ]['live_secret_key'] = '';
722 $settings['gateways_' . $gateway_id ]['live_public_key'] = '';
723 $settings['gateways_' . $gateway_id ]['test_secret_key'] = '';
724 $settings['gateways_' . $gateway_id ]['test_public_key'] = '';
725
726 $settings['gateways_' . $gateway_id ][ $prefix . '_secret_key' ] = isset( $data['secret_key'] ) ? sanitize_text_field( $data['secret_key'] ) : false;
727 $settings['gateways_' . $gateway_id ][ $prefix . '_public_key' ] = isset( $data['publishable_key'] ) ? sanitize_text_field( $data['publishable_key'] ) : false;
728
729 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) {
730 error_log( 'save_account_information' );
731 error_log( print_r( $settings, true ) );
732 }
733
734 update_option( 'charitable_settings', $settings );
735
736 charitable_get_admin_notices()->add_success( __( 'Stripe Information Updated', 'charitable' ) );
737
738 do_action( 'charitable_save_account_information_gateway_' . $gateway_id, $settings, $data, $gateway_id );
739
740 }
741
742 /**
743 * Disconnects from Stripe by removing associated account information.
744 *
745 * This does not deauthorize the application within the Stripe account.
746 *
747 * @since 4.2.2
748 *
749 * @return void
750 */
751 public function disconnect_gateways() {
752
753 // Do not need to handle this request, bail.
754 if (
755 ! ( isset( $_GET['page'] ) && 'charitable-settings' === $_GET['page'] ) ||
756 ! isset( $_GET['wpcharitable-stripe-disconnect'] )
757 ) {
758 return;
759 }
760
761 // Current user cannot handle this request.
762 if ( ! current_user_can( 'manage_options' ) ) {
763 return;
764 }
765
766 if ( ! isset( $_GET['_wpnonce'] ) ) {
767 return;
768 }
769
770 // Invalid nonce, bail.
771 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'wpcharitable-stripe-connect-disconnect' ) ) {
772 return;
773 }
774
775 $settings = get_option( 'charitable_settings' );
776
777 unset( $settings['gateways_stripe'][ 'live_secret_key' ] );
778 unset( $settings['gateways_stripe'][ 'live_public_key' ] );
779 unset( $settings['gateways_stripe'][ 'test_secret_key' ] );
780 unset( $settings['gateways_stripe'][ 'test_public_key' ] );
781
782 update_option( 'charitable_settings', $settings );
783
784 // update the option to track that we are using stripe connect.
785 delete_option( 'charitable_using_stripe_connect' );
786
787 charitable_get_admin_notices()->add_success( __( 'Stripe Connected Removed', 'charitable' ) );
788
789 do_action( 'charitable_remove_connection_gateway_stripe', $settings );
790
791 $redirect_url = add_query_arg(
792 array(
793 'tab' => 'gateways',
794 'page' => 'charitable-settings',
795 'group' => 'gateways_stripe',
796 ),
797 admin_url( 'admin.php' )
798 );
799
800 wp_safe_redirect( $redirect_url );
801 exit;
802
803 }
804
805 /**
806 * Redirects to the gateway settings page once a gateway has been activated.
807 *
808 * @since 1.7.0
809 *
810 * @param string $gateway Gateway ID.
811 * @return void
812 */
813 public function redirect_gateway_settings( $gateway ) {
814
815 $settings_url = add_query_arg( array(
816 'group' => 'gateways_' . $gateway,
817 ), admin_url( 'admin.php?page=charitable-settings&tab=gateways' ) );
818
819 wp_safe_redirect( $settings_url );
820 exit;
821 }
822
823 /**
824 * Sort the active gateways, placing the default gateway first.
825 *
826 * @since 1.4.0
827 *
828 * @param string $a Gateway to compare.
829 * @param string $b Gateway to compare against.
830 * @return int
831 */
832 protected function sort_by_default( $a, $b ) {
833 $default = $this->get_default_gateway();
834
835 if ( $a == $default ) {
836 return -1;
837 }
838
839 if ( $b == $default ) {
840 return 1;
841 }
842
843 return 0;
844 }
845
846 /**
847 * Ensure there is a settings option when landing on settings pages, even if init blank. Mostly applicable to new installs.
848 *
849 * @since 1.4.0
850 *
851 */
852 public function check_settings() {
853
854 if ( isset( $_GET['page'] ) && 'charitable-settings' !== $_GET['page'] ) {
855 return;
856 }
857
858 $settings = get_option( 'charitable_settings' );
859
860 // attach this to CHARITABLE_DEBUG constant.
861 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) {
862 error_log( 'checking_settings');
863 error_log( print_r( $settings, true ) );
864 }
865
866 if ( false === $settings ||( is_array( $settings ) && empty( $settings ) ) ) {
867 $settings_blank_slate = array( 'default_gateway' => 'stripe' ); // settings are brand new and include setting the default gateway
868 update_option( 'charitable_settings', $settings_blank_slate );
869 }
870
871 }
872
873
874 /**
875 * Display the test and live fields that represent the manual keys for Stripe that users might be already using prior to 1.7.0.
876 *
877 * @since 1.7.0
878 *
879 */
880 public function show_stripe_manual_keys_ajax() {
881
882 if ( ! isset( $_POST ) || 'charitable-show-stripe-keys' !== $_POST['action'] ) {
883 return;
884 }
885
886 $settings = get_option( 'charitable_settings' );
887
888 if ( false === $settings ) {
889 return;
890 }
891
892 $gateway_id = 'stripe';
893
894 $live_secret_key = $settings['gateways_' . $gateway_id ][ 'live_secret_key' ];
895 $live_public_key = $settings['gateways_' . $gateway_id ][ 'live_public_key' ];
896 $test_secret_key = $settings['gateways_' . $gateway_id ][ 'test_secret_key' ];
897 $test_public_key = $settings['gateways_' . $gateway_id ][ 'test_public_key' ];
898
899 $html = '<tr><th scope="row">Live Settings</th><td><hr />
900 </td></tr><tr class="wide"><th scope="row">Live Secret Key</th><td><input type="text"
901 id="charitable_settings_gateways_stripe_live_secret_key"
902 name="charitable_settings[gateways_stripe][live_secret_key]"
903 value="' . trim( $live_secret_key ) . '"
904 class="charitable-settings-field wide"
905 />
906 </td></tr><tr class="wide"><th scope="row">Live Publishable Key</th><td><input type="text"
907 id="charitable_settings_gateways_stripe_live_public_key"
908 name="charitable_settings[gateways_stripe][live_public_key]"
909 value="' . trim( $live_public_key ) . '"
910 class="charitable-settings-field wide"
911 />
912 </td></tr><tr><th scope="row">Test Settings</th><td><hr />
913 </td></tr><tr class="wide"><th scope="row">Test Secret Key</th><td><input type="text"
914 id="charitable_settings_gateways_stripe_test_secret_key"
915 name="charitable_settings[gateways_stripe][test_secret_key]"
916 value="' . trim( $test_secret_key ) . '"
917 class="charitable-settings-field wide"
918 />
919 </td></tr><tr class="wide"><th scope="row">Test Publishable Key</th><td><input type="text"
920 id="charitable_settings_gateways_stripe_test_public_key"
921 name="charitable_settings[gateways_stripe][test_public_key]"
922 value="' . trim( $test_public_key ) . '"
923 class="charitable-settings-field wide"
924 />
925 </td></tr>';
926
927 wp_send_json_success( $html );
928
929 }
930
931
932 }
933
934 endif;
935