Skip to content

Commit e240887

Browse files
author
Anna Bukatar
committed
ACP2E-1155: Refunding Issue Rest API
1 parent 7f5e674 commit e240887

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo/Validation/QuantityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function validate($entity)
8888
}
8989
}
9090

91-
if ($entity->getGrandTotal() <= 0) {
91+
if (!$entity->isValidGrandTotal()) {
9292
$messages[] = __('The credit memo\'s total must be positive.');
9393
} elseif ($totalQuantity < 0 && !$this->canRefundShipping($order)) {
9494
$messages[] = __('You can\'t create a creditmemo without products.');

app/code/Magento/Sales/Model/Order/Invoice/Validation/CanRefund.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
namespace Magento\Sales\Model\Order\Invoice\Validation;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
89
use Magento\Payment\Model\InfoInterface;
910
use Magento\Payment\Model\MethodInterface;
1011
use Magento\Sales\Api\Data\InvoiceInterface;
1112
use Magento\Sales\Api\OrderPaymentRepositoryInterface;
1213
use Magento\Sales\Api\OrderRepositoryInterface;
1314
use Magento\Sales\Model\Order\Invoice;
1415
use Magento\Sales\Model\ValidatorInterface;
16+
use Magento\Framework\App\ObjectManager;
1517

1618
/**
1719
* Class CanRefund
@@ -28,18 +30,26 @@ class CanRefund implements ValidatorInterface
2830
*/
2931
private $orderRepository;
3032

33+
/**
34+
* @var ScopeConfigInterface;
35+
*/
36+
private $scopeConfig;
37+
3138
/**
3239
* CanRefund constructor.
3340
*
3441
* @param OrderPaymentRepositoryInterface $paymentRepository
3542
* @param OrderRepositoryInterface $orderRepository
43+
* @param ScopeConfigInterface|null $scopeConfig
3644
*/
3745
public function __construct(
3846
OrderPaymentRepositoryInterface $paymentRepository,
39-
OrderRepositoryInterface $orderRepository
47+
OrderRepositoryInterface $orderRepository,
48+
?ScopeConfigInterface $scopeConfig = null
4049
) {
4150
$this->paymentRepository = $paymentRepository;
4251
$this->orderRepository = $orderRepository;
52+
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
4353
}
4454

4555
/**
@@ -69,7 +79,10 @@ private function isPaymentAllowRefund(InvoiceInterface $invoice)
6979
return false;
7080
}
7181
$method = $payment->getMethodInstance();
72-
return $this->canPartialRefund($method, $payment) || $this->canFullRefund($invoice, $method);
82+
if (!$method instanceof \Magento\Payment\Model\Method\Free) {
83+
return $this->canPartialRefund($method, $payment) || $this->canFullRefund($invoice, $method);
84+
}
85+
return true;
7386
}
7487

7588
/**
@@ -78,7 +91,22 @@ private function isPaymentAllowRefund(InvoiceInterface $invoice)
7891
*/
7992
private function isGrandTotalEnoughToRefund(InvoiceInterface $entity)
8093
{
81-
return abs($entity->getBaseGrandTotal() - $entity->getBaseTotalRefunded()) >= .0001;
94+
return abs($entity->getBaseGrandTotal() - $entity->getBaseTotalRefunded()) >= .0001 ||
95+
$this->isAllowZeroGrandTotal();
96+
}
97+
98+
/**
99+
* Return Zero GrandTotal availability.
100+
*
101+
* @return bool
102+
*/
103+
private function isAllowZeroGrandTotal()
104+
{
105+
$isAllowed = $this->scopeConfig->getValue(
106+
'sales/zerograndtotal_creditmemo/allow_zero_grandtotal',
107+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
108+
);
109+
return $isAllowed;
82110
}
83111

84112
/**

app/code/Magento/Sales/Model/Order/Validation/CanRefund.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\Sales\Model\Order\Validation;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
89
use Magento\Framework\Pricing\PriceCurrencyInterface;
910
use Magento\Sales\Api\Data\OrderInterface;
1011
use Magento\Sales\Model\Order;
1112
use Magento\Sales\Model\ValidatorInterface;
13+
use Magento\Framework\App\ObjectManager;
1214

1315
/**
1416
* Class CanRefund
@@ -20,14 +22,23 @@ class CanRefund implements ValidatorInterface
2022
*/
2123
private $priceCurrency;
2224

25+
/**
26+
* @var ScopeConfigInterface;
27+
*/
28+
private $scopeConfig;
29+
2330
/**
2431
* CanRefund constructor.
2532
*
2633
* @param PriceCurrencyInterface $priceCurrency
34+
* @param ScopeConfigInterface|null $scopeConfig
2735
*/
28-
public function __construct(PriceCurrencyInterface $priceCurrency)
29-
{
36+
public function __construct(
37+
PriceCurrencyInterface $priceCurrency,
38+
?ScopeConfigInterface $scopeConfig = null
39+
) {
3040
$this->priceCurrency = $priceCurrency;
41+
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
3142
}
3243

3344
/**
@@ -62,6 +73,22 @@ public function validate($entity)
6273
*/
6374
private function isTotalPaidEnoughForRefund(OrderInterface $order)
6475
{
65-
return !abs($this->priceCurrency->round($order->getTotalPaid()) - $order->getTotalRefunded()) < .0001;
76+
return !abs($this->priceCurrency->round($order->getTotalPaid()) - $order->getTotalRefunded()) < .0001 ||
77+
$order->getTotalPaid() == 0 &&
78+
$this->isAllowZeroGrandTotal();
79+
}
80+
81+
/**
82+
* Return Zero GrandTotal availability.
83+
*
84+
* @return bool
85+
*/
86+
private function isAllowZeroGrandTotal()
87+
{
88+
$isAllowed = $this->scopeConfig->getValue(
89+
'sales/zerograndtotal_creditmemo/allow_zero_grandtotal',
90+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
91+
);
92+
return $isAllowed;
6693
}
6794
}

0 commit comments

Comments
 (0)