The following code creates a Magento credit memo / refund.

$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 / refund 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.
Tags: