From 02ff6fe7e36fbc68a5954e6b0f969287c678086f Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Tue, 30 Jul 2019 10:19:40 +0200 Subject: [PATCH 1/4] Execute the $order->cancel() method so the `sales_order_payment_cancel` event is thrown --- Helper/General.php | 30 +++++++++++++---- Model/Client/Payments.php | 2 ++ Test/Unit/Helper/GeneralTest.php | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/Helper/General.php b/Helper/General.php index debecdeeeee..7dd8ac1d26b 100755 --- a/Helper/General.php +++ b/Helper/General.php @@ -12,6 +12,8 @@ use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\Locale\Resolver; use Magento\Framework\Math\Random as MathRandom; +use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\OrderManagementInterface; use Magento\Sales\Model\Order; use Magento\Sales\Model\OrderRepository; use Magento\Store\Model\StoreManagerInterface; @@ -21,6 +23,7 @@ use Mollie\Payment\Logger\MollieLogger; use Magento\SalesRule\Model\Coupon; use Magento\SalesRule\Model\ResourceModel\Coupon\Usage as CouponUsage; +use Mollie\Payment\Service\Order\OrderCommentHistory; /** * Class General @@ -131,6 +134,14 @@ class General extends AbstractHelper * @var CouponUsage */ private $couponUsage; + /** + * @var OrderCommentHistory + */ + private $orderCommentHistory; + /** + * @var OrderManagementInterface + */ + private $orderManagement; /** * General constructor. @@ -147,6 +158,8 @@ class General extends AbstractHelper * @param MollieLogger $logger * @param Coupon $coupon * @param CouponUsage $couponUsage + * @param OrderCommentHistory $orderCommentHistory + * @param OrderManagementInterface $orderManagement */ public function __construct( Context $context, @@ -160,7 +173,9 @@ public function __construct( MathRandom $mathRandom, MollieLogger $logger, Coupon $coupon, - CouponUsage $couponUsage + CouponUsage $couponUsage, + OrderCommentHistory $orderCommentHistory, + OrderManagementInterface $orderManagement ) { $this->paymentHelper = $paymentHelper; $this->storeManager = $storeManager; @@ -174,7 +189,9 @@ public function __construct( $this->logger = $logger; $this->coupon = $coupon; $this->couponUsage = $couponUsage; + $this->orderCommentHistory = $orderCommentHistory; parent::__construct($context); + $this->orderManagement = $orderManagement; } /** @@ -783,14 +800,13 @@ public function getStatusPending($storeId = 0) } /** - * @param \Magento\Sales\Model\Order $order - * @param $status + * @param OrderInterface $order + * @param null $status * * @return bool * @throws \Exception - * @throws \Magento\Framework\Exception\LocalizedException */ - public function registerCancellation($order, $status = null) + public function registerCancellation(OrderInterface $order, $status = null) { if ($order->getId() && $order->getState() != Order::STATE_CANCELED) { $comment = __('The order was canceled'); @@ -798,7 +814,9 @@ public function registerCancellation($order, $status = null) $comment = __('The order was canceled, reason: payment %1', $status); } $this->addTolog('info', $order->getIncrementId() . ' ' . $comment); - $order->registerCancellation($comment); + $this->orderCommentHistory->add($order, $comment); + $order->getPayment()->setMessage($comment); + $this->orderManagement->cancel($order); if ($order->getCouponCode()) { $this->resetCouponAfterCancellation($order); diff --git a/Model/Client/Payments.php b/Model/Client/Payments.php index f0e4980bf1a..e4502dd41e9 100644 --- a/Model/Client/Payments.php +++ b/Model/Client/Payments.php @@ -310,7 +310,9 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook', if ($status == 'canceled' || $status == 'failed' || $status == 'expired') { if ($type == 'webhook') { $this->mollieHelper->registerCancellation($order, $status); + $order->cancel(); } + $msg = ['success' => false, 'status' => $status, 'order_id' => $orderId, 'type' => $type]; $this->mollieHelper->addTolog('success', $msg); return $msg; diff --git a/Test/Unit/Helper/GeneralTest.php b/Test/Unit/Helper/GeneralTest.php index b7fc00d2e38..833cf809184 100644 --- a/Test/Unit/Helper/GeneralTest.php +++ b/Test/Unit/Helper/GeneralTest.php @@ -4,6 +4,9 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Quote\Api\Data\PaymentInterface; +use Magento\Sales\Api\OrderManagementInterface; +use Magento\Sales\Model\Order as OrderModel; use Magento\Store\Api\Data\StoreInterface; use Magento\Store\Model\StoreManagerInterface; use Mollie\Api\MollieApiClient; @@ -108,4 +111,57 @@ public function testReturnsNullIfNoPaymentsAreAvailable() $this->assertNull($status); } + + public function testRegisterCancellationReturnsFalseWhenAlreadyCanceled() + { + /** @var OrderModel $order */ + $order = $this->objectManager->getObject(OrderModel::class); + $order->setId(999); + $order->setState(OrderModel::STATE_CANCELED); + + /** @var General $instance */ + $instance = $this->objectManager->getObject(General::class); + $result = $instance->registerCancellation($order, 'payment canceled'); + + $this->assertFalse($result); + } + + public function testRegisterCancellationCancelsTheOrder() + { + $orderManagementMock = $this->createMock(OrderManagementInterface::class); + $orderManagementMock->expects($this->once())->method('cancel'); + + /** @var OrderModel $order */ + $order = $this->createPartialMock(OrderModel::class, ['cancel']); + $order->setId(999); + $order->setState(OrderModel::STATE_PROCESSING); + + $payment = $this->objectManager->getObject(OrderModel\Payment::class); + $order->setPayment($payment); + + /** @var General $instance */ + $instance = $this->objectManager->getObject(General::class, [ + 'orderManagement' => $orderManagementMock, + ]); + $result = $instance->registerCancellation($order, 'payment canceled'); + + $this->assertTrue($result); + } + + public function testRegisterCancellationSetsTheCorrectMessage() + { + /** @var OrderModel $order */ + $order = $this->objectManager->getObject(OrderModel::class); + $order->setId(999); + $order->setState(OrderModel::STATE_PROCESSING); + + $payment = $this->objectManager->getObject(OrderModel\Payment::class); + $order->setPayment($payment); + + /** @var General $instance */ + $instance = $this->objectManager->getObject(General::class); + $instance->registerCancellation($order, 'canceled'); + + $this->assertEquals('The order was canceled, reason: payment canceled', $payment->getMessage()->render()); + } } From 7481e31510830877bfaddd271191667bd169fb02 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Tue, 30 Jul 2019 12:06:58 +0200 Subject: [PATCH 2/4] Bugfix: There is always an orderLine object returned, but it can be empty when no records found --- Model/OrderLines.php | 3 +- Test/Integration/Model/OrderLinesTest.php | 45 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Model/OrderLines.php b/Model/OrderLines.php index 11e4bf791a3..a812daf0a5c 100644 --- a/Model/OrderLines.php +++ b/Model/OrderLines.php @@ -420,7 +420,8 @@ public function getCreditmemoOrderLines($creditmemo, $addShipping) $orderLines[] = ['id' => $shippingFeeItemLine->getLineId(), 'quantity' => 1]; } - if ($storeCreditLine = $this->getStoreCreditItemLineOrder($orderId)) { + $storeCreditLine = $this->getStoreCreditItemLineOrder($orderId); + if ($storeCreditLine->getId()) { $orderLines[] = ['id' => $storeCreditLine->getLineId(), 'quantity' => 1]; } diff --git a/Test/Integration/Model/OrderLinesTest.php b/Test/Integration/Model/OrderLinesTest.php index 866e514693e..dc2f55a4716 100644 --- a/Test/Integration/Model/OrderLinesTest.php +++ b/Test/Integration/Model/OrderLinesTest.php @@ -2,6 +2,7 @@ namespace Mollie\Payment\Model; +use Magento\Sales\Api\Data\CreditmemoInterface; use Mollie\Payment\Test\Integration\IntegrationTestCase; class OrderLinesTest extends IntegrationTestCase @@ -54,4 +55,48 @@ public function testGetOrderLinesShippingFeeCalculation() $this->assertEquals(12.1, $row['totalAmount']['value']); $this->assertEquals(2.1, $row['vatAmount']['value']); } + + public function testGetCreditmemoOrderLines() + { + $creditmemo = $this->objectManager->get(CreditmemoInterface::class); + + /** @var OrderLines $instance */ + $instance = $this->objectManager->get(OrderLines::class); + $result = $instance->getCreditmemoOrderLines($creditmemo, false); + + $this->assertCount(0, $result['lines']); + } + + public function testGetCreditmemoOrderLinesIncludesTheStoreCredit() + { + $this->rollbackCreditmemos(); + + $orderLine = $this->objectManager->get(\Mollie\Payment\Model\OrderLinesFactory::class)->create(); + $orderLine->setOrderId(999); + $orderLine->setLineId('ord_abc123'); + $orderLine->setType('store_credit'); + $orderLine->save(); + + $creditmemo = $this->objectManager->get(CreditmemoInterface::class); + $creditmemo->setOrderId(999); + + /** @var OrderLines $instance */ + $instance = $this->objectManager->get(OrderLines::class); + $result = $instance->getCreditmemoOrderLines($creditmemo, false); + + $this->assertCount(1, $result['lines']); + + $line = $result['lines'][0]; + $this->assertEquals('ord_abc123', $line['id']); + $this->assertEquals(1, $line['quantity']); + } + + private function rollbackCreditmemos() + { + $collection = $this->objectManager->get(\Mollie\Payment\Model\ResourceModel\OrderLines\Collection::class); + + foreach ($collection as $creditmemo) { + $creditmemo->delete(); + } + } } From ca659a850f81f6a4a9cc63a70e91914faa2ed80e Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Tue, 30 Jul 2019 14:56:15 +0200 Subject: [PATCH 3/4] Bugfix: When sending the full order the invoice email would not be send --- Model/Client/Orders.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/Client/Orders.php b/Model/Client/Orders.php index 064a1ea8ae8..a60e62529c8 100644 --- a/Model/Client/Orders.php +++ b/Model/Client/Orders.php @@ -609,7 +609,7 @@ public function createShipment(Order\Shipment $shipment, Order $order) */ $payment = $order->getPayment(); if (!$payment->getIsTransactionClosed()) { - $invoice = null; + $invoice = $order->getInvoiceCollection()->getLastItem(); if (!$shipAll) { $invoice = $this->partialInvoice->createFromShipment($shipment); } @@ -620,7 +620,7 @@ public function createShipment(Order\Shipment $shipment, Order $order) $this->orderRepository->save($order); $sendInvoice = $this->mollieHelper->sendInvoice($order->getStoreId()); - if ($invoice && !$invoice->getEmailSent() && $sendInvoice) { + if ($invoice && $invoice->getId() && !$invoice->getEmailSent() && $sendInvoice) { $this->invoiceSender->send($invoice); $message = __('Notified customer about invoice #%1', $invoice->getIncrementId()); $this->orderCommentHistory->add($order, $message, true); From 7a63b9374b11e80a8df4b72d38b847c020b84cca Mon Sep 17 00:00:00 2001 From: Marvin-Magmodules Date: Wed, 31 Jul 2019 11:24:40 +0200 Subject: [PATCH 4/4] Version bump --- composer.json | 2 +- etc/module.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 64b434cae31..377e7e598a4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "mollie/magento2", "description": "Mollie Payment Module for Magento 2", - "version": "1.6.4", + "version": "1.6.5", "keywords": [ "mollie", "payment", diff --git a/etc/module.xml b/etc/module.xml index 457023283da..23741f21939 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,4 +1,4 @@ - +