Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #53 - cancel an empty payment #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading