title: Magento - Create credit memo / refund programmatically through code
body: <p>Is there any way of creating a Credit Memo / Refund dynamically through code. I am aware of the fact that Credit Memo can be created using back-end functionality on an invoice but I would like to create a credit memo for a specific order separately based on the following requirements </p>
  
  <ul>
  <li>For selected items</li>
  <li>Deduct the shipping cost from the refund </li>
  <li>Updating the Stock Qty back</li>
  <li>Contacting my payment gateway to perform this refund and transfer the money back into customer's account</li>
  </ul>
  
  <p>I have Sagepay module installed as well for payment purposes and the Refund happens fine using Magento's in built functionality and Sagepay together. But I would like to create a Refund when a customer after shipment asks for refund for few items or all items of a order.</p>
  
  <p>I have created a custom module and am calling a function to perform the refund action. So on call of this function I want to dynamically create Refund for selected items and contact Sagepay to perform the refund transaction for the customer and Magento to update the stock back.</p>
  
  <p>I would just like some help on the starting bits as to where to look for the code and how to combine Sagepay's refund functionality along with Magento's in built one in my module's function call etc ?</p>
  
  <p>Any help is appreciated. Thanks in advance. </p>