WooCommerce PayPal Powered by Braintree Payment Gateway - Version 1.1.0

Version Description

  • Fixed a bug which would cause the gateway settings to report that the gateway was enabled when it actually was not fully enabled.
  • Updated contributors list
Download this release

Release Info

Developer allendav
Plugin Icon 128x128 WooCommerce PayPal Powered by Braintree Payment Gateway
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.0 to 1.1.0

classes/class-wc-gateway-paypal-braintree-subscription.php CHANGED
@@ -72,7 +72,7 @@ abstract class WC_Gateway_Paypal_Braintree_Subscription extends WC_Gateway_Paypa
72
  $paypal_braintree_nonce = self::get_posted_variable( 'paypalbraintree_nonce' );
73
  if ( empty( $paypal_braintree_nonce ) ) {
74
  $this->log( __FUNCTION__, "Error: The paypal_braintree_nonce was unexpectedly empty" );
75
- wc_add_notice( __( 'Error: PayPal Powered by Braintree did not supply a payment nonce. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree' ), 'error' );
76
  return false;
77
  }
78
 
@@ -93,7 +93,7 @@ abstract class WC_Gateway_Paypal_Braintree_Subscription extends WC_Gateway_Paypa
93
  $braintree_customer_id = get_user_meta( $user_id, '_wc_paypal_braintree_customer_id', true );
