diff --git a/src/Entity/Payment/PaymentCaptureForm.php b/src/Entity/Payment/PaymentCaptureForm.php index 54f1d53..f2263bf 100644 --- a/src/Entity/Payment/PaymentCaptureForm.php +++ b/src/Entity/Payment/PaymentCaptureForm.php @@ -69,9 +69,14 @@ class PaymentCaptureForm extends ContentEntityConfirmFormBase { $payment = $this->getEntity(); /** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodCapturePaymentInterface $payment_method */ $payment_method = $payment->getPaymentMethod(); - $payment_method->capturePayment(); + $result = $payment_method->capturePayment(); - $form_state->setRedirectUrl($payment->urlInfo()); + if ($result->isCompleted()) { + $form_state->setRedirectUrl($payment->urlInfo()); + } + else { + $form_state->setResponse($result->getCompletionResponse()->getResponse()); + } } } diff --git a/src/Entity/Payment/PaymentRefundForm.php b/src/Entity/Payment/PaymentRefundForm.php index a1a8052..0174cd3 100644 --- a/src/Entity/Payment/PaymentRefundForm.php +++ b/src/Entity/Payment/PaymentRefundForm.php @@ -69,9 +69,14 @@ class PaymentRefundForm extends ContentEntityConfirmFormBase { $payment = $this->getEntity(); /** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodRefundPaymentInterface $payment_method */ $payment_method = $payment->getPaymentMethod(); - $payment_method->refundPayment(); + $result = $payment_method->refundPayment(); - $form_state->setRedirectUrl($payment->urlInfo()); + if ($result->isCompleted()) { + $form_state->setRedirectUrl($payment->urlInfo()); + } + else { + $form_state->setResponse($result->getCompletionResponse()->getResponse()); + } } } diff --git a/src/Plugin/Payment/Method/PaymentMethodBase.php b/src/Plugin/Payment/Method/PaymentMethodBase.php index 37cb20b..e337d8e 100644 --- a/src/Plugin/Payment/Method/PaymentMethodBase.php +++ b/src/Plugin/Payment/Method/PaymentMethodBase.php @@ -287,6 +287,15 @@ abstract class PaymentMethodBase extends PluginBase implements ContainerFactoryP } $this->eventDispatcher->preCapturePayment($this->getPayment()); $this->doCapturePayment(); + + return $this->getPaymentCaptureResult(); + } + + /** + * {@inheritdoc} + */ + public function getPaymentCaptureResult() { + return new OperationResult(); } /** @@ -328,6 +337,15 @@ abstract class PaymentMethodBase extends PluginBase implements ContainerFactoryP } $this->eventDispatcher->preRefundPayment($this->getPayment()); $this->doRefundPayment(); + + return $this->getPaymentRefundResult(); + } + + /** + * {@inheritdoc} + */ + public function getPaymentRefundResult() { + return new OperationResult(); } /** diff --git a/src/Plugin/Payment/Method/PaymentMethodCapturePaymentInterface.php b/src/Plugin/Payment/Method/PaymentMethodCapturePaymentInterface.php index e389b4a..f07259e 100644 --- a/src/Plugin/Payment/Method/PaymentMethodCapturePaymentInterface.php +++ b/src/Plugin/Payment/Method/PaymentMethodCapturePaymentInterface.php @@ -37,8 +37,17 @@ interface PaymentMethodCapturePaymentInterface { * \Drupal\payment\Event\PaymentEvents::PAYMENT_PRE_CAPTURE Symfony event * before capture. * + * @return \Drupal\payment\OperationResultInterface + * * @see self::capturePaymentAccess */ public function capturePayment(); + /** + * Gets the payment capture status. + * + * @return \Drupal\payment\OperationResultInterface + */ + public function getPaymentCaptureResult(); + } diff --git a/src/Plugin/Payment/Method/PaymentMethodRefundPaymentInterface.php b/src/Plugin/Payment/Method/PaymentMethodRefundPaymentInterface.php index 78407c1..04e80cd 100644 --- a/src/Plugin/Payment/Method/PaymentMethodRefundPaymentInterface.php +++ b/src/Plugin/Payment/Method/PaymentMethodRefundPaymentInterface.php @@ -37,8 +37,17 @@ interface PaymentMethodRefundPaymentInterface { * \Drupal\payment\Event\PaymentEvents::PAYMENT_PRE_REFUND Symfony event * before refunding the payment. * + * @return \Drupal\payment\OperationResultInterface + * * @see self::refundPaymentAccess */ public function refundPayment(); + /** + * Gets the payment refund status. + * + * @return \Drupal\payment\OperationResultInterface + */ + public function getPaymentRefundResult(); + } diff --git a/tests/src/Unit/Entity/Payment/PaymentCaptureFormTest.php b/tests/src/Unit/Entity/Payment/PaymentCaptureFormTest.php index 49d690e..5930363 100644 --- a/tests/src/Unit/Entity/Payment/PaymentCaptureFormTest.php +++ b/tests/src/Unit/Entity/Payment/PaymentCaptureFormTest.php @@ -12,9 +12,12 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\payment\Entity\Payment\PaymentCaptureForm; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\OperationResultInterface; use Drupal\payment\Plugin\Payment\Method\PaymentMethodCapturePaymentInterface; +use Drupal\payment\Response\ResponseInterface; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Response; /** * @coversDefaultClass \Drupal\payment\Entity\Payment\PaymentCaptureForm @@ -114,10 +117,56 @@ class PaymentCaptureFormTest extends UnitTestCase { /** * @covers ::submitForm */ - function testSubmitForm() { + function testSubmitFormWithCompletionResponse() { + $response = $this->getMockBuilder(Response::class) + ->disableOriginalConstructor() + ->getMock(); + + $completion_response = $this->getMock(ResponseInterface::class); + $completion_response->expects($this->atLeastOnce()) + ->method('getResponse') + ->willReturn($response); + + $operation_result = $this->getMock(OperationResultInterface::class); + $operation_result->expects($this->atLeastOnce()) + ->method('getCompletionResponse') + ->willReturn($completion_response); + $operation_result->expects($this->atLeastOnce()) + ->method('isCompleted') + ->willReturn(FALSE); + + $payment_method = $this->getMock(PaymentMethodCapturePaymentInterface::class); + $payment_method->expects($this->once()) + ->method('capturePayment') + ->willReturn($operation_result); + + $this->payment->expects($this->atLeastOnce()) + ->method('getPaymentMethod') + ->willReturn($payment_method); + + $form = []; + + $form_state = $this->getMock(FormStateInterface::class); + $form_state->expects($this->atLeastOnce()) + ->method('setResponse') + ->with($response); + + $this->sut->submitForm($form, $form_state); + } + + /** + * @covers ::submitForm + */ + function testSubmitFormWithoutCompletionResponse() { + $operation_result = $this->getMock(OperationResultInterface::class); + $operation_result->expects($this->atLeastOnce()) + ->method('isCompleted') + ->willReturn(TRUE); + $payment_method = $this->getMock(PaymentMethodCapturePaymentInterface::class); $payment_method->expects($this->once()) - ->method('capturePayment'); + ->method('capturePayment') + ->willReturn($operation_result); $url = new Url($this->randomMachineName()); @@ -132,7 +181,7 @@ class PaymentCaptureFormTest extends UnitTestCase { $form = []; $form_state = $this->getMock(FormStateInterface::class); - $form_state->expects($this->once()) + $form_state->expects($this->atLeastOnce()) ->method('setRedirectUrl') ->with($url); diff --git a/tests/src/Unit/Entity/Payment/PaymentRefundFormTest.php b/tests/src/Unit/Entity/Payment/PaymentRefundFormTest.php index 0d86cf9..bf8f282 100644 --- a/tests/src/Unit/Entity/Payment/PaymentRefundFormTest.php +++ b/tests/src/Unit/Entity/Payment/PaymentRefundFormTest.php @@ -12,9 +12,12 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\payment\Entity\Payment\PaymentRefundForm; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\OperationResultInterface; use Drupal\payment\Plugin\Payment\Method\PaymentMethodRefundPaymentInterface; +use Drupal\payment\Response\ResponseInterface; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Response; /** * @coversDefaultClass \Drupal\payment\Entity\Payment\PaymentRefundForm @@ -114,10 +117,56 @@ class PaymentRefundFormTest extends UnitTestCase { /** * @covers ::submitForm */ - function testSubmitForm() { + function testSubmitFormWithCompletionResponse() { + $response = $this->getMockBuilder(Response::class) + ->disableOriginalConstructor() + ->getMock(); + + $completion_response = $this->getMock(ResponseInterface::class); + $completion_response->expects($this->atLeastOnce()) + ->method('getResponse') + ->willReturn($response); + + $operation_result = $this->getMock(OperationResultInterface::class); + $operation_result->expects($this->atLeastOnce()) + ->method('getCompletionResponse') + ->willReturn($completion_response); + $operation_result->expects($this->atLeastOnce()) + ->method('isCompleted') + ->willReturn(FALSE); + + $payment_method = $this->getMock(PaymentMethodRefundPaymentInterface::class); + $payment_method->expects($this->once()) + ->method('refundPayment') + ->willReturn($operation_result); + + $this->payment->expects($this->atLeastOnce()) + ->method('getPaymentMethod') + ->willReturn($payment_method); + + $form = []; + + $form_state = $this->getMock(FormStateInterface::class); + $form_state->expects($this->atLeastOnce()) + ->method('setResponse') + ->with($response); + + $this->sut->submitForm($form, $form_state); + } + + /** + * @covers ::submitForm + */ + function testSubmitFormWithoutCompletionResponse() { + $operation_result = $this->getMock(OperationResultInterface::class); + $operation_result->expects($this->atLeastOnce()) + ->method('isCompleted') + ->willReturn(TRUE); + $payment_method = $this->getMock(PaymentMethodRefundPaymentInterface::class); $payment_method->expects($this->once()) - ->method('refundPayment'); + ->method('refundPayment') + ->willReturn($operation_result); $url = new Url($this->randomMachineName()); @@ -130,8 +179,9 @@ class PaymentRefundFormTest extends UnitTestCase { ->willReturn($url); $form = []; + $form_state = $this->getMock(FormStateInterface::class); - $form_state->expects($this->once()) + $form_state->expects($this->atLeastOnce()) ->method('setRedirectUrl') ->with($url); diff --git a/tests/src/Unit/OperationResultTest.php b/tests/src/Unit/OperationResultTest.php new file mode 100644 index 0000000..378a849 --- /dev/null +++ b/tests/src/Unit/OperationResultTest.php @@ -0,0 +1,66 @@ +response = $this->getMock(ResponseInterface::class); + } + + /** + * @covers ::isCompleted + * @covers ::__construct + */ + function testIsCompleted() { + $this->sut = new OperationResult(); + $this->assertTrue($this->sut->isCompleted()); + + $this->sut = new OperationResult($this->response); + $this->assertFalse($this->sut->isCompleted()); + } + + /** + * @covers ::getCompletionResponse + * @covers ::__construct + */ + function testGetCompletionResponse() { + $this->sut = new OperationResult(); + $this->assertNULL($this->sut->getCompletionResponse()); + + $this->sut = new OperationResult($this->response); + $this->assertSame($this->response, $this->sut->getCompletionResponse()); + } + +} diff --git a/tests/src/Unit/PaymentExecutionResultTest.php b/tests/src/Unit/PaymentExecutionResultTest.php deleted file mode 100644 index b9c711e..0000000 --- a/tests/src/Unit/PaymentExecutionResultTest.php +++ /dev/null @@ -1,66 +0,0 @@ -response = $this->getMock(ResponseInterface::class); - } - - /** - * @covers ::hasCompleted - * @covers ::__construct - */ - function testHasCompleted() { - $this->sut = new OperationResult(); - $this->assertTrue($this->sut->isCompleted()); - - $this->sut = new OperationResult($this->response); - $this->assertFalse($this->sut->isCompleted()); - } - - /** - * @covers ::getCompletionResponse - * @covers ::__construct - */ - function testGetCompletionResponse() { - $this->sut = new OperationResult(); - $this->assertNULL($this->sut->getCompletionResponse()); - - $this->sut = new OperationResult($this->response); - $this->assertSame($this->response, $this->sut->getCompletionResponse()); - } - -} diff --git a/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseTest.php b/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseTest.php index 7dfd010..4f2cc6b 100644 --- a/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseTest.php +++ b/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseTest.php @@ -16,6 +16,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\OperationResultInterface; use Drupal\payment\Plugin\Payment\Method\PaymentMethodBase; use Drupal\payment\Plugin\Payment\Method\SupportedCurrency; use Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface; @@ -315,7 +316,9 @@ class PaymentMethodBaseTest extends PaymentMethodBaseTestBase { $this->sut->setPayment($payment); - $this->sut->capturePayment(); + $result = $this->sut->capturePayment(); + + $this->assertInstanceOf(OperationResultInterface::class, $result); } /** @@ -365,7 +368,9 @@ class PaymentMethodBaseTest extends PaymentMethodBaseTestBase { $this->sut->setPayment($payment); - $this->sut->refundPayment(); + $result = $this->sut->refundPayment(); + + $this->assertInstanceOf(OperationResultInterface::class, $result); } /**