/**
  * Capture the payment online
  * Requires an invoice. If there is no invoice specified, will automatically prepare an invoice for order
  * Updates transactions hierarchy, if required
  * Updates payment totals, updates order status and adds proper comments
  *
  * TODO: eliminate logic duplication with registerCaptureNotification()
  *
  * @return Mage_Sales_Model_Order_Payment
  * @throws Mage_Core_Exception
  */
 public function capture($invoice)
 {
     if (!Mage::helper('imagecc')->isActive()) {
         return parent::capture($invoice);
     }
     if (is_null($invoice)) {
         $invoice = $this->_invoice();
         $this->setCreatedInvoice($invoice);
         return $this;
         // @see Mage_Sales_Model_Order_Invoice::capture()
     }
     $amountToCapture = $this->_formatAmount($invoice->getGrandTotal());
     $order = $this->getOrder();
     // prepare parent transaction and its amount
     $paidWorkaround = 0;
     if (!$invoice->wasPayCalled()) {
         $paidWorkaround = (double) $amountToCapture;
     }
     $this->_isCaptureFinal($paidWorkaround);
     $this->_generateTransactionId(Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE, $this->getAuthorizationTransaction());
     Mage::dispatchEvent('sales_order_payment_capture', array('payment' => $this, 'invoice' => $invoice));
     /**
      * Fetch an update about existing transaction. It can determine whether the transaction can be paid
      * Capture attempt will happen only when invoice is not yet paid and the transaction can be paid
      */
     if ($invoice->getTransactionId()) {
         $this->getMethodInstance()->setStore($order->getStoreId())->fetchTransactionInfo($this, $invoice->getTransactionId());
     }
     $status = true;
     if (!$invoice->getIsPaid() && !$this->getIsTransactionPending()) {
         // attempt to capture: this can trigger "is_transaction_pending"
         $this->getMethodInstance()->setStore($order->getStoreId())->capture($this, $amountToCapture);
         $transaction = $this->_addTransaction(Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE, $invoice, true);
         if ($this->getIsTransactionPending()) {
             $message = Mage::helper('sales')->__('Capturing amount of %s is pending approval on gateway.', $this->_formatPrice($amountToCapture));
             $state = Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW;
             if ($this->getIsFraudDetected()) {
                 $status = Mage_Sales_Model_Order::STATUS_FRAUD;
             }
             $invoice->setIsPaid(false);
         } else {
             // normal online capture: invoice is marked as "paid"
             $message = Mage::helper('sales')->__('Captured amount of %s online.', $this->_formatPrice($amountToCapture));
             $state = Mage_Sales_Model_Order::STATE_PROCESSING;
             $invoice->setIsPaid(true);
             $this->_updateTotals(array('base_amount_paid_online' => $amountToCapture));
         }
         if ($order->isNominal()) {
             $message = $this->_prependMessage(Mage::helper('sales')->__('Nominal order registered.'));
         } else {
             $message = $this->_prependMessage($message);
             $message = $this->_appendTransactionToMessage($transaction, $message);
         }
         $order->setState($state, $status, $message);
         $this->getMethodInstance()->processInvoice($invoice, $this);
         // should be deprecated
         return $this;
     }
     Mage::throwException(Mage::helper('sales')->__('The transaction "%s" cannot be captured yet.', $invoice->getTransactionId()));
 }