Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing the new test mode class #3584

Merged
merged 18 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function __construct() {
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
Expand Down
4 changes: 2 additions & 2 deletions includes/admin/class-wc-rest-stripe-account-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function get_account() {
return new WP_REST_Response(
[
'account' => $this->account->get_cached_account_data(),
'testmode' => WC_Stripe_Webhook_State::get_testmode(),
'testmode' => WC_Stripe_Mode::is_test(),
'webhook_status_code' => WC_Stripe_Webhook_State::get_webhook_status_code(),
'webhook_status_message' => WC_Stripe_Webhook_State::get_webhook_status_message(),
'webhook_url' => WC_Stripe_Helper::get_webhook_url(),
Expand Down Expand Up @@ -134,7 +134,7 @@ public function get_account_summary() {
],
'country' => $account['country'] ?? WC()->countries->get_base_country(),
'is_live' => $account['charges_enabled'] ?? false,
'test_mode' => WC_Stripe_Webhook_State::get_testmode(),
'test_mode' => WC_Stripe_Mode::is_test(),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ public function set_account_keys( WP_REST_Request $request ) {
$settings['test_connection_type'] = '';
$settings['refresh_token'] = '';
$settings['test_refresh_token'] = '';
$this->record_manual_account_disconnect_track_event( 'yes' === $settings['testmode'] );
$this->record_manual_account_disconnect_track_event( WC_Stripe_Mode::is_test() );
} else {
$this->record_manual_account_key_update_track_event( 'yes' === $settings['testmode'] );
$this->record_manual_account_key_update_track_event( WC_Stripe_Mode::is_test() );
}

// Before saving the settings, decommission any previously automatically configured webhook endpoint.
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-wc-stripe-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function stripe_check_environment() {
$show_sca_notice = get_option( 'wc_stripe_show_sca_notice' );
$changed_keys_notice = get_option( 'wc_stripe_show_changed_keys_notice' );
$options = WC_Stripe_Helper::get_stripe_settings();
$testmode = ( isset( $options['testmode'] ) && 'yes' === $options['testmode'] ) ? true : false;
$testmode = WC_Stripe_Mode::is_test();
$test_pub_key = isset( $options['test_publishable_key'] ) ? $options['test_publishable_key'] : '';
$test_secret_key = isset( $options['test_secret_key'] ) ? $options['test_secret_key'] : '';
$live_pub_key = isset( $options['publishable_key'] ) ? $options['publishable_key'] : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function admin_scripts() {

$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$params = [
'key' => 'yes' === $stripe_settings['testmode'] ? $stripe_settings['test_publishable_key'] : $stripe_settings['publishable_key'],
'key' => WC_Stripe_Mode::is_test() ? $stripe_settings['test_publishable_key'] : $stripe_settings['publishable_key'],
'locale' => WC_Stripe_Helper::convert_wc_locale_to_stripe_locale( get_locale() ),
'is_ece_enabled' => WC_Stripe_Feature_Flags::is_stripe_ece_enabled(),
];
Expand Down
6 changes: 2 additions & 4 deletions includes/class-wc-stripe-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ private function cache_account( $mode = null ) {
* @return string Transient key of test mode when testmode is enabled, otherwise returns the key of live mode.
*/
private function get_transient_key( $mode = null ) {
$settings_options = WC_Stripe_Helper::get_stripe_settings();

// If the mode is not provided or is invalid, we'll check the current mode.
if ( is_null( $mode ) || ! in_array( $mode, [ 'test', 'live' ] ) ) {
Mayisha marked this conversation as resolved.
Show resolved Hide resolved
$mode = isset( $settings_options['testmode'] ) && 'yes' === $settings_options['testmode'] ? 'test' : 'live';
if ( ! in_array( $mode, [ 'test', 'live' ], true ) ) {
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
}

return 'test' === $mode ? self::TEST_ACCOUNT_OPTION : self::LIVE_ACCOUNT_OPTION;
Expand Down
9 changes: 3 additions & 6 deletions includes/class-wc-stripe-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static function set_secret_key_for_mode( $mode = null ) {
$secret_key = $options['secret_key'] ?? '';
$test_secret_key = $options['test_secret_key'] ?? '';

if ( is_null( $mode ) || ! in_array( $mode, [ 'test', 'live' ] ) ) {
$mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'] ? 'test' : 'live';
if ( ! in_array( $mode, [ 'test', 'live' ], true ) ) {
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
}

self::set_secret_key( 'test' === $mode ? $test_secret_key : $secret_key );
Expand Down Expand Up @@ -389,11 +389,8 @@ public static function detach_payment_method_from_customer( string $customer_id,
* @return bool True if the payment should be detached, false otherwise.
*/
public static function should_detach_payment_method_from_customer() {
$options = WC_Stripe_Helper::get_stripe_settings();
$test_mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'];

// If we are in test mode, we can always detach the payment method.
if ( $test_mode ) {
if ( WC_Stripe_Mode::is_test() ) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-stripe-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public static function get_upe_individual_payment_method_settings( $gateway ) {
*/
public static function get_upe_ordered_payment_method_ids( $gateway ) {
$stripe_settings = self::get_stripe_settings();
$testmode = isset( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'];
$testmode = WC_Stripe_Mode::is_test();
$ordered_payment_method_ids = isset( $stripe_settings['stripe_upe_payment_method_order'] ) ? $stripe_settings['stripe_upe_payment_method_order'] : [];

// When switched to the new checkout experience, the UPE method order is not set. Copy the legacy order to the UPE order to persist previous settings.
Expand Down
4 changes: 2 additions & 2 deletions includes/class-wc-stripe-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WC_Stripe_Mode {
*/
public static function is_live() {
$settings = WC_Stripe_Helper::get_stripe_settings();
return 'yes' !== $settings['testmode'];
return 'yes' !== ( $settings['testmode'] ?? 'no' );
}

/**
Expand All @@ -24,6 +24,6 @@ public static function is_live() {
*/
public static function is_test() {
$settings = WC_Stripe_Helper::get_stripe_settings();
return 'yes' === $settings['testmode'];
return 'yes' === ( $settings['testmode'] ?? 'no' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a very unusual scenario, but if in any case $settings['testmode'] is not defined the is_live and is_test both functions will return false.

Probably is_live should return true in this scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that as well. But I did a test here, and it behaves as you suggested already:

var_dump( 'yes' !== ( $settings['testmode'] ?? 'no' ) ); // `is_live()`: prints `true`
var_dump( 'yes' === ( $settings['testmode'] ?? 'no' ) ); // `is_test()`: prints `false`

}
}
9 changes: 1 addition & 8 deletions includes/class-wc-stripe-order-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,8 @@ public function woocommerce_tracks_event_properties( $properties, $prefixed_even
return $properties;
}

// Due diligence done. Collect the metadata.
$is_live = true;
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
if ( array_key_exists( 'testmode', $stripe_settings ) ) {
$is_live = 'no' === $stripe_settings['testmode'];
}

$properties['admin_email'] = get_option( 'admin_email' );
$properties['is_live'] = $is_live;
$properties['is_live'] = WC_Stripe_Mode::is_live();
$properties['woocommerce_gateway_stripe_version'] = WC_STRIPE_VERSION;
$properties['woocommerce_default_country'] = get_option( 'woocommerce_default_country' );

Expand Down
3 changes: 1 addition & 2 deletions includes/class-wc-stripe-payment-tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ public function woocommerce_payment_token_deleted( $token_id, $token ) {
* When all conditions are met, we don't want to delete the payment method from Stripe.
* This is to avoid detaching the payment method from the live stripe account on non production environments.
*/
$settings = WC_Stripe_Helper::get_stripe_settings();
if (
'no' === $settings['testmode'] &&
WC_Stripe_Mode::is_live() &&
is_admin() &&
'production' !== wp_get_environment_type()
) {
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-stripe-webhook-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WC_Stripe_Webhook_Handler extends WC_Stripe_Payment_Gateway {
public function __construct() {
$this->retry_interval = 2;
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$this->testmode = ( ! empty( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$secret_key = ( $this->testmode ? 'test_' : '' ) . 'webhook_secret';
$this->secret = ! empty( $stripe_settings[ $secret_key ] ) ? $stripe_settings[ $secret_key ] : false;

Expand Down
24 changes: 14 additions & 10 deletions includes/class-wc-stripe-webhook-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class WC_Stripe_Webhook_State {
*
* @since 5.0.0
* @return bool
*
* @deprecated 8.9.0
*/
public static function get_testmode() {
wc_deprecated_function( __METHOD__, '8.9.0', 'WC_Stripe_Mode::is_test()' );

$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
return ( ! empty( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'] ) ? true : false;
}
Expand Down Expand Up @@ -70,7 +74,7 @@ public static function clear_state( $mode = 'all' ) {
* @return integer UTC seconds since 1970.
*/
public static function get_monitoring_began_at() {
$option = self::get_testmode() ? self::OPTION_TEST_MONITORING_BEGAN_AT : self::OPTION_LIVE_MONITORING_BEGAN_AT;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_MONITORING_BEGAN_AT : self::OPTION_LIVE_MONITORING_BEGAN_AT;
$monitoring_began_at = get_option( $option, 0 );
if ( 0 == $monitoring_began_at ) {
$monitoring_began_at = time();
Expand All @@ -93,7 +97,7 @@ public static function get_monitoring_began_at() {
* @param integer UTC seconds since 1970.
*/
public static function set_last_webhook_success_at( $timestamp ) {
$option = self::get_testmode() ? self::OPTION_TEST_LAST_SUCCESS_AT : self::OPTION_LIVE_LAST_SUCCESS_AT;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_LAST_SUCCESS_AT : self::OPTION_LIVE_LAST_SUCCESS_AT;
update_option( $option, $timestamp );
}

Expand All @@ -105,7 +109,7 @@ public static function set_last_webhook_success_at( $timestamp ) {
* @return integer UTC seconds since 1970 | 0.
*/
public static function get_last_webhook_success_at() {
$option = self::get_testmode() ? self::OPTION_TEST_LAST_SUCCESS_AT : self::OPTION_LIVE_LAST_SUCCESS_AT;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_LAST_SUCCESS_AT : self::OPTION_LIVE_LAST_SUCCESS_AT;
return get_option( $option, 0 );
}

Expand All @@ -116,7 +120,7 @@ public static function get_last_webhook_success_at() {
* @param integer UTC seconds since 1970.
*/
public static function set_last_webhook_failure_at( $timestamp ) {
$option = self::get_testmode() ? self::OPTION_TEST_LAST_FAILURE_AT : self::OPTION_LIVE_LAST_FAILURE_AT;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_LAST_FAILURE_AT : self::OPTION_LIVE_LAST_FAILURE_AT;
update_option( $option, $timestamp );
}

Expand All @@ -128,7 +132,7 @@ public static function set_last_webhook_failure_at( $timestamp ) {
* @return integer UTC seconds since 1970 | 0.
*/
public static function get_last_webhook_failure_at() {
$option = self::get_testmode() ? self::OPTION_TEST_LAST_FAILURE_AT : self::OPTION_LIVE_LAST_FAILURE_AT;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_LAST_FAILURE_AT : self::OPTION_LIVE_LAST_FAILURE_AT;
return get_option( $option, 0 );
}

Expand All @@ -139,7 +143,7 @@ public static function get_last_webhook_failure_at() {
* @param string Reason code.
*/
public static function set_last_error_reason( $reason ) {
$option = self::get_testmode() ? self::OPTION_TEST_LAST_ERROR : self::OPTION_LIVE_LAST_ERROR;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_LAST_ERROR : self::OPTION_LIVE_LAST_ERROR;
update_option( $option, $reason );
}

Expand All @@ -150,7 +154,7 @@ public static function set_last_error_reason( $reason ) {
* @return string Reason the last webhook failed.
*/
public static function get_last_error_reason() {
$option = self::get_testmode() ? self::OPTION_TEST_LAST_ERROR : self::OPTION_LIVE_LAST_ERROR;
$option = WC_Stripe_Mode::is_test() ? self::OPTION_TEST_LAST_ERROR : self::OPTION_LIVE_LAST_ERROR;
$last_error = get_option( $option, false );

if ( self::VALIDATION_SUCCEEDED == $last_error ) {
Expand Down Expand Up @@ -196,8 +200,8 @@ public static function get_last_error_reason() {
* @return int The status code for the webhook processing.
*/
public static function get_webhook_status_code() {
$last_success_at = self::get_last_webhook_success_at();
$last_failure_at = self::get_last_webhook_failure_at();
$last_success_at = self::get_last_webhook_success_at();
$last_failure_at = self::get_last_webhook_failure_at();

// Case 1 (Nominal case): Most recent = success
if ( $last_success_at > $last_failure_at ) {
Expand Down Expand Up @@ -229,7 +233,7 @@ public static function get_webhook_status_message() {
$last_success_at = self::get_last_webhook_success_at();
$last_failure_at = self::get_last_webhook_failure_at();
$last_error = self::get_last_error_reason();
$test_mode = self::get_testmode();
$test_mode = WC_Stripe_Mode::is_test();
$code = self::get_webhook_status_code();

$date_format = 'Y-m-d H:i:s e';
Expand Down
15 changes: 5 additions & 10 deletions includes/connect/class-wc-stripe-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,12 @@ private function get_default_stripe_config() {
* @return bool True if connected, false otherwise.
*/
public function is_connected( $mode = null ) {
$options = WC_Stripe_Helper::get_stripe_settings();

// If the mode is not provided, we'll check the current mode.
if ( is_null( $mode ) ) {
$mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'] ? 'test' : 'live';
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
}

$options = WC_Stripe_Helper::get_stripe_settings();
if ( 'test' === $mode ) {
return isset( $options['test_publishable_key'], $options['test_secret_key'] ) && trim( $options['test_publishable_key'] ) && trim( $options['test_secret_key'] );
} else {
Expand Down Expand Up @@ -290,11 +289,9 @@ public function is_connected_via_oauth( $mode = 'live' ) {
* @return bool True if connected via Stripe App OAuth, false otherwise.
*/
public function is_connected_via_app_oauth( $mode = null ) {
$options = WC_Stripe_Helper::get_stripe_settings();

// If the mode is not provided, we'll check the current mode.
if ( is_null( $mode ) ) {
$mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'] ? 'test' : 'live';
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
}

return 'app' === $this->get_connection_type( $mode );
Expand Down Expand Up @@ -323,13 +320,11 @@ private function record_account_connect_track_event( bool $had_error ) {
return;
}

$options = WC_Stripe_Helper::get_stripe_settings();
$is_test = isset( $options['testmode'] ) && 'yes' === $options['testmode'];
$event_name = ! $had_error ? 'wcstripe_stripe_connected' : 'wcstripe_stripe_connect_error';

// We're recording this directly instead of queueing it because
// a queue wouldn't be processed due to the redirect that comes after.
WC_Tracks::record_event( $event_name, [ 'is_test_mode' => $is_test ] );
WC_Tracks::record_event( $event_name, [ 'is_test_mode' => WC_Stripe_Mode::is_test() ] );
}

/**
Expand Down Expand Up @@ -380,7 +375,7 @@ public function refresh_connection() {
}

$options = WC_Stripe_Helper::get_stripe_settings();
$mode = isset( $options['testmode'] ) && 'yes' === $options['testmode'] ? 'test' : 'live';
$mode = WC_Stripe_Mode::is_test() ? 'test' : 'live';
$prefix = 'test' === $mode ? 'test_' : '';
$refresh_token = $options[ $prefix . 'refresh_token' ];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct() {
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct() {
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
Expand Down
2 changes: 1 addition & 1 deletion includes/payment-methods/class-wc-gateway-stripe-eps.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct() {
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct() {
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
Expand Down
2 changes: 1 addition & 1 deletion includes/payment-methods/class-wc-gateway-stripe-ideal.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct() {
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->testmode = WC_Stripe_Mode::is_test();
$this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
Expand Down
Loading
Loading