/**
  * This method returns a list of line items with the residual amount to refund.
  *
  * @return Customweb_Payment_Authorization_IInvoiceItem[]
  */
 public function getNonRefundedLineItems()
 {
     $resultingItems = $this->getCapturedLineItems();
     foreach ($this->getRefunds() as $refund) {
         $items = $refund->getRefundItems();
         if ($items === null) {
             $items = Customweb_Util_Invoice::getItemsByReductionAmount($resultingItems, $refund->getAmount(), $this->getCurrencyCode());
         }
         $resultingItems = Customweb_Util_Invoice::substractLineItems($resultingItems, $items);
     }
     return $resultingItems;
 }
 /**
  * The transaction is captured with the specified amount.
  *
  * @param Varien_Object $payment The payment object containing all the payment specific data for the order.
  * @param unknown_type $amount The amount to capture. This may be less than the amount of the order but not more.
  * @return Customweb_SaferpayCw_Model_Method
  */
 public function capture(Varien_Object $payment, $amount)
 {
     $invoice = Mage::registry('current_invoice');
     try {
         $transaction = $this->getHelper()->loadTransactionByPayment($payment->getId());
         $order = $payment->getOrder();
         Customweb_SaferpayCw_Model_ConfigurationAdapter::setStore($order);
         $adapter = $this->getHelper()->createContainer()->getBean('Customweb_Payment_BackendOperation_Adapter_Service_ICapture');
         if ($transaction->getTransactionObject()->isCapturePossible()) {
             if ($transaction->getTransactionObject()->isPartialCapturePossible()) {
                 if ($invoice instanceof Mage_Sales_Model_Order_Invoice) {
                     $items = $this->getInvoiceItems($invoice);
                 } else {
                     $items = Customweb_Util_Invoice::getItemsByReductionAmount($transaction->getTransactionObject()->getTransactionContext()->getOrderContext()->getInvoiceItems(), $amount, $transaction->getTransactionObject()->getCurrencyCode());
                 }
                 $adapter->partialCapture($transaction->getTransactionObject(), $items, true);
             } else {
                 $adapter->capture($transaction->getTransactionObject());
             }
             $transaction->save();
         }
         if ($transaction->getTransactionObject()->isCaptured()) {
             $payment->setIsTransactionClosed(false);
             $captures = $transaction->getTransactionObject()->getCaptures();
             $capture = end($captures);
             $payment->setTransactionAdditionalInfo(Mage_Sales_Model_Order_Payment_Transaction::RAW_DETAILS, array('TRANSACTIONID' => $transaction->getTransactionObject()->getPaymentId(), 'CAPTUREID' => $capture->getCaptureId(), 'AMOUNT' => $capture->getAmount(), 'AMT' => $capture->getAmount(), 'STATUS' => $capture->getStatus()));
             $this->_generateTransactionId($payment, Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE);
             return $this;
         } else {
             $this->getHelper()->log("Capture failed. Transaction error messages : " . print_r($transaction->getTransactionObject()->getErrorMessages(), true));
             $messages = $transaction->getTransactionObject()->getErrorMessages();
             Mage::throwException($this->getHelper()->__('The invoice could not be captured and processed. Reason: ') . end($messages));
         }
     } catch (Exception $e) {
         $this->getHelper()->log("Exception in Method::capture() : " . $e->getMessage());
         Mage::getSingleton('core/session')->addError($e->getMessage());
         throw $e;
     }
 }