From e5a569e0a0cbefe991ee430685a444284400d748 Mon Sep 17 00:00:00 2001 From: David Remer Date: Fri, 25 Sep 2020 11:38:22 +0300 Subject: [PATCH] seperate state detection by production/sandbox --- modules/ppcp-onboarding/src/class-state.php | 106 ++++++++++++++++---- modules/ppcp-wc-gateway/services.php | 11 +- 2 files changed, 93 insertions(+), 24 deletions(-) diff --git a/modules/ppcp-onboarding/src/class-state.php b/modules/ppcp-onboarding/src/class-state.php index c1507593e..c02367daa 100644 --- a/modules/ppcp-onboarding/src/class-state.php +++ b/modules/ppcp-onboarding/src/class-state.php @@ -50,34 +50,96 @@ class State { } /** - * Returns the current onboarding state. + * Returns the current active onboarding state. * * @return int */ public function current_state(): int { - $value = self::STATE_START; - /** - * Having provided the merchant email means, we are at least - * in the progressive phase of our onboarding. - */ - if ( - $this->settings->has( 'merchant_email' ) - && is_email( $this->settings->get( 'merchant_email' ) ) - ) { - $value = self::STATE_PROGRESSIVE; + + return $this->state_by_keys( + array( + 'merchant_email', + ), + array( + 'merchant_email', + 'merchant_id', + 'client_id', + 'client_secret', + ) + ); + } + + /** + * Returns the onboarding state of the sandbox. + * + * @return int + */ + public function sandbox_state() : int { + + return $this->state_by_keys( + array( + 'merchant_email_sandbox', + ), + array( + 'merchant_email_sandbox', + 'merchant_id_sandbox', + 'client_id_sandbox', + 'client_secret_sandbox', + ) + ); + } + + /** + * Returns the onboarding state of the production mode. + * + * @return int + */ + public function production_state() : int { + + return $this->state_by_keys( + array( + 'merchant_email_production', + ), + array( + 'merchant_email_production', + 'merchant_id_production', + 'client_id_production', + 'client_secret_production', + ) + ); + } + + /** + * Returns the state based on progressive and onboarded values being looked up in the settings. + * + * @param array $progressive_keys The keys which need to be present to be at least in progressive state. + * @param array $onboarded_keys The keys which need to be present to be in onboarded state. + * + * @return int + */ + private function state_by_keys( array $progressive_keys, array $onboarded_keys ) : int { + $state = self::STATE_START; + $is_progressive = true; + foreach ( $progressive_keys as $key ) { + if ( ! $this->settings->has( $key ) || ! $this->settings->get( $key ) ) { + $is_progressive = false; + } + } + if ( $is_progressive ) { + $state = self::STATE_PROGRESSIVE; } - /** - * Once we can fetch credentials we are completely onboarded. - */ - if ( - $this->settings->has( 'client_id' ) && $this->settings->get( 'client_id' ) - && $this->settings->has( 'client_secret' ) && $this->settings->get( 'client_secret' ) - && $this->settings->has( 'merchant_email' ) && $this->settings->get( 'merchant_email' ) - && $this->settings->has( 'merchant_id' ) && $this->settings->get( 'merchant_id' ) - ) { - $value = self::STATE_ONBOARDED; + $is_onboarded = true; + foreach ( $onboarded_keys as $key ) { + if ( ! $this->settings->has( $key ) || ! $this->settings->get( $key ) ) { + $is_onboarded = false; + } } - return $value; + + if ( $is_onboarded ) { + $state = self::STATE_ONBOARDED; + } + + return $state; } } diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 4560b8988..bccef7a21 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1660,10 +1660,17 @@ return array( unset( $fields['vault_enabled'] ); } - if ( $settings->has( 'merchant_email_production' ) && $settings->get( 'merchant_email_production' ) ) { + $state = $container->get('onboarding.state'); + /** + * The state. + * + * @var State $state + */ + + if ( State::STATE_ONBOARDED === $state->production_state() ) { unset( $fields['ppcp_onboarding_production'] ); } - if ( $settings->has( 'merchant_email_sandbox' ) && $settings->get( 'merchant_email_sandbox' ) ) { + if ( State::STATE_ONBOARDED === $state->sandbox_state() ) { unset( $fields['ppcp_onboarding_sandbox'] ); }