Skip to content

Commit 1e4a049

Browse files
authored
LYNX-240: Cancel order after refund for offline-paid orders
1 parent c22a9db commit 1e4a049

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

app/code/Magento/OrderCancellation/Model/CancelOrder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,18 @@ public function execute(
9494
} else {
9595
if ($payment->getMethodInstance()->isOffline()) {
9696
$this->refundOrder->execute($order->getEntityId());
97+
// for partially invoiced orders we need to cancel after doing the refund
98+
// so not invoiced items are cancelled and the whole order is set to cancelled
99+
$order = $this->orderRepository->get($order->getId());
100+
$order->cancel();
97101
} else {
98102
/** @var Order\Invoice $invoice */
99103
foreach ($order->getInvoiceCollection() as $invoice) {
100104
$this->refundInvoice->execute($invoice->getEntityId());
101105
}
106+
// in this case order needs to be re-instantiated
107+
$order = $this->orderRepository->get($order->getId());
102108
}
103-
// in this case order needs to be re-instantiated
104-
$order = $this->orderRepository->get($order->getId());
105109
}
106110

107111
$result = $this->sender->send(

dev/tests/api-functional/testsuite/Magento/GraphQl/OrderCancellation/CancelOrderTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,92 @@ public function testCancelOrderAttemptingXSSPassedThroughReasonField()
559559
$this->assertEquals('closed', $comment->getStatus());
560560
}
561561

562+
#[
563+
DataFixture(Store::class),
564+
DataFixture(
565+
Customer::class,
566+
[
567+
'email' => '[email protected]',
568+
'password' => 'password'
569+
],
570+
'customer'
571+
),
572+
DataFixture(ProductFixture::class, as: 'product1'),
573+
DataFixture(ProductFixture::class, as: 'product2'),
574+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'),
575+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product1.id$']),
576+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product2.id$']),
577+
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
578+
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
579+
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
580+
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
581+
DataFixture(PlaceOrderFixture::class, ['cart_id' => '$cart.id$'], 'order'),
582+
DataFixture(
583+
InvoiceFixture::class,
584+
[
585+
'order_id' => '$order.id$',
586+
'items' => ['$product1.sku$']
587+
],
588+
'invoice'
589+
),
590+
Config('sales/cancellation/enabled', 1)
591+
]
592+
public function testCancelPartiallyInvoicedOrder()
593+
{
594+
/**
595+
* @var $order OrderInterface
596+
*/
597+
$order = DataFixtureStorageManager::getStorage()->get('order');
598+
$query = <<<QUERY
599+
mutation {
600+
cancelOrder(
601+
input: {
602+
order_id: "{$order->getEntityId()}"
603+
reason: "Cancel sample reason"
604+
}
605+
){
606+
error
607+
order {
608+
status
609+
}
610+
}
611+
}
612+
QUERY;
613+
$customerToken = $this->getHeaders();
614+
615+
$response = $this->graphQlMutation(
616+
$query,
617+
[],
618+
'',
619+
$customerToken
620+
);
621+
622+
$this->assertEquals(
623+
[
624+
'cancelOrder' =>
625+
[
626+
'error' => null,
627+
'order' => [
628+
'status' => 'Canceled'
629+
]
630+
]
631+
],
632+
$response
633+
);
634+
635+
$comments = $order->getStatusHistories();
636+
637+
$comment = array_pop($comments);
638+
$this->assertEquals("We refunded $20.00 offline.", $comment->getComment());
639+
640+
$comment = array_pop($comments);
641+
$this->assertEquals("Order cancellation notification email was sent.", $comment->getComment());
642+
643+
$comment = array_pop($comments);
644+
$this->assertEquals('Cancel sample reason', $comment->getComment());
645+
$this->assertEquals('canceled', $comment->getStatus());
646+
}
647+
562648
/**
563649
* @return string[]
564650
* @throws AuthenticationException|LocalizedException

0 commit comments

Comments
 (0)