PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
1.8.13.1 1.8.13 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 All 212 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.11, at includes/gateways/class-charitable-gateways.php

937 lines 26.1 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 if ( isset( $active_gateways['stripe'] ) ) {
200 $active_gateways['stripe'] = 'Charitable_Gateway_Stripe_AM';
201 }
202
203 /**
204 * Filter the list of active gateways.
205 *
206 * @since 1.0.0
207 *
208 * @param array $gateways Active gateways.
209 */
210 return apply_filters( 'charitable_active_gateways', $active_gateways );
211 }
212
213 /**
214 * Returns an array of the active gateways, in ID => name format.
215 *
216 * This is useful for select/radio input fields.
217 *
218 * @since 1.0.0
219 *
220 * @return string[]
221 */
222 public function get_gateway_choices() {
223 $gateways = array();
224
225 foreach ( $this->get_active_gateways() as $id => $class ) {
226 $gateway = new $class;
227 $gateways[ $id ] = $gateway->get_label();
228 }
229
230 return $gateways;
231 }
232
233 /**
234 * Returns a text description of the active gateways.
235 *
236 * @since 1.3.0
237 *
238 * @return string[]
239 */
240 public function get_active_gateways_names() {
241 $gateways = array();
242
243 foreach ( $this->get_active_gateways() as $id => $class ) {
244 $gateway = new $class;
245 $gateways[] = $gateway->get_name();
246 }
247
248 return $gateways;
249 }
250
251 /**
252 * Return the gateway class name for a given gateway.
253 *
254 * @since 1.0.0
255 *
256 * @param string $gateway Gateway ID.
257 * @return string|false
258 */
259 public function get_gateway( $gateway ) {
260 return isset( $this->gateways[ $gateway ] ) ? $this->gateways[ $gateway ] : false;
261 }
262
263 /**
264 * Return the gateway object for a given gateway.
265 *
266 * @since 1.0.0
267 *
268 * @param string $gateway Gateway ID.
269 * @return Charitable_Gateway|null
270 */
271 public function get_gateway_object( $gateway ) {
272 $class = $this->get_gateway( $gateway );
273 $object = $class ? new $class : null;
274
275 /**
276 * Filter the gateway object.
277 *
278 * @since 1.6.30
279 *
280 * @param Charitable_Gateway|null $object The gateway object.
281 */
282 return apply_filters( 'charitable_gateway_object_' . $gateway, $object, $gateway );
283 }
284
285 /**
286 * Returns whether the passed gateway is active.
287 *
288 * @since 1.0.0
289 *
290 * @param string $gateway_id Gateway ID.
291 * @return boolean
292 */
293 public function is_active_gateway( $gateway_id ) {
294 return array_key_exists( $gateway_id, $this->get_active_gateways() );
295 }
296
297 /**
298 * Checks whether the submitted gateway is valid.
299 *
300 * @since 1.4.3
301 *
302 * @param string $gateway Gateway ID.
303 * @return boolean
304 */
305 public function is_valid_gateway( $gateway ) {
306 /**
307 * Validate a particular gatewya.
308 *
309 * @since 1.4.3
310 *
311 * @param boolean $valid Whether a gateway is valid.
312 * @param string $gateway The gateway ID.
313 */
314
315 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) {
316 error_log( 'is_valid_gateway' );
317 error_log( print_r( $gateway, true ) );
318 error_log( print_r( $this->gateways, true ) );
319 }
320
321 return apply_filters( 'charitable_is_valid_gateway', array_key_exists( $gateway, $this->gateways ), $gateway );
322 }
323
324 /**
325 * Returns the default gateway.
326 *
327 * @since 1.0.0
328 *
329 * @return string
330 */
331 public function get_default_gateway() {
332 return charitable_get_option( 'default_gateway', '' );
333 }
334
335 /**
336 * Provide default gateway settings fields.
337 *
338 * @since 1.0.0
339 *
340 * @param array $settings Gateway settings.
341 * @param Charitable_Gateway $gateway The gateway's helper object.
342 * @return array
343 */
344 public function register_gateway_settings( $settings, Charitable_Gateway $gateway ) {
345 add_filter( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), array( $gateway, 'default_gateway_settings' ), 5 );
346 add_filter( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), array( $gateway, 'gateway_settings' ), 15 );
347
348 /**
349 * Filter the settings to show for a particular gateway.
350 *
351 * @since 1.0.0
352 *
353 * @param array $settings Gateway settings.
354 */
355 return apply_filters( 'charitable_settings_fields_gateways_gateway_' . $gateway->get_gateway_id(), $settings );
356 }
357
358 /**
359 * Returns true if test mode is enabled.
360 *
361 * @since 1.0.0
362 *
363 * @return boolean
364 */
365 public function in_test_mode() {
366 $enabled = charitable_get_option( 'test_mode', false );
367
368 /**
369 * Return whether Charitable is in test mode.
370 *
371 * @since 1.0.0
372 *
373 * @param boolean $enabled Whether test mode is on.
374 */
375 return apply_filters( 'charitable_in_test_mode', $enabled );
376 }
377
378 /**
379 * Checks whether all of the active gateways support a feature.
380 *
381 * If ANY gateway doesn't support the feature, this returns false.
382 *
383 * @since 1.4.0
384 *
385 * @param string $feature Feature to search for.
386 * @return boolean
387 */
388 public function all_gateways_support( $feature ) {
389 foreach ( $this->get_active_gateways() as $gateway_id => $gateway_class ) {
390
391 $gateway_object = new $gateway_class;
392
393 if ( false === $gateway_object->supports( $feature ) ) {
394 return false;
395 }
396 }
397
398 return true;
399 }
400
401 /**
402 * Checks whether any of the active gateways support a feature.
403 *
404 * If any gateway supports the feature, this returns true. Otherwise false.
405 *
406 * @since 1.4.0
407 *
408 * @param string $feature Feature to check for.
409 * @return boolean
410 */
411 public function any_gateway_supports( $feature ) {
412 foreach ( $this->get_active_gateways() as $gateway_id => $gateway_class ) {
413
414 $gateway_object = new $gateway_class;
415
416 if ( true === $gateway_object->supports( $feature ) ) {
417 return true;
418 }
419 }
420
421 return false;
422 }
423
424 /**
425 * Checks whether all of the active gateways support AJAX.
426 *
427 * If ANY gateway doesn't support AJAX, this returns false.
428 *
429 * @since 1.3.0
430 *
431 * @return boolean
432 */
433 public function gateways_support_ajax() {
434 return $this->all_gateways_support( '1.3.0' );
435 }
436
437 /**
438 * Return an array of recommended gateways for the current site.
439 *
440 * Note that this will only return gateways that are not already
441 * available on the site. i.e. If you have Stripe installed, it
442 * will not suggest that.
443 *
444 * @since 1.5.0
445 *
446 * @return array
447 */
448 public function get_recommended_gateways() {
449 $available = $this->get_available_gateways();
450 $gateways = array(
451 'payfast' => __( 'Payfast', 'charitable' ),
452 'paystack' => __( 'Paystack', 'charitable' ),
453 'stripe' => __( 'Stripe', 'charitable' ),
454 'authorize_net' => __( 'Authorize.Net', 'charitable' ),
455 'windcave' => __( 'Windcave', 'charitable' ),
456 'braintree' => __( 'Braintree', 'charitable' ),
457 'mollie' => __( 'Mollie', 'charitable' ),
458 'gocardless' => __( 'GoCardless', 'charitable' ),
459 'payrexx' => __( 'Payrexx', 'charitable' ),
460 );
461
462 /* If the user has already enabled one of these, leave them alone. :) */
463 foreach ( $gateways as $gateway_id => $gateway ) {
464 if ( array_key_exists( $gateway_id, $available ) ) {
465 return array();
466 }
467 }
468
469 $currency = charitable_get_default_currency();
470 $locale = get_locale();
471
472 if ( 'en_ZA' == $locale || 'ZAR' == $currency ) {
473 return charitable_array_subset( $gateways, array( 'payfast', 'paystack' ) );
474 }
475
476 if ( in_array( $currency, array( 'NGN', 'GHS' ) ) ) {
477 return charitable_array_subset( $gateways, array( 'paystack' ) );
478 }
479
480 if ( in_array( $locale, array( 'en_NZ', 'en_AU', 'en_GB' ) ) || in_array( $currency, array( 'NZD', 'AUD', 'GBP' ) ) ) {
481 return charitable_array_subset( $gateways, array( 'stripe', 'gocardless' ) );
482 }
483
484 if ( in_array( $locale, array( 'ms_MY', 'ja', 'zh_HK' ) ) || in_array( $currency, array( 'MYR', 'JPY', 'HKD' ) ) ) {
485 return charitable_array_subset( $gateways, array( 'stripe', 'windcave' ) );
486 }
487
488 if ( in_array( $locale, array( 'th' ) ) || in_array( $currency, array( 'BND', 'FJD', 'KWD', 'PGK', 'SBD', 'THB', 'TOP', 'VUV', 'WST' ) ) ) {
489 return charitable_array_subset( $gateways, array( 'windcave' ) );
490 }
491
492 if ( in_array( $currency, array( 'EUR' ) ) ) {
493 return charitable_array_subset( $gateways, array( 'stripe', 'mollie' ) );
494 }
495
496 if ( in_array( $currency, array( 'CHF', 'DKK' ) ) ) {
497 return charitable_array_subset( $gateways, array( 'stripe', 'payrexx' ) );
498 }
499
500 return charitable_array_subset( $gateways, array( 'stripe', 'braintree' ) );
501 }
502
503 /**
504 * Sets the default gateway.
505 *
506 * @since 1.0.0
507 *
508 * @param string $gateway Gateway ID.
509 * @return void
510 */
511 protected function set_default_gateway( $gateway ) {
512 $settings = get_option( 'charitable_settings' );
513
514 $settings['default_gateway'] = $gateway;
515
516 update_option( 'charitable_settings', $settings );
517
518 charitable_get_admin_notices()->add_success( __( 'Default Gateway Updated', 'charitable' ) );
519
520 do_action( 'charitable_set_gateway_gateway', $gateway );
521 }
522
523 /**
524 * Enable a payment gateway.
525 *
526 * @since 1.0.0
527 *
528 * @param string $gateway Gateway ID.
529 * @return void
530 */
531 protected function enable_gateway( $gateway ) {
532 $settings = get_option( 'charitable_settings' );
533
534 $active_gateways = isset( $settings['active_gateways'] ) ? $settings['active_gateways'] : array();
535 $active_gateways[ $gateway ] = $this->gateways[ $gateway ];
536 $settings['active_gateways'] = $active_gateways;
537
538 /* If this is the only gateway, make it the default gateway */
539 if ( 1 == count( $settings['active_gateways'] ) ) {
540 $settings['default_gateway'] = $gateway;
541 }
542
543 update_option( 'charitable_settings', $settings );
544
545 Charitable_Settings::get_instance()->add_update_message( __( 'Gateway enabled', 'charitable' ), 'success' );
546
547 /**
548 * Do something when a payment gateway is enabled.
549 *
550 * @since 1.0.0
551 *
552 * @param string $gateway The payment gateway.
553 */
554 do_action( 'charitable_gateway_enable', $gateway );
555 }
556
557 /**
558 * Disable a payment gateway.
559 *
560 * @since 1.0.0
561 *
562 * @param string $gateway Gateway ID.
563 * @return void
564 */
565 protected function disable_gateway( $gateway ) {
566 $settings = get_option( 'charitable_settings' );
567
568 if ( ! isset( $settings['active_gateways'][ $gateway ] ) ) {
569 return;
570 }
571
572 unset( $settings['active_gateways'][ $gateway ] );
573
574 /* Set a new default gateway */
575 if ( $gateway == $this->get_default_gateway() ) {
576
577 $settings['default_gateway'] = count( $settings['active_gateways'] ) ? key( $settings['active_gateways'] ) : '';
578
579 }
580
581 update_option( 'charitable_settings', $settings );
582
583 Charitable_Settings::get_instance()->add_update_message( __( 'Gateway disabled', 'charitable' ), 'success' );
584
585 /**
586 * Do something when a payment gateway is disabled.
587 *
588 * @since 1.0.0
589 *
590 * @param string $gateway The payment gateway.
591 */
592 do_action( 'charitable_gateway_disable', $gateway );
593 }
594
595 /**
596 * Connects to Stripe by saving account information passed back to the plugin.
597 *
598 * @since 4.2.2
599 *
600 * @return void
601 */
602 public function connect_gateways() {
603 // Current user cannot handle this request.
604 if ( ! current_user_can( 'manage_options' ) ) {
605 return;
606 }
607
608 // Do not need to handle this request, bail.
609 if (
610 ! isset( $_GET['wpcharitable_gateway_connect_completion'] ) ||
611 'stripe_connect' !== $_GET['wpcharitable_gateway_connect_completion'] ||
612 ! isset( $_GET['state'] )
613 ) {
614 return;
615 }
616
617 // Unable to redirect, bail.
618 if ( headers_sent() ) {
619 return;
620 }
621
622 if ( isset( $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] ) ) {
623 $current_url = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
624 } else {
625 $current_url = '';
626 }
627
628 $customer_site_url = remove_query_arg(
629 array(
630 'state',
631 'wpcharitable_gateway_connect_completion'
632 ),
633 $current_url
634 );
635
636 $wpcharitable_credentials_url = add_query_arg(
637 array(
638 'live_mode' => (int) ! charitable_get_option( 'test_mode' ), // (int) ! $this->is_test_mode(),
639 'state' => sanitize_text_field( $_GET['state'] ),
640 'customer_site_url' => urlencode( $customer_site_url ),
641 ),
642 'https://wpcharitable.com/stripe-connect/?wpcharitable_gateway_connect_credentials=stripe_connect' // todo: filter market site url
643 );
644
645 $response = wp_remote_get( esc_url_raw( $wpcharitable_credentials_url ) ); // add add_filter('https_ssl_verify', '__return_false'); here if testing local
646
647 if (
648 is_wp_error( $response ) ||
649 200 !== wp_remote_retrieve_response_code( $response )
650 ) {
651
652 $stripe_account_settings_url = add_query_arg(
653 array(
654 'tab' => 'gateways',
655 'page' => 'charitable-settings',
656 'group' => 'gateways_stripe',
657 ),
658 admin_url( 'admin.php' )
659 );
660
661 $message = wpautop(
662 sprintf(
663 /* translators: %1$s Opening anchor tag, do not translate. %2$s Closing anchor tag, do not translate. */
664 __(
665 'There was an error getting your Stripe credentials. Please %1$stry again%2$s. If you continue to have this problem, please contact support.',
666 'stripe'
667 ),
668 '<a href="' . esc_url( $stripe_account_settings_url ) . '">',
669 '</a>'
670 )
671 );
672
673 wp_die( $message );
674 }
675
676 $body = wp_remote_retrieve_body( $response );
677
678 /** @var string $body */
679 $body = json_decode( $body, true );
680
681 /** @var array<array<string>> $body */
682 $account_data = $body['data'];
683
684 $this->save_account_information( $account_data );
685
686 // update the option to track that we are using stripe connect.
687 update_option( 'charitable_using_stripe_connect', time() );
688
689 /**
690 * Allow further processing after connecting a Stripe account.
691 *
692 * @since 3.6.0
693 *
694 * @param array $data Stripe response data.
695 */
696 do_action( 'wpcharitable_stripe_account_connected', $account_data );
697
698 // 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.
699 if ( false === $this->get_default_gateway() || 'offline' === $this->get_default_gateway() ) {
700 $this->set_default_gateway( 'stripe' );
701 }
702
703 wp_redirect( esc_url_raw( $customer_site_url ) );
704 exit;
705 }
706
707 /**
708 * Saves the account information sent back from Stripe, alongside other, to identify the connected account.
709 *
710 * @since 4.4.2
711 *
712 * @param array<string> $data Stripe oAuth account data.
713 * @return void
714 */
715 private function save_account_information( $data, $gateway_id = 'stripe' ) {
716
717 $prefix = $this->in_test_mode()
718 ? 'test'
719 : 'live';
720
721 $settings = get_option( 'charitable_settings' );
722
723 $settings['gateways_' . $gateway_id ]['live_secret_key'] = '';
724 $settings['gateways_' . $gateway_id ]['live_public_key'] = '';
725 $settings['gateways_' . $gateway_id ]['test_secret_key'] = '';
726 $settings['gateways_' . $gateway_id ]['test_public_key'] = '';
727
728 $settings['gateways_' . $gateway_id ][ $prefix . '_secret_key' ] = isset( $data['secret_key'] ) ? sanitize_text_field( $data['secret_key'] ) : false;
729 $settings['gateways_' . $gateway_id ][ $prefix . '_public_key' ] = isset( $data['publishable_key'] ) ? sanitize_text_field( $data['publishable_key'] ) : false;
730
731 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) {
732 error_log( 'save_account_information' );
733 error_log( print_r( $settings, true ) );
734 }
735
736 update_option( 'charitable_settings', $settings );
737
738 charitable_get_admin_notices()->add_success( __( 'Stripe Information Updated', 'charitable' ) );
739
740 do_action( 'charitable_save_account_information_gateway_' . $gateway_id, $settings, $data, $gateway_id );
741
742 }
743
744 /**
745 * Disconnects from Stripe by removing associated account information.
746 *
747 * This does not deauthorize the application within the Stripe account.
748 *
749 * @since 4.2.2
750 *
751 * @return void
752 */
753 public function disconnect_gateways() {
754
755 // Do not need to handle this request, bail.
756 if (
757 ! ( isset( $_GET['page'] ) && 'charitable-settings' === $_GET['page'] ) ||
758 ! isset( $_GET['wpcharitable-stripe-disconnect'] )
759 ) {
760 return;
761 }
762
763 // Current user cannot handle this request.
764 if ( ! current_user_can( 'manage_options' ) ) {
765 return;
766 }
767
768 if ( ! isset( $_GET['_wpnonce'] ) ) {
769 return;
770 }
771
772 // Invalid nonce, bail.
773 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'wpcharitable-stripe-connect-disconnect' ) ) {
774 return;
775 }
776
777 $settings = get_option( 'charitable_settings' );
778
779 unset( $settings['gateways_stripe'][ 'live_secret_key' ] );
780 unset( $settings['gateways_stripe'][ 'live_public_key' ] );
781 unset( $settings['gateways_stripe'][ 'test_secret_key' ] );
782 unset( $settings['gateways_stripe'][ 'test_public_key' ] );
783
784 update_option( 'charitable_settings', $settings );
785
786 // update the option to track that we are using stripe connect.
787 delete_option( 'charitable_using_stripe_connect' );
788
789 charitable_get_admin_notices()->add_success( __( 'Stripe Connected Removed', 'charitable' ) );
790
791 do_action( 'charitable_remove_connection_gateway_stripe', $settings );
792
793 $redirect_url = add_query_arg(
794 array(
795 'tab' => 'gateways',
796 'page' => 'charitable-settings',
797 'group' => 'gateways_stripe',
798 ),
799 admin_url( 'admin.php' )
800 );
801
802 wp_safe_redirect( $redirect_url );
803 exit;
804
805 }
806
807 /**
808 * Redirects to the gateway settings page once a gateway has been activated.
809 *
810 * @since 1.7.0
811 *
812 * @param string $gateway Gateway ID.
813 * @return void
814 */
815 public function redirect_gateway_settings( $gateway ) {
816
817 $settings_url = add_query_arg( array(
818 'group' => 'gateways_' . $gateway,
819 ), admin_url( 'admin.php?page=charitable-settings&tab=gateways' ) );
820
821 wp_safe_redirect( $settings_url );
822 exit;
823 }
824
825 /**
826 * Sort the active gateways, placing the default gateway first.
827 *
828 * @since 1.4.0
829 *
830 * @param string $a Gateway to compare.
831 * @param string $b Gateway to compare against.
832 * @return int
833 */
834 protected function sort_by_default( $a, $b ) {
835 $default = $this->get_default_gateway();
836
837 if ( $a == $default ) {
838 return -1;
839 }
840
841 if ( $b == $default ) {
842 return 1;
843 }
844
845 return 0;
846 }
847
848 /**
849 * Ensure there is a settings option when landing on settings pages, even if init blank. Mostly applicable to new installs.
850 *
851 * @since 1.4.0
852 *
853 */
854 public function check_settings() {
855
856 if ( isset( $_GET['page'] ) && 'charitable-settings' !== $_GET['page'] ) {
857 return;
858 }
859
860 $settings = get_option( 'charitable_settings' );
861
862 // attach this to CHARITABLE_DEBUG constant.
863 if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG && defined( 'CHARITABLE_DEBUG_SETTINGS' ) && CHARITABLE_DEBUG_SETTINGS ) {
864 error_log( 'checking_settings');
865 error_log( print_r( $settings, true ) );
866 }
867
868 if ( false === $settings ||( is_array( $settings ) && empty( $settings ) ) ) {
869 $settings_blank_slate = array( 'default_gateway' => 'stripe' ); // settings are brand new and include setting the default gateway
870 update_option( 'charitable_settings', $settings_blank_slate );
871 }
872
873 }
874
875
876 /**
877 * Display the test and live fields that represent the manual keys for Stripe that users might be already using prior to 1.7.0.
878 *
879 * @since 1.7.0
880 *
881 */
882 public function show_stripe_manual_keys_ajax() {
883
884 if ( ! isset( $_POST ) || 'charitable-show-stripe-keys' !== $_POST['action'] ) {
885 return;
886 }
887
888 $settings = get_option( 'charitable_settings' );
889
890 if ( false === $settings ) {
891 return;
892 }
893
894 $gateway_id = 'stripe';
895
896 $live_secret_key = $settings['gateways_' . $gateway_id ][ 'live_secret_key' ];
897 $live_public_key = $settings['gateways_' . $gateway_id ][ 'live_public_key' ];
898 $test_secret_key = $settings['gateways_' . $gateway_id ][ 'test_secret_key' ];
899 $test_public_key = $settings['gateways_' . $gateway_id ][ 'test_public_key' ];
900
901 $html = '<tr><th scope="row">Live Settings</th><td><hr />
902 </td></tr><tr class="wide"><th scope="row">Live Secret Key</th><td><input type="text"
903 id="charitable_settings_gateways_stripe_live_secret_key"
904 name="charitable_settings[gateways_stripe][live_secret_key]"
905 value="' . trim( $live_secret_key ) . '"
906 class="charitable-settings-field wide"
907 />
908 </td></tr><tr class="wide"><th scope="row">Live Publishable Key</th><td><input type="text"
909 id="charitable_settings_gateways_stripe_live_public_key"
910 name="charitable_settings[gateways_stripe][live_public_key]"
911 value="' . trim( $live_public_key ) . '"
912 class="charitable-settings-field wide"
913 />
914 </td></tr><tr><th scope="row">Test Settings</th><td><hr />
915 </td></tr><tr class="wide"><th scope="row">Test Secret Key</th><td><input type="text"
916 id="charitable_settings_gateways_stripe_test_secret_key"
917 name="charitable_settings[gateways_stripe][test_secret_key]"
918 value="' . trim( $test_secret_key ) . '"
919 class="charitable-settings-field wide"
920 />
921 </td></tr><tr class="wide"><th scope="row">Test Publishable Key</th><td><input type="text"
922 id="charitable_settings_gateways_stripe_test_public_key"
923 name="charitable_settings[gateways_stripe][test_public_key]"
924 value="' . trim( $test_public_key ) . '"
925 class="charitable-settings-field wide"
926 />
927 </td></tr>';
928
929 wp_send_json_success( $html );
930
931 }
932
933
934 }
935
936 endif;
937