diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 667cd2766..e1aadeb83 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -96,6 +96,13 @@ class PayPalGateway extends \WC_Payment_Gateway { */ private $refund_processor; + /** + * Whether the plugin is in onboarded state. + * + * @var bool + */ + private $onboarded; + /** * PayPalGateway constructor. * @@ -132,8 +139,9 @@ public function __construct( $this->session_handler = $session_handler; $this->refund_processor = $refund_processor; $this->transaction_url_provider = $transaction_url_provider; + $this->onboarded = $state->current_state() === State::STATE_ONBOARDED; - if ( $state->current_state() === State::STATE_ONBOARDED ) { + if ( $this->onboarded ) { $this->supports = array( 'refunds' ); } if ( @@ -185,7 +193,7 @@ public function __construct( */ public function needs_setup(): bool { - return true; + return ! $this->onboarded; } /** @@ -373,4 +381,20 @@ public function get_transaction_url( $order ): string { return parent::get_transaction_url( $order ); } + + /** + * Updates WooCommerce gateway option. + * + * @param string $key The option key. + * @param string $value The option value. + * @return bool|void + */ + public function update_option( $key, $value = '' ) { + parent::update_option( $key, $value ); + + if ( 'enabled' === $key ) { + $this->config->set( 'enabled', 'yes' === $value ); + $this->config->persist(); + } + } } diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index 716a86ac2..f46ac5865 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -4,6 +4,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway; +use Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; @@ -365,6 +366,45 @@ public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $e $this->assertFalse($testee->capture_authorized_payment($wcOrder)); } + + /** + * @dataProvider dataForTestNeedsSetup + */ + public function testNeedsSetup($currentState, $needSetup) + { + expect('is_admin')->andReturn(true); + $settingsRenderer = Mockery::mock(SettingsRenderer::class); + $orderProcessor = Mockery::mock(OrderProcessor::class); + $authorizedOrdersProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class); + $authorizeOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class); + $config = Mockery::mock(ContainerInterface::class); + $config + ->shouldReceive('has') + ->andReturn(false); + $sessionHandler = Mockery::mock(SessionHandler::class); + $refundProcessor = Mockery::mock(RefundProcessor::class); + $onboardingState = Mockery::mock(State::class); + $onboardingState + ->expects('current_state') + ->andReturn($currentState); + $transactionUrlProvider = Mockery::mock(TransactionUrlProvider::class); + $subscriptionHelper = Mockery::mock(SubscriptionHelper::class); + + $testee = new PayPalGateway( + $settingsRenderer, + $orderProcessor, + $authorizedOrdersProcessor, + $authorizeOrderActionNotice, + $config, + $sessionHandler, + $refundProcessor, + $onboardingState, + $transactionUrlProvider, + $subscriptionHelper + ); + + $this->assertSame($needSetup, $testee->needs_setup()); + } public function dataForTestCaptureAuthorizedPaymentNoActionableFailures() : array { @@ -383,4 +423,13 @@ public function dataForTestCaptureAuthorizedPaymentNoActionableFailures() : arra ], ]; } + + public function dataForTestNeedsSetup(): array + { + return [ + [State::STATE_START, true], + [State::STATE_PROGRESSIVE, true], + [State::STATE_ONBOARDED, false] + ]; + } }