Skip to content

Commit

Permalink
Merge pull request #185 from mollie/1.6.5
Browse files Browse the repository at this point in the history
1.6.5
  • Loading branch information
Marvin-Magmodules authored Jul 31, 2019
2 parents 9772b25 + 8ee0df6 commit fe20a2b
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 11 deletions.
30 changes: 24 additions & 6 deletions Helper/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -131,6 +134,14 @@ class General extends AbstractHelper
* @var CouponUsage
*/
private $couponUsage;
/**
* @var OrderCommentHistory
*/
private $orderCommentHistory;
/**
* @var OrderManagementInterface
*/
private $orderManagement;

/**
* General constructor.
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -783,22 +800,23 @@ 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');
if ($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);
Expand Down
4 changes: 2 additions & 2 deletions Model/Client/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Model/Client/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion Model/OrderLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
45 changes: 45 additions & 0 deletions Test/Integration/Model/OrderLinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mollie\Payment\Model;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Mollie\Payment\Test\Integration\IntegrationTestCase;

class OrderLinesTest extends IntegrationTestCase
Expand Down Expand Up @@ -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();
}
}
}
56 changes: 56 additions & 0 deletions Test/Unit/Helper/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mollie_Payment" setup_version="1.6.4" />
<module name="Mollie_Payment" setup_version="1.6.5" />
</config>

0 comments on commit fe20a2b

Please sign in to comment.