The following code can create a credit memo for an order.
$invoices = array();
foreach ($this->getInvoiceCollection() as $invoice) {
$invoices[] = $invoice;
}
$service = Mage::getModel('sales/service_order', $this);
foreach ($invoices as $invoice) {
$creditmemo = $service->prepareInvoiceCreditmemo($invoice);
$creditmemo->register();
$creditmemo->save();
}
Creating a credit memo programmatically
Need help implementing this solution, or still looking for a fix to your Magento problem? Get in touch and I will be happy to help with your project.