/**
  * This method does post processing of successfull payments. Order and quote states are
  * set and and invoice is generated (and captured) according to the settings in the backend.
  *         	   		  	 	 	
  * Attention: This method is called from within redirectAction  (access to checkout session)
  * and from within processAction (no access to checkout session)
  *
  * @param Mage_Sales_Model_Order $order
  * @param string $transactionId
  */
 public function processPayment(Mage_Sales_Model_Order $order, Customweb_SaferpayCw_Model_Transaction $transaction)
 {
     if ($order->getId()) {
         Customweb_SaferpayCw_Model_ConfigurationAdapter::setStore($order);
         Mage::getSingleton('core/session')->unsetSaferpayCwTransactionId();
         if ($transaction->getTransactionObject()->isAuthorized()) {
             $transaction->saveIgnoreOrderStatus();
             $order->setState(self::STATE_PROCESSING);
             $order->save();
             Mage::dispatchEvent('saferpaycw_payment_success', array('order' => $order));
             $quote = Mage::getModel('sales/quote');
             $quote->load($order->getQuoteId());
             $quote->setIsActive(1);
             if ($transaction->getTransactionObject()->isAuthorizationUncertain()) {
                 $order->addStatusHistoryComment($this->getHelper()->__('Authorization is uncertain.') . "\n<br>PaymentId:" . $transaction->getTransactionObject()->getPaymentId());
                 $order->save();
                 $this->createAuthPaymentTransaction($order, $transaction);
             } else {
                 $order->addStatusHistoryComment($this->getHelper()->__('Customer successfully returned from Saferpay') . "\n<br>PaymentId:" . $transaction->getTransactionObject()->getPaymentId());
                 $order->save();
                 $this->createAuthPaymentTransaction($order, $transaction);
                 if ($this->createInvoice($order, $transaction)) {
                     $this->getHelper()->log("Method::processPayment(): Invoice was created successfully.");
                     $order->addStatusHistoryComment($this->getHelper()->__('Invoice was created successfully'));
                     $order->save();
                     Mage::dispatchEvent('saferpaycw_invoice_created', array('order' => $order));
                 }
             }
             $quote->setIsActive(0);
             $quote->save();
             if ($transaction->getTransactionObject()->getTransactionContext()->isSendOrderEmail()) {
                 $order->sendNewOrderEmail();
             }
             $this->getHelper()->log("Method::processPayment(): Payment processed successfully.");
         } else {
             $this->getHelper()->log("Method::processPayment(): Transaction is not authorized.");
         }
         $order->save();
     } else {
         $this->getHelper()->log("Method::processPayment(): Order is not available.");
     }
 }