Skip to content
Open
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
20 changes: 9 additions & 11 deletions Service/Order/ProcessAdjustmentFee.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Service\Order;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Api\MollieApiClient;
use Mollie\Payment\Helper\General as MollieHelper;
use Mollie\Payment\Service\Mollie\Order\RefundUsingPayment;

class ProcessAdjustmentFee
{
/**
* @var MollieHelper
*/
private $mollieHelper;

/**
* @var RefundUsingPayment
*/
Expand All @@ -26,15 +24,15 @@ class ProcessAdjustmentFee
private $doNotRefundInMollie = false;

public function __construct(
MollieHelper $mollieHelper,
RefundUsingPayment $refundUsingPayment
) {
$this->mollieHelper = $mollieHelper;
$this->refundUsingPayment = $refundUsingPayment;
}

public function handle(MollieApiClient $mollieApi, OrderInterface $order, CreditmemoInterface $creditmemo)
{
$this->doNotRefundInMollie = false;

if ($creditmemo->getAdjustment() > 0) {
$this->positive($mollieApi, $order, $creditmemo);
}
Expand All @@ -44,12 +42,12 @@ public function handle(MollieApiClient $mollieApi, OrderInterface $order, Credit
}
}

public function doNotRefundInMollie()
public function doNotRefundInMollie(): bool
{
return $this->doNotRefundInMollie;
}

private function positive(MollieApiClient $mollieApi, OrderInterface $order, CreditmemoInterface $creditmemo)
private function positive(MollieApiClient $mollieApi, OrderInterface $order, CreditmemoInterface $creditmemo): void
{
$this->doNotRefundInMollie = false;

Expand All @@ -61,7 +59,7 @@ private function positive(MollieApiClient $mollieApi, OrderInterface $order, Cre
);
}

private function negative(MollieApiClient $mollieApi, OrderInterface $order, CreditmemoInterface $creditmemo)
private function negative(MollieApiClient $mollieApi, OrderInterface $order, CreditmemoInterface $creditmemo): void
{
$this->doNotRefundInMollie = true;

Expand Down
42 changes: 42 additions & 0 deletions Test/Fakes/Service/Mollie/Order/RefundUsingPaymentFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Test\Fakes\Service\Mollie\Order;

use Mollie\Api\MollieApiClient;
use Mollie\Payment\Service\Mollie\Order\RefundUsingPayment;

class RefundUsingPaymentFake extends RefundUsingPayment
{
private $calls = [];
private $disableParentCall = false;


public function disableParentCall(): void
{
$this->disableParentCall = true;
}

public function getCalls(): array
{
return $this->calls;
}

public function execute(MollieApiClient $mollieApi, $transactionId, $currencyCode, $amount)
{
$this->calls[] = [
'transactionId' => $transactionId,
'currencyCode' => $currencyCode,
'amount' => $amount,
];

if ($this->disableParentCall) {
return;
}

parent::execute($mollieApi, $transactionId, $currencyCode, $amount);
}
}
83 changes: 83 additions & 0 deletions Test/Integration/Service/Order/ProcessAdjustmentFeeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Test\Integration\Service\Order;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Service\Mollie\Order\RefundUsingPayment;
use Mollie\Payment\Service\Order\ProcessAdjustmentFee;
use Mollie\Payment\Test\Fakes\Service\Mollie\Order\RefundUsingPaymentFake;
use Mollie\Payment\Test\Integration\IntegrationTestCase;

class ProcessAdjustmentFeeTest extends IntegrationTestCase
{
public function testRefundsPositive(): void
{
$refundUsingPayment = $this->objectManager->create(RefundUsingPaymentFake::class);
$refundUsingPayment->disableParentCall();
$this->objectManager->addSharedInstance($refundUsingPayment, RefundUsingPayment::class);

$mollieApiClient = $this->objectManager->create(\Mollie\Api\MollieApiClient::class);

$instance = $this->objectManager->create(ProcessAdjustmentFee::class);

$order = $this->objectManager->create(OrderInterface::class);
$creditmemo = $this->objectManager->create(CreditmemoInterface::class);
$creditmemo->setAdjustment(10);

$instance->handle($mollieApiClient, $order, $creditmemo);

$this->assertFalse($instance->doNotRefundInMollie());
$this->assertCount(1, $refundUsingPayment->getCalls());
}

public function testRefundsNegative(): void
{
$refundUsingPayment = $this->objectManager->create(RefundUsingPaymentFake::class);
$refundUsingPayment->disableParentCall();
$this->objectManager->addSharedInstance($refundUsingPayment, RefundUsingPayment::class);

$mollieApiClient = $this->objectManager->create(\Mollie\Api\MollieApiClient::class);

$instance = $this->objectManager->create(ProcessAdjustmentFee::class);

$order = $this->objectManager->create(OrderInterface::class);
$creditmemo = $this->objectManager->create(CreditmemoInterface::class);
$creditmemo->setData('adjustment_negative', 10);

$instance->handle($mollieApiClient, $order, $creditmemo);

$this->assertTrue($instance->doNotRefundInMollie());
$this->assertCount(1, $refundUsingPayment->getCalls());
}

public function testResetsTheRefundInMollieFlag(): void
{
// Scenario: Do a negative creditmemo first, then a creditmemo without adjustments.

$refundUsingPayment = $this->objectManager->create(RefundUsingPaymentFake::class);
$refundUsingPayment->disableParentCall();
$this->objectManager->addSharedInstance($refundUsingPayment, RefundUsingPayment::class);

$mollieApiClient = $this->objectManager->create(\Mollie\Api\MollieApiClient::class);

$instance = $this->objectManager->create(ProcessAdjustmentFee::class);

$negativeCreditmemo = $this->objectManager->create(CreditmemoInterface::class);
$negativeCreditmemo->setData('adjustment_negative', 10);

$order = $this->objectManager->create(OrderInterface::class);

$instance->handle($mollieApiClient, $order, $negativeCreditmemo);
$this->assertTrue($instance->doNotRefundInMollie());

$creditmemoWithoutAdjustments = $this->objectManager->create(CreditmemoInterface::class);

$instance->handle($mollieApiClient, $order, $creditmemoWithoutAdjustments);
$this->assertFalse($instance->doNotRefundInMollie());
}
}
Loading