seperate state detection by production/sandbox

This commit is contained in:
David Remer 2020-09-25 11:38:22 +03:00
parent 3733576dff
commit e5a569e0a0
2 changed files with 93 additions and 24 deletions

View file

@ -50,34 +50,96 @@ class State {
} }
/** /**
* Returns the current onboarding state. * Returns the current active onboarding state.
* *
* @return int * @return int
*/ */
public function current_state(): int { public function current_state(): int {
$value = self::STATE_START;
/** return $this->state_by_keys(
* Having provided the merchant email means, we are at least array(
* in the progressive phase of our onboarding. 'merchant_email',
*/ ),
if ( array(
$this->settings->has( 'merchant_email' ) 'merchant_email',
&& is_email( $this->settings->get( 'merchant_email' ) ) 'merchant_id',
) { 'client_id',
$value = self::STATE_PROGRESSIVE; '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;
} }
/** $is_onboarded = true;
* Once we can fetch credentials we are completely onboarded. foreach ( $onboarded_keys as $key ) {
*/ if ( ! $this->settings->has( $key ) || ! $this->settings->get( $key ) ) {
if ( $is_onboarded = false;
$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;
} }
return $value;
if ( $is_onboarded ) {
$state = self::STATE_ONBOARDED;
}
return $state;
} }
} }

View file

@ -1660,10 +1660,17 @@ return array(
unset( $fields['vault_enabled'] ); 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'] ); 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'] ); unset( $fields['ppcp_onboarding_sandbox'] );
} }