How do you refund to store credit a Credit Memo in Magento?

In Magento I am trying to create a Credit Memo API and have already found a good starting point. That allows me to create credit memos. The one thing that user is able to do from the front end that I can not see how to replicate is if the user creates a credit memo from the front end they get a “Refund to Store Credit” option which I need to add to the API. How do you refund a credit memo to a store credit? When looking through the Creditmemo.php file in Magento I do not see any methods pertaining to creating a store credit for the credit memo.

Below is the credit memo API I found on the Magento Forums

    class Mage_Sales_Model_Order_Creditmemo_Api extends Mage_Sales_Model_Api_Resource
{
    public function __construct()
    {
    }

    /**
     * Retrive creditmemos by filters
     *
     * @param array $filters
     * @return array
     */
    public function items($filters = null)
    {
        //TODO: add full name logic
        $collection = Mage::getResourceModel('sales/order_creditmemo_collection')
            ->addAttributeToSelect('increment_id')
            ->addAttributeToSelect('state');

        $result = array();

        foreach ($collection as $creditmemo) {
            $result[] = $this->_getAttributes($creditmemo, 'creditmemo');
        }

        return $result;
    }

    /**
     * Retrieve creditmemo information
     *
     * @param string $creditmemoId
     * @return array
     */
    public function info($creditmemoId)
    {
        $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);

        if (!$creditmemo->getId()) {
            $this->_fault('not_exists');
        }

        $result = $this->_getAttributes($creditmemo, 'creditmemo');
        $result['order_increment_id'] = $creditmemo->getOrderIncrementId();

        $result['items'] = array();
        foreach ($creditmemo->getAllItems() as $item) {
            $result['items'][] = $this->_getAttributes($item, 'creditmemo_item');
        }

        $result['comments'] = array();
        foreach ($creditmemo->getCommentsCollection() as $comment) {
            $result['comments'][] = $this->_getAttributes($comment, 'creditmemo_comment');
        }

        return $result;
    }

    /**
     * Create creditmemo
     * We can save only new creditmemo. Existing creditmemos are not editable
     */

    /**
     * Create new creditmemo from order
     *
     * @param array     $data // Built the with the same structure as the AdminHtml form used on backend.
     * @param string    $data[order_increment_id]
     * @param integer   $data[invoice_id]
     * @param array     $data[creditmemo][items] // for each of the items on order/invoice
     * @param array     $data[creditmemo][items][{item_id}] // item_id from sales_flat_order_item
     * @param boolean   $data[creditmemo][items][{item_id}]['back_to_stock']
     * @param integer   $data[creditmemo][items][{item_id}]['qty'] // Qty to return
     * @param string    $data[creditmemo][comment_text]
     * @param string    $data[creditmemo][comment_customer_notify]
     * @param float     $data[creditmemo][shipping_amount]
     * @param float     $data[creditmemo][adjustment_positive]
     * @param float     $data[creditmemo][adjustment_negative]
     * @param booleam   $data[creditmemo][do_offline]
     * @param boolean   $data[creditmemo][send_email]
     * @return array
     */

    public function create($data = array())
    {
        $this->tempData = $data;

        try {
            $creditmemo = $this->_initCreditmemo();
            if ($creditmemo){
                if (($creditmemo->getGrandTotal() <=0) && (!$creditmemo->getAllowZeroGrandTotal())) {
                    $this->_fault('credit_total', $e->getMessage());
               }

                $comment = '';
                if (!empty($data['creditmemo']['comment_text'])) {
                    $creditmemo->addComment($data['creditmemo']['comment_text'], isset($data['creditmemo']['comment_customer_notify']));
                    if (isset($data['creditmemo']['comment_customer_notify'])) {
                        $comment = $data['creditmemo']['comment_text'];
                    }
                }

                if (isset($data['creditmemo']['do_refund'])) {
                    $creditmemo->setRefundRequested(true);
                }
                if (isset($data['creditmemo']['do_offline'])) {
                    $creditmemo->setOfflineRequested((bool)(int)$data['creditmemo']['do_offline']);
                    $this->tempData['message'][] = 'The credit memo was done offline';
                }

                $creditmemo->register();
                if (!empty($data['creditmemo']['send_email'])) {
                    $creditmemo->setEmailSent(true);
                }

                $creditmemo->getOrder()->setCustomerNoteNotify(!empty($data['creditmemo']['send_email']));
                $this->_saveCreditmemo($creditmemo);
                $creditmemo->sendEmail(!empty($data['creditmemo']['send_email']), $comment);
                $this->tempData['message'][] = 'The credit memo has been created.';
            } 
        } catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $this->tempData;
    }

    protected function _saveCreditmemo($creditmemo)
    {
        $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($creditmemo)
            ->addObject($creditmemo->getOrder());
        if ($creditmemo->getInvoice()) {
            $transactionSave->addObject($creditmemo->getInvoice());
        }
        $transactionSave->save();

        return;
    }

    /**
     * Initialize creditmemo model instance
     *
     * @return Mage_Sales_Model_Order_Creditmemo
     */
    protected function _initCreditmemo($update = false)
    {
        $this->tempData['message'][] = 'init credit memo.';
        $creditmemo = false;
        $creditmemoId = $this->tempData['creditmemo_id'];
        $orderIncrementId = $this->tempData['order_increment_id'];
        $invoiceId = $this->tempData['invoice_id'];
        if ($creditmemoId) {
            $creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);
        } elseif ($orderIncrementId) {
            $data   = $this->tempData['creditmemo'];
            $order  = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
            if ($invoiceId) {
                $invoice = Mage::getModel('sales/order_invoice')
                    ->load($invoiceId)
                    ->setOrder($order);
                $this->tempData['message'][] = 'loaded_invoice_number: '.$invoice->getId();
            }
            if (!$order->canCreditmemo()) {
                $this->tempData['message'][] = 'cannot create credit memo'; 
                if(!$order->isPaymentReview())
                {
                    $this->tempData['message'][] = 'cannot credit memo Payment is in review'; 
                }
                if(!$order->canUnhold())
                {
                    $this->tempData['message'][] = 'cannot credit memo Order is on hold'; 
                }
                if(abs($order->getTotalPaid()-$order->getTotalRefunded())<.0001)
                {
                    $this->tempData['message'][] = 'cannot credit memo Amount Paid is equal or less than amount refunded'; 
                }
                if($order->getActionFlag('edit') === false)
                {
                    $this->tempData['message'][] = 'cannot credit memo Action Flag of Edit not set'; 
                }
                if ($order->hasForcedCanCreditmemo()) {
                    $this->tempData['message'][] = 'cannot credit memo Can Credit Memo has been forced set'; 
                }
                return false;
            }

            if(isset($data['items'])) {$savedData = $data['items'];} else { $savedData = array();}

            $qtys = array();
            $backToStock = array();
            foreach ($savedData as $orderItemId =>$itemData) {
                if (isset($itemData['qty'])) {
                    $qtys[$orderItemId] = $itemData['qty'];
                }
                if (isset($itemData['back_to_stock'])) {
                    $backToStock[$orderItemId] = true;
                }
            }
            $data['qtys'] = $qtys;

            $service = Mage::getModel('sales/service_order', $order);
            if ($invoice) {
                $creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
            } else {
                $creditmemo = $service->prepareCreditmemo($data);
            }

            /**
             * Process back to stock flags
             */
            foreach ($creditmemo->getAllItems() as $creditmemoItem) {
                $orderItem = $creditmemoItem->getOrderItem();
                $parentId = $orderItem->getParentItemId();
                if (isset($backToStock[$orderItem->getId()])) {
                    $creditmemoItem->setBackToStock(true);
                } elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) {
                    $creditmemoItem->setBackToStock(true);
                } elseif (empty($savedData)) {
                    $creditmemoItem->setBackToStock(Mage::helper('cataloginventory')->isAutoReturnEnabled());
                } else {
                    $creditmemoItem->setBackToStock(false);
                }
                $this->tempData['message'][] = $orderItem->getId();
            }
        }

        return $creditmemo;
    }

} // Class Mage_Sales_Model_Order_Creditmemo_Api End

