Creating a credit memo / refund programmatically (Magento 1)

The following code creates a Magento credit memo / refund.