From b91ede355cfed90dc2bff2cce5fedf97689548c0 Mon Sep 17 00:00:00 2001 From: Kirill Braslavsky Date: Tue, 27 Jul 2021 19:20:51 +0300 Subject: [PATCH 1/5] store onboarded flag in the class properties --- .../src/Gateway/class-paypalgateway.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 667cd2766..aed294bb7 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -95,7 +95,14 @@ class PayPalGateway extends \WC_Payment_Gateway { * @var RefundProcessor */ 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 ( From 257842b7f7e5f585f044e31dc8230c43d5f1f42d Mon Sep 17 00:00:00 2001 From: Kirill Braslavsky Date: Tue, 27 Jul 2021 19:53:54 +0300 Subject: [PATCH 2/5] don't redirect to gateway settings if onboarded --- modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index aed294bb7..a18f36764 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -193,7 +193,7 @@ public function __construct( */ public function needs_setup(): bool { - return true; + return ! $this->onboarded; } /** From 4bd4613255d64ac9b98f0d6a0b656eb7f337ebba Mon Sep 17 00:00:00 2001 From: Kirill Braslavsky Date: Tue, 27 Jul 2021 19:56:59 +0300 Subject: [PATCH 3/5] auto fix phpcs errors --- modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index a18f36764..0748445b9 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -95,14 +95,14 @@ class PayPalGateway extends \WC_Payment_Gateway { * @var RefundProcessor */ private $refund_processor; - + /** * Whether the plugin is in onboarded state. * * @var bool */ private $onboarded; - + /** * PayPalGateway constructor. * From 282ff63330c44cc88d60f37da39fd8e6f256b731 Mon Sep 17 00:00:00 2001 From: Kirill Braslavsky Date: Wed, 28 Jul 2021 10:03:43 +0300 Subject: [PATCH 4/5] Add tests --- .../WcGateway/Gateway/WcGatewayTest.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) 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] + ]; + } } From 008fcbc5f9e221b68f8db0beb4f2ba367ed8307d Mon Sep 17 00:00:00 2001 From: dinamiko Date: Mon, 2 Aug 2021 16:24:41 +0200 Subject: [PATCH 5/5] Implement `update_option` method in PayPal gateway to synchronize gateway enabled option --- .../src/Gateway/class-paypalgateway.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php index 0748445b9..e1aadeb83 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-paypalgateway.php @@ -381,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(); + } + } }