/** * Prepare credit memo * * @param $amount * @param $baseGrandTotal * @param false|Invoice $invoice * @return mixed */ protected function prepareCreditMemo($amount, $baseGrandTotal, $invoice) { $entity = $invoice ?: $this->getOrder(); if ($entity->getBaseTotalRefunded() > 0) { $adjustment = ['adjustment_positive' => $amount]; } else { $adjustment = ['adjustment_negative' => $baseGrandTotal - $amount]; } if ($invoice) { $creditMemo = $this->creditmemoFactory->createByInvoice($invoice, $adjustment); } else { $creditMemo = $this->creditmemoFactory->createByOrder($this->getOrder(), $adjustment); } if ($creditMemo) { $totalRefunded = $entity->getBaseTotalRefunded() + $creditMemo->getBaseGrandTotal(); $this->setShouldCloseParentTransaction($entity->getBaseGrandTotal() <= $totalRefunded); } return $creditMemo; }