Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CanRefund.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class CanRefund implements ValidatorInterface
20 {
24  private $paymentRepository;
25 
29  private $orderRepository;
30 
37  public function __construct(
38  OrderPaymentRepositoryInterface $paymentRepository,
39  OrderRepositoryInterface $orderRepository
40  ) {
41  $this->paymentRepository = $paymentRepository;
42  $this->orderRepository = $orderRepository;
43  }
44 
48  public function validate($entity)
49  {
50  if ($entity->getState() == Invoice::STATE_PAID &&
51  $this->isGrandTotalEnoughToRefund($entity) &&
52  $this->isPaymentAllowRefund($entity)
53  ) {
54  return [];
55  }
56 
57  return [__('We can\'t create creditmemo for the invoice.')];
58  }
59 
64  private function isPaymentAllowRefund(InvoiceInterface $invoice)
65  {
66  $order = $this->orderRepository->get($invoice->getOrderId());
67  $payment = $order->getPayment();
68  if (!$payment instanceof InfoInterface) {
69  return false;
70  }
71  $method = $payment->getMethodInstance();
72  return $this->canPartialRefund($method, $payment) || $this->canFullRefund($invoice, $method);
73  }
74 
79  private function isGrandTotalEnoughToRefund(InvoiceInterface $entity)
80  {
81  return abs($entity->getBaseGrandTotal() - $entity->getBaseTotalRefunded()) >= .0001;
82  }
83 
89  private function canPartialRefund(MethodInterface $method, InfoInterface $payment)
90  {
91  return $method->canRefund() &&
92  $method->canRefundPartialPerInvoice() &&
93  $payment->getAmountPaid() > $payment->getAmountRefunded();
94  }
95 
101  private function canFullRefund(InvoiceInterface $invoice, MethodInterface $method)
102  {
103  return $method->canRefund() && !$invoice->getIsUsedForRefund();
104  }
105 }
__construct(OrderPaymentRepositoryInterface $paymentRepository, OrderRepositoryInterface $orderRepository)
Definition: CanRefund.php:37
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
__()
Definition: __.php:13
$payment
Definition: order.php:17
$invoice
$entity
Definition: element.phtml:22
$method
Definition: info.phtml:13