diff --git a/modules/ppcp-wc-gateway/src/Helper/ConnectionState.php b/modules/ppcp-wc-gateway/src/Helper/ConnectionState.php new file mode 100644 index 000000000..5eb164d1d --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Helper/ConnectionState.php @@ -0,0 +1,119 @@ +is_connected = $is_connected; + $this->environment = $environment; + } + + /** + * Set connection status to "connected to PayPal" (end onboarding). + * + * @param bool $is_sandbox Whether to connect to a sandbox environment. + */ + public function connect( bool $is_sandbox = false ) : void { + $this->is_connected = true; + $this->environment->set_environment( $is_sandbox ); + } + + /** + * Set connection status to "not connected to PayPal" (start onboarding). + */ + public function disconnect() : void { + $this->is_connected = false; + } + + /** + * Returns the managed environment instance. + * + * @return Environment The environment instance. + */ + public function get_environment() : Environment { + return $this->environment; + } + + /** + * Is the merchant connected to a PayPal account? + * + * @return bool True, if onboarding was completed and connection details are present. + */ + public function is_connected() : bool { + return $this->is_connected; + } + + /** + * Is the merchant currently in the "onboarding phase"? + * + * @return bool True, if we don't know merchant connection details. + */ + public function is_onboarding() : bool { + return ! $this->is_connected; + } + + /** + * Is the merchant connected to a sandbox environment? + * + * @return bool True, if connected to a sandbox environment. + */ + public function is_sandbox() : bool { + return $this->is_connected && $this->environment->is_sandbox(); + } + + /** + * Is the merchant connected to a production environment and can receive payments? + * + * @return bool True, if connected to a production environment. + */ + public function is_production() : bool { + return $this->is_connected && $this->environment->is_production(); + } + + /** + * Returns the current environment's name. + * + * @return string Name of the currently connected environment; empty string if not connected. + */ + public function current_environment() : string { + return $this->is_connected ? $this->environment->current_environment() : ''; + } +} diff --git a/modules/ppcp-wc-gateway/src/Helper/Environment.php b/modules/ppcp-wc-gateway/src/Helper/Environment.php index 141e329f5..72df40681 100644 --- a/modules/ppcp-wc-gateway/src/Helper/Environment.php +++ b/modules/ppcp-wc-gateway/src/Helper/Environment.php @@ -45,7 +45,7 @@ class Environment { * * @param bool $is_sandbox Whether this instance represents a sandbox environment. */ - private function set_environment( bool $is_sandbox ) : void { + public function set_environment( bool $is_sandbox ) : void { if ( $is_sandbox ) { $this->environment_name = self::SANDBOX; } else {