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

Change subject: Encapsulate AstroPay staging methods
......................................................................


Encapsulate AstroPay staging methods

Bug: T130075
Change-Id: I9f3c16b63aa79e2624e8119026ea43eb2bbdd748
---
M DonationInterface.php
A astropay_gateway/AstroPayFinancialNumbers.php
A astropay_gateway/AstroPayMethodCodec.php
M astropay_gateway/astropay.adapter.php
A gateway_common/DonorEmail.php
A gateway_common/DonorFullName.php
M gateway_common/gateway.adapter.php
7 files changed, 115 insertions(+), 97 deletions(-)

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



diff --git a/DonationInterface.php b/DonationInterface.php
index 40af499..e4d8e71 100644
--- a/DonationInterface.php
+++ b/DonationInterface.php
@@ -46,6 +46,8 @@
 $wgAutoloadClasses['DonationLogProcessor'] = __DIR__ . 
'/gateway_common/DonationLogProcessor.php';
 $wgAutoloadClasses['DonationProfiler'] = __DIR__ . 
'/gateway_common/DonationProfiler.php';
 $wgAutoloadClasses['DonationQueue'] = __DIR__ . 
'/gateway_common/DonationQueue.php';
+$wgAutoloadClasses['DonorEmail'] = __DIR__ . '/gateway_common/DonorEmail.php';
+$wgAutoloadClasses['DonorFullName'] = __DIR__ . 
'/gateway_common/DonorFullName.php';
 $wgAutoloadClasses['EncodingMangler'] = __DIR__ . 
'/gateway_common/EncodingMangler.php';
 $wgAutoloadClasses['FinalStatus'] = __DIR__ . 
'/gateway_common/FinalStatus.php';
 $wgAutoloadClasses['GatewayAdapter'] = __DIR__ . 
'/gateway_common/gateway.adapter.php';
@@ -96,6 +98,8 @@
 $wgAutoloadClasses['AstropayGateway'] = __DIR__ . 
'/astropay_gateway/astropay_gateway.body.php';
 $wgAutoloadClasses['AstropayGatewayResult'] = __DIR__ . 
'/astropay_gateway/astropay_resultswitcher.body.php';
 $wgAutoloadClasses['AstropayAdapter'] = __DIR__ . 
'/astropay_gateway/astropay.adapter.php';
+$wgAutoloadClasses['AstroPayFinancialNumbers'] = __DIR__ . 
'/astropay_gateway/AstroPayFinancialNumbers.php';
+$wgAutoloadClasses['AstroPayMethodCodec'] = __DIR__ . 
'/astropay_gateway/AstroPayMethodCodec.php';
 
 // Paypal
 $wgAutoloadClasses['PaypalGateway'] = __DIR__ . 
