jenkins-bot has submitted this change and it was merged.

Change subject: Move remaining controller logic into GatewayAdapter::doPayment()
......................................................................


Move remaining controller logic into GatewayAdapter::doPayment()

We want to separate controller and view code, so we can eventually
replace our horrific templating with modernized and reusable code.

Bug: T86251
Change-Id: I3e542ad4b38fe294744084e15d46cb91010a2e6d
---
M adyen_gateway/adyen.adapter.php
M amazon_gateway/amazon.adapter.php
M amazon_gateway/amazon_gateway.body.php
M gateway_common/GatewayPage.php
M gateway_common/PaymentResult.php
M gateway_common/gateway.adapter.php
M globalcollect_gateway/globalcollect.adapter.php
M globalcollect_gateway/globalcollect_gateway.body.php
M paypal_gateway/paypal.adapter.php
M paypal_gateway/paypal_gateway.body.php
M tests/includes/test_gateway/TestingGenericAdapter.php
M worldpay_gateway/worldpay.adapter.php
M worldpay_gateway/worldpay.api.php
M worldpay_gateway/worldpay_gateway.body.php
14 files changed, 264 insertions(+), 221 deletions(-)

Approvals:
  Ejegg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/adyen_gateway/adyen.adapter.php b/adyen_gateway/adyen.adapter.php
index 9fae997..cb0afa7 100644
--- a/adyen_gateway/adyen.adapter.php
+++ b/adyen_gateway/adyen.adapter.php
@@ -176,13 +176,15 @@
                );
        }
 
+       function doPayment() {
+               return PaymentResult::fromResults(
+                       $this->do_transaction( 'donate' ),
+                       $this->getFinalStatus()
+               );
+       }
+
        /**
-        * Because GC has some processes that involve more than one 
do_transaction 
-        * chained together, we're catching those special ones in an overload 
and 
-        * letting the rest behave normally.
-        * @TODO: If this is a pattern we want to be able to reuse, it should be
-        * represented in the base class.
-        * I can't help but feel like it's bad that the parent's do_transaction
+        * FIXME: I can't help but feel like it's bad that the parent's 
do_transaction
         * is never used at all.
         */
        function do_transaction( $transaction ) {
diff --git a/amazon_gateway/amazon.adapter.php 
b/amazon_gateway/amazon.adapter.php
index 75047df..47b9732 100644
--- a/amazon_gateway/amazon.adapter.php
+++ b/amazon_gateway/amazon.adapter.php
@@ -188,6 +188,19 @@
                return $queryparams;
        }
 
+       public function doPayment() {
+               if ( $this->getData_Unstaged_Escaped( 'recurring' ) ) {
+                       $resultData = $this->do_transaction( 'DonateMonthly' );
+               } else {
+                       $resultData = $this->do_transaction( 'Donate' );
+               }
+
+               return PaymentResult::fromResults(
+                       $resultData,
+                       $this->getFinalStatus()
+               );
+       }
+
        function do_transaction( $transaction ) {
                global $wgRequest, $wgOut;
                $this->session_addDonorData();
@@ -237,7 +250,7 @@
 
                                //@TODO: This shouldn't be happening here. Oh 
Amazon... Why can't you be more like PayPalAdapter?
                                $wgOut->redirect("{$this->getGlobal( "URL" 
)}?{$query_str}&signature={$signature}");
-                               return;
+                               break;
 
                        case 'VerifySignature':
                                // We don't currently use this. In fact we just 
ignore the return URL signature.
@@ -255,19 +268,20 @@
                                        $this->runPostProcessHooks();
                                        $this->doLimboStompTransaction( true );
                                }
-                               return;
+                               break;
 
                        case 'ProcessAmazonReturn':
                                // What we need to do here is make sure
                                $this->addDataFromURI();
                                $this->analyzeReturnStatus();
-                               return;
+                               break;
 
                        default:
                                $this->log( "At $transaction; THIS IS NOT 
DEFINED!", LOG_CRIT );
                                $this->finalizeInternalStatus( 'failed' );
-                               return;
                }
+
+               return $this->getTransactionAllResults();
        }
 
        static function getCurrencies() {
@@ -492,6 +506,8 @@
                                if ( $childnode->nodeName === "Message" ) {
                                        $message = $childnode->nodeValue;
                                }
+                               // TODO: Convert to internal codes and 
translate.
+                               // $errors[$code] = $message;
                        }
                }
                return $errors;
diff --git a/amazon_gateway/amazon_gateway.body.php 
b/amazon_gateway/amazon_gateway.body.php
index 785f4b9..6f356d9 100644
--- a/amazon_gateway/amazon_gateway.body.php
+++ b/amazon_gateway/amazon_gateway.body.php
@@ -43,13 +43,17 @@
                                if ( $this->getRequest()->getText( 'ffname', 
'default' ) === 'amazon-recurring'
                                        ||  $this->getRequest()->getText( 
'recurring', 0 )
                                ) {
-                                       $this->adapter->do_transaction( 
'DonateMonthly' );
-                               } else {
-                                       $this->adapter->do_transaction( 
'Donate' );
+                                       // FIXME: do this in the form param 
harvesting step
+                                       $this->adapter->addData( array(
+                                               'recurring' => 1,
+                                       ) );
                                }
+                               $this->adapter->doPayment();
+                               // TODO: move redirect here.
                                return;
                        }
 
+                       // TODO: move resultswitching out
                        $this->log( 'At gateway return with params: ' . 
json_encode( $this->getRequest()->getValues() ), LOG_INFO );
                        if ( $this->adapter->checkTokens() && 
$this->getRequest()->getText( 'status' ) ) {
                                $this->adapter->do_transaction( 
'ProcessAmazonReturn' );
diff --git a/gateway_common/GatewayPage.php b/gateway_common/GatewayPage.php
index f51046c..28c806d 100644
--- a/gateway_common/GatewayPage.php
+++ b/gateway_common/GatewayPage.php
@@ -346,18 +346,16 @@
 
                // dispatch forms/handling
                if ( $this->adapter->checkTokens() ) {
-                       // If the user posted to this form, or we've been sent 
here with an
-                       // immediate "redirect=1" param, try to process the 
transaction.
-                       if ( $this->adapter->posted
-                               || $this->adapter->getData_Unstaged_Escaped( 
'redirect' )
-                       ) {
+                       if ( $this->isProcessImmediate() ) {
                                // Check form for errors
+                               // FIXME: Should this be rolled into 
adapter.doPayment?
                                $form_errors = $this->validateForm();
 
                                // If there were errors, redisplay form, 
otherwise proceed to next step
                                if ( $form_errors ) {
                                        $this->displayForm();
                                } else {
+                                       // Attempt to process the payment, and 
render the response.
                                        $this->processPayment();
                                }
                        } else {
@@ -365,10 +363,22 @@
                                $this->displayForm();
                        }
                } else { //token mismatch
-                       $error['general']['token-mismatch'] = wfMsg( 
'donate_interface-token-mismatch' );
+                       $error['general']['token-mismatch'] = $this->msg( 
'donate_interface-token-mismatch' );
                        $this->adapter->addManualError( $error );
                        $this->displayForm();
                }
+       }
+
+       /**
+        * Determine if we should attempt to process the payment now
+        *
+        * @return bool True if we should attempt processing.
+        */
+       protected function isProcessImmediate() {
+               // If the user posted to this form, or we've been sent here 
with an
+               // immediate "redirect=1" param, try to process the transaction.
+               return $this->adapter->posted
+                       || $this->adapter->getData_Unstaged_Escaped( 'redirect' 
) === '1';
        }
 
        /**
@@ -395,6 +405,7 @@
                        $liberated = true;
                }
 
+               // XXX need to know whether we were in an iframe or not.
                global $wgServer;
                if ( ( strpos( $referrer, $wgServer ) === false ) && 
!$liberated ) {
                        $_SESSION[ 'order_status' ][ $oid ] = 'liberated';
@@ -445,4 +456,62 @@
        protected function processPayment() {
                $this->renderResponse( $this->adapter->doPayment() );
        }
+
+       /**
+        * Take UI action suggested by the payment result
+        */
+       protected function renderResponse( PaymentResult $result ) {
+               if ( $result->isFailed() ) {
+                       $this->getOutput()->redirect( 
$this->adapter->getFailPage() );
+               } elseif ( $url = $result->getRedirect() ) {
+                       $this->getOutput()->redirect( $url );
+               } elseif ( $url = $result->getIframe() ) {
+                       // Show a form containing an iframe.
+
+                       // Well, that's sketchy.  See TODO in renderIframe: we 
should
+                       // accomplish this entirely by passing an iframeSrcUrl 
parameter
+                       // to the template.
+                       $this->displayForm();
+
+                       $this->renderIframe( $url );
+               } elseif ( $form = $result->getForm() ) {
+                       // Show another form.
+
+                       $this->adapter->addRequestData( array(
+                               'ffname' => $form,
+                       ) );
+                       $this->displayForm();
+               } elseif ( $errors = $result->getErrors() ) {
+                       // FIXME: Creepy.  Currently, the form inspects adapter 
errors.  Use
+                       // the stuff encapsulated in PaymentResult instead.
+                       $this->displayForm();
+               } else {
+                       // Success.
+                       $this->getOutput()->redirect( 
$this->adapter->getThankYouPage() );
+               }
+       }
+
+       /**
+        * Append iframe
+        *
+        * TODO: Should be rendered by the template.
+        *
+        * @param string $url
+        */
+       protected function renderIframe( $url ) {
+               $attrs = array(
+                       'id' => 'paymentiframe',
+                       'name' => 'paymentiframe',
+                       'width' => '680',
+                       'height' => '300'
+               );
+
+               $attrs['frameborder'] = '0';
+               $attrs['style'] = 'display:block;';
+               $attrs['src'] = $url;
+               $paymentFrame = Xml::openElement( 'iframe', $attrs );
+               $paymentFrame .= Xml::closeElement( 'iframe' );
+
+               $this->getOutput()->addHTML( $paymentFrame );
+       }
 }
diff --git a/gateway_common/PaymentResult.php b/gateway_common/PaymentResult.php
index 731a646..bed6add 100644
--- a/gateway_common/PaymentResult.php
+++ b/gateway_common/PaymentResult.php
@@ -24,11 +24,12 @@
  *     in the URL's GET params.
  */
 class PaymentResult {
-       protected $iframe = false;
-       protected $form = false;
-       protected $redirect = false;
-       protected $refresh = false;
+       protected $iframe;
+       protected $form;
+       protected $redirect;
+       protected $refresh;
        protected $errors = array();
+       protected $failed;
 
        protected function __construct() {
        }
@@ -63,11 +64,18 @@
                return $response;
        }
 
+       static public function newFailure() {
+               $response = new PaymentResult();
+               $response->failed = true;
+               return $response;
+       }
+
        static public function newEmpty() {
                $response = new PaymentResult();
                $response->errors = array(
                        'internal-0000' => 'Internal error: no results yet.',
                );
+               $response->failed = true;
                return $response;
        }
 
@@ -91,10 +99,30 @@
                return $this->errors;
        }
 
-       static public function fromResultsArray( $data ) {
+       public function isFailed() {
+               return $this->failed;
+       }
+
+       /**
+        * Build a PaymentResult object from adapter results
+        *
+        * @param array $data getTransactionAllResults contents.
+        * @param string $finalStatus final transaction status.
+        */
+       static public function fromResults( $data, $finalStatus ) {
+               if ( $finalStatus === 'failed' ) {
+                       return PaymentResult::newFailure();
+               }
                if ( $data === false ) {
                        return PaymentResult::newEmpty();
                }
+               if ( array_key_exists( 'errors', $data )
+                       && $data['errors']
+               ) {
+                       // 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( $data['errors'] );
+               }
                if ( array_key_exists( 'redirect', $data ) ) {
                        return PaymentResult::newRedirect( $data['redirect'] );
                }
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index b001b6e..e01838a 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -157,6 +157,13 @@
        function setGatewayDefaults();
 
        static function getCurrencies();
+
+       /**
+        * Attempt the default transaction for the current DonationData
+        *
+        * @return PaymentResult hints for the next donor interaction
+        */
+       function doPayment();
 }
 
 /**
diff --git a/globalcollect_gateway/globalcollect.adapter.php 
b/globalcollect_gateway/globalcollect.adapter.php
index b5200a0..11e45df 100644
--- a/globalcollect_gateway/globalcollect.adapter.php
+++ b/globalcollect_gateway/globalcollect.adapter.php
@@ -1064,6 +1064,64 @@
                );
        }
 
+       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 gc.cc.js 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->getTransactionAllResults(),
+                       $this->getFinalStatus()
+               );
+       }
+
        /**
         * Because GC has some processes that involve more than one 
do_transaction
         * chained together, we're catching those special ones in an overload 
and
diff --git a/globalcollect_gateway/globalcollect_gateway.body.php 
b/globalcollect_gateway/globalcollect_gateway.body.php
index f18cd72..3509eca 100644
--- a/globalcollect_gateway/globalcollect_gateway.body.php
+++ b/globalcollect_gateway/globalcollect_gateway.body.php
@@ -38,162 +38,25 @@
        protected function handleRequest() {
                $this->getOutput()->allowClickjacking();
 
-               $this->setHeaders();
-
-               // dispatch forms/handling
-               if ( $this->adapter->checkTokens() ) {
-                       if ( $this->adapter->posted ) {
-                               // The form was submitted and the payment 
method has been set
-                               $payment_method = 
$this->adapter->getPaymentMethod();
-
-                               if ( $payment_method === 'dd'
-                                               and 
!$this->adapter->getPaymentSubmethod() ) {
-                                       // Synthesize a submethod based on the 
country.
-                                       $country_code = strtolower( 
$this->adapter->getData_Unstaged_Escaped( 'country' ) );
-                                       $this->adapter->addRequestData( array(
-                                               'payment_submethod' => 
"dd_{$country_code}",
-                                       ) );
-                               }
-
-                               // If there were errors, redisplay form, 
otherwise proceed to next step
-                               if ( $this->validateForm() ) {
-                                       $this->displayForm();
-                               } else { // The submitted form data is valid, 
so process it
-                                       // allow any external validators to 
have their way with the data
-                                       // Execute the proper transaction code:
-                                       
-                                       switch ( $payment_method ){
-                                               case 'cc': 
-                                                       // FIXME: we don't 
actually use this code path, it's done from gc.cc.js instead.
-
-                                                       
$this->adapter->do_transaction( 'INSERT_ORDERWITHPAYMENT' );
-
-                                                       // Display an iframe 
for credit cards
-                                                       if ( 
$this->executeIframeForCreditCard() ) {
-                                                               
$this->displayResultsForDebug();
-                                                               // Nothing left 
to process
-                                                               return;
-                                                       }
-                                                       break;
-                                                       
-                                               case 'bt':
-                                               case 'obt':
-                                                       
$this->adapter->do_transaction( 'INSERT_ORDERWITHPAYMENT' );
-
-                                                       if ( in_array( 
$this->adapter->getFinalStatus(), $this->adapter->getGoToThankYouOn() ) ) {
-                                                               return 
$this->displayEndTransactionInfo( $payment_method );
-                                                       }
-                                                       break;
-                                                       
-                                               case 'dd':
-                                                       $result = 
$this->adapter->do_transaction('Direct_Debit');
-                                                       break;
-                                                       
-                                               case 'ew':
-                                               case 'rtbt':
-                                               case 'cash':
-                                                       
$this->adapter->do_transaction( 'INSERT_ORDERWITHPAYMENT' );
-                                                       $formAction = 
$this->adapter->getTransactionDataFormAction();
-
-                                                       // Redirect to the bank
-                                                       if ( !empty( 
$formAction ) ) {
-                                                               return 
$this->getOutput()->redirect( $formAction );
-                                                       }
-                                                       break;
-                                               
-                                               default: 
-                                                       
$this->adapter->do_transaction( 'INSERT_ORDERWITHPAYMENT' );
-                                       }
-
-                                       return $this->resultHandler();
-
-                               }
-                       } else {
-                               // Display form
-
-                               //TODO: NO.
-                               //This probably has something to do with the 
dumbass way that bt, rtbt, and dd were done.
-                               //
-//                             // See GlobalCollectAdapter::stage_returnto()
-//                             $oid = $this->getRequest()->getText( 'order_id' 
);
-//                             if ( $oid ) {
-//                                     $this->adapter->do_transaction( 
'GET_ORDERSTATUS' );
-//                                     $this->displayResultsForDebug();
-//                             }
-                               //TODO: Get rid of $data out here completely, 
by putting this logic inside the adapter somewhere.
-                               //All we seem to be doing with it now, is 
internal adapter logic outside of the adapter.
-                               $data = 
$this->adapter->getData_Unstaged_Escaped();
-
-                               // If the result of the previous transaction 
was failure, set the retry message.
-                               if ( $data && array_key_exists( 'response', 
$data ) && $data['response'] == 'failure' ) {
-                                       $error['retryMsg'] = $this->msg( 
'php-response-declined' )->text();
-                                       $this->adapter->addManualError( $error 
);
-                               }
-
-                               $this->displayForm();
-                       }
-               } else { //token mismatch
-                       $error['general']['token-mismatch'] = $this->msg( 
'donate_interface-token-mismatch' )->text();
-                       $this->adapter->addManualError( $error );
-                       $this->displayForm();
-               }
+               $this->handleDonationRequest();
        }
 
-       /**
-        * Execute iframe for credit card
-        *
-        * @return boolean      Returns true if formaction exists for iframe.
-        */
-       protected function executeIframeForCreditCard() {
-               $formAction = $this->adapter->getTransactionDataFormAction();
-               $mercid = $this->adapter->getMerchantID();
-
-               $attrs = array(
-                       'id' => 'globalcollectframe',
-                       'name' => 'globalcollectframe',
-                       'width' => '680',
-                       'height' => '300'
-               );
-
-               if ( $formAction ) {
-                       if ( $mercid === 'test' ) {
-                               $paymentFrame = (
-                                       Xml::openElement( 'div', $attrs ) .
-                                       '<input type="button" 
id="globalcollect_gateway-fakesucceed"' .
-                                       'value="' . $this->msg( 
'globalcollect_gateway-fakesucceed' ) . '" />' .
-                                       '<input type="button" 
id="globalcollect_gateway-fakefail"' .
-                                       'value="' . $this->msg( 
'globalcollect_gateway-fakefail' ) . '" />' .
-                                       Xml::closeElement( 'div' )
-                               );
-                       } else {
-                               $attrs['frameborder'] = '0';
-                               $attrs['style'] = 'display:block;';
-                               $attrs['src'] = $formAction;
-                               $paymentFrame = Xml::openElement( 'iframe', 
$attrs );
-                               $paymentFrame .= Xml::closeElement( 'iframe' );
-                       }
-
-                       $this->getOutput()->addHTML( $paymentFrame );
-
-                       return true;
-               }
-
-               return false;
-       }
-       
-       protected function displayEndTransactionInfo( $payment_method ){
-               switch ( $payment_method ){
-                       case 'bt':
-                               return $this->displayBankTransferInformation();
-                               break;
-                       case 'obt':
-                               return 
$this->displayOnlineBankTransferInformation();
-                               break;
+       protected function renderResponse( PaymentResult $result ) {
+               // FIXME: This workaround can be deleted once we convert
+               // these two result pages to render using normal templates.
+               if ( $result->getForm() === 'end-bt' ) {
+                       $this->displayBankTransferInformation();
+               } elseif ( $result->getForm() === 'end-obt' ) {
+                       $this->displayOnlineBankTransferInformation();
+               } else {
+                       parent::renderResponse( $result );
                }
        }
 
        /**
         * Display information for bank transfer
+        *
+        * @deprecated
         */
        protected function displayBankTransferInformation() {
                $data = $this->adapter->getTransactionData();
@@ -241,11 +104,13 @@
                $return .= Xml::tags( 'p', array( 'style' => 
'text-align:center;' ), $link );
                $return .= Xml::closeElement( 'div' );  // $id
 
-               return $this->getOutput()->addHTML( $return );
+               $this->getOutput()->addHTML( $return );
        }
 
        /**
         * Display information for online bank transfer
+        *
+        * @deprecated
         */
        protected function displayOnlineBankTransferInformation() {
                global $wgScriptPath;
@@ -305,6 +170,6 @@
                $return .= Xml::tags( 'p', array(), $link );
                $return .= Xml::closeElement( 'div' );  // $id
 
-               return $this->getOutput()->addHTML( $return );
+               $this->getOutput()->addHTML( $return );
        }
 }
diff --git a/paypal_gateway/paypal.adapter.php 
b/paypal_gateway/paypal.adapter.php
index b3e98a6..0ac8a1f 100644
--- a/paypal_gateway/paypal.adapter.php
+++ b/paypal_gateway/paypal.adapter.php
@@ -172,6 +172,24 @@
                );
        }
 
+       public function doPayment() {
+               if ( $this->getData_Unstaged_Escaped( 'recurring' ) ) {
+                       $resultData = $this->do_transaction( 'DonateRecurring' 
);
+               } else {
+                       $country = $this->getData_Unstaged_Escaped( 'country' );
+                       if ( in_array( $country, $this->getGlobal( 
'XclickCountries' ) ) ) {
+                               $resultData = $this->do_transaction( 
'DonateXclick' );
+                       } else {
+                               $resultData = $this->do_transaction( 'Donate' );
+                       }
+               }
+
+               return PaymentResult::fromResults(
+                       $resultData,
+                       $this->getFinalStatus()
+               );
+       }
+
        function do_transaction( $transaction ) {
                $this->session_addDonorData();
                $this->setCurrentTransaction( $transaction );
diff --git a/paypal_gateway/paypal_gateway.body.php 
b/paypal_gateway/paypal_gateway.body.php
index 1d703f8..8ae2b63 100644
--- a/paypal_gateway/paypal_gateway.body.php
+++ b/paypal_gateway/paypal_gateway.body.php
@@ -32,32 +32,13 @@
        protected function handleRequest() {
                $this->getOutput()->allowClickjacking();
 
-               $this->setHeaders();
-
-               if ( $this->validateForm() ) {
-                       $this->displayForm();
-                       return;
+               if ( $this->getRequest()->getText( 'ffname', 'default' ) === 
'paypal-recurring' ) {
+                       // FIXME: do this in the form param harvesting step
+                       $this->adapter->addData( array(
+                               'recurring' => 1,
+                       ) );
                }
 
-               // We also switch on the form name--if we're redirecting 
without stopping
-               // for user interaction, the form name is our only clue that 
this is recurring.
-               if ( $this->getRequest()->getText( 'ffname', 'default' ) === 
'paypal-recurring'
-                       or $this->getRequest()->getText( 'recurring', 0 )
-               ) {
-                       $result = $this->adapter->do_transaction( 
'DonateRecurring' );
-               } else {
-                       $country = $this->adapter->getData_Unstaged_Escaped( 
'country' );
-                       if ( array_search( $country, $this->adapter->getGlobal( 
'XclickCountries' ) ) !== false ) {
-                               $result = $this->adapter->do_transaction( 
'DonateXclick' );
-                       } else {
-                               $result = $this->adapter->do_transaction( 
'Donate' );
-                       }
-               }
-
-               if ( !empty( $result['redirect'] ) ) {
-                       $this->getOutput()->redirect( $result['redirect'] );
-               }
-
-               $this->displayForm();
+               $this->handleDonationRequest();
        }
 }
diff --git a/tests/includes/test_gateway/TestingGenericAdapter.php 
b/tests/includes/test_gateway/TestingGenericAdapter.php
index a70a404..c80bcb5 100644
--- a/tests/includes/test_gateway/TestingGenericAdapter.php
+++ b/tests/includes/test_gateway/TestingGenericAdapter.php
@@ -100,4 +100,6 @@
                return TestingGenericAdapter::$acceptedCurrencies;
        }
 
