r107609 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107608‎ | r107609 | r107610 >
Date:01:05, 30 December 2011
Author:khorn
Status:deferred
Tags:fundraising 
Comment:
Replacing most of the haphazard validation found throughout DonationInterface, with everything that has moved to the DataValidator class. There is definitely still work to be done here: This commit was intended to not move the ball so much as move to the new validation methods without losing anything.
* Now loads and uses the DataValidator class
* Clears a lot of the stuff that started in DonationData, but moved to the DataValidator
* Added this... concept of "manual" errors to the main gateway object. The idea there is that you don't want errors you've set manually to disappear when you re-validate the gateway adapter data set.
* ALL FORM CLASSES now pull their form errors straight from the gateway. Finally down to one parameter. Woot.
* Changed an old 'invalidamount' error token to 'amount', which is the only thing RapidHTML knows about, and it was clear it was supposed to be the same thing all along.
Modified paths:
  • /trunk/extensions/DonationInterface/donationinterface.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_common/DataValidator.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_common/DonationData.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_common/GatewayForm.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_common/gateway.adapter.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/Form.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/OneStepTwoColumn.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/RapidHtml.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/SingleColumn.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter2.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter3.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter4.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter5.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter6.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter7.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoColumnPayPal.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumn.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetter.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetter2.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetter3.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetterCA.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnPremium.php (modified) (history)
  • /trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnPremiumUS.php (modified) (history)
  • /trunk/extensions/DonationInterface/globalcollect_gateway/globalcollect_gateway.body.php (modified) (history)
  • /trunk/extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php (modified) (history)
  • /trunk/extensions/DonationInterface/tests/resources/forms/UniversalTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DonationInterface/donationinterface.php
@@ -87,6 +87,7 @@
8888 $wgAutoloadClasses['DonationData'] = $donationinterface_dir . 'gateway_common/DonationData.php';
8989 $wgAutoloadClasses['GatewayAdapter'] = $donationinterface_dir . 'gateway_common/gateway.adapter.php';
9090 $wgAutoloadClasses['GatewayForm'] = $donationinterface_dir . 'gateway_common/GatewayForm.php';
 91+$wgAutoloadClasses['DataValidator'] = $donationinterface_dir . 'gateway_common/DataValidator.php';
9192
9293 //load all possible form classes
9394 $wgAutoloadClasses['Gateway_Form'] = $donationinterface_dir . 'gateway_forms/Form.php';
Index: trunk/extensions/DonationInterface/tests/resources/forms/UniversalTest.php
@@ -158,7 +158,7 @@
159159
160160 $return .= $this->getFormMessagesByType('general');
161161
162 - $return .= $this->getFormMessagesByType('invalidamount');
 162+ $return .= $this->getFormMessagesByType('amount');
