Skip to content

Commit

Permalink
Merge pull request #54 from cbastienbaron/fix/issue-53-cancel-empty-p…
Browse files Browse the repository at this point in the history
…ayment

Fix #53 - cancel an empty payment
  • Loading branch information
Prometee authored Jun 11, 2024
2 parents 7ee8088 + a862c77 commit 3034701
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion features/admin/cancel_authorized_order.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions features/admin/cancel_order.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/CommandHandler/CancelPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function __invoke(CancelPayment $command): void
return;
}

if (0 === count($payment->getDetails())) {
return;
}

$gatewayName = $this->getGatewayNameFromPayment($payment);

if (null === $gatewayName) {
Expand Down
22 changes: 19 additions & 3 deletions tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)$/
*/
Expand All @@ -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();
}
Expand Down

0 comments on commit 3034701

Please sign in to comment.