'/paypal_gateway/paypal_gateway.body.php';
diff --git a/astropay_gateway/AstroPayFinancialNumbers.php 
b/astropay_gateway/AstroPayFinancialNumbers.php
new file mode 100644
index 0000000..9c86d59
--- /dev/null
+++ b/astropay_gateway/AstroPayFinancialNumbers.php
@@ -0,0 +1,42 @@
+<?php
+
+class AstroPayFinancialNumbers implements StagingHelper {
+       public function stage( GatewayType $adapter, $unstagedData, 
&$stagedData ) {
+               $this->stage_donor_id( $adapter, $unstagedData, $stagedData );
+               $this->stage_bank_code( $adapter, $unstagedData, $stagedData );
+               $this->stage_fiscal_number( $adapter, $unstagedData, 
$stagedData );
+       }
+
+       /**
+        * They need a 20 char string for a customer ID - give them the first 20
+        * characters of the email address for easy lookup
+        */
+       protected function stage_donor_id( GatewayType $adapter, $unstagedData, 
&$stagedData ) {
+               // We use these to look up donations by email, so strip out the 
trailing
+               // spam-tracking sub-address to get the email we'd see 
complaints from.
+               $email = preg_replace( '/\+[^@]*/', '', $stagedData['email'] );
+               $stagedData['donor_id'] = substr( $email, 0, 20 );
+       }
+
+       protected function stage_bank_code( GatewayType $adapter, 
$unstagedData, &$stagedData ) {
+               $submethod = $adapter->getPaymentSubmethod();
+               if ( $submethod ) {
+                       $meta = $adapter->getPaymentSubmethodMeta( $submethod );
+                       if ( isset( $meta['bank_code'] ) ) {
+                               $stagedData['bank_code'] = $meta['bank_code'];
+                       }
+               }
+       }
+
+       /**
+        * Strip any punctuation from fiscal number before submitting
+        */
+       protected function stage_fiscal_number( GatewayType $adapter, 
$unstagedData, &$stagedData ) {
+               if ( !empty( $unstagedData['fiscal_number'] ) ) {
+                       $stagedData['fiscal_number'] = preg_replace( 
'/[^a-zA-Z0-9]/', '', $unstagedData['fiscal_number'] );
+               }
+       }
+
+       // No-op
+       public function unstage( GatewayType $adapter, $stagedData, 
&$unstagedData ) {}
+}
diff --git a/astropay_gateway/AstroPayMethodCodec.php 
b/astropay_gateway/AstroPayMethodCodec.php
new file mode 100644
index 0000000..9b70cfd
--- /dev/null
+++ b/astropay_gateway/AstroPayMethodCodec.php
@@ -0,0 +1,28 @@
+<?php
+
+class AstroPayMethodCodec implements StagingHelper {
+       // No-op
+       public function stage( GatewayType $adapter, $unstagedData, 
&$stagedData ) {}
+
+       /**
+        * Transforms the astropay payment method into our method name
+        */
+       public function unstage( GatewayType $adapter, $stagedData, 
&$unstagedData ) {
+               $method = $stagedData['payment_method'];
+               $bank = $stagedData['bank_code'];
+               if ( !$method || !$bank ) {
+                       return;
+               }
+               $filter = function( $submethod ) use ( $method, $bank ) {
+                       $groups = (array) $submethod['group'];
+                       return in_array( $groups, $method ) && 
$submethod['bank_code'] === $bank;
+               };
+               $candidates = array_filter( $adapter->getPaymentSubmethods(), 
$filter );
+
+               if ( count( $candidates ) !== 1 ) {
+                       throw new UnexpectedValueException( "No unique payment 
submethod defined for payment method $method and bank code $bank." );
+               }
+               $keys = array_keys( $candidates );
+               $unstagedData['payment_submethod'] = $keys[0];
+       }
+}
diff --git a/astropay_gateway/astropay.adapter.php 
b/astropay_gateway/astropay.adapter.php
index 34166dc..455a4d9 100644
--- a/astropay_gateway/astropay.adapter.php
+++ b/astropay_gateway/astropay.adapter.php
@@ -20,6 +20,7 @@
 /**
  * AstropayAdapter
  * Implementation of GatewayAdapter for processing payments via Astropay
+ * FIXME: camlcase "P"
  */
 class AstropayAdapter extends GatewayAdapter {
        const GATEWAY_NAME = 'Astropay';
@@ -72,14 +73,7 @@
                );
        }
 
-       function defineStagedVars() {
-               $this->staged_vars = array(
-                       'bank_code',
-                       'donor_id',
-                       'fiscal_number',
-                       'full_name',
-               );
-       }
+       function defineStagedVars() {}
 
        /**
         * Define var_map
@@ -667,6 +661,9 @@
        function defineStagingHelpers() {
                // Skip AmountInCents.
                $this->staging_helpers = array(
+                       new AstroPayFinancialNumbers(),
+                       new AstroPayMethodCodec(),
+                       new DonorFullName(),
                        new StreetAddress(),
                );
        }
@@ -738,52 +735,6 @@
                        . /* street omitted */ 'P'
                        . /* city omitted */ 'S'
                        . /* state omitted */ 'P' );
-       }
-
-       /**
-        * They need a 20 char string for a customer ID - give them the first 20
-        * characters of the email address for easy lookup
-        */
-       protected function stage_donor_id() {
-               // We use these to look up donations by email, so strip out the 
trailing
-               // spam-tracking sub-address to get the email we'd see 
complaints from.
-               $email = preg_replace( '/\+[^@]*/', '', $this->getData_Staged( 
'email' ) );
-               $this->staged_data['donor_id'] = substr( $email, 0, 20 );
-       }
-
-       protected function stage_bank_code() {
-               $submethod = $this->getPaymentSubmethod();
-               if ( $submethod ) {
-                       $meta = $this->getPaymentSubmethodMeta( $submethod );
-                       if ( isset( $meta['bank_code'] ) ) {
-                               $this->staged_data['bank_code'] = 
$meta['bank_code'];
-                       }
-               }
-       }
-
-       /**
-        * Strip any punctuation from fiscal number before submitting
-        */
-       protected function stage_fiscal_number() {
-               $value = $this->getData_Unstaged_Escaped( 'fiscal_number' );
-               if ( $value ) {
-                       $this->staged_data['fiscal_number'] = preg_replace( 
'/[^a-zA-Z0-9]/', '', $value );
-               }
-       }
-
-       protected function unstage_payment_submethod() {
-               $method = $this->getData_Staged( 'payment_method' );
-               $bank = $this->getData_Staged( 'bank_code' );
-               $filter = function( $submethod ) use ( $method, $bank ) {
-                       $groups = (array) $submethod['group'];
-                       return in_array( $groups, $method ) && 
$submethod['bank_code'] === $bank;
-               };
-               $candidates = array_filter( $this->payment_submethods, $filter 
);
-               if ( count( $candidates ) !== 1 ) {
-                       throw new UnexpectedValueException( "No unique payment 
submethod defined for payment method $method and bank code $bank." );
-               }
-               $keys = array_keys( $candidates );
-               $this->unstaged_data['payment_submethod'] = $keys[0];
        }
 
        public function getCurrencies( $options = array() ) {
@@ -1012,11 +963,5 @@
                return strtoupper(
                        hash_hmac( 'sha256', pack( 'A*', $message ), pack( 
'A*', $key ) )
                );
-       }
-
-       protected function unstage_amount() {
-               // FIXME: if GlobalCollect is the only processor who needs 
amount in
-               // cents, move its stage and unstage functions out of base 
adapter
-               $this->unstaged_data['amount'] = $this->getData_Staged( 
'amount' );
        }
 }
