PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce-gateway-stripe/woocommerce-gateway-stripe.php

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