public function createOrder(Customweb_Payment_ExternalCheckout_IContext $context)
 {
     if (!$context instanceof Customweb_Payment_ExternalCheckout_AbstractContext) {
         throw new Customweb_Core_Exception_CastException('Customweb_Payment_ExternalCheckout_AbstractContext');
     }
     try {
         if ($context->getState() == Customweb_Payment_ExternalCheckout_IContext::STATE_COMPLETED) {
             $transcationId = $context->getTransactionId();
             if (empty($transcationId)) {
                 throw new Exception("Invalid state. The context can not be in state COMPLETED without transaction id set.");
             }
             return $this->getTransactionHandler()->findTransactionByTransactionId($transcationId);
         } else {
             if ($context->getState() == Customweb_Payment_ExternalCheckout_IContext::STATE_FAILED) {
                 throw new Exception("A failed context cannot be completed.");
             }
         }
         $this->checkContextCompleteness($context);
         $this->getTransactionHandler()->beginTransaction();
         $transactionContext = $this->createTransactionContextFromContext($context);
         $transactionObject = $this->getProviderService()->createTransaction($transactionContext, $context);
         $this->getTransactionHandler()->persistTransactionObject($transactionObject);
         $context->setTransactionId($transactionObject->getTransactionContext()->getTransactionId());
         $context->setState(Customweb_Payment_ExternalCheckout_IContext::STATE_COMPLETED);
         $this->entityManager->persist($context);
         $this->getTransactionHandler()->commitTransaction();
         return $transactionObject;
     } catch (Exception $e) {
         if ($this->getTransactionHandler()->isTransactionRunning()) {
             $this->getTransactionHandler()->rollbackTransaction();
         }
         throw $e;
     }
 }