def capture_funds
      
      resp = StdClass.new      
      it = InvoiceTransaction.where(:invoice_id => self.id, :success => true).first
            
      if self.financial_status == Invoice::FINANCIAL_STATUS_CAPTURED
        resp.error = "Funds for this invoice have already been captured."    
      elsif self.total > it.amount
        resp.error = "The invoice total exceeds the authorized amount."
      elsif it.nil?
        resp.error = "This invoice doesn't seem to be authorized."
      else
                        
        sc = self.site.store_config                
        case sc.pp_name
          
          #when 'authorize.net'
          #  transaction = AuthorizeNet::AIM::Transaction.new(sc.authnet_api_login_id, sc.authnet_api_transaction_key)
          #  response = transaction.prior_auth_capture(t.transaction_id, self.total)
          #  
          #  ot = Caboose::InvoiceTransaction.create(
          #    :invoice_id        => self.id,
          #    :date_processed    => DateTime.now.utc,
          #    :transaction_type  => InvoiceTransaction::TYPE_CAPTURE,
          #    :payment_processor => sc.pp_name,
          #    :amount            => self.total,        
          #    :success           => response.response_code && response.response_code == '1',            
          #    :transaction_id    => response.transaction_id,
          #    :auth_code         => response.authorization_code,
          #    :response_code     => response.response_code
          #  )
          #  if ot.success
          #    self.date_captured = DateTime.now.utc
          #    self.save              
          #  end                                    
          #  self.update_attribute(:financial_status, Invoice::FINANCIAL_STATUS_CAPTURED)
          #  resp.success = 'Captured funds successfully'
                                    
          when StoreConfig::PAYMENT_PROCESSOR_STRIPE
                                          
            it = Caboose::InvoiceTransaction.where(:invoice_id => self.id, :success => true).first
            if it.nil?
              resp.error = "Error capturing funds for invoice #{self.id}. No previous successful authorization for this invoice exists."
              return false
            else                        
              Stripe.api_key = sc.stripe_secret_key.strip
              bt = nil
              begin
                c = Stripe::Charge.retrieve(it.transaction_id)
                c = c.capture
                bt = Stripe::BalanceTransaction.retrieve(c.balance_transaction)
              rescue Exception => ex
                resp.error = "Error during capture process\n#{ex.message}"                
              end
              
              if resp.error.nil?
                InvoiceTransaction.create(
                  :invoice_id => self.id,
                  :transaction_id => bt.id,
                  :transaction_type => InvoiceTransaction::TYPE_CAPTURE,
                  :payment_processor => sc.pp_name,
                  :amount => bt.amount / 100,                
                  :date_processed => DateTime.strptime(bt.created.to_s, '%s'),
                  :success => bt.status == 'succeeded' || bt.status == 'pending'
                )
                if bt.status == 'succeeded' || bt.status == 'pending'
                  self.financial_status = Invoice::FINANCIAL_STATUS_CAPTURED
                  self.save
                  resp.success = true
                else
                  resp.error = "Error capturing funds."
                end
              end
            end
          
        end        
      end      
      return resp
    end
def capture_funds
      
      resp = StdClass.new      
      it = InvoiceTransaction.where(:invoice_id => self.id, :success => true).first
            
      if self.financial_status == Invoice::FINANCIAL_STATUS_CAPTURED
        resp.error = "Funds for this invoice have already been captured."    
      elsif self.total > it.amount
        resp.error = "The invoice total exceeds the authorized amount."
      elsif it.nil?
        resp.error = "This invoice doesn't seem to be authorized."
      else
                        
        sc = self.site.store_config                
        case sc.pp_name
          
          #when 'authorize.net'
          #  transaction = AuthorizeNet::AIM::Transaction.new(sc.authnet_api_login_id, sc.authnet_api_transaction_key)
          #  response = transaction.prior_auth_capture(t.transaction_id, self.total)
          #  
          #  ot = Caboose::InvoiceTransaction.create(
          #    :invoice_id        => self.id,
          #    :date_processed    => DateTime.now.utc,
          #    :transaction_type  => InvoiceTransaction::TYPE_CAPTURE,
          #    :payment_processor => sc.pp_name,
          #    :amount            => self.total,        
          #    :success           => response.response_code && response.response_code == '1',            
          #    :transaction_id    => response.transaction_id,
          #    :auth_code         => response.authorization_code,
          #    :response_code     => response.response_code
          #  )
          #  if ot.success
          #    self.date_captured = DateTime.now.utc
          #    self.save              
          #  end                                    
          #  self.update_attribute(:financial_status, Invoice::FINANCIAL_STATUS_CAPTURED)
          #  resp.success = 'Captured funds successfully'
                                    
          when StoreConfig::PAYMENT_PROCESSOR_STRIPE
                                          
            it = Caboose::InvoiceTransaction.where(:invoice_id => self.id, :success => true).first
            if it.nil?
              resp.error = "Error capturing funds for invoice #{self.id}. No previous successful authorization for this invoice exists."
              return false
            else                        
              Stripe.api_key = sc.stripe_secret_key.strip
              bt = nil
              begin
                c = Stripe::Charge.retrieve(it.transaction_id)
                c = c.capture
                bt = Stripe::BalanceTransaction.retrieve(c.balance_transaction)
              rescue Exception => ex
                resp.error = "Error during capture process\n#{ex.message}"                
              end
              
              if resp.error.nil?
                InvoiceTransaction.create(
                  :invoice_id => self.id,
                  :transaction_id => bt.id,
                  :transaction_type => InvoiceTransaction::TYPE_CAPTURE,
                  :payment_processor => sc.pp_name,
                  :amount => bt.amount / 100,                
                  :date_processed => DateTime.strptime(bt.created.to_s, '%s'),
                  :success => bt.status == 'succeeded' || bt.status == 'pending'
                )
                if bt.status == 'succeeded' || bt.status == 'pending'
                  self.financial_status = Invoice::FINANCIAL_STATUS_CAPTURED
                  self.save
                  resp.success = true
                else
                  resp.error = "Error capturing funds."
                end
              end
            end
          
        end        
      end      
      return resp
    end
Capture funds from a previously authorized transaction