mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
seperate state detection by production/sandbox
This commit is contained in:
parent
3733576dff
commit
e5a569e0a0
2 changed files with 93 additions and 24 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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'] );
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue