/**
  * Build a PaymentResult object from adapter results
  *
  * @param PaymentTransactionResponse $response processed response object
  * @param string $finalStatus final transaction status.
  *
  * @return PaymentResult
  */
 public static function fromResults(PaymentTransactionResponse $response, $finalStatus)
 {
     if ($finalStatus === FinalStatus::FAILED) {
         return PaymentResult::newFailure($response->getErrors());
     }
     if (!$response) {
         return PaymentResult::newEmpty();
     }
     if ($response->getErrors()) {
         // TODO: We will probably want the ability to refresh to a new form
         // as well and display errors at the same time.
         return PaymentResult::newRefresh($response->getErrors());
     }
     if ($response->getRedirect()) {
         return PaymentResult::newRedirect($response->getRedirect());
     }
     return PaymentResult::newSuccess();
 }
 public function doPayment()
 {
     $payment_method = $this->getPaymentMethod();
     // FIXME: this should happen during normalization, and before validatation.
     if ($payment_method === 'dd' and !$this->getPaymentSubmethod()) {
         // Synthesize a submethod based on the country.
         $country_code = strtolower($this->getData_Unstaged_Escaped('country'));
         $this->addRequestData(array('payment_submethod' => "dd_{$country_code}"));
     }
     // Execute the proper transaction code:
     switch ($payment_method) {
         case 'cc':
             // FIXME: we don't actually use this code path, it's done from
             // the donation api instead.
             $this->do_transaction('INSERT_ORDERWITHPAYMENT');
             // Display an iframe for credit cards
             return PaymentResult::newIframe($this->getTransactionDataFormAction());
         case 'bt':
         case 'obt':
             $this->do_transaction('INSERT_ORDERWITHPAYMENT');
             if (in_array($this->getFinalStatus(), $this->getGoToThankYouOn())) {
                 return PaymentResult::newForm('end-' . $payment_method);
             }
             break;
         case 'dd':
             $this->do_transaction('Direct_Debit');
             break;
         case 'ew':
         case 'rtbt':
         case 'cash':
             $this->do_transaction('INSERT_ORDERWITHPAYMENT');
             $formAction = $this->getTransactionDataFormAction();
             // Redirect to the bank
             if ($formAction) {
                 return PaymentResult::newRedirect($formAction);
             }
             break;
         default:
             $this->do_transaction('INSERT_ORDERWITHPAYMENT');
     }
     return PaymentResult::fromResults($this->transaction_response, $this->getFinalStatus());
 }