94
  if ( empty( $braintree_customer_id ) ) {
95
 
96
- $this->log( __FUNCTION__, "Info: Did not find braintree customer id on user meta. Need to create customer" );
97
 
98
  // Create a new customer id, passing the nonce so we can add the card to the vault
99
  // ref https://developers.braintreepayments.com/reference/request/customer/create/php
@@ -110,7 +110,7 @@ abstract class WC_Gateway_Paypal_Braintree_Subscription extends WC_Gateway_Paypa
110
 
111
  if ( ! $result->success ) {
112
  $this->log( __FUNCTION__, "Error: Unable to create customer: {$result->message}" );
113
- wc_add_notice( __( 'Error: PayPal Powered by Braintree was unable to create a customer record for you. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree' ), 'error' );
114
  return false;
115
  }
116
 
@@ -184,7 +184,7 @@ abstract class WC_Gateway_Paypal_Braintree_Subscription extends WC_Gateway_Paypa
184
  // We have a customer id now, so let's do the sale and store the payment method in the vault
185
  $result = $gateway->transaction()->sale( $sale_args );
186
  if ( ! $result->success ) {
187
- $notice = sprintf( __( 'Error: PayPal Powered by Braintree was unable to complete the transaction. Please try again later or use another means of payment. Reason: %s', 'woocommerce-gateway-paypal-braintree' ), $error_message );
188
  wc_add_notice( $notice, 'error' );
189
  $this->log( __FUNCTION__, "Error: Unable to complete transaction. Reason: {$result->message}" );
190
  return false;
72
  $paypal_braintree_nonce = self::get_posted_variable( 'paypalbraintree_nonce' );
73
  if ( empty( $paypal_braintree_nonce ) ) {
74
  $this->log( __FUNCTION__, "Error: The paypal_braintree_nonce was unexpectedly empty" );
75
+ wc_add_notice( __( 'Error: PayPal Powered by Braintree did not supply a payment nonce. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree' ), 'error' );
76
  return false;
77
  }
78
 
93
  $braintree_customer_id = get_user_meta( $user_id, '_wc_paypal_braintree_customer_id', true );
94
  if ( empty( $braintree_customer_id ) ) {
95
 
96
+ $this->log( __FUNCTION__, "Info: Did not find braintree customer id on user meta. Need to create customer" );
97
 
98
  // Create a new customer id, passing the nonce so we can add the card to the vault
99
  // ref https://developers.braintreepayments.com/reference/request/customer/create/php
110
 
111
  if ( ! $result->success ) {
112
  $this->log( __FUNCTION__, "Error: Unable to create customer: {$result->message}" );
113
+ wc_add_notice( __( 'Error: PayPal Powered by Braintree was unable to create a customer record for you. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree' ), 'error' );
114
  return false;
115
  }
116
 
184
  // We have a customer id now, so let's do the sale and store the payment method in the vault
185
  $result = $gateway->transaction()->sale( $sale_args );
186
  if ( ! $result->success ) {
187
+ $notice = sprintf( __( 'Error: PayPal Powered by Braintree was unable to complete the transaction. Please try again later or use another means of payment. Reason: %s', 'woocommerce-gateway-paypal-braintree' ), $error_message );
188
  wc_add_notice( $notice, 'error' );
189
  $this->log( __FUNCTION__, "Error: Unable to complete transaction. Reason: {$result->message}" );
190
  return false;
classes/class-wc-gateway-paypal-braintree.php CHANGED
@@ -24,10 +24,16 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
24
  public $checkout_template = '';
25
 
26
  /**
27
- * Connection notices (array)
28
- * @var array
 
29
  */
30
- public $notices = [];
 
 
 
 
 
31
 
32
  /**
33
  * Constructor
@@ -36,6 +42,8 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
36
 
37
  $this->icon = false;
38
  $this->has_fields = true;
 
 
39
 
40
  $this->method_title = __( 'PayPal Powered by Braintree', 'woocommerce-gateway-paypal-braintree' );
41
  $this->method_description = sprintf(
@@ -51,7 +59,7 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
51
  $just_disconnected = $this->possibly_discard_access_token();
52
 
53
  // After the merchant uses the Connect to Braintree button, they will
54
- // have an access token we can use. Note - this is stored in WP options
55
  $this->merchant_access_token = get_option( 'wc_paypal_braintree_merchant_access_token', '' );
56
  $this->merchant_id = get_option( 'wc_paypal_braintree_merchant_id', '' );
57
  $this->testmode = get_option( 'wc_paypal_braintree_environment', 'sandbox' ) === 'sandbox';
@@ -59,28 +67,25 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
59
  $this->init_form_fields();
60
  $this->init_settings();
61
 
62
- $this->debug = $this->get_option( 'debug' ) === 'yes';
63
- $this->title = '';
64
- $this->description = '';
65
 
66
  // Now that $this->debug is set, we can use logging
67
  if ( $just_connected ) {
68
- $this->log( __FUNCTION__, "Info: Connected to PayPal Braintree successfully. Merchant ID = {$this->merchant_id}" );
69
  }
70
 
71
  if ( $just_disconnected ) {
72
  $this->log( __FUNCTION__, "Info: Disconnected from PayPal Braintree." );
73
  }
74
 
75
- if ( ! $this->is_valid_for_use() ) {
76
- $this->enabled = 'no';
77
- }
78
 
79
  add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
80
  add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
81
- add_action( 'admin_notices', array( $this, 'admin_notices' ) );
82
 
83
  if ( ! $this->is_valid_for_use() ) {
 
84
  return;
85
  }
86
 
@@ -93,7 +98,7 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
93
 
94
 
95
  /**
96
- * If we see an access token, save it and add a notice. Returns true on successful connection
97
  *
98
  * @return bool
99
  * @since 1.0.0
@@ -145,9 +150,10 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
145
  $environment = $gateway->config->getEnvironment(); // sandbox or production
146
  update_option( 'wc_paypal_braintree_environment', $environment );
147
 
148
- $this->notices[] = array(
149
- 'class' => 'updated',
150
- 'message' => __( 'Connected successfully.', 'woocommerce-gateway-paypal-braintree' )
 
151
  );
152
 
153
  return true;
@@ -188,9 +194,10 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
188
  delete_option( 'wc_paypal_braintree_merchant_access_token' );
189
  delete_option( 'wc_paypal_braintree_merchant_id' );
190
 
191
- $this->notices[] = array(
192
- 'class' => 'updated',
193
- 'message' => __( 'Disconnected successfully.', 'woocommerce-gateway-paypal-braintree' )
 
194
  );
195
 
196
  return true;
@@ -315,42 +322,47 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
315
 
316
 
317
  /**
318
- * Check for required settings, and if SSL is enabled
 
319
  */
320
  public function admin_notices() {
321
 
322
- // Display any notices we've collected thus far (e.g. for connection, disconnection)
323
- foreach ( (array) $this->notices as $notice ) {
324
- echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
325
- echo esc_html( $notice['message'] ) ;
326
- echo "</p></div>";
327
- }
328
 
329
- // Next, if the gateway is enabled, check for required settings
330
- if ( $this->enabled == 'no' ) {
331
- return;
332
- }
333
 
334
- $general_settings_url = admin_url( 'admin.php?page=wc-settings' );
335
- $checkout_settings_url = admin_url( 'admin.php?page=wc-settings&tab=checkout' );
336
- $gateway_settings_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wc_gateway_paypal_braintree' );
 
 
 
 
 
337
 
338
- // Check for access token
339
- if ( empty( $this->merchant_access_token ) ) {
340
- echo '<div class="error"><p>' . sprintf( __( 'PayPal Powered by Braintree error: Please complete connecting <a href="%s">here</a>', 'woocommerce-gateway-paypal-braintree' ), $gateway_settings_url ) . '</p></div>';
341
- return;
342
- }
 
 
 
343
 
344
- // Check Currency
345
- if ( ! $this->is_shop_currency_supported() ) {
346
- echo '<div class="error"><p>' . sprintf( __( "PayPal Powered by Braintree error: Your shop's currency is not supported. Please check and re-enter.", 'woocommerce-gateway-paypal-braintree' ), $general_settings_url ) . '</p></div>';
347
- return;
348
- }
 
 
 
349
 
350
- // Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected
351
- if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'no' && ! class_exists( 'WordPressHTTPS' ) ) {
352
- echo '<div class="error"><p>' . sprintf( __( 'PayPal Powered by Braintree is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - PayPal Powered by Braintree will only work in test mode.', 'woocommerce-gateway-paypal-braintree' ), $checkout_settings_url ) . '</p></div>';
353
  }
 
354
  }
355
 
356
 
@@ -727,7 +739,7 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
727
  $paypal_braintree_nonce = self::get_posted_variable( 'paypalbraintree_nonce' );
728
  if ( empty( $paypal_braintree_nonce ) ) {
729
  $this->log( __FUNCTION__, "Error: The paypal_braintree_nonce was unexpectedly empty" );
730
- wc_add_notice( __( 'Error: PayPal Powered by Braintree did not supply a payment nonce. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree' ), 'error' );
731
  return false;
732
  }
733
 
@@ -778,7 +790,7 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
778
 
779
  // Check result
780
  if ( ! $result->success ) {
781
- $notice = sprintf( __( 'Error: PayPal Powered by Braintree was unable to complete the transaction. Please try again later or use another means of payment. Reason: %s', 'woocommerce-gateway-paypal-braintree' ), $result->message );
782
  wc_add_notice( $notice, 'error' );
783
  $this->log( __FUNCTION__, "Error: Unable to complete transaction. Reason: {$result->message}" );
784
  return false;
@@ -846,7 +858,7 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
846
  $order = wc_get_order( $order_id );
847
 
848
  if ( ! $this->can_refund_order( $order ) ) {
849
- $this->log( __FUNCTION__, "Error: Unable to refund/void order {$order_id}. Order has no transaction ID." );
850
  return false;
851
  }
852
 
@@ -1019,7 +1031,9 @@ abstract class WC_Gateway_Paypal_Braintree extends WC_Payment_Gateway {
1019
  * @since 1.0.0
1020
  */
1021
  protected function log( $context, $message ) {
1022
- WC_PayPal_Braintree_Loader::getInstance()->log( $context, $message );
 
 
1023
  }
1024
 
1025
  }
24
  public $checkout_template = '';
25
 
26
  /**
27
+ * Whether or not the gateway is enabled in wp-admin
28
+ * This is initialized from the option, but can differ from $this->enabled
29
+ * in the event the gateway is declared not-valid-for-use during construction.
30
  */
31
+ protected $enabled_original_setting = '';
32
+
33
+ /**
34
+ * Whether or not debug is enabled in wp-admin
35
+ */
36
+ protected $debug = false;
37
 
38
  /**
39
  * Constructor
42
 
43
  $this->icon = false;
44
  $this->has_fields = true;
45
+ $this->title = '';
46
+ $this->description = '';
47
 
48
  $this->method_title = __( 'PayPal Powered by Braintree', 'woocommerce-gateway-paypal-braintree' );
49
  $this->method_description = sprintf(
59
  $just_disconnected = $this->possibly_discard_access_token();
60
 
61
  // After the merchant uses the Connect to Braintree button, they will
62
+ // have an access token we can use. Note - this is stored in WP options
63
  $this->merchant_access_token = get_option( 'wc_paypal_braintree_merchant_access_token', '' );
64
  $this->merchant_id = get_option( 'wc_paypal_braintree_merchant_id', '' );
65
  $this->testmode = get_option( 'wc_paypal_braintree_environment', 'sandbox' ) === 'sandbox';
67
  $this->init_form_fields();
68
  $this->init_settings();
69
 
70
+ $this->debug = $this->get_option( 'debug' ) === 'yes';
71
+ $this->enabled_original_setting = $this->enabled;
 
72
 
73
  // Now that $this->debug is set, we can use logging
74
  if ( $just_connected ) {
75
+ $this->log( __FUNCTION__, "Info: Connected to PayPal Braintree successfully. Merchant ID = {$this->merchant_id}" );
76
  }
77
 
78
  if ( $just_disconnected ) {
79
  $this->log( __FUNCTION__, "Info: Disconnected from PayPal Braintree." );
80
  }
81
 
82
+ add_action( 'admin_notices', array( $this, 'admin_notices' ) );
 
 
83
 
84
  add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
85
  add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
 
86
 
87
  if ( ! $this->is_valid_for_use() ) {
88
+ $this->enabled = 'no';
89
  return;
90
  }
91
 
98
 
99
 
100
  /**
101
+ * If we see an access token, save it and add a notice. Returns true on successful connection
102
  *
103
  * @return bool
104
  * @since 1.0.0
150
  $environment = $gateway->config->getEnvironment(); // sandbox or production
151
  update_option( 'wc_paypal_braintree_environment', $environment );
152
 
153
+ WC_PayPal_Braintree_Loader::getInstance()->add_admin_notice(
154
+ 'connected_successfully',
155
+ 'updated',
156
+ __( 'Connected successfully.', 'woocommerce-gateway-paypal-braintree' )
157
  );
158
 
159
  return true;
194
  delete_option( 'wc_paypal_braintree_merchant_access_token' );
195
  delete_option( 'wc_paypal_braintree_merchant_id' );
196
 
197
+ WC_PayPal_Braintree_Loader::getInstance()->add_admin_notice(
198
+ 'disconnected_successfully',
199
+ 'updated',
200
+ __( 'Disconnected successfully.', 'woocommerce-gateway-paypal-braintree' )
201
  );
202
 
203
  return true;
322
 
323
 
324
  /**
325
+ * Check for required settings, and if SSL is enabled. We use a slug keyed array to avoid
326
+ * duplicate notices from the paypal and card concrete classes
327
  */
328
  public function admin_notices() {
329
 
330
+ // If the gateway is supposed to be enabled, check for required settings
331
+ if ( 'yes' === $this->enabled_original_setting ) {
 
 
 
 
332
 
333
+ $general_settings_url = add_query_arg( 'page', 'wc-settings', admin_url( 'admin.php' ) );
334
+ $checkout_settings_url = add_query_arg( 'tab', 'checkout', $general_settings_url );
335
+ $gateway_settings_url = add_query_arg( 'section', strtolower( get_class( $this ) ), $checkout_settings_url );
 
336
 
337
+ // Check for access token
338
+ if ( empty( $this->merchant_access_token ) ) {
339
+ WC_PayPal_Braintree_Loader::getInstance()->add_admin_notice(
340
+ 'merchant_access_token_empty',
341
+ 'error',
342
+ sprintf( __( 'The PayPal Powered by Braintree gateway is enabled but not connected. Please complete connecting <a href="%s">here</a>.', 'woocommerce-gateway-paypal-braintree' ), $gateway_settings_url )
343
+ );
344
+ }
345
 
346
+ // Check Currency
347
+ if ( ! $this->is_shop_currency_supported() ) {
348
+ WC_PayPal_Braintree_Loader::getInstance()->add_admin_notice(
349
+ 'shop_currency_not_supported',
350
+ 'error',
351
+ sprintf( __( 'PayPal Powered by Braintree error: Your shop\'s currency is not supported. Please check <a href="%s">here</a>.', 'woocommerce-gateway-paypal-braintree' ), $general_settings_url )
352
+ );
353
+ }
354
 
355
+ // Show message if enabled and FORCE SSL is disabled and WordpressHTTPS plugin is not detected
356
+ if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'no' && ! class_exists( 'WordPressHTTPS' ) ) {
357
+ WC_PayPal_Braintree_Loader::getInstance()->add_admin_notice(
358
+ 'ssl_lacking',
359
+ 'error',
360
+ sprintf( __( 'PayPal Powered by Braintree is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - PayPal Powered by Braintree will only work in test mode.', 'woocommerce-gateway-paypal-braintree' ), $checkout_settings_url )
361
+ );
362
+ }
363
 
 
 
 
364
  }
365
+
366
  }
367
 
368
 
739
  $paypal_braintree_nonce = self::get_posted_variable( 'paypalbraintree_nonce' );
740
  if ( empty( $paypal_braintree_nonce ) ) {
741
  $this->log( __FUNCTION__, "Error: The paypal_braintree_nonce was unexpectedly empty" );
742
+ wc_add_notice( __( 'Error: PayPal Powered by Braintree did not supply a payment nonce. Please try again later or use another means of payment.', 'woocommerce-gateway-paypal-braintree' ), 'error' );
743
  return false;
744
  }
745
 
790
 
791
  // Check result
792
  if ( ! $result->success ) {
793
+ $notice = sprintf( __( 'Error: PayPal Powered by Braintree was unable to complete the transaction. Please try again later or use another means of payment. Reason: %s', 'woocommerce-gateway-paypal-braintree' ), $result->message );
794
  wc_add_notice( $notice, 'error' );
795
  $this->log( __FUNCTION__, "Error: Unable to complete transaction. Reason: {$result->message}" );
796
  return false;
858
  $order = wc_get_order( $order_id );
859
 
860
  if ( ! $this->can_refund_order( $order ) ) {
861
+ $this->log( __FUNCTION__, "Error: Unable to refund/void order {$order_id}. Order has no transaction ID." );
862
  return false;
863
  }
864
 
1031
  * @since 1.0.0
1032
  */
1033
  protected function log( $context, $message ) {
1034
+ if ( $this->debug ) {
1035
+ WC_PayPal_Braintree_Loader::getInstance()->log( $context, $message );
1036
+ }
1037
  }
1038
 
1039
  }
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === WooCommerce PayPal Powered by Braintree Payment Gateway ===
2
- Contributors: automattic, woothemes, allendav
3
  Tags: ecommerce, e-commerce, commerce, woothemes, wordpress ecommerce, store, sales, sell, shop, shopping, cart, checkout, configurable, paypal, braintree
4
  Requires at least: 4.4
5
  Tested up to: 4.4
6
- Stable tag: 1.0.0
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
@@ -61,7 +61,7 @@ Yes!
61
 
62
  Yes!
63
 
64
- = Does this support both production mode and sandbox mode for testing?
65
 
66
  Yes it does - production and sandbox mode is driven by how you connect. You may choose to connect in either mode, and disconnect and reconnect in the other mode whenever you want.
67
 
@@ -92,5 +92,14 @@ New feature requests and bugs reports can be made in the plugin forum.
92
 
93
  == Changelog ==
94
 
 
 
 
 
 
 
 
 
 
95
  = 1.0.0 =
96
  * Initial release
1
  === WooCommerce PayPal Powered by Braintree Payment Gateway ===
2
+ Contributors: automattic, woothemes, allendav, slash1andy, woosteve, spraveenitpro, mikedmoore, fernashes, shellbeezy
3
  Tags: ecommerce, e-commerce, commerce, woothemes, wordpress ecommerce, store, sales, sell, shop, shopping, cart, checkout, configurable, paypal, braintree
4
  Requires at least: 4.4
5
  Tested up to: 4.4
6
+ Stable tag: 1.1.0
7
  License: GPLv3
8
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
9
 
61
 
62
  Yes!
63
 
64
+ = Does this support both production mode and sandbox mode for testing? =
65
 
66
  Yes it does - production and sandbox mode is driven by how you connect. You may choose to connect in either mode, and disconnect and reconnect in the other mode whenever you want.
67
 
92
 
93
  == Changelog ==
94
 
95
+ = 1.1.0 =
96
+ * Fixed a bug which would cause the gateway settings to report that the gateway was enabled when it actually was not fully enabled.
97
+ * Updated contributors list
98
+
99
+ = 1.0.1 =
100
+ * Remove duplicate SSL warnings
101
+ * Update environment check to also check after activation for environment problems
102
+ * Fix link in enabled-but-not-connected notice
103
+
104
  = 1.0.0 =
105
  * Initial release
woocommerce-gateway-paypal-powered-by-braintree.php CHANGED
@@ -1,11 +1,11 @@
1
  <?php
2
  /**
3
  * Plugin Name: WooCommerce PayPal Powered by Braintree Gateway
4
- * Plugin URI: http://woothemes.com/products/woocommerce-gateway-paypal-powered-by-braintree/
5
  * Description: Receive payments using Paypal Powered by Braintree. A server with cURL, SSL support, and a valid SSL certificate is required (for security reasons) for this gateway to function. Requires PHP 5.4+
6
  * Author: WooThemes
7
  * Author URI: http://woothemes.com/
8
- * Version: 1.0.0
9
  *
10
  * Copyright (c) 2015 WooThemes
11
  *
@@ -73,12 +73,27 @@ class WC_PayPal_Braintree_Loader {
73
  /** @var whether or not we need to load code for / support subscriptions */
74
  private $subscription_support_enabled = false;
75
 
 
 
 
 
 
 
76
  /**
77
  * Protected constructor to prevent creating a new instance of the
78
  * *Singleton* via the `new` operator from outside of this class.
79
  */
80
  protected function __construct() {
81
 
 
 
 
 
 
 
 
 
 
82
  add_action( 'plugins_loaded', array( $this, 'init_gateways' ), 0 );
83
 
84
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
@@ -94,6 +109,71 @@ class WC_PayPal_Braintree_Loader {
94
 
95
  }
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  /**
99
  * Adds plugin action links
@@ -119,8 +199,20 @@ class WC_PayPal_Braintree_Loader {
119
  return array_merge( $plugin_links, $links );
120
  }
121
 
 
122
  /**
123
- * Initialize the gateway. Called very early - in the context of the plugins_loaded action
 
 
 
 
 
 
 
 
 
 
 
124
  *
125
  * @since 1.0.0
126
  */
@@ -442,7 +534,7 @@ class WC_PayPal_Braintree_Loader {
442
 
443
 
444
  /**
445
- * Used when cart based Checkout with PayPal is in effect. Hooked to woocommerce_cart_emptied
446
  * Also called by WC_PayPal_Braintree_Loader::possibly_cancel_checkout_with_paypal
447
  *
448
  * @since 1.0.0
@@ -467,7 +559,7 @@ class WC_PayPal_Braintree_Loader {
467
  'label' => '',
468
  'type' => 'checkbox',
469
  'description' => __( 'This controls whether or not this gateway is enabled within WooCommerce.', 'woocommerce-gateway-paypal-braintree' ),
470
- 'default' => 'yes',
471
  'desc_tip' => true
472
  ),
473
  'title_paypal' => array(
@@ -554,7 +646,7 @@ class WC_PayPal_Braintree_Loader {
554
  $sections_to_remove = in_array( $current_section, $paypal_sections ) ? $card_sections : $paypal_sections;
555
 
556
  // And, let's also remove simplify commerce from the sections if it is not enabled and it is not the
557
- // current section. (Note: The option will be empty if it has never been enabled)
558
 
559
  $simplify_commerce_options = get_option( 'woocommerce_simplify_commerce_settings', array() );
560
  if ( empty( $simplify_commerce_options ) || ( "no" === $simplify_commerce_options['enabled'] ) ) {
@@ -598,22 +690,5 @@ class WC_PayPal_Braintree_Loader {
598
  }
599
  }
600
 
601
- register_activation_hook( __FILE__, 'wc_paypal_braintree_loader_activation' );
602
-
603
- function wc_paypal_braintree_loader_activation() {
604
- if ( version_compare( phpversion(), WC_PAYPAL_BRAINTREE_MIN_PHP_VER, '<' ) ) {
605
- wp_die(
606
- sprintf(
607
- __( 'The plugin could not be activated. The minimum PHP version required for this plugin is %s. You are running %s.', 'woocommerce-gateway-paypal-braintree' ),
608
- WC_PAYPAL_BRAINTREE_MIN_PHP_VER,
609
- phpversion()
610
- )
611
- );
612
- }
613
-
614
- if ( ! function_exists( 'curl_init' ) ) {
615
- wp_die( __( 'The plugin could not be activated. cURL is not installed.', 'woocommerce-gateway-paypal-braintree' ) );
616
- }
617
- }
618
-
619
  $GLOBALS['wc_paypal_braintree_loader'] = WC_PayPal_Braintree_Loader::getInstance();
 
1
  <?php
2
  /**
3
  * Plugin Name: WooCommerce PayPal Powered by Braintree Gateway
4
+ * Plugin URI: https://docs.woothemes.com/document/woocommerce-gateway-paypal-powered-by-braintree/
5
  * Description: Receive payments using Paypal Powered by Braintree. A server with cURL, SSL support, and a valid SSL certificate is required (for security reasons) for this gateway to function. Requires PHP 5.4+
6
  * Author: WooThemes
7
  * Author URI: http://woothemes.com/
8
+ * Version: 1.1.0
9
  *
10
  * Copyright (c) 2015 WooThemes
11
  *
73
  /** @var whether or not we need to load code for / support subscriptions */
74
  private $subscription_support_enabled = false;
75
 
76
+ /**
77
+ * Notices (array)
78
+ * @var array
79
+ */
80
+ public $notices = [];
81
+
82
  /**
83
  * Protected constructor to prevent creating a new instance of the
84
  * *Singleton* via the `new` operator from outside of this class.
85
  */
86
  protected function __construct() {
87
 
88
+ add_action( 'admin_init', array( $this, 'check_environment' ) );
89
+ // admin_notices is prioritized later to allow concrete classes to use admin_notices to push entries to the notices array
90
+ add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
91
+
92
+ // Don't hook anything else in the plugin if we're in an incompatible environment
93
+ if ( self::get_environment_warning() ) {
94
+ return;
95
+ }
96
+
97
  add_action( 'plugins_loaded', array( $this, 'init_gateways' ), 0 );
98
 
99
  add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
109
 
110
  }
111
 
112
+ /**
113
+ * Allow this class and other classes to add slug keyed notices (to avoid duplication)
114
+ */
115
+ public function add_admin_notice( $slug, $class, $message ) {
116
+ $this->notices[ $slug ] = array(
117
+ 'class' => $class,
118
+ 'message' => $message
119
+ );
120
+ }
121
+
122
+ /**
123
+ * The primary sanity check, automatically disable the plugin on activation if it doesn't
124
+ * meet minimum requirements.
125
+ *
126
+ * Based on http://wptavern.com/how-to-prevent-wordpress-plugins-from-activating-on-sites-with-incompatible-hosting-environments
127
+ */
128
+ public static function activation_check() {
129
+ $environment_warning = self::get_environment_warning( true );
130
+ if ( $environment_warning ) {
131
+ deactivate_plugins( plugin_basename( __FILE__ ) );
132
+ wp_die( $environment_warning );
133
+ }
134
+ }
135
+
136
+ /**
137
+ * The backup sanity check, in case the plugin is activated in a weird way,
138
+ * or the environment changes after activation.
139
+ */
140
+ public function check_environment() {
141
+ $environment_warning = self::get_environment_warning();
142
+ if ( $environment_warning && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
143
+ deactivate_plugins( plugin_basename( __FILE__ ) );
144
+ $this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
145
+ if ( isset( $_GET['activate'] ) ) {
146
+ unset( $_GET['activate'] );
147
+ }
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Checks the environment for compatibility problems. Returns a string with the first incompatibility
153
+ * found or false if the environment has no problems.
154
+ */
155
+ static function get_environment_warning( $during_activation = false ) {
156
+
157
+ if ( version_compare( phpversion(), WC_PAYPAL_BRAINTREE_MIN_PHP_VER, '<' ) ) {
158
+ if ( $during_activation ) {
159
+ $message = __( 'The plugin could not be activated. The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-paypal-braintree', 'woocommerce-gateway-paypal-braintree' );
160
+ } else {
161
+ $message = __( 'The WooCommerce PayPal Powered by Braintree plugin has been deactivated. The minimum PHP version required for this plugin is %1$s. You are running %2$s.', 'woocommerce-gateway-paypal-braintree' );
162
+ }
163
+ return sprintf( $message, WC_PAYPAL_BRAINTREE_MIN_PHP_VER, phpversion() );
164
+ }
165
+
166
+ if ( ! function_exists( 'curl_init' ) ) {
167
+ if ( $during_activation ) {
168
+ return __( 'The plugin could not be activated. cURL is not installed.', 'woocommerce-gateway-paypal-braintree' );
169
+ }
170
+
171
+ return __( 'The WooCommerce PayPal Powered by Braintree plugin has been deactivated. cURL is not installed.', 'woocommerce-gateway-paypal-braintree' );
172
+ }
173
+
174
+ return false;
175
+ }
176
+
177
 
178
  /**
179
  * Adds plugin action links
199
  return array_merge( $plugin_links, $links );
200
  }
201
 
202
+
203
  /**
204
+ * Display any notices we've collected thus far (e.g. for connection, disconnection)
205
+ */
206
+ public function admin_notices() {
207
+ foreach ( (array) $this->notices as $notice_key => $notice ) {
208
+ echo "<div class='" . esc_attr( sanitize_html_class( $notice['class'] ) ) . "'><p>";
209
+ echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
210
+ echo "</p></div>";
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Initialize the gateway. Called very early - in the context of the plugins_loaded action
216
  *
217
  * @since 1.0.0
218
  */
534
 
535
 
536
  /**
537
+ * Used when cart based Checkout with PayPal is in effect. Hooked to woocommerce_cart_emptied
538
  * Also called by WC_PayPal_Braintree_Loader::possibly_cancel_checkout_with_paypal
539
  *
540
  * @since 1.0.0
559
  'label' => '',
560
  'type' => 'checkbox',
561
  'description' => __( 'This controls whether or not this gateway is enabled within WooCommerce.', 'woocommerce-gateway-paypal-braintree' ),
562
+ 'default' => 'no',
563
  'desc_tip' => true
564
  ),
565
  'title_paypal' => array(
646
  $sections_to_remove = in_array( $current_section, $paypal_sections ) ? $card_sections : $paypal_sections;
647
 
648
  // And, let's also remove simplify commerce from the sections if it is not enabled and it is not the
649
+ // current section. (Note: The option will be empty if it has never been enabled)
650
 
651
  $simplify_commerce_options = get_option( 'woocommerce_simplify_commerce_settings', array() );
652
  if ( empty( $simplify_commerce_options ) || ( "no" === $simplify_commerce_options['enabled'] ) ) {
690
  }
691
  }
692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693
  $GLOBALS['wc_paypal_braintree_loader'] = WC_PayPal_Braintree_Loader::getInstance();
694
+ register_activation_hook( __FILE__, array( 'WC_PayPal_Braintree_Loader', 'activation_check' ) );