0

I'm trying to create an online refund of a certain amount (e.g. 10 EUR = is lower than refundable amount). The refund is independent of order items. I can refund online in admin panel but everytime I do it programmatically it will do an offline refund. Please help! I must have overlooked something.

# $order and $invoice are given here
$service = Mage::getModel('sales/service_order', $order);
$data = array();
$data['shipping_amount'] = 0;
$data['grand_total'] = 0;
$data['base_grand_total'] = 0;
$data['adjustment_positive'] = 10;
$data['qtys'] = array(0 => 0);
$creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
$creditmemo->setShippingAmount(0);
$creditmemo->setGrandTotal($data['adjustment_positive']);
$creditmemo->setRefundRequested(true);
$creditmemo->setOfflineRequested(false);
$creditmemo->setPaymentRefundDisallowed(false);
if(Mage::registry('current_creditmemo')) {
    Mage::unregister('current_creditmemo');
}
Mage::register('current_creditmemo', $creditmemo);
$creditmemo->refund();
# here follows the transactionSave: $this->_saveCreditmemo($creditmemo);
0

Check this

1. @param string $orderIncrementId

2.

 @param array $creditmemoData array('qtys' => array('sku1' => qty1, ... , 'skuN' => qtyN),
          'shipping_amount' => value, 'adjustment_positive' => value, 'adjustment_negative' => value)

3. @param string|null $comment

4. @param bool $includeComment

5. @param string $refundToStoreCreditAmount

6 @return string $creditmemoIncrementId

$orderIncrementId ='200000010'; 


$creditmemoData = array(
'qtys' => array(
'order_item_id' => 3,
'qty' => '1'),
'shipping_amount' => null,
'adjustment_positive' => '0',
'adjustment_negative' => null);


 $comment= 'comment for credit memo';

$notifyCustomer =  true;
 $includeComment = false;
$refundToStoreCreditAmount= '1';

 $order = Mage::getModel('sales/order')->load($orderIncrementId, 'increment_id');

       if ($order->getId() && $order->canCreditmemo()) {

             /** @var $service Mage_Sales_Model_Service_Order */
        $service = Mage::getModel('sales/service_order', $order);





        $data = isset($data) ? $data : array();

        if (isset($data['qtys']) && count($data['qtys'])) {
            $qtysArray = array();
            foreach ($data['qtys'] as $qKey => $qVal) {
                // Save backward compatibility
                if (is_array($qVal)) {
                    if (isset($qVal['order_item_id']) && isset($qVal['qty'])) {
                        $qtysArray[$qVal['order_item_id']] = $qVal['qty'];
                    }
                } else {
                    $qtysArray[$qKey] = $qVal;
                }
            }
            $data['qtys'] = $qtysArray;
        }


        /** @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()) {
               // '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
 // add comment to creditmemo
        if (!empty($comment)) {
            $creditmemo->addComment($comment, $notifyCustomer);
        }
                   Mage::getModel('core/resource_transaction')
                ->addObject($creditmemo)
                ->addObject($order)
                ->save();
            // send email notification
            $creditmemo->sendEmail($notifyCustomer, ($includeComment ? $comment : ''));

        }
  • Hi @Amit Can we have this for Magento 2? – Ankit Shah Apr 3 '17 at 5:08
  • @AnkitShah Did you get soln for Magento 2 creditmemo with adjustment fee ...? – Lead Developer Jan 19 '18 at 13:58
0

I finally got it! I just had to replace the last line $creditmemo->refund(); with:

$creditmemo->register();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.