diff --git a/gateway_common/DonorEmail.php b/gateway_common/DonorEmail.php
new file mode 100644
index 0000000..5a0e537
--- /dev/null
+++ b/gateway_common/DonorEmail.php
@@ -0,0 +1,12 @@
+<?php
+
+class DonorEmail implements StagingHelper {
+       public function stage( GatewayType $adapter, $unstagedData, 
&$stagedData ) {
+               if ( empty( $stagedData['email'] ) ) {
+                       $stagedData['email'] = $adapter->getGlobal( 
'DefaultEmail' );
+               }
+       }
+
+       // No-op
+       public function unstage( GatewayType $adapter, $stagedData, 
&$unstagedData ) {}
+}
diff --git a/gateway_common/DonorFullName.php b/gateway_common/DonorFullName.php
new file mode 100644
index 0000000..6a4b7b8
--- /dev/null
+++ b/gateway_common/DonorFullName.php
@@ -0,0 +1,21 @@
+<?php
+
+class DonorFullName implements StagingHelper {
+       /*
+        * Seems more sane to do it this way than provide a single input box
+        * and try to parse out fname and lname.
+        */
+       public function stage( GatewayType $adapter, $unstagedData, 
&$stagedData ) {
+               $name_parts = array();
+               if ( isset( $unstagedData['fname'] ) ) {
+                       $name_parts[] = $unstagedData['fname'];
+               }
+               if ( isset( $unstagedData['lname'] ) ) {
+                       $name_parts[] = $unstagedData['lname'];
+               }
+               $stagedData['full_name'] = implode( ' ', $name_parts );
+       }
+
+       // No-op.
+       public function unstage( GatewayType $adapter, $stagedData, 
&$unstagedData ) {}
+}
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index b33abd1..b88c42b 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -373,11 +373,11 @@
                }
        }
 
-       /**
-        * FIXME: sloppy.  Not sure what we want to do instead.
-        */
        public function getCoreStagingHelpers() {
                return array(
+                       // Always stage email address first, to set default if 
missing
+                       new DonorEmail(),
+                       new DonorFullName(),
                        new AmountInCents(),
                        new StreetAddress(),
                );
@@ -1163,16 +1163,6 @@
                return $this->getData_Unstaged_Escaped( 'payment_submethod' );
        }
 
-       /**
-        * Define payment methods
-        *
-        * @todo
-        * - this is not implemented in all adapters yet
-        *
-        * Not all payment submethods are available within an adapter
-        *
-        * @return      array   Returns the available payment submethods for 
the specific adapter
-        */
        public function getPaymentSubmethods() {
                return $this->payment_submethods;
        }
@@ -1699,9 +1689,6 @@
                // object initialization.
                $this->defineStagedVars();
 
-               // Always stage email address first, to set default if missing
-               array_unshift( $this->staged_vars, 'email' );
-
                foreach ( $this->staging_helpers as $staging_helper ) {
                        $staging_helper->stage( $this, $this->unstaged_data, 
$this->staged_data );
                }
@@ -1771,27 +1758,6 @@
 
                        $this->staged_data[ $field ] = $value;
                }
-       }
-
-       protected function stage_email() {
-               if ( empty( $this->staged_data['email'] ) ) {
-                       $this->staged_data['email'] = $this->getGlobal( 
'DefaultEmail' );
-               }
-       }
-
-       /*
-        * Seems more sane to do it this way than provide a single input box
-        * and try to parse out fname and lname.
-        */
-       protected function stage_full_name() {
-               $name_parts = array();
-               if ( isset( $this->unstaged_data['fname'] ) ) {
-                       $name_parts[] = $this->unstaged_data['fname'];
-               }
-               if ( isset( $this->unstaged_data['lname'] ) ) {
-                       $name_parts[] = $this->unstaged_data['lname'];
-               }
-               $this->staged_data['full_name'] = implode( ' ', $name_parts );
        }
 
        protected function buildRequestParams() {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I9f3c16b63aa79e2624e8119026ea43eb2bbdd748
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Awight <awi...@wikimedia.org>
Gerrit-Reviewer: AndyRussG <andrew.green...@gmail.com>
Gerrit-Reviewer: Cdentinger <cdentin...@wikimedia.org>
Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: Ssmith <ssm...@wikimedia.org>
Gerrit-Reviewer: XenoRyet <dkozlow...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to