PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.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.8.0, at includes/gateways/class-charitable-gateways.php

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