+       public function doPayment() {
+       }
 }
diff --git a/worldpay_gateway/worldpay.adapter.php 
b/worldpay_gateway/worldpay.adapter.php
index 3f2bb5b..5c36b35 100644
--- a/worldpay_gateway/worldpay.adapter.php
+++ b/worldpay_gateway/worldpay.adapter.php
@@ -736,6 +736,13 @@
                        && in_array( $this->staged_data['wp_storeid'], 
$this->account_config['SpecialSnowflakeStoreIDs'] );
        }
 
+       public function doPayment() {
+               return PaymentResult::fromResults(
+                       $this->do_transaction( 'QueryAuthorizeDeposit' ),
+                       $this->getFinalStatus()
+               );
+       }
+
        public function do_transaction( $transaction ) {
                $this->url = $this->getGlobal( 'URL' );
 
diff --git a/worldpay_gateway/worldpay.api.php 
b/worldpay_gateway/worldpay.api.php
index b500b6c..004926a 100644
--- a/worldpay_gateway/worldpay.api.php
+++ b/worldpay_gateway/worldpay.api.php
@@ -18,6 +18,8 @@
        public function execute() {
                $adapter = new WorldPayAdapter( array( 'api_request' => true ) 
);
 
+               // FIXME: move this workflow into the adapter class.
+
                // Do some validity checking and what not
                if ( $adapter->checkTokens() ) {
                        $adapter->revalidate();
diff --git a/worldpay_gateway/worldpay_gateway.body.php 
b/worldpay_gateway/worldpay_gateway.body.php
index 2b6e149..5d319be 100644
--- a/worldpay_gateway/worldpay_gateway.body.php
+++ b/worldpay_gateway/worldpay_gateway.body.php
@@ -39,28 +39,12 @@
        protected function handleRequest() {
                $this->getOutput()->addModules( 
'ext.donationinterface.worldpay.styles' ); //loads early
                $this->getOutput()->addModules( 
'ext.donationinterface.worldpay.code' ); //loads at normal time
-               $this->setHeaders();
 
-               // dispatch forms/handling
-               if ( $this->adapter->checkTokens() ) {
-                       $ott = $this->getRequest()->getText( 'OTT' );
-                       if ( $ott ) {
-                               $this->adapter->do_transaction( 
'QueryAuthorizeDeposit' );
-                               if ( $this->adapter->getFinalStatus() === 
'failed' ) {
-                                       $this->getOutput()->redirect( 
$this->adapter->getFailPage() );
-                               } else {
-                                       $this->getOutput()->redirect( 
$this->adapter->getThankYouPage() );
-                               }
+               $this->handleDonationRequest();
+       }
 
-                       } else {
-                               // Show either the initial form or an error form
-                               $this->adapter->session_addDonorData();
-                               $this->displayForm();
-                       }
-               } else { //token mismatch
-                       $error['general']['token-mismatch'] = $this->msg( 
'donate_interface-token-mismatch' )->text();
-                       $this->adapter->addManualError( $error );
-                       $this->displayForm();
-               }
+       protected function isProcessImmediate() {
+               // FIXME: should be checked by the adapter rather than looking 
at the request.
+               return $this->getRequest()->getText( 'OTT' );
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/189153
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3e542ad4b38fe294744084e15d46cb91010a2e6d
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to