One thought on “How do you refund to store credit a Credit Memo in Magento?”

  1. You can create Credit Memo using default API of magento which is salesOrderCreditmemoCreate.
    Here’s a magento commerce link that may help you : LINK

    public function create($orderIncrementId, $creditmemoData = null, $comment = null, $notifyCustomer = false,
        $includeComment = false, $refundToStoreCreditAmount = null)
    {
        /** @var $order Mage_Sales_Model_Order */
        $order = Mage::getModel('sales/order')->load($orderIncrementId, 'increment_id');
        if (!$order->getId()) {
            $this->_fault('order_not_exists');
        }
        if (!$order->canCreditmemo()) {
            $this->_fault('cannot_create_creditmemo');
        }
        $creditmemoData = $this->_prepareCreateData($creditmemoData);
    
        /** @var $service Mage_Sales_Model_Service_Order */
        $service = Mage::getModel('sales/service_order', $order);
        /** @var $creditmemo Mage_Sales_Model_Order_Creditmemo */
        $creditmemo = $service->prepareCreditmemo($creditmemoData);
    
        // refund to Store Credit
        if ($refundToStoreCreditAmount) {
            // check if refund to Store Credit is available
            if ($order->getCustomerIsGuest()) {
                $this->_fault('cannot_refund_to_storecredit');
            }
            $refundToStoreCreditAmount = max(
                0,
                min($creditmemo->getBaseCustomerBalanceReturnMax(), $refundToStoreCreditAmount)
            );
            if ($refundToStoreCreditAmount) {
                $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice($refundToStoreCreditAmount);
                $creditmemo->setBaseCustomerBalanceTotalRefunded($refundToStoreCreditAmount);
                $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice(
                    $refundToStoreCreditAmount*$order->getStoreToOrderRate()
                );
                // this field can be used by customer balance observer
                $creditmemo->setBsCustomerBalTotalRefunded($refundToStoreCreditAmount);
                // setting flag to make actual refund to customer balance after credit memo save
                $creditmemo->setCustomerBalanceRefundFlag(true);
            }
        }
        $creditmemo->setPaymentRefundDisallowed(true)->register();
        // add comment to creditmemo
        if (!empty($comment)) {
            $creditmemo->addComment($comment, $notifyCustomer);
        }
        try {
            Mage::getModel('core/resource_transaction')
                ->addObject($creditmemo)
                ->addObject($order)
                ->save();
            // send email notification
            $creditmemo->sendEmail($notifyCustomer, ($includeComment ? $comment : ''));
        } catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $creditmemo->getIncrementId();
    }
    

Leave a Reply

Your email address will not be published. Required fields are marked *