$order = Mage::getModel(‘sales/order’)->load(‘100000001’, ‘increment_id’);
if (!$order->getId()) {
$this->_fault(‘order_not_exists’);
}
if (!$order->canCreditmemo()) {
$this->_fault(‘cannot_create_creditmemo’);
}
$data = array();
$service = Mage::getModel(‘sales/service_order’, $order);
$creditmemo = $service->prepareCreditmemo($data);
// 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());
}
echo $creditmemo->getIncrementId();