diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php index 4ae2120f2..e3aa4b0b1 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/WcGateway.php @@ -92,18 +92,24 @@ class WcGateway extends WcGatewayBase public function captureAuthorizedPayment(\WC_Order $wcOrder): bool { - $result = $this->authorizedPayments->process($wcOrder); + $isProcessed = $this->authorizedPayments->process($wcOrder); + $this->renderAuthorizationMessageForStatus($this->authorizedPayments->lastStatus()); - if ($result === AuthorizedPaymentsProcessor::INACCESSIBLE) { - $this->notice->displayMessage(AuthorizeOrderActionNotice::NO_INFO); - return false; - } - if ($result === AuthorizedPaymentsProcessor::NOT_FOUND) { - $this->notice->displayMessage(AuthorizeOrderActionNotice::NOT_FOUND); - return false; + if ($isProcessed) { + $wcOrder->add_order_note( + __( + 'Payment successfully captured.', + 'woocommerce-paypal-gateway' + ) + ); + + $wcOrder->set_status('processing'); + $wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true'); + $wcOrder->save(); + return true; } - if ($result === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) { + if ($this->authorizedPayments->lastStatus() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED) { if ($wcOrder->get_status() === 'on-hold') { $wcOrder->add_order_note( __( @@ -116,28 +122,19 @@ class WcGateway extends WcGatewayBase $wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true'); $wcOrder->save(); - $this->notice->displayMessage(AuthorizeOrderActionNotice::ALREADY_CAPTURED); return true; } + } + private function renderAuthorizationMessageForStatus(string $status) { - if ($result !== AuthorizedPaymentsProcessor::SUCCESSFUL) { - $this->notice->displayMessage(AuthorizeOrderActionNotice::FAILED); - return false; - } - - $wcOrder->add_order_note( - __( - 'Payment successfully captured.', - 'woocommerce-paypal-gateway' - ) - ); - - $wcOrder->set_status('processing'); - $wcOrder->update_meta_data(self::CAPTURED_META_KEY, 'true'); - $wcOrder->save(); - - $this->notice->displayMessage(AuthorizeOrderActionNotice::SUCCESS); - return true; + $messageMapping = [ + AuthorizedPaymentsProcessor::SUCCESSFUL => AuthorizeOrderActionNotice::SUCCESS, + AuthorizedPaymentsProcessor::ALREADY_CAPTURED => AuthorizeOrderActionNotice::ALREADY_CAPTURED, + AuthorizedPaymentsProcessor::INACCESSIBLE => AuthorizeOrderActionNotice::NO_INFO, + AuthorizedPaymentsProcessor::NOT_FOUND => AuthorizeOrderActionNotice::NOT_FOUND, + ]; + $displayMessage = (isset($messageMapping[$status])) ? $messageMapping[$status] : AuthorizeOrderActionNotice::FAILED; + $this->notice->displayMessage($displayMessage); } } diff --git a/modules.local/ppcp-wc-gateway/src/Processor/AuthorizedPaymentsProcessor.php b/modules.local/ppcp-wc-gateway/src/Processor/AuthorizedPaymentsProcessor.php index 2020591ef..921c788f1 100644 --- a/modules.local/ppcp-wc-gateway/src/Processor/AuthorizedPaymentsProcessor.php +++ b/modules.local/ppcp-wc-gateway/src/Processor/AuthorizedPaymentsProcessor.php @@ -21,6 +21,7 @@ class AuthorizedPaymentsProcessor public const NOT_FOUND = 'NOT_FOUND'; private $orderEndpoint; private $paymentsEndpoint; + private $lastStatus = ''; public function __construct( OrderEndpoint $orderEndpoint, @@ -31,30 +32,39 @@ class AuthorizedPaymentsProcessor $this->paymentsEndpoint = $paymentsEndpoint; } - public function process(\WC_Order $wcOrder): string + public function process(\WC_Order $wcOrder): bool { try { $order = $this->payPalOrderFromWcOrder($wcOrder); } catch (Exception $exception) { if ($exception->getCode() === 404) { - return self::NOT_FOUND; + $this->lastStatus = self::NOT_FOUND; + return false; } - return self::INACCESSIBLE; + $this->lastStatus = self::INACCESSIBLE; + return false; } $authorizations = $this->allAuthorizations($order); if (!$this->areAuthorizationToCapture(...$authorizations)) { - return self::ALREADY_CAPTURED; + $this->lastStatus = self::ALREADY_CAPTURED; + return false; } try { $this->captureAuthorizations(...$authorizations); } catch (Exception $exception) { - return self::FAILED; + $this->lastStatus = self::FAILED; + return false; } - return self::SUCCESSFUL; + $this->lastStatus = self::SUCCESSFUL; + return true; + } + + public function lastStatus() : string { + return $this->lastStatus; } private function payPalOrderFromWcOrder(\WC_Order $wcOrder): Order diff --git a/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php index 10f998a2d..17109e236 100644 --- a/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/AuthorizedPaymentsProcessorTest.php @@ -66,8 +66,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase ->expects('get_meta') ->with(WcGateway::ORDER_ID_META_KEY) ->andReturn($orderId); - $result = $testee->process($wcOrder); - $this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $result); + $this->assertTrue($testee->process($wcOrder)); + $this->assertEquals(AuthorizedPaymentsProcessor::SUCCESSFUL, $testee->lastStatus()); } public function testInaccessible() { @@ -85,8 +85,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase ->expects('get_meta') ->with(WcGateway::ORDER_ID_META_KEY) ->andReturn($orderId); - $result = $testee->process($wcOrder); - $this->assertEquals(AuthorizedPaymentsProcessor::INACCESSIBLE, $result); + $this->assertFalse($testee->process($wcOrder)); + $this->assertEquals(AuthorizedPaymentsProcessor::INACCESSIBLE, $testee->lastStatus()); } public function testNotFound() { @@ -104,8 +104,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase ->expects('get_meta') ->with(WcGateway::ORDER_ID_META_KEY) ->andReturn($orderId); - $result = $testee->process($wcOrder); - $this->assertEquals(AuthorizedPaymentsProcessor::NOT_FOUND, $result); + $this->assertFalse($testee->process($wcOrder)); + $this->assertEquals(AuthorizedPaymentsProcessor::NOT_FOUND, $testee->lastStatus()); } public function testCaptureFails() { @@ -156,8 +156,8 @@ class AuthorizedPaymentsProcessorTest extends TestCase ->expects('get_meta') ->with(WcGateway::ORDER_ID_META_KEY) ->andReturn($orderId); - $result = $testee->process($wcOrder); - $this->assertEquals(AuthorizedPaymentsProcessor::FAILED, $result); + $this->assertFalse($testee->process($wcOrder)); + $this->assertEquals(AuthorizedPaymentsProcessor::FAILED, $testee->lastStatus()); } public function testAllAreCaptured() { @@ -204,7 +204,7 @@ class AuthorizedPaymentsProcessorTest extends TestCase ->expects('get_meta') ->with(WcGateway::ORDER_ID_META_KEY) ->andReturn($orderId); - $result = $testee->process($wcOrder); - $this->assertEquals(AuthorizedPaymentsProcessor::ALREADY_CAPTURED, $result); + $this->assertFalse($testee->process($wcOrder)); + $this->assertEquals(AuthorizedPaymentsProcessor::ALREADY_CAPTURED, $testee->lastStatus()); } } \ No newline at end of file