blob: ca6715936c10a709eb39da2675e23b3253b643d3 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2/**
swissChili61db4b22023-01-05 17:40:46 -05003 * Plugin Name: WooCommerce Stripe Gateway (no php_uname fix)
4 * Plugin URI: https://github.com/swissChili/woocomerce-gateway-stripe-no-php-uname
swissChilif0cbdc32023-01-05 17:21:38 -05005 * Description: Take credit card payments on your store using Stripe.
6 * Author: WooCommerce
7 * Author URI: https://woocommerce.com/
8 * Version: 7.0.1
9 * Requires at least: 5.8
10 * Tested up to: 6.0
11 * WC requires at least: 6.8
12 * WC tested up to: 7.0
13 * Text Domain: woocommerce-gateway-stripe
14 * Domain Path: /languages
15 */
16
17if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19}
20
21/**
22 * Required minimums and constants
23 */
24define( 'WC_STRIPE_VERSION', '7.0.1' ); // WRCS: DEFINED_VERSION.
25define( 'WC_STRIPE_MIN_PHP_VER', '7.3.0' );
26define( 'WC_STRIPE_MIN_WC_VER', '6.8' );
27define( 'WC_STRIPE_FUTURE_MIN_WC_VER', '6.9' );
28define( 'WC_STRIPE_MAIN_FILE', __FILE__ );
29define( 'WC_STRIPE_ABSPATH', __DIR__ . '/' );
30define( 'WC_STRIPE_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
31define( 'WC_STRIPE_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
32
33// phpcs:disable WordPress.Files.FileName
34
35/**
36 * WooCommerce fallback notice.
37 *
38 * @since 4.1.2
39 */
40function woocommerce_stripe_missing_wc_notice() {
41 /* translators: 1. URL link. */
42 echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Stripe requires WooCommerce to be installed and active. You can download %s here.', 'woocommerce-gateway-stripe' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
43}
44
45/**
46 * WooCommerce not supported fallback notice.
47 *
48 * @since 4.4.0
49 */
50function woocommerce_stripe_wc_not_supported() {
51 /* translators: $1. Minimum WooCommerce version. $2. Current WooCommerce version. */
52 echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Stripe requires WooCommerce %1$s or greater to be installed and active. WooCommerce %2$s is no longer supported.', 'woocommerce-gateway-stripe' ), WC_STRIPE_MIN_WC_VER, WC_VERSION ) . '</strong></p></div>';
53}
54
55function woocommerce_gateway_stripe() {
56
57 static $plugin;
58
59 if ( ! isset( $plugin ) ) {
60
61 class WC_Stripe {
62
63 /**
64 * The *Singleton* instance of this class
65 *
66 * @var WC_Stripe
67 */
68 private static $instance;
69
70 /**
71 * Returns the *Singleton* instance of this class.
72 *
73 * @return WC_Stripe The *Singleton* instance.
74 */
75 public static function get_instance() {
76 if ( null === self::$instance ) {
77 self::$instance = new self();
78 }
79 return self::$instance;
80 }
81
82 /**
83 * Stripe Connect API
84 *
85 * @var WC_Stripe_Connect_API
86 */
87 private $api;
88
89 /**
90 * Stripe Connect
91 *
92 * @var WC_Stripe_Connect
93 */
94 public $connect;
95
96 /**
97 * Stripe Payment Request configurations.
98 *
99 * @var WC_Stripe_Payment_Request
100 */
101 public $payment_request_configuration;
102
103 /**
104 * Stripe Account.
105 *
106 * @var WC_Stripe_Account
107 */
108 public $account;
109
110 /**
111 * The main Stripe gateway instance. Use get_main_stripe_gateway() to access it.
112 *
113 * @var null|WC_Stripe_Payment_Gateway
114 */
115 protected $stripe_gateway = null;
116
117 /**
118 * Private clone method to prevent cloning of the instance of the
119 * *Singleton* instance.
120 *
121 * @return void
122 */
123 public function __clone() {}
124
125 /**
126 * Private unserialize method to prevent unserializing of the *Singleton*
127 * instance.
128 *
129 * @return void
130 */
131 public function __wakeup() {}
132
133 /**
134 * Protected constructor to prevent creating a new instance of the
135 * *Singleton* via the `new` operator from outside of this class.
136 */
137 public function __construct() {
138 add_action( 'admin_init', [ $this, 'install' ] );
139
140 $this->init();
141
142 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
143 }
144
145 /**
146 * Init the plugin after plugins_loaded so environment variables are set.
147 *
148 * @since 1.0.0
149 * @version 5.0.0
150 */
151 public function init() {
152 if ( is_admin() ) {
153 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-privacy.php';
154 }
155
156 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-feature-flags.php';
157 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-upe-compatibility.php';
158 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-exception.php';
159 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-logger.php';
160 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-helper.php';
161 include_once dirname( __FILE__ ) . '/includes/class-wc-stripe-api.php';
162 require_once dirname( __FILE__ ) . '/includes/compat/trait-wc-stripe-subscriptions-utilities.php';
163 require_once dirname( __FILE__ ) . '/includes/compat/trait-wc-stripe-subscriptions.php';
164 require_once dirname( __FILE__ ) . '/includes/compat/trait-wc-stripe-pre-orders.php';
165 require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway.php';
166 require_once dirname( __FILE__ ) . '/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php';
167 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-state.php';
168 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-webhook-handler.php';
169 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-sepa-payment-token.php';
170 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-link-payment-token.php';
171 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-apple-pay-registration.php';
172 require_once dirname( __FILE__ ) . '/includes/class-wc-gateway-stripe.php';
173 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php';
174 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method.php';
175 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php';
176 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-giropay.php';
177 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-ideal.php';
178 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-bancontact.php';
179 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php';
180 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php';
181 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-eps.php';
182 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-sepa.php';
183 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-p24.php';
184 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-sofort.php';
185 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-upe-payment-method-link.php';
186 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-bancontact.php';
187 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-sofort.php';
188 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-giropay.php';
189 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-eps.php';
190 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-ideal.php';
191 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-p24.php';
192 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-alipay.php';
193 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-sepa.php';
194 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-multibanco.php';
195 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-boleto.php';
196 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-gateway-stripe-oxxo.php';
197 require_once dirname( __FILE__ ) . '/includes/payment-methods/class-wc-stripe-payment-request.php';
198 require_once dirname( __FILE__ ) . '/includes/compat/class-wc-stripe-woo-compat-utils.php';
199 require_once dirname( __FILE__ ) . '/includes/connect/class-wc-stripe-connect.php';
200 require_once dirname( __FILE__ ) . '/includes/connect/class-wc-stripe-connect-api.php';
201 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-order-handler.php';
202 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-payment-tokens.php';
203 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-customer.php';
204 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-intent-controller.php';
205 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-inbox-notes.php';
206 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-upe-compatibility-controller.php';
207 require_once dirname( __FILE__ ) . '/includes/migrations/class-allowed-payment-request-button-types-update.php';
208 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-account.php';
209 new Allowed_Payment_Request_Button_Types_Update();
210
211 $this->api = new WC_Stripe_Connect_API();
212 $this->connect = new WC_Stripe_Connect( $this->api );
213 $this->payment_request_configuration = new WC_Stripe_Payment_Request();
214 $this->account = new WC_Stripe_Account( $this->connect, 'WC_Stripe_API' );
215
216 if ( is_admin() ) {
217 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-admin-notices.php';
218 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-settings-controller.php';
219
220 if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) {
221 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-old-settings-upe-toggle-controller.php';
222 new WC_Stripe_Old_Settings_UPE_Toggle_Controller();
223 }
224
225 if ( isset( $_GET['area'] ) && 'payment_requests' === $_GET['area'] ) {
226 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-payment-requests-controller.php';
227 new WC_Stripe_Payment_Requests_Controller();
228 } else {
229 new WC_Stripe_Settings_Controller( $this->account );
230 }
231
232 if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
233 require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-payment-gateways-controller.php';
234 new WC_Stripe_Payment_Gateways_Controller();
235 }
236 }
237
238 // REMOVE IN THE FUTURE.
239 require_once dirname( __FILE__ ) . '/includes/deprecated/class-wc-stripe-apple-pay.php';
240
241 add_filter( 'woocommerce_payment_gateways', [ $this, 'add_gateways' ] );
242 add_filter( 'pre_update_option_woocommerce_stripe_settings', [ $this, 'gateway_settings_update' ], 10, 2 );
243 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ $this, 'plugin_action_links' ] );
244 add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
245
246 // Update the email field position.
247 if ( ! is_admin() ) {
248 add_filter( 'woocommerce_billing_fields', [ $this, 'checkout_update_email_field_priority' ], 50 );
249 }
250
251 // Modify emails emails.
252 add_filter( 'woocommerce_email_classes', [ $this, 'add_emails' ], 20 );
253
254 if ( version_compare( WC_VERSION, '3.4', '<' ) ) {
255 add_filter( 'woocommerce_get_sections_checkout', [ $this, 'filter_gateway_order_admin' ] );
256 }
257
258 new WC_Stripe_UPE_Compatibility_Controller();
259 }
260
261 /**
262 * Updates the plugin version in db
263 *
264 * @since 3.1.0
265 * @version 4.0.0
266 */
267 public function update_plugin_version() {
268 delete_option( 'wc_stripe_version' );
269 update_option( 'wc_stripe_version', WC_STRIPE_VERSION );
270 }
271
272 /**
273 * Handles upgrade routines.
274 *
275 * @since 3.1.0
276 * @version 3.1.0
277 */
278 public function install() {
279 if ( ! is_plugin_active( plugin_basename( __FILE__ ) ) ) {
280 return;
281 }
282
283 if ( ! defined( 'IFRAME_REQUEST' ) && ( WC_STRIPE_VERSION !== get_option( 'wc_stripe_version' ) ) ) {
284 do_action( 'woocommerce_stripe_updated' );
285
286 if ( ! defined( 'WC_STRIPE_INSTALLING' ) ) {
287 define( 'WC_STRIPE_INSTALLING', true );
288 }
289
290 add_woocommerce_inbox_variant();
291 $this->update_plugin_version();
292
293 // TODO: Remove this when we're reasonably sure most merchants have had their
294 // settings updated like this. ~80% of merchants is a good threshold.
295 // - @reykjalin
296 $this->update_prb_location_settings();
297 }
298 }
299
300 /**
301 * Updates the PRB location settings based on deprecated filters.
302 *
303 * The filters were removed in favor of plugin settings. This function can, and should,
304 * be removed when we're reasonably sure most merchants have had their settings updated
305 * through this function. Maybe ~80% of merchants is a good threshold?
306 *
307 * @since 5.5.0
308 * @version 5.5.0
309 */
310 public function update_prb_location_settings() {
311 $stripe_settings = get_option( 'woocommerce_stripe_settings', [] );
312 $prb_locations = isset( $stripe_settings['payment_request_button_locations'] )
313 ? $stripe_settings['payment_request_button_locations']
314 : [];
315 if ( ! empty( $stripe_settings ) && empty( $prb_locations ) ) {
316 global $post;
317
318 $should_show_on_product_page = ! apply_filters( 'wc_stripe_hide_payment_request_on_product_page', false, $post );
319 $should_show_on_cart_page = apply_filters( 'wc_stripe_show_payment_request_on_cart', true );
320 $should_show_on_checkout_page = apply_filters( 'wc_stripe_show_payment_request_on_checkout', false, $post );
321
322 $new_prb_locations = [];
323
324 if ( $should_show_on_product_page ) {
325 $new_prb_locations[] = 'product';
326 }
327
328 if ( $should_show_on_cart_page ) {
329 $new_prb_locations[] = 'cart';
330 }
331
332 if ( $should_show_on_checkout_page ) {
333 $new_prb_locations[] = 'checkout';
334 }
335
336 $stripe_settings['payment_request_button_locations'] = $new_prb_locations;
337 update_option( 'woocommerce_stripe_settings', $stripe_settings );
338 }
339 }
340
341 /**
342 * Add plugin action links.
343 *
344 * @since 1.0.0
345 * @version 4.0.0
346 */
347 public function plugin_action_links( $links ) {
348 $plugin_links = [
349 '<a href="admin.php?page=wc-settings&tab=checkout&section=stripe">' . esc_html__( 'Settings', 'woocommerce-gateway-stripe' ) . '</a>',
350 ];
351 return array_merge( $plugin_links, $links );
352 }
353
354 /**
355 * Add plugin action links.
356 *
357 * @since 4.3.4
358 * @param array $links Original list of plugin links.
359 * @param string $file Name of current file.
360 * @return array $links Update list of plugin links.
361 */
362 public function plugin_row_meta( $links, $file ) {
363 if ( plugin_basename( __FILE__ ) === $file ) {
364 $row_meta = [
365 'docs' => '<a href="' . esc_url( apply_filters( 'woocommerce_gateway_stripe_docs_url', 'https://woocommerce.com/document/stripe/' ) ) . '" title="' . esc_attr( __( 'View Documentation', 'woocommerce-gateway-stripe' ) ) . '">' . __( 'Docs', 'woocommerce-gateway-stripe' ) . '</a>',
366 'support' => '<a href="' . esc_url( apply_filters( 'woocommerce_gateway_stripe_support_url', 'https://woocommerce.com/my-account/create-a-ticket?select=18627' ) ) . '" title="' . esc_attr( __( 'Open a support request at WooCommerce.com', 'woocommerce-gateway-stripe' ) ) . '">' . __( 'Support', 'woocommerce-gateway-stripe' ) . '</a>',
367 ];
368 return array_merge( $links, $row_meta );
369 }
370 return (array) $links;
371 }
372
373 /**
374 * Add the gateways to WooCommerce.
375 *
376 * @since 1.0.0
377 * @version 5.6.0
378 */
379 public function add_gateways( $methods ) {
380 $methods[] = $this->get_main_stripe_gateway();
381
382 if ( ! WC_Stripe_Feature_Flags::is_upe_preview_enabled() || ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
383 // These payment gateways will be hidden when UPE is enabled:
384 $methods[] = WC_Gateway_Stripe_Sepa::class;
385 $methods[] = WC_Gateway_Stripe_Giropay::class;
386 $methods[] = WC_Gateway_Stripe_Ideal::class;
387 $methods[] = WC_Gateway_Stripe_Bancontact::class;
388 $methods[] = WC_Gateway_Stripe_Eps::class;
389 $methods[] = WC_Gateway_Stripe_Sofort::class;
390 $methods[] = WC_Gateway_Stripe_P24::class;
391 $methods[] = WC_Gateway_Stripe_Boleto::class;
392 $methods[] = WC_Gateway_Stripe_Oxxo::class;
393 }
394
395 // These payment gateways will always be visible, regardless if UPE is enabled or disabled:
396 $methods[] = WC_Gateway_Stripe_Alipay::class;
397 $methods[] = WC_Gateway_Stripe_Multibanco::class;
398
399 return $methods;
400 }
401
402 /**
403 * Modifies the order of the gateways displayed in admin.
404 *
405 * @since 4.0.0
406 * @version 4.0.0
407 */
408 public function filter_gateway_order_admin( $sections ) {
409 unset( $sections['stripe'] );
410 if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) {
411 unset( $sections['stripe_upe'] );
412 }
413 unset( $sections['stripe_bancontact'] );
414 unset( $sections['stripe_sofort'] );
415 unset( $sections['stripe_giropay'] );
416 unset( $sections['stripe_eps'] );
417 unset( $sections['stripe_ideal'] );
418 unset( $sections['stripe_p24'] );
419 unset( $sections['stripe_alipay'] );
420 unset( $sections['stripe_sepa'] );
421 unset( $sections['stripe_multibanco'] );
422
423 $sections['stripe'] = 'Stripe';
424 if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) {
425 $sections['stripe_upe'] = 'Stripe checkout experience';
426 }
427 $sections['stripe_bancontact'] = __( 'Stripe Bancontact', 'woocommerce-gateway-stripe' );
428 $sections['stripe_sofort'] = __( 'Stripe Sofort', 'woocommerce-gateway-stripe' );
429 $sections['stripe_giropay'] = __( 'Stripe giropay', 'woocommerce-gateway-stripe' );
430 $sections['stripe_eps'] = __( 'Stripe EPS', 'woocommerce-gateway-stripe' );
431 $sections['stripe_ideal'] = __( 'Stripe iDEAL', 'woocommerce-gateway-stripe' );
432 $sections['stripe_p24'] = __( 'Stripe P24', 'woocommerce-gateway-stripe' );
433 $sections['stripe_alipay'] = __( 'Stripe Alipay', 'woocommerce-gateway-stripe' );
434 $sections['stripe_sepa'] = __( 'Stripe SEPA Direct Debit', 'woocommerce-gateway-stripe' );
435 $sections['stripe_multibanco'] = __( 'Stripe Multibanco', 'woocommerce-gateway-stripe' );
436
437 return $sections;
438 }
439
440 /**
441 * Provide default values for missing settings on initial gateway settings save.
442 *
443 * @since 4.5.4
444 * @version 4.5.4
445 *
446 * @param array $settings New settings to save.
447 * @param array|bool $old_settings Existing settings, if any.
448 * @return array New value but with defaults initially filled in for missing settings.
449 */
450 public function gateway_settings_update( $settings, $old_settings ) {
451 if ( false === $old_settings ) {
452 $gateway = new WC_Gateway_Stripe();
453 $fields = $gateway->get_form_fields();
454 $old_settings = array_merge( array_fill_keys( array_keys( $fields ), '' ), wp_list_pluck( $fields, 'default' ) );
455 $settings = array_merge( $old_settings, $settings );
456 }
457
458 if ( ! WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) {
459 return $settings;
460 }
461
462 return $this->toggle_upe( $settings, $old_settings );
463 }
464
465 /**
466 * Enable or disable UPE.
467 *
468 * When enabling UPE: For each currently enabled Stripe LPM, the corresponding UPE method is enabled.
469 *
470 * When disabling UPE: For each currently enabled UPE method, the corresponding LPM is enabled.
471 *
472 * @param array $settings New settings to save.
473 * @param array|bool $old_settings Existing settings, if any.
474 * @return array New value but with defaults initially filled in for missing settings.
475 */
476 protected function toggle_upe( $settings, $old_settings ) {
477 if ( false === $old_settings || ! isset( $old_settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) ) {
478 $old_settings = [ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME => 'no' ];
479 }
480 if ( ! isset( $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) || $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] === $old_settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) {
481 return $settings;
482 }
483
484 if ( 'yes' === $settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] ) {
485 return $this->enable_upe( $settings );
486 }
487
488 return $this->disable_upe( $settings );
489 }
490
491 protected function enable_upe( $settings ) {
492 $settings['upe_checkout_experience_accepted_payments'] = [];
493 $payment_gateways = WC()->payment_gateways->payment_gateways();
494 foreach ( WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS as $method_class ) {
495 if ( ! defined( "$method_class::LPM_GATEWAY_CLASS" ) ) {
496 continue;
497 }
498
499 $lpm_gateway_id = constant( $method_class::LPM_GATEWAY_CLASS . '::ID' );
500 if ( isset( $payment_gateways[ $lpm_gateway_id ] ) && 'yes' === $payment_gateways[ $lpm_gateway_id ]->enabled ) {
501 // DISABLE LPM
502 if ( 'stripe' !== $lpm_gateway_id ) {
503 /**
504 * TODO: This can be replaced with:
505 *
506 * $payment_gateways[ $lpm_gateway_id ]->update_option( 'enabled', 'no' );
507 * $payment_gateways[ $lpm_gateway_id ]->enabled = 'no';
508 *
509 * ...once the minimum WC version is 3.4.0.
510 */
511 $payment_gateways[ $lpm_gateway_id ]->settings['enabled'] = 'no';
512 update_option(
513 $payment_gateways[ $lpm_gateway_id ]->get_option_key(),
514 apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $payment_gateways[ $lpm_gateway_id ]::ID, $payment_gateways[ $lpm_gateway_id ]->settings ),
515 'yes'
516 );
517 }
518 // ENABLE UPE METHOD
519 $settings['upe_checkout_experience_accepted_payments'][] = $method_class::STRIPE_ID;
520 }
521 }
522 if ( empty( $settings['upe_checkout_experience_accepted_payments'] ) ) {
523 $settings['upe_checkout_experience_accepted_payments'] = [ 'card' ];
524 } else {
525 // The 'stripe' gateway must be enabled for UPE if any LPMs were enabled.
526 $settings['enabled'] = 'yes';
527 }
528
529 return $settings;
530 }
531
532 protected function disable_upe( $settings ) {
533 $upe_gateway = new WC_Stripe_UPE_Payment_Gateway();
534 $upe_enabled_method_ids = $upe_gateway->get_upe_enabled_payment_method_ids();
535 foreach ( WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS as $method_class ) {
536 if ( ! defined( "$method_class::LPM_GATEWAY_CLASS" ) || ! in_array( $method_class::STRIPE_ID, $upe_enabled_method_ids, true ) ) {
537 continue;
538 }
539 // ENABLE LPM
540 $gateway_class = $method_class::LPM_GATEWAY_CLASS;
541 $gateway = new $gateway_class();
542 /**
543 * TODO: This can be replaced with:
544 *
545 * $gateway->update_option( 'enabled', 'yes' );
546 *
547 * ...once the minimum WC version is 3.4.0.
548 */
549 $gateway->settings['enabled'] = 'yes';
550 update_option( $gateway->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $gateway::ID, $gateway->settings ), 'yes' );
551 }
552 // Disable main Stripe/card LPM if 'card' UPE method wasn't enabled.
553 if ( ! in_array( 'card', $upe_enabled_method_ids, true ) ) {
554 $settings['enabled'] = 'no';
555 }
556 // DISABLE ALL UPE METHODS
557 if ( ! isset( $settings['upe_checkout_experience_accepted_payments'] ) ) {
558 $settings['upe_checkout_experience_accepted_payments'] = [];
559 }
560 return $settings;
561 }
562
563 /**
564 * Adds the failed SCA auth email to WooCommerce.
565 *
566 * @param WC_Email[] $email_classes All existing emails.
567 * @return WC_Email[]
568 */
569 public function add_emails( $email_classes ) {
570 require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-authentication.php';
571 require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-renewal-authentication.php';
572 require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-preorder-authentication.php';
573 require_once WC_STRIPE_PLUGIN_PATH . '/includes/compat/class-wc-stripe-email-failed-authentication-retry.php';
574
575 // Add all emails, generated by the gateway.
576 $email_classes['WC_Stripe_Email_Failed_Renewal_Authentication'] = new WC_Stripe_Email_Failed_Renewal_Authentication( $email_classes );
577 $email_classes['WC_Stripe_Email_Failed_Preorder_Authentication'] = new WC_Stripe_Email_Failed_Preorder_Authentication( $email_classes );
578 $email_classes['WC_Stripe_Email_Failed_Authentication_Retry'] = new WC_Stripe_Email_Failed_Authentication_Retry( $email_classes );
579
580 return $email_classes;
581 }
582
583 /**
584 * Register REST API routes.
585 *
586 * New endpoints/controllers can be added here.
587 */
588 public function register_routes() {
589 /** API includes */
590 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-stripe-rest-base-controller.php';
591 require_once WC_STRIPE_PLUGIN_PATH . '/includes/abstracts/abstract-wc-stripe-connect-rest-controller.php';
592 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-account-controller.php';
593 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-connection-tokens-controller.php';
594 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-locations-controller.php';
595 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-orders-controller.php';
596 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-tokens-controller.php';
597 require_once WC_STRIPE_PLUGIN_PATH . '/includes/connect/class-wc-stripe-connect-rest-oauth-init-controller.php';
598 require_once WC_STRIPE_PLUGIN_PATH . '/includes/connect/class-wc-stripe-connect-rest-oauth-connect-controller.php';
599
600 $connection_tokens_controller = new WC_REST_Stripe_Connection_Tokens_Controller( $this->get_main_stripe_gateway() );
601 $locations_controller = new WC_REST_Stripe_Locations_Controller();
602 $orders_controller = new WC_REST_Stripe_Orders_Controller( $this->get_main_stripe_gateway() );
603 $stripe_tokens_controller = new WC_REST_Stripe_Tokens_Controller();
604 $oauth_init = new WC_Stripe_Connect_REST_Oauth_Init_Controller( $this->connect, $this->api );
605 $oauth_connect = new WC_Stripe_Connect_REST_Oauth_Connect_Controller( $this->connect, $this->api );
606 $stripe_account_controller = new WC_REST_Stripe_Account_Controller( $this->get_main_stripe_gateway(), $this->account );
607
608 $connection_tokens_controller->register_routes();
609 $locations_controller->register_routes();
610 $orders_controller->register_routes();
611 $stripe_tokens_controller->register_routes();
612 $oauth_init->register_routes();
613 $oauth_connect->register_routes();
614 $stripe_account_controller->register_routes();
615
616 if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) {
617 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-settings-controller.php';
618 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-stripe-rest-upe-flag-toggle-controller.php';
619 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-account-keys-controller.php';
620 require_once WC_STRIPE_PLUGIN_PATH . '/includes/admin/class-wc-rest-stripe-payment-gateway-controller.php';
621
622 $upe_flag_toggle_controller = new WC_Stripe_REST_UPE_Flag_Toggle_Controller();
623 $upe_flag_toggle_controller->register_routes();
624
625 $settings_controller = new WC_REST_Stripe_Settings_Controller( $this->get_main_stripe_gateway() );
626 $settings_controller->register_routes();
627
628 $stripe_account_keys_controller = new WC_REST_Stripe_Account_Keys_Controller( $this->account );
629 $stripe_account_keys_controller->register_routes();
630
631 $settings_controller = new WC_REST_Stripe_Payment_Gateway_Controller();
632 $settings_controller->register_routes();
633 }
634 }
635
636 /**
637 * Returns the main Stripe payment gateway class instance.
638 *
639 * @return WC_Stripe_Payment_Gateway
640 */
641 public function get_main_stripe_gateway() {
642 if ( ! is_null( $this->stripe_gateway ) ) {
643 return $this->stripe_gateway;
644 }
645
646 if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() && WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
647 $this->stripe_gateway = new WC_Stripe_UPE_Payment_Gateway();
648
649 return $this->stripe_gateway;
650 }
651
652 $this->stripe_gateway = new WC_Gateway_Stripe();
653
654 return $this->stripe_gateway;
655 }
656
657 /**
658 * Move the email field to the top of the Checkout page.
659 *
660 * @param array $fields WooCommerce checkout fields.
661 *
662 * @return array WooCommerce checkout fields.
663 */
664 public function checkout_update_email_field_priority( $fields ) {
665 if ( isset( $fields['billing_email'] ) && WC_Stripe_UPE_Payment_Method_Link::is_link_enabled() ) {
666 // Update the field priority.
667 $fields['billing_email']['priority'] = 1;
668
669 // Add extra `wcpay-checkout-email-field` class.
670 $fields['billing_email']['class'][] = 'stripe-gateway-checkout-email-field';
671
672 // Append StripeLink modal trigger button for logged in users.
673 $fields['billing_email']['label'] = $fields['billing_email']['label']
674 . ' <button class="stripe-gateway-stripelink-modal-trigger"></button>';
675 }
676
677 return $fields;
678 }
679 }
680
681 $plugin = WC_Stripe::get_instance();
682
683 }
684
685 return $plugin;
686}
687
688add_action( 'plugins_loaded', 'woocommerce_gateway_stripe_init' );
689
690function woocommerce_gateway_stripe_init() {
691 load_plugin_textdomain( 'woocommerce-gateway-stripe', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
692
693 if ( ! class_exists( 'WooCommerce' ) ) {
694 add_action( 'admin_notices', 'woocommerce_stripe_missing_wc_notice' );
695 return;
696 }
697
698 if ( version_compare( WC_VERSION, WC_STRIPE_MIN_WC_VER, '<' ) ) {
699 add_action( 'admin_notices', 'woocommerce_stripe_wc_not_supported' );
700 return;
701 }
702
703 woocommerce_gateway_stripe();
704}
705
706/**
707 * Add woocommerce_inbox_variant for the Remote Inbox Notification.
708 *
709 * P2 post can be found at https://wp.me/paJDYF-1uJ.
710 */
711if ( ! function_exists( 'add_woocommerce_inbox_variant' ) ) {
712 function add_woocommerce_inbox_variant() {
713 $config_name = 'woocommerce_inbox_variant_assignment';
714 if ( false === get_option( $config_name, false ) ) {
715 update_option( $config_name, wp_rand( 1, 12 ) );
716 }
717 }
718}
719register_activation_hook( __FILE__, 'add_woocommerce_inbox_variant' );
720
721function wcstripe_deactivated() {
722 // admin notes are not supported on older versions of WooCommerce.
723 require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-upe-compatibility.php';
724 if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
725 // requirements for the note
726 require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-feature-flags.php';
727 require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
728 WC_Stripe_UPE_Availability_Note::possibly_delete_note();
729
730 require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
731 WC_Stripe_UPE_StripeLink_Note::possibly_delete_note();
732 }
733}
734register_deactivation_hook( __FILE__, 'wcstripe_deactivated' );
735
736// Hook in Blocks integration. This action is called in a callback on plugins loaded, so current Stripe plugin class
737// implementation is too late.
738add_action( 'woocommerce_blocks_loaded', 'woocommerce_gateway_stripe_woocommerce_block_support' );
739
740function woocommerce_gateway_stripe_woocommerce_block_support() {
741 if ( class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
742 require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-blocks-support.php';
743 // priority is important here because this ensures this integration is
744 // registered before the WooCommerce Blocks built-in Stripe registration.
745 // Blocks code has a check in place to only register if 'stripe' is not
746 // already registered.
747 add_action(
748 'woocommerce_blocks_payment_method_type_registration',
749 function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
750 // I noticed some incompatibility with WP 5.x and WC 5.3 when `_wcstripe_feature_upe_settings` is enabled.
751 if ( ! class_exists( 'WC_Stripe_Payment_Request' ) ) {
752 return;
753 }
754
755 $container = Automattic\WooCommerce\Blocks\Package::container();
756 // registers as shared instance.
757 $container->register(
758 WC_Stripe_Blocks_Support::class,
759 function() {
760 if ( class_exists( 'WC_Stripe' ) ) {
761 return new WC_Stripe_Blocks_Support( WC_Stripe::get_instance()->payment_request_configuration );
762 } else {
763 return new WC_Stripe_Blocks_Support();
764 }
765 }
766 );
767 $payment_method_registry->register(
768 $container->get( WC_Stripe_Blocks_Support::class )
769 );
770 },
771 5
772 );
773 }
774}
775
776add_action(
777 'before_woocommerce_init',
778 function() {
779 if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
780 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, false );
781 }
782 }
783);