Magento 2 contains Out of the Box Native feature of Credit memo generation from admin panel for an order after invoice generated.
What is a Credit Memo?
After a customer places and pays for an order for an online payment method and an invoice has been issued for the specific order, Merchant can create a credit memo to refund all or part of the amount paid for any returned or undelivered items.
The memo restores funds to the customer account or refunds to the customer bank account used for an order so that the customer can make future purchases.
Database Table used the store the data:
sales_creditmemo
You can get full details of Creditmemo related data using credit memo id in Magento 2.
Using Magento\Sales\Api\CreditmemoRepositoryInterface class you can get details of credit memo related data in Magento 2.
<?php namespace Path\To\Class; use Exception; use Psr\Log\LoggerInterface; use Magento\Sales\Api\Data\CreditmemoInterface; use Magento\Sales\Api\CreditmemoRepositoryInterface; class Creditmemo { /** * @var CreditmemoRepositoryInterface */ private $creditmemoRepository; /** * @var LoggerInterface */ private $logger; public function __construct( CreditmemoRepositoryInterface $creditmemoRepository, LoggerInterface $logger ) { $this->creditmemoRepository = $creditmemoRepository; $this->logger = $logger; } /** * Get Creditmemo data * * @return CreditmemoInterface|null */ public function getCreditmemoData(): ?CreditmemoInterface { $creditmemoId = 1; try { $creditmemoData = $this->creditmemoRepository->get($creditmemoId); } catch (Exception $exception) { $this->logger->critical($exception->getMessage()); $creditmemoData = null; } return $creditmemoData; } }
Pass Creditmemo id to fetch creditmemo records from database table.
echo “<pre>”;print_r($creditMemo->debug());exit;
echo $creditmemo = $this->getCreditmemoData(); echo "<pre>";print_r($creditMemo->debug());exit;