diff --git a/features/admin/cancel_authorized_order.feature b/features/admin/cancel_authorized_order.feature index ea306a4..3ee4100 100644 --- a/features/admin/cancel_authorized_order.feature +++ b/features/admin/cancel_authorized_order.feature @@ -16,7 +16,7 @@ Feature: Canceling an authorized order And I am logged in as an administrator @ui - Scenario: Initializing the Stripe refund + Scenario: Cancelling the order with an authorized payment Given I am viewing the summary of this order And I am prepared to cancel this order When I cancel this order diff --git a/features/admin/cancel_order.feature b/features/admin/cancel_order.feature index e15b7ec..81039df 100644 --- a/features/admin/cancel_order.feature +++ b/features/admin/cancel_order.feature @@ -16,9 +16,19 @@ Feature: Canceling an order And I am logged in as an administrator @ui - Scenario: Initializing the Stripe refund + Scenario: Cancelling the order when a checkout session is still available Given I am viewing the summary of this order - And I am prepared to expire the checkout session this order + And I am prepared to expire the checkout session on this order + When I cancel this order + Then I should be notified that it has been successfully updated + And it should have payment with state cancelled + And it should have payment state cancelled + + @ui + Scenario: Cancelling the order after the customer canceled the payment + Given this order payment has been canceled + And I am viewing the summary of this order + And I am prepared to cancel this order When I cancel this order Then I should be notified that it has been successfully updated And it should have payment with state cancelled diff --git a/src/CommandHandler/CancelPaymentHandler.php b/src/CommandHandler/CancelPaymentHandler.php index c562036..e46fbe6 100644 --- a/src/CommandHandler/CancelPaymentHandler.php +++ b/src/CommandHandler/CancelPaymentHandler.php @@ -35,6 +35,10 @@ public function __invoke(CancelPayment $command): void return; } + if (0 === count($payment->getDetails())) { + return; + } + $gatewayName = $this->getGatewayNameFromPayment($payment); if (null === $gatewayName) { diff --git a/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php b/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php index 492fbeb..e298a47 100644 --- a/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php +++ b/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php @@ -8,13 +8,14 @@ use Doctrine\Persistence\ObjectManager; use SM\Factory\FactoryInterface; use Stripe\Checkout\Session; -use Stripe\Event; use Stripe\PaymentIntent; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; +use Sylius\Component\Core\Model\Payment; use Sylius\Component\Payment\PaymentTransitions; use Sylius\Component\Resource\StateMachine\StateMachineInterface; use Tests\FluxSE\SyliusPayumStripePlugin\Behat\Mocker\StripeCheckoutSessionMocker; +use Webmozart\Assert\Assert; class ManagingOrdersContext implements Context { @@ -102,6 +103,21 @@ public function thisOrderIsNotYetPaid(OrderInterface $order, string $stripeCheck $this->objectManager->flush(); } + /** + * @Given /^(this order) payment has been canceled$/ + */ + public function thisOrderPaymentHasBeenCancelled(OrderInterface $order): void + { + /** @var PaymentInterface $payment */ + $payment = $order->getPayments()->first(); + + /** @var StateMachineInterface $stateMachine */ + $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); + $stateMachine->apply(PaymentTransitions::TRANSITION_CANCEL); + + $this->objectManager->flush(); + } + /** * @Given /^I am prepared to cancel (this order)$/ */ @@ -118,9 +134,9 @@ public function iAmPreparedToCancelThisOrder(OrderInterface $order): void } /** - * @Given I am prepared to expire the checkout session this order + * @Given I am prepared to expire the checkout session on this order */ - public function iAmPreparedToExpireTheCheckoutSessionThisOrder(): void + public function iAmPreparedToExpireTheCheckoutSessionOnThisOrder(): void { $this->stripeSessionCheckoutMocker->mockExpirePayment(); }