title: Magento partial refund programmatically
body: <p>How can i create partial create memo? i.e. if total order value is £50 and I want to create a credit memo of £10. I am using the below code to create credit memo:</p>
  
  <pre><code>function createCreditMemo($orderId) {
  $order = Mage::getModel('sales/order')-&gt;load($orderId, 'increment_id');
  if (!$order-&gt;getId()) {
      print_r('order_not_exists');
  }
  if (!$order-&gt;canCreditmemo()) {
      Mage::log('cannot_create_creditmemo', null, 'product.txt');
      print_r('cannot_create_creditmemo');
  }
  $data = array();
  $refundToStoreCreditAmount = '10.22';
  
  $service = Mage::getModel('sales/service_order', $order);
  
  $creditmemo = $service-&gt;prepareCreditmemo($data);
  
  // refund to Store Credit
  if ($refundToStoreCreditAmount) {
      // check if refund to Store Credit is available
      if ($order-&gt;getCustomerIsGuest()) {
          print_r('cannot_refund_to_storecredit');
      }
      $refundToStoreCreditAmount = max(
          0,
          min($creditmemo-&gt;getBaseCustomerBalanceReturnMax(), $refundToStoreCreditAmount)
      );
  
  
      if ($refundToStoreCreditAmount) {
          $refundToStoreCreditAmount = $creditmemo-&gt;getStore()-&gt;roundPrice($refundToStoreCreditAmount);
  
          $creditmemo-&gt;setBaseCustomerBalanceTotalRefunded($refundToStoreCreditAmount);
          $refundToStoreCreditAmount = $creditmemo-&gt;getStore()-&gt;roundPrice(
              $refundToStoreCreditAmount*$order-&gt;getStoreToOrderRate()
          );
          // this field can be used by customer balance observer
          $creditmemo-&gt;setBsCustomerBalTotalRefunded($refundToStoreCreditAmount);
          // setting flag to make actual refund to customer balance after credit memo save
          $creditmemo-&gt;setCustomerBalanceRefundFlag(true);
          print_r($refundToStoreCreditAmount.'&lt;br/&gt;');
          //die('2');
      }
  
  
  }
  //Mage::log($creditmemo, null, 'product.txt');
  $creditmemo-&gt;setPaymentRefundDisallowed(true);
  
  $creditmemo-&gt;register();
  
  $orderCreditMemoStatusCode = Mage_Sales_Model_Order::STATE_CLOSED;
  $orderCreditMemoStatusComment = 'Order Refunded.';
  $saveTransaction = Mage::getModel('core/resource_transaction')-&gt;addObject ($creditmemo )-&gt;addObject ( $order )-&gt;save ();
  $order-&gt;addStatusToHistory ( $orderCreditMemoStatusCode, $orderCreditMemoStatusComment, true );
  
  $notifyCustomer = true;
  $comment = 'testing refund';
  $includeComment = true;
  
  $creditmemo-&gt;setEmailSent(true);
  
  // add comment to creditmemo
  if (!empty($comment)) {
      $creditmemo-&gt;addComment($comment, $notifyCustomer);
  }
  
  try {
      Mage::getModel('core/resource_transaction')
          -&gt;addObject($creditmemo)
          -&gt;addObject($order)
          -&gt;save();
      // send email notification
  
      $creditmemo-&gt;sendEmail($notifyCustomer, ($includeComment ? $comment : ''));
  } catch (Mage_Core_Exception $e) {
      print_r('data_invalid', $e-&gt;getMessage());
  }
  
  echo $creditmemo-&gt;getIncrementId();
  </code></pre>
  
  <p>}</p>
  
  <p>In the below code, even if I set the <code>$refundToStoreCreditAmount = 10;</code> it still refunds the full amount. Any idea how to refund partially?</p>