163163
164164 $return .= $this->getFormMessagesByType('retryMsg');
165165
Index: trunk/extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php
@@ -39,10 +39,10 @@
4040 if ( $this->adapter->posted ) {
4141 // The form was submitted and the payment method has been set
4242 // Check form for errors
43 - $form_errors = $this->validateForm( $this->errors );
 43+ $form_errors = $this->validateForm();
4444 // If there were errors, redisplay form, otherwise proceed to next step
4545 if ( $form_errors ) {
46 - $this->displayForm( $this->errors );
 46+ $this->displayForm();
4747 } else { // The submitted form data is valid, so process it
4848 $result = $this->adapter->do_transaction( 'Card' );
4949
@@ -58,11 +58,12 @@
5959 }
6060 } else {
6161 // Display form for the first time
62 - $this->displayForm( $this->errors );
 62+ $this->displayForm();
6363 }
6464 } else {//token mismatch
65 - $this->errors['general']['token-mismatch'] = wfMsg( 'donate_interface-token-mismatch' );
66 - $this->displayForm( $this->errors );
 65+ $error['general']['token-mismatch'] = wfMsg( 'donate_interface-token-mismatch' );
 66+ $this->adapter->addManualError( $error );
 67+ $this->displayForm();
6768 }
6869 }
6970
@@ -94,8 +95,9 @@
9596 } elseif ( ( $errorCode == '3' ) && ( $data['numAttempt'] < '5' ) ) {
9697 $this->log( $msgPrefix . "Transaction unsuccessful (invalid info).", LOG_DEBUG );
9798 // pass responseMsg as an array key as required by displayForm
98 - $this->errors['retryMsg'] = $responseMsg;
99 - $this->displayForm( $this->errors );
 99+ $error['retryMsg'] = $responseMsg;
 100+ $this->adapter->addManualError( $error );
 101+ $this->displayForm();
100102 // if declined or if user has already made two attempts, decline
101103 } elseif ( ( $errorCode == '2' ) || ( $data['numAttempt'] >= '3' ) ) {
102104 $this->log( $msgPrefix . "Transaction declined.", LOG_DEBUG );
@@ -108,14 +110,16 @@
109111 $this->fnPayflowDisplayPending( $data, $responseMsg );
110112 } elseif ( strpos( $errorCode, 'internal' ) === 0 ) {
111113 $this->log( $msgPrefix . "Transaction unsuccessful (communication failure).", LOG_DEBUG );
112 - $this->errors['retryMsg'] = $responseMsg;
113 - $this->displayForm( $this->errors );
 114+ $error['retryMsg'] = $responseMsg;
 115+ $this->adapter->addManualError( $error );
 116+ $this->displayForm();
114117 } elseif ( !empty( $errorCode ) ) {
115118 // This should not be hit.
116119 $this->log( $msgPrefix . "Transaction unsuccessful (unknown failure).", LOG_DEBUG );
117120 $this->fnPayflowDisplayOtherResults( $responseMsg );
118 - $this->errors['retryMsg'] = $errorCode;
119 - $this->displayForm( $this->errors );
 121+ $error['retryMsg'] = $errorCode;
 122+ $this->adapter->addManualError( $error );
 123+ $this->displayForm();
120124 }
121125 }
122126
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter2.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter2 extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormStart() {
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter3.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter3 extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormStart() {
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter4.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter4 extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter4.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormStart() {
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter5.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter5 extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter5.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormStart() {
@@ -92,7 +92,7 @@
9393 $amount = $this->getEscapedValue( 'amountOther' );
9494 }
9595 $form .= '<tr>';
96 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 96+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
9797 $form .= '</tr>';
9898 $form .= '<tr>';
9999 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter6.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter6 extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter6.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormStart() {
@@ -92,7 +92,7 @@
9393 $amount = $this->getEscapedValue( 'amountOther' );
9494 }
9595 $form .= '<tr>';
96 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 96+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
9797 $form .= '</tr>';
9898 $form .= '<tr>';
9999 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetter.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class Gateway_Form_TwoStepTwoColumnLetter extends Gateway_Form_TwoStepTwoColumn {
5 - public function __construct( &$gateway, &$form_errors ) {
 5+ public function __construct( &$gateway ) {
66 global $wgScriptPath;
77
88 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -9,7 +9,7 @@
1010 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoStepTwoColumnLetter.css' );
1111 }
1212
13 - parent::__construct( $gateway, $form_errors );
 13+ parent::__construct( $gateway );
1414 }
1515
1616 public function generateFormStart() {
@@ -84,7 +84,7 @@
8585
8686 // amount
8787 $form .= '<tr>';
88 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 88+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
8989 $form .= '</tr>';
9090 $form .= '<tr>';
9191 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter7.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter7 extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter7.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function loadPlaceholders() {
@@ -213,7 +213,7 @@
214214 $amount = $this->getEscapedValue( 'amountOther' );
215215 }
216216 $form .= '<tr>';
217 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 217+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
218218 $form .= '</tr>';
219219 $form .= '<tr>';
220220 $form .= '<td class="label"><div style="padding-top:4px;">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</div></td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnPayPal.php
@@ -1,8 +1,8 @@
22 <?php
33
44 class Gateway_Form_TwoColumnPayPal extends Gateway_Form_OneStepTwoColumn {
5 - public function __construct( &$gateway, &$form_errors ) {
6 - parent::__construct( $gateway, $form_errors );
 5+ public function __construct( &$gateway ) {
 6+ parent::__construct( $gateway );
77 }
88
99 public function generateFormStart() {
Index: trunk/extensions/DonationInterface/gateway_forms/SingleColumn.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_SingleColumn extends Gateway_Form_TwoColumnLetter {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/SingleColumn.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormEnd() {
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetterCA.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class Gateway_Form_TwoStepTwoColumnLetterCA extends Gateway_Form_TwoStepTwoColumn {
5 - public function __construct( &$gateway, &$form_errors ) {
 5+ public function __construct( &$gateway ) {
66 global $wgScriptPath;
77
88 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -9,7 +9,7 @@
1010 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoStepTwoColumnLetter.css' );
1111 }
1212
13 - parent::__construct( $gateway, $form_errors );
 13+ parent::__construct( $gateway );
1414 }
1515
1616 public function generateFormStart() {
@@ -84,7 +84,7 @@
8585
8686 // amount
8787 $form .= '<tr>';
88 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 88+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
8989 $form .= '</tr>';
9090 $form .= '<tr>';
9191 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/Form.php
@@ -62,12 +62,16 @@
6363 */
6464 abstract function getForm();
6565
66 - public function __construct( &$gateway, &$error ) {
 66+ public function __construct( &$gateway ) {
6767 global $wgOut, $wgRequest;
6868
6969 $this->gateway = & $gateway;
7070 $this->test = $this->gateway->getGlobal( "Test" );
71 - $this->form_errors = & $error;
 71+ $gateway_errors = $this->gateway->getAllErrors();
 72+ if ( !is_array( $gateway_errors ) ){
 73+ $gateway_errors = array();
 74+ }
 75+ $this->form_errors = array_merge( DataValidator::getEmptyErrorArray(), $gateway_errors );
7276 $this->paypal = $wgRequest->getBool( 'paypal', false );
7377
7478 /**
@@ -599,7 +603,7 @@
600604 $amount = $this->getEscapedValue( 'amountOther' );
601605 }
602606 $form = '<tr>';
603 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 607+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
604608 $form .= '</tr>';
605609 $form .= '<tr>';
606610 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoColumnLetter.php
@@ -2,7 +2,7 @@
33
44 class Gateway_Form_TwoColumnLetter extends Gateway_Form_OneStepTwoColumn {
55
6 - public function __construct( &$gateway, &$form_errors ) {
 6+ public function __construct( &$gateway ) {
77 global $wgScriptPath;
88
99 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -10,7 +10,7 @@
1111 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoColumnLetter.css' );
1212 }
1313
14 - parent::__construct( $gateway, $form_errors );
 14+ parent::__construct( $gateway );
1515 }
1616
1717 public function generateFormStart() {
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnPremium.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class Gateway_Form_TwoStepTwoColumnPremium extends Gateway_Form_TwoStepTwoColumn {
5 - public function __construct( &$gateway, &$form_errors ) {
 5+ public function __construct( &$gateway ) {
66 global $wgScriptPath;
77
88 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -9,7 +9,7 @@
1010 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoStepTwoColumnPremium.css' );
1111 }
1212
13 - parent::__construct( $gateway, $form_errors );
 13+ parent::__construct( $gateway );
1414 }
1515
1616 public function generateFormStart() {
@@ -96,7 +96,7 @@
9797
9898 // amount
9999 $form .= '<tr>';
100 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 100+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
101101 $form .= '</tr>';
102102 $form .= '<tr>';
103103 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumn.php
@@ -2,8 +2,8 @@
33
44 class Gateway_Form_TwoStepTwoColumn extends Gateway_Form {
55
6 - public function __construct( &$gateway, &$form_errors ) {
7 - parent::__construct( $gateway, $form_errors );
 6+ public function __construct( &$gateway ) {
 7+ parent::__construct( $gateway );
88 }
99
1010 public function loadPlaceholders() {
@@ -166,7 +166,7 @@
167167
168168 // amount
169169 $form = '<tr>';
170 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 170+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
171171 $form .= '</tr>';
172172 $form .= '<tr>';
173173 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetter2.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class Gateway_Form_TwoStepTwoColumnLetter2 extends Gateway_Form_TwoStepTwoColumnLetter {
5 - public function __construct( &$gateway, &$form_errors ) {
 5+ public function __construct( &$gateway ) {
66 global $wgScriptPath;
77
88 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -9,6 +9,6 @@
1010 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoStepTwoColumnLetter2.css' );
1111 }
1212
13 - parent::__construct( $gateway, $form_errors );
 13+ parent::__construct( $gateway );
1414 }
1515 }
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnLetter3.php
@@ -1,14 +1,14 @@
22 <?php
33
44 class Gateway_Form_TwoStepTwoColumnLetter3 extends Gateway_Form_TwoStepTwoColumn {
5 - public function __construct( &$gateway, &$form_errors ) {
 5+ public function __construct( &$gateway ) {
66 global $wgExtensionAssetsPath;
77
88 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
99 if ( !strlen( $this->getStylePath() ) ) {
1010 $this->setStylePath( $wgExtensionAssetsPath . '/DonationInterface/gateway_forms/css/TwoStepTwoColumnLetter3.css' );
1111 }
12 - parent::__construct( $gateway, $form_errors );
 12+ parent::__construct( $gateway );
1313 }
1414
1515 public function loadPlaceholders() {
@@ -195,7 +195,7 @@
196196
197197 // amount
198198 $form .= '<tr>';
199 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 199+ $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['amount'] . '</span></td>';
200200 $form .= '</tr>';
201201 $form .= '<tr>';
202202 $form .= '<td colspan="2">';
Index: trunk/extensions/DonationInterface/gateway_forms/RapidHtml.php
@@ -90,9 +90,9 @@
9191 '#emailAdd',
9292 );
9393
94 - public function __construct( &$gateway, &$form_errors ) {
 94+ public function __construct( &$gateway ) {
9595 global $wgRequest;
96 - parent::__construct( $gateway, $form_errors );
 96+ parent::__construct( $gateway );
9797
9898 $this->loadValidateJs();
9999
Index: trunk/extensions/DonationInterface/gateway_forms/TwoStepTwoColumnPremiumUS.php
@@ -1,7 +1,7 @@
22 <?php
33
44 class Gateway_Form_TwoStepTwoColumnPremiumUS extends Gateway_Form_TwoStepTwoColumn {
5 - public function __construct( &$gateway, &$form_errors ) {
 5+ public function __construct( &$gateway ) {
66 global $wgScriptPath;
77
88 // set the path to css, before the parent constructor is called, checking to make sure some child class hasn't already set this
@@ -9,7 +9,7 @@
1010 $this->setStylePath( $wgScriptPath . '/extensions/DonationInterface/gateway_forms/css/TwoStepTwoColumnPremiumUS.css' );
1111 }
1212
13 - parent::__construct( $gateway, $form_errors );
 13+ parent::__construct( $gateway );
1414 }
1515
1616 public function loadPlaceholders() {
Index: trunk/extensions/DonationInterface/gateway_forms/OneStepTwoColumn.php
@@ -2,8 +2,8 @@
33
44 class Gateway_Form_OneStepTwoColumn extends Gateway_Form {
55
6 - public function __construct( &$gateway, &$form_errors ) {
7 - parent::__construct( $gateway, $form_errors );
 6+ public function __construct( &$gateway ) {
 7+ parent::__construct( $gateway );
88
99 // update the list of hidden fields we need to use in this form.
1010 $this->updateHiddenFields();
Index: trunk/extensions/DonationInterface/globalcollect_gateway/globalcollect_gateway.body.php
@@ -86,12 +86,11 @@
8787 $payment_submethod = $this->adapter->getPaymentSubmethod();
8888
8989 // Check form for errors
90 - $form_errors = $this->validateForm( $this->errors, $this->adapter->getPaymentSubmethodFormValidation() );
 90+ $form_errors = $this->validateForm( $this->adapter->getPaymentSubmethodFormValidation() );
9191
9292 // If there were errors, redisplay form, otherwise proceed to next step
9393 if ( $form_errors ) {
94 -
95 - $this->displayForm( $this->errors );
 94+ $this->displayForm();
9695 } else { // The submitted form data is valid, so process it
9796 // allow any external validators to have their way with the data
9897 // Execute the proper transaction code:
@@ -194,14 +193,16 @@
195194
196195 // If the result of the previous transaction was failure, set the retry message.
197196 if ( $data && array_key_exists( 'response', $data ) && $data['response'] == 'failure' ) {
198 - $this->errors['retryMsg'] = wfMsg( 'php-response-declined' );
 197+ $error['retryMsg'] = wfMsg( 'php-response-declined' );
 198+ $this->adapter->addManualError( $error );
199199 }
200200
201 - $this->displayForm( $this->errors );
 201+ $this->displayForm();
202202 }
203203 } else { //token mismatch
204 - $this->errors['general']['token-mismatch'] = wfMsg( 'donate_interface-token-mismatch' );
205 - $this->displayForm( $this->errors );
 204+ $error['general']['token-mismatch'] = wfMsg( 'donate_interface-token-mismatch' );
 205+ $this->adapter->addManualError( $error );
 206+ $this->displayForm();
206207 }
207208 }
208209
Index: trunk/extensions/DonationInterface/gateway_common/DataValidator.php
@@ -120,7 +120,33 @@
121121 return $error_token;
122122 }
123123
 124+ /**
 125+ * getEmptyErrorArray
 126+ * This only exists anymore, to make badly-coded forms happy when they start
 127+ * pulling keys all over the place without checking to see if they're set or
 128+ * not.
 129+ * @return array All the possible error tokens as keys, with blank errors.
 130+ */
 131+ public static function getEmptyErrorArray() {
 132+ return array(
 133+ 'general' => '',
 134+ 'retryMsg' => '',
 135+ 'amount' => '',
 136+ 'card_num' => '',
 137+ 'card_type' => '',
 138+ 'cvv' => '',
 139+ 'fname' => '',
 140+ 'lname' => '',
 141+ 'city' => '',
 142+ 'country' => '',
 143+ 'street' => '',
 144+ 'state' => '',
 145+ 'zip' => '',
 146+ 'emailAdd' => '',
 147+ );
 148+ }
124149
 150+
125151 /**
126152 * getErrorMessage - returns the translated error message appropriate for a
127153 * validation error on the specified field, of the specified type.
Index: trunk/extensions/DonationInterface/gateway_common/gateway.adapter.php
@@ -1895,7 +1895,8 @@
18961896 return get_called_class();
18971897 }
18981898
1899 - public function setValidationErrors( $errors ) {
 1899+ //only the gateway should be setting validation errors. Everybody else should set manual errors.
 1900+ protected function setValidationErrors( $errors ) {
19001901 $this->validation_errors = $errors;
19011902 }
19021903
@@ -1907,6 +1908,35 @@
19081909 }
19091910 }
19101911
 1912+ public function addManualError( $errors, $reset = false ) {
 1913+ if ( $reset ){
 1914+ $this->manual_errors = array();
 1915+ return;
 1916+ }
 1917+ $this->manual_errors = array_merge( $this->manual_errors, $errors );
 1918+ }
 1919+
 1920+ public function getManualErrors() {
 1921+ if ( !empty( $this->manual_errors ) ) {
 1922+ return $this->manual_errors;
 1923+ } else {
 1924+ return false;
 1925+ }
 1926+ }
 1927+
 1928+ public function getAllErrors(){
 1929+ $validation = $this->getValidationErrors();
 1930+ $manual = $this->getManualErrors();
 1931+ $return = array();
 1932+ if ( is_array( $validation ) ){
 1933+ $return = array_merge( $return, $validation );
 1934+ }
 1935+ if ( is_array( $manual ) ){
 1936+ $return = array_merge( $return, $manual );
 1937+ }
 1938+ return $return;
 1939+ }
 1940+
19111941 public function incrementNumAttempt() {
19121942 $this->dataObj->incrementNumAttempt();
19131943 $this->refreshGatewayValueFromSource( 'numAttempt' );
@@ -2104,5 +2134,23 @@
21052135 return $this->batch;
21062136 }
21072137 }
 2138+
 2139+ public function getOriginalValidationErrors( ){
 2140+ return $this->dataObj->getValidationErrors();
 2141+ }
 2142+
 2143+ //TODO: Maybe validate on $unstaged_data directly?
 2144+ public function revalidate( $check_not_empty = array() ){
 2145+ $validation_errors = $this->dataObj->getValidationErrors( true, $check_not_empty );
 2146+ $this->setValidationErrors( $validation_errors );
 2147+ return $this->validatedOK();
 2148+ }
21082149
 2150+ public function validatedOK(){
 2151+ if ( $this->getValidationErrors() === false ){
 2152+ return true;
 2153+ }
 2154+ return false;
 2155+ }
 2156+
21092157 }
\ No newline at end of file
Index: trunk/extensions/DonationInterface/gateway_common/DonationData.php
@@ -17,7 +17,7 @@
1818
1919 protected $normalized = array( );
2020 public $boss;
21 - protected $validationErrors = false;
 21+ protected $validationErrors = null;
2222
2323 /**
2424 * DonationData constructor
@@ -395,8 +395,8 @@
396396 $this->handleContributionTrackingID();
397397 $this->setCurrencyCode();
398398 $this->setFormClass();
399 - //TODO: Uncomment the next line when we want to start actually using the input validation.
400 -// $this->validateAllInput();
 399+
 400+ $this->getValidationErrors();
401401 }
402402 }
403403
@@ -1294,388 +1294,35 @@
12951295 }
12961296
12971297 /**
1298 - * validateAllInput
 1298+ * getValidationErrors
12991299 * This function will go through all the data we have pulled from wherever
13001300 * we've pulled it, and make sure it's safe and expected and everything.
 1301+ * If it is not, it will return an array of errors ready for any
 1302+ * DonationInterface form class derivitive to display.
13011303 */
1302 - protected function validateAllInput(){
1303 - $rules = $this->buildValidationRules();
1304 - foreach ( $this->normalized as $key => $val ){
1305 - if ( $this->isSomething( $key ) ){
1306 - //make sure there's a rule for it.
1307 - if ( !isset( $rules[$key] ) ){
1308 - $this->log( "Validate Error: There is no rule for $key!" );
1309 - //TODO: i18n
1310 - $this->validate_setError( 'general', "Validate Error: There is no rule for $key!" );
1311 - } else {
1312 - $function = $rules[$key]['validate_function'];
1313 - if (method_exists( $this, $function )){
1314 - $this->{$function}( $key, $rules[$key]['error_form_token'] );
1315 - } else {
1316 - $this->log( "Validate Error: There is no $function function!" );
1317 - //TODO: i18n
1318 - $this->validate_setError( 'general', "Validate Error: There is no $function function!" );
1319 - }
1320 - }
1321 -
1322 - }
 1304+ public function getValidationErrors( $recalculate = false, $check_not_empty = array() ){
 1305+ if ( is_null( $this->validationErrors ) || $recalculate ) {
 1306+ $this->validationErrors = DataValidator::validate( $this->normalized, $check_not_empty );
13231307 }
 1308+ return $this->validationErrors;
13241309 }
13251310
13261311 /**
1327 - * Builds the validation rule set for all the keys we're pulling.
1328 - * TODO: Do some mapping here with i18n messages, too. We can't take this
1329 - * into prod just throwing the generic at everything.
1330 - * TODO: In general with this whole thing, the alphanumeric strategy should
1331 - * be... revised. Clearly we can't have a list of 'good' characters.
1332 - * Instead, we need to clean stuff that is clearly dangerous.
1333 - * Also, maybe we just auto-assign the alphanumeric clean function to
1334 - * anything that doesn't have other assignments, instead of dying nastily
1335 - * when we have one with no assignment. That's probably more pleasant.
1336 - * @return array An array of keys, the ways we want to validate them, and
1337 - * what messages to set if they don't pass.
1338 - * $array[$key] = array(
1339 - * 'validate_function' => $function_name,
1340 - * 'error_form_token' => $error_token,
1341 - *
1342 - * )
1343 - */
1344 - protected function buildValidationRules(){
1345 - $rules = array();
1346 -
1347 - //you should group these jerks by type or something.
1348 -
1349 - //initial build based on general functions to run for validation.
1350 - $numeric = array(
1351 - 'amountGiven',
1352 - 'amountOther',
1353 - 'card_num',
1354 - 'cvv',
1355 - 'contribution_tracking_id',
1356 - 'account_number',
1357 - 'expiration',
1358 - 'order_id',
1359 - 'i_order_id',
1360 - 'numAttempt'
1361 - );
1362 -
1363 - foreach ($numeric as $key){
1364 - $rules[$key]['validate_function'] = 'validate_numeric';
1365 - }
1366 -
1367 - $alphanumeric = array(
1368 - 'fname',
1369 - 'mname',
1370 - 'lname',
1371 - 'street',
1372 - 'city',
1373 - 'state',
1374 - 'zip',
1375 - 'country',
1376 - 'fname2',
1377 - 'lname2',
1378 - 'street2',
1379 - 'city2',
1380 - 'state2',
1381 - 'zip2',
1382 - 'country2',
1383 - 'size',
1384 - 'premium_language',
1385 - 'card_type',
1386 - 'currency',
1387 - 'currency_code',
1388 - 'payment_method',
1389 - 'payment_submethod',
1390 - 'form_name',
1391 - 'ffname',
1392 -
1393 - //for lack of a better idea what to do with these things, I'm just gonna leave these here.
1394 - //If any of them need to be numeric or boolean or dealt with specially, do that.
1395 - 'issuer_id',
1396 - 'referrer',
1397 - 'utm_source',
1398 - 'utm_source_id',
1399 - 'utm_medium',
1400 - 'utm_campaign',
1401 - 'language',
1402 - 'uselang',
1403 - 'comment',
1404 - 'token',
1405 - 'data_hash',
1406 - 'action',
1407 - 'gateway',
1408 - 'owa_session',
1409 - 'owa_ref',
1410 - 'descriptor',
1411 - 'account_name',
1412 - 'authorization_id',
1413 - 'bank_check_digit',
1414 - 'bank_name',
1415 - 'bank_code',
1416 - 'branch_code',
1417 - 'country_code_bank',
1418 - 'date_collect',
1419 - 'direct_debit_text',
1420 - 'iban',
1421 - 'transaction_type',
1422 - );
1423 -
1424 - foreach ($alphanumeric as $key){
1425 - $rules[$key]['validate_function'] = 'validate_alphanumeric_with_cleaning';
1426 - }
1427 -
1428 - $boolean = array(
1429 - 'comment-option',
1430 - 'email-opt',
1431 - '_cache_',
1432 - 'anonymous',
1433 - 'optout',
1434 - 'recurring',
1435 - 'posted',
1436 - );
1437 -
1438 - foreach ($boolean as $key){
1439 - $rules[$key]['validate_function'] = 'validate_boolean';
1440 - }
1441 -
1442 - $rules['email']['validate_function'] = 'validate_email';
1443 - $rules['amount']['validate_amount'] = 'validate_amount';
1444 -
1445 -
1446 - //now, set the error token to use...
1447 -
1448 - foreach ( $rules as $key => $value ){
1449 - $error_token = 'general';
1450 - switch ( $key ) {
1451 - case 'amountGiven' :
1452 - case 'amountOther' :
1453 - $error_token = 'amount';
1454 - break;
1455 - case 'email' :
1456 - $error_token = 'emailAdd';
1457 - break;
1458 - case 'amount' :
1459 - case 'card_num':
1460 - case 'card_type':
1461 - case 'cvv':
1462 - case 'fname':
1463 - case 'lname':
1464 - case 'city':
1465 - case 'country':
1466 - case 'street':
1467 - case 'state':
1468 - case 'zip':
1469 - $error_token = $key;
1470 - break;
1471 - }
1472 - $rules[$key]['error_form_token'] = $error_token;
1473 - }
1474 -
1475 - return $rules;
1476 - }
1477 -
1478 - /**
1479 - * Sets an error found during validation.
1480 - * @param string $error_token The error token to set for this error.
1481 - * (See RapidHTML::$error_tokens for possibilities)
1482 - * @param string $msg The translated message to display at that error token.
1483 - */
1484 - protected function validate_setError( $error_token, $msg ){
1485 - if ( !$this->validationErrors ){
1486 - $this->validationErrors = array();
1487 - }
1488 - $this->validationErrors[$error_token] = $msg;
1489 -
1490 - }
1491 -
1492 - /**
14931312 * validatedOK
14941313 * Checks to see if the data validated ok (no errors).
14951314 * @return boolean True if no errors, false if errors exist.
14961315 */
14971316 public function validatedOK() {
1498 - if ( is_array( $this->validationErrors ) ){
1499 - return false;
 1317+ if ( is_null( $this->validationErrors ) ){
 1318+ $this->getValidationErrors();
15001319 }
1501 - return true;
1502 - }
1503 -
1504 - /**
1505 - * getValidationErrors
1506 - * Returns the errors set on data validation.
1507 - * @return mixed Array if errors are set, else false.
1508 - */
1509 - public function getValidationErrors() {
1510 - return $this->validationErrors;
1511 - }
1512 -
1513 - /**
1514 - * validate_email
1515 - * validateAllInput helper function
1516 - * To validate any input value using this function, add a line to
1517 - * $this->buildValidationRules() specifying the function name as the field
1518 - * name's 'validate_function'.
1519 - * @param string $key The name of the field to validate.
1520 - * @param string $error_token As in RapidHTML, the pre-defined area of the
1521 - * form in which to display the error.
1522 - */
1523 - protected function validate_email( $key, $error_token ){
1524 - // is email address valid?
1525 - $isEmail = User::isValidEmailAddr( $this->getVal($key) );
1526 -
1527 - // create error message (supercedes empty field message)
1528 - if ( !$isEmail ) {
1529 - $this->log( __FUNCTION__ . " $key is not an email address.", LOG_DEBUG );
1530 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-email' ) );
1531 - }
1532 - }
1533 -
1534 - /**
1535 - * validate_amount
1536 - * validateAllInput helper function
1537 - * To validate any input value using this function, add a line to
1538 - * $this->buildValidationRules() specifying the function name as the field
1539 - * name's 'validate_function'.
1540 - * @param string $key The name of the field to validate.
1541 - * @param string $error_token As in RapidHTML, the pre-defined area of the
1542 - * form in which to display the error.
1543 - */
1544 - protected function validate_amount( $key, $error_token ){
1545 - if ( !$this->isSomething( $key ) ) {
1546 - $this->log( __FUNCTION__ . " $key is not something.", LOG_DEBUG );
1547 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-invalid-amount' ) );
1548 - return;
1549 - }
15501320
1551 - if ( !$this->isSomething( 'currency_code' ) ) {
1552 - $this->log( __FUNCTION__ . " currency_code is not something.", LOG_DEBUG );
1553 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-general' ) );
1554 - return;
 1321+ if ( count( $this->validationErrors ) === 0 ){
 1322+ return true;
15551323 }
1556 - $currency_code = $this->getVal( 'currency_code' );
1557 -
1558 - $val = $this->getVal( $key );
1559 - if ( !is_numeric( $val ) ) {
1560 - $this->log( __FUNCTION__ . " $key is not valid numeric format. $val", LOG_DEBUG );
1561 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-invalid-amount' ) );
1562 - }
1563 -
1564 - // check amount
1565 - $priceFloor = $this->adapter->getGlobal( 'PriceFloor' );
1566 - $priceCeiling = $this->adapter->getGlobal( 'PriceCeiling' );
1567 - if ( !preg_match( '/^\d+(\.(\d+)?)?$/', $val ) ||
1568 - ( ( float ) $this->convert_to_usd( $currency_code, $val ) < ( float ) $priceFloor ||
1569 - ( float ) $this->convert_to_usd( $currency_code, $val ) > ( float ) $priceCeiling ) ) {
1570 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-invalid-amount' ) );
1571 - }
 1324+ return false;
15721325 }
15731326
1574 - /**
1575 - * validate_boolean
1576 - * validateAllInput helper function
1577 - * To validate any input value using this function, add a line to
1578 - * $this->buildValidationRules() specifying the function name as the field
1579 - * name's 'validate_function'.
1580 - * @param string $key The name of the field to validate.
1581 - * @param string $error_token As in RapidHTML, the pre-defined area of the
1582 - * form in which to display the error.
1583 - */
1584 - protected function validate_boolean( $key, $error_token ){
1585 - $val = $this->getVal($key);
1586 - switch ($val) {
1587 - case 0:
1588 - case '0':
1589 - case false:
1590 - case 'false':
1591 - $this->setVal( $key, 0 );
1592 - break;
1593 - case 1:
1594 - case '1':
1595 - case true:
1596 - case 'true':
1597 - $this->setVal( $key, 1 );
1598 - break;
1599 - default:
1600 - $this->log( __FUNCTION__ . " $key is not boolean." );
1601 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-general' ), LOG_DEBUG );
1602 - break;
1603 - }
1604 -
1605 - }
1606 -
1607 - /**
1608 - * validate_alphanumeric_with_cleaning
1609 - * validateAllInput helper function
1610 - * To validate and clean any input value using this function, add a line to
1611 - * $this->buildValidationRules() specifying the function name as the field
1612 - * name's 'validate_function'.
1613 - * TODO: Something even remotely useful here. Like the cleaning promised in
1614 - * the function name.
1615 - * @param string $key The name of the field to validate.
1616 - * @param string $error_token As in RapidHTML, the pre-defined area of the
1617 - * form in which to display the error.
1618 - */
1619 - protected function validate_alphanumeric_with_cleaning( $key, $error_token ){
1620 - $val = $this->getVal($key);
1621 -
1622 - //instead of validating here, we should probably be proactively removing badness.
1623 - if ( preg_match( '/%/', $val ) ) { //this is dumb and bad. Fixit.
1624 - $this->log( __FUNCTION__ . " $key is not valid alphanumeric. $val", LOG_DEBUG );
1625 - //oooh. This blows.
1626 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-general' ) );
1627 - }
1628 - }
1629 -
1630 - /**
1631 - * validate_numeric
1632 - * validateAllInput helper function
1633 - * To validate any input value using this function, add a line to
1634 - * $this->buildValidationRules() specifying the function name as the field
1635 - * name's 'validate_function'.
1636 - * @param string $key The name of the field to validate.
1637 - * @param string $error_token As in RapidHTML, the pre-defined area of the
1638 - * form in which to display the error.
1639 - */
1640 - protected function validate_numeric( $key, $error_token ){
1641 - $val = $this->getVal($key);
1642 -
1643 - //instead of validating here, we should probably be doing something else entirely.
1644 - if ( !is_numeric( $val ) ) {
1645 - $this->log( __FUNCTION__ . " $key is not valid numeric format. $val", LOG_DEBUG );
1646 - //oooh. This blows.
1647 - $this->validate_setError( $error_token, wfMsg( 'donate_interface-error-msg-general' ) );
1648 - }
1649 - }
1650 -
1651 - /**
1652 - * Convert an amount for a particular currency to an amount in USD
1653 - *
1654 - * This is grosley rudimentary and likely wildly inaccurate.
1655 - * This mimicks the hard-coded values used by the WMF to convert currencies
1656 - * for validatoin on the front-end on the first step landing pages of their
1657 - * donation process - the idea being that we can get a close approximation
1658 - * of converted currencies to ensure that contributors are not going above
1659 - * or below the price ceiling/floor, even if they are using a non-US currency.
1660 - *
1661 - * In reality, this probably ought to use some sort of webservice to get real-time
1662 - * conversion rates.
1663 - *
1664 - * @param string $currency_code
1665 - * @param float $amount
1666 - * @return float
1667 - */
1668 - static function convert_to_usd( $currency_code, $amount ) {
1669 - require_once( dirname( __FILE__ ) . '/currencyRates.inc' );
1670 - $rates = getCurrencyRates();
1671 - $code = strtoupper( $currency_code );
1672 - if ( array_key_exists( $code, $rates ) ) {
1673 - $usd_amount = $amount / $rates[$code];
1674 - } else {
1675 - $usd_amount = $amount;
1676 - }
1677 - return $usd_amount;
1678 - }
1679 -
16801327 }
16811328
16821329 ?>
Index: trunk/extensions/DonationInterface/gateway_common/GatewayForm.php
@@ -48,265 +48,85 @@
4949 public function __construct() {
5050 $me = get_called_class();
5151 parent::__construct( $me );
52 - $this->errors = $this->getPossibleErrors();
5352 }
5453
5554 /**
56 - * Checks posted form data for errors and returns array of messages
57 - *
58 - * @param array $data Reference to the data of the form
59 - * @param array $error Reference to the error messages of the form
 55+ * Checks current dataset for validation errors
 56+ * TODO: As with every other bit of gateway-related logic that should
 57+ * definitely be available to every entry point, and functionally has very
 58+ * little to do with being contained within what in an ideal world would be
 59+ * a piece of mostly UI, this function needs to be moved inside the gateway
 60+ * adapter class.
6061 * @param array $options
61 - * OPTIONAL - You may require certain field groups to be validated
62 - * - address - Validates: street, city, state, zip
63 - * - amount - Validates: amount
64 - * - creditCard - Validates: card_num, cvv, expiration and sets the card
65 - * - email - Validates: email
66 - * - name - Validates: fname, lname
 62+ * OPTIONAL - In addition to all non-optional validation which verifies
 63+ * that all populated fields contain an appropriate data type, you may
 64+ * require certain field groups to be non-empty.
 65+ * - address - Validation requires non-empty: street, city, state, zip
 66+ * - amount - Validation requires non-empty: amount
 67+ * - creditCard - Validation requires non-empty: card_num, cvv, expiration and card_type
 68+ * - email - Validation requires non-empty: email
 69+ * - name - Validation requires non-empty: fname, lname
6770 *
68 - * @return 0|1 Returns 0 on success and 1 on failure
 71+ * @return boolean Returns true on an error-free validation, otherwise false.
6972 */
70 - public function validateForm( &$error, $options = array() ) {
 73+ public function validateForm( $options = array() ) {
7174
72 - $data = $this->adapter->getData_Unstaged_Escaped();
 75+ $check_not_empty = array();
7376
74 - extract( $options );
75 -
76 - // Set which items will be validated
77 - $address = isset( $address ) ? ( boolean ) $address : true;
78 - $amount = isset( $amount ) ? ( boolean ) $amount : true;
79 - $creditCard = isset( $creditCard ) ? ( boolean ) $creditCard : false;
80 - $email = isset( $email ) ? ( boolean ) $email : true;
81 - $name = isset( $name ) ? ( boolean ) $name : true;
82 -
83 - // These are set in the order they will most likely appear on the form.
84 -
85 - if ( $name ) {
86 - $this->validateName( $data, $error );
 77+ foreach ( $options as $option ){
 78+ $add_checks = array();
 79+ switch( $option ){
 80+ case 'address' :
 81+ $add_checks = array(
 82+ 'street',
 83+ 'city',
 84+ 'state',
 85+ 'country',
 86+ 'zip', //this should really be added or removed, depending on the country and/or gateway requirements.
 87+ //however, that's not happening in this class in the code I'm replacing, so...
 88+ //TODO: Something clever in the DataValidator with data groups like these.
 89+ );
 90+ break;
 91+ case 'amount' :
 92+ $add_checks[] = 'amount';
 93+ break;
 94+ case 'creditCard' :
 95+ $add_checks = array(
 96+ 'card_num',
 97+ 'cvv',
 98+ 'expiration',
 99+ 'card_type'
 100+ );
 101+ break;
 102+ case 'email' :
 103+ $add_checks[] = 'email';
 104+ break;
 105+ case 'name' :
 106+ $add_checks = array(
 107+ 'fname',
 108+ 'lname'
 109+ );
 110+ break;
 111+ }
 112+ $check_not_empty = array_merge( $check_not_empty, $add_checks );
87113 }
88 -
89 - if ( $address ) {
90 - $this->validateAddress( $data, $error );
91 - }
92 -
93 - if ( $amount ) {
94 - $this->validateAmount( $data, $error );
95 - }
96 -
97 - if ( $email ) {
98 - $this->validateEmail( $data, $error );
99 - }
100 -
101 - if ( $creditCard ) {
102 - $this->validateCreditCard( $data, $error );
103 - }
104 -
105 - /*
106 - * $error_result would return 0 on success, 1 on failure.
107 - *
108 - * This is done for backward compatibility.
109 - */
110 - return $this->getValidateFormResult() ? 0 : 1;
111 - }
112 -
113 - /**
114 - * Validates the address
115 - *
116 - * Required:
117 - * - street
118 - * - city
119 - * - state
120 - * - zip
121 - * - country
122 - *
123 - * @param array $data Reference to the data of the form
124 - * @param array $error Reference to the error messages of the form
125 - *
126 - * @see GatewayForm::validateForm()
127 - */
128 - public function validateAddress( &$data, &$error ) {
129 -
130 - if ( empty( $data['street'] ) ) {
131 -
132 - $error['street'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-street' ) );
133 -
134 - $this->setValidateFormResult( false );
135 - }
136 -
137 - if ( empty( $data['city'] ) ) {
138 -
139 - $error['city'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-city' ) );
140 -
141 - $this->setValidateFormResult( false );
142 - }
143 -
144 - if ( empty( $data['state'] ) || $data['state'] == 'YY' ) {
145 -
146 - $error['state'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-state-province' ) );
147 -
148 - $this->setValidateFormResult( false );
149 - }
150 -
151 - if ( empty( $data['country'] ) || !array_key_exists( $data['country'], $this->getCountries() )) {
152 -
153 - $error['country'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-country' ) );
154 -
155 - $this->setValidateFormResult( false );
156 - }
157 -
158 - $ignoreCountries = array();
159114
160 - if ( empty( $data['zip'] ) && !in_array( $data['country'], $ignoreCountries ) ) {
 115+ $validate_errors = $this->adapter->revalidate( $check_not_empty );
161116
162 - $error['zip'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-zip' ) );
163 -
164 - $this->setValidateFormResult( false );
165 - }
 117+ return $validate_errors;
166118 }
167119
168120 /**
169 - * Validates the amount contributed
170 - *
171 - * @param array $data Reference to the data of the form
172 - * @param array $error Reference to the error messages of the form
173 - *
174 - * @see GatewayForm::validateForm()
175 - */
176 - public function validateAmount( &$data, &$error ) {
177 -
178 - if ( empty( $data['amount'] ) ) {
179 -
180 - $error['amount'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-amount' ) );
181 -
182 - $this->setValidateFormResult( false );
183 - }
184 -
185 - // check amount
186 - $priceFloor = $this->adapter->getGlobal( 'PriceFloor' );
187 - $priceCeiling = $this->adapter->getGlobal( 'PriceCeiling' );
188 - if ( !preg_match( '/^\d+(\.(\d+)?)?$/', $data['amount'] ) ||
189 - ( ( float ) $this->convert_to_usd( $data['currency_code'], $data['amount'] ) < ( float ) $priceFloor ||
190 - ( float ) $this->convert_to_usd( $data['currency_code'], $data['amount'] ) > ( float ) $priceCeiling ) ) {
191 -
192 - $error['invalidamount'] = wfMsg( 'donate_interface-error-msg-invalid-amount' );
193 -
194 - $this->setValidateFormResult( false );
195 - }
196 - }
197 -
198 - /**
199 - * Validates a credit card
200 - *
201 - * @param array $data Reference to the data of the form
202 - * @param array $error Reference to the error messages of the form
203 - *
204 - * @see GatewayForm::validateForm()
205 - */
206 - public function validateCreditCard( &$data, &$error ) {
207 -
208 - if ( empty( $data['card_num'] ) ) {
209 -
210 - $error['card_num'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-card_num' ) );
211 -
212 - $this->setValidateFormResult( false );
213 - }
214 -
215 - if ( empty( $data['cvv'] ) ) {
216 -
217 - $error['cvv'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-cvv' ) );
218 -
219 - $this->setValidateFormResult( false );
220 - }
221 -
222 - if ( empty( $data['expiration'] ) ) {
223 -
224 - $error['expiration'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-expiration' ) );
225 -
226 - $this->setValidateFormResult( false );
227 - }
228 -
229 - // validate that credit card number entered is correct and set the card type
230 - if ( preg_match( '/^3[47][0-9]{13}$/', $data['card_num'] ) ) { // american express
231 - $data['card'] = 'american';
232 - } elseif ( preg_match( '/^5[1-5][0-9]{14}$/', $data['card_num'] ) ) { // mastercard
233 - $data['card'] = 'mastercard';
234 - } elseif ( preg_match( '/^4[0-9]{12}(?:[0-9]{3})?$/', $data['card_num'] ) ) {// visa
235 - $data['card'] = 'visa';
236 - } elseif ( preg_match( '/^6(?:011|5[0-9]{2})[0-9]{12}$/', $data['card_num'] ) ) { // discover
237 - $data['card'] = 'discover';
238 - } else { // an invalid credit card number was entered
239 - $error['card_num'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-card-num' ) );
240 -
241 - $this->setValidateFormResult( false );
242 - }
243 - }
244 -
245 - /**
246 - * Validates an email address.
247 - *
248 - * @param array $data Reference to the data of the form
249 - * @param array $error Reference to the error messages of the form
250 - *
251 - * @see GatewayForm::validateForm()
252 - */
253 - public function validateEmail( &$data, &$error ) {
254 -
255 - if ( empty( $data['email'] ) ) {
256 -
257 - $error['email'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-email-empty' ) );
258 -
259 - $this->setValidateFormResult( false );
260 - }
261 -
262 - // is email address valid?
263 - $isEmail = User::isValidEmailAddr( $data['email'] );
264 -
265 - // create error message (supercedes empty field message)
266 - if ( !$isEmail ) {
267 - $error['email'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-email' ) );
268 -
269 - $this->setValidateFormResult( false );
270 - }
271 - }
272 -
273 - /**
274 - * Validates the name
275 - *
276 - * @param array $data Reference to the data of the form
277 - * @param array $error Reference to the error messages of the form
278 - *
279 - * @see GatewayForm::validateForm()
280 - */
281 - public function validateName( &$data, &$error ) {
282 -
283 - if ( empty( $data['fname'] ) ) {
284 -
285 - $error['fname'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-fname' ) );
286 -
287 - $this->setValidateFormResult( false );
288 - }
289 -
290 - if ( empty( $data['lname'] ) ) {
291 -
292 - $error['lname'] = wfMsg( 'donate_interface-error-msg', wfMsg( 'donate_interface-error-msg-lname' ) );
293 -
294 - $this->setValidateFormResult( false );
295 - }
296 - }
297 -
298 - /**
299121 * Build and display form to user
300122 *
301 - * @param $error Array: array of error messages returned by validate_form function
302 - *
303123 * The message at the top of the form can be edited in the payflow_gateway.i18n.php file
304124 */
305 - public function displayForm( &$error ) {
 125+ public function displayForm() {
306126 global $wgOut;
307127
308128 $form_class = $this->getFormClass();
309129 if ( $form_class && class_exists( $form_class ) ){
310 - $form_obj = new $form_class( $this->adapter, $error );
 130+ $form_obj = new $form_class( $this->adapter );
311131 $form = $form_obj->getForm();
312132 $wgOut->addHTML( $form );
313133 } else {
@@ -385,25 +205,6 @@
386206 }
387207 }
388208
389 - public function getPossibleErrors() {
390 - return array(
391 - 'general' => '',
392 - 'retryMsg' => '',
393 - 'invalidamount' => '',
394 - 'card_num' => '',
395 - 'card_type' => '',
396 - 'cvv' => '',
397 - 'fname' => '',
398 - 'lname' => '',
399 - 'city' => '',
400 - 'country' => '',
401 - 'street' => '',
402 - 'state' => '',
403 - 'zip' => '',
404 - 'emailAdd' => '',
405 - );
406 - }
407 -
408209 /**
409210 * Convert an amount for a particular currency to an amount in USD
410211 *
@@ -451,7 +252,8 @@
452253 // if we don't have a URL enabled throw a graceful error to the user
453254 if ( !strlen( $this->adapter->getGlobal( 'PaypalURL' ) ) ) {
454255 $gateway_identifier = $this->adapter->getIdentifier();
455 - $this->errors['general']['nopaypal'] = wfMsg( $gateway_identifier . '_gateway-error-msg-nopaypal' );
 256+ $error['general']['nopaypal'] = wfMsg( $gateway_identifier . '_gateway-error-msg-nopaypal' );
 257+ $this->adapter->addManualError( $error );
456258 return;
457259 }
458260 // submit the data to the paypal redirect URL
@@ -534,21 +336,19 @@
535337 // Display debugging results
536338 $this->displayResultsForDebug();
537339
538 - $this->errors['general'] = ( !isset( $this->errors['general'] ) || empty( $this->errors['general'] ) ) ? array() : (array) $this->errors['general'];
539 -
540 - $this->errors['retryMsg'] = ( !isset( $this->errors['retryMsg'] ) || empty( $this->errors['retryMsg'] ) ) ? array() : (array) $this->errors['retryMsg'];
541 -
542340 foreach ( $this->adapter->getTransactionErrors() as $code => $message ) {
543341
 342+ $error = array();
544343 if ( strpos( $code, 'internal' ) === 0 ) {
545 - $this->errors['retryMsg'][ $code ] = $message;
 344+ $error['retryMsg'][ $code ] = $message;
546345 }
547346 else {
548 - $this->errors['general'][ $code ] = $message;
 347+ $error['general'][ $code ] = $message;
549348 }
 349+ $this->adapter->addManualError( $error );
550350 }
551351
552 - return $this->displayForm( $this->errors );
 352+ return $this->displayForm();
553353 }
554354
555355 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r107690followup r107609...khorn01:56, 31 December 2011
r107981followup r107609...khorn01:16, 4 January 2012
r112245New DataValidator class, and donationinterface.php Only: MFT r107299, r10755...khorn21:42, 23 February 2012
r112287MFT r101785, r105938, r105941, r105953, r106109, r106158, r106259, r106366, r...khorn01:29, 24 February 2012

Status & tagging log