Adamw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/64348


Change subject: PaymentMethod utility class
......................................................................

PaymentMethod utility class

Change-Id: I34602faa9d8cca99132aa3b8067247e0f0a58b9e
---
M adyen_gateway/adyen.adapter.php
M amazon_gateway/amazon.adapter.php
M donationinterface.php
M gateway_common/DonationData.php
A gateway_common/PaymentMethod.php
M gateway_common/gateway.adapter.php
M globalcollect_gateway/globalcollect.adapter.php
M paypal_gateway/paypal.adapter.php
8 files changed, 150 insertions(+), 47 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface 
refs/changes/48/64348/1

diff --git a/adyen_gateway/adyen.adapter.php b/adyen_gateway/adyen.adapter.php
index 1ad4f1f..9ee939d 100644
--- a/adyen_gateway/adyen.adapter.php
+++ b/adyen_gateway/adyen.adapter.php
@@ -146,7 +146,14 @@
                        'iframe' => TRUE,
                );
        }
-       
+
+       public function definePaymentMethods() {
+               $this->payment_methods = array_flip( array(
+                       'adyen',
+               ) );
+               PaymentMethod::registerMethods( $this->payment_methods );
+       }
+
        protected function getAllowedPaymentMethods() {
                return array(
                        'card',
diff --git a/amazon_gateway/amazon.adapter.php 
b/amazon_gateway/amazon.adapter.php
index f171836..b2f8d96 100644
--- a/amazon_gateway/amazon.adapter.php
+++ b/amazon_gateway/amazon.adapter.php
@@ -163,6 +163,13 @@
                );
        }
 
+       public function definePaymentMethods() {
+               $this->payment_methods = array_flip( array(
+                       'amazon',
+               ) );
+               PaymentMethod::registerMethods( $this->payment_methods );
+       }
+
        protected function buildRequestParams() {
                // Look up the request structure for our current transaction 
type in the transactions array
                $structure = $this->getTransactionRequestStructure();
diff --git a/donationinterface.php b/donationinterface.php
index 4900929..084a90a 100644
--- a/donationinterface.php
+++ b/donationinterface.php
@@ -94,6 +94,7 @@
 $wgAutoloadClasses['GatewayAdapter'] = $donationinterface_dir . 
'gateway_common/gateway.adapter.php';
 $wgAutoloadClasses['GatewayForm'] = $donationinterface_dir . 
'gateway_common/GatewayForm.php';
 $wgAutoloadClasses['DataValidator'] = $donationinterface_dir . 
'gateway_common/DataValidator.php';
+$wgAutoloadClasses['PaymentMethod'] = $donationinterface_dir . 
'gateway_common/PaymentMethod.php';
 
 //load all possible form classes
 $wgAutoloadClasses['Gateway_Form'] = $donationinterface_dir . 
'gateway_forms/Form.php';
diff --git a/gateway_common/DonationData.php b/gateway_common/DonationData.php
index c07ce78..9bd6912 100644
--- a/gateway_common/DonationData.php
+++ b/gateway_common/DonationData.php
@@ -906,34 +906,34 @@
                $utm_source = $this->getVal( 'utm_source' );
                $utm_source_id = $this->getVal( 'utm_source_id' );
                
-               //TODO: Seriously, you need to move this. 
-               if ( $this->isSomething('payment_method') ){
-                       $payment_method = $this->getVal( 'payment_method' );
-               } else {
-                       $payment_method = 'cc';
-               }
-               
-               // this is how the payment method portion of the utm_source 
should be defined
-               $correct_payment_method_source = ( $utm_source_id ) ? 
$payment_method . $utm_source_id . '.' . $payment_method : $payment_method;
-
-               // check to see if the utm_source is already correct - if so, 
return
-               if ( !is_null( $utm_source ) && preg_match( '/' . str_replace( 
".", "\.", $correct_payment_method_source ) . '$/', $utm_source ) ) {
-                       return; //nothing to do. 
-               }
+               $payment_method = PaymentMethod::getUtmSourceName(
+                       $this->boss->getPaymentMethod(),
+                       $this->getVal( 'recurring' )
+               );
 
                // split the utm_source into its parts for easier manipulation
                $source_parts = explode( ".", $utm_source );
 
                // if there are no sourceparts element, then the banner portion 
of the string needs to be set.
                // since we don't know what it is, set it to an empty string
-               if ( !count( $source_parts ) )
+               if ( empty( $source_parts[0] ) ) {
                        $source_parts[0] = '';
+               }
 
-               // if the utm_source_id is set, set the landing page portion of 
the string to cc#
-               $source_parts[1] = ( $utm_source_id ) ? $payment_method . 
$utm_source_id : ( isset( $source_parts[1] ) ? $source_parts[1] : '' );
+               // if the utm_source_id is set, set the landing page portion of 
the string to "cc{$utm_source_id}"
+               //FIXME: do we still do that?
+               if ( $utm_source_id ) {
+                       $source_parts[1] = $payment_method . $utm_source_id;
+               } else {
+                       if ( empty( $source_parts[1] ) ) {
+                               $source_parts[1] = '';
+                       }
+               }
 
-               // the payment instrument portion should always be 'cc' if this 
method is being accessed
                $source_parts[2] = $payment_method;
+               if ( empty( $source_parts[2] ) ) {
+                       $source_parts[2] = '';
+               }
 
                // reconstruct, and set the value.
                $utm_source = implode( ".", $source_parts );
diff --git a/gateway_common/PaymentMethod.php b/gateway_common/PaymentMethod.php
new file mode 100644
index 0000000..4b56350
--- /dev/null
+++ b/gateway_common/PaymentMethod.php
@@ -0,0 +1,101 @@
+<?php
+
+class PaymentMethod {
+       static protected $specs = array();
+
+       //FIXME: this will be done as gateways are brought online, not from 
gateway->definePaymentMethods
+       static public function registerMethods( $methods_meta ) {
+               foreach ( $methods_meta as $name => $meta ) {
+                       if ( !array_key_exists( $name, self::$specs ) ) {
+                               self::$specs[$name] = array();
+                       }
+                       if ( !is_array( $meta ) ) {
+                               $meta = array();
+                       }
+                       //FIXME: the last declaration wins
+                       self::$specs[$name] = $meta + self::$specs[$name];
+               }
+       }
+
+       static protected function getMethodMeta( $method ) {
+               if ( array_key_exists( $method, self::$specs ) ) {
+                       return self::$specs[$method];
+               }
+               return null;
+       }
+
+       /**
+        * Convert a unique payment method name into the method/submethod form
+        *
+        * @return list( $payment_method, $payment_submethod )
+        */
+       static public function getCompoundMethod( $id ) {
+               if ( strpos( "_", $id ) !== false ) {
+                       $segments = explode( "_", $id );
+                       $payment_method = array_shift( $segments );
+                       // OR could be $payment_method = 
PaymentMethod::getFamily( $id );
+                       $payment_submethod = implode( "_", $segments );
+               } else {
+                       $payment_method = $id;
+                       $payment_submethod = $id;
+               }
+               return array( $payment_method, $payment_submethod );
+       }
+
+       /**
+        * @return unique method id
+        */
+       static public function parseCompoundMethod( $bareMethod, $subMethod ) {
+               //TODO special cases
+               if ( $bareMethod === $subMethod ) {
+                       return $bareMethod;
+               } else {
+                       return "{$bareMethod}_{$subMethod}";
+               }
+       }
+
+       /**
+        * @return true if the $method descends from a more general $ancestor 
method, or if they are equal.
+        */
+       static public function isInstanceOf( $method, $ancestor ) {
+               do {
+                       $parent = PaymentMethod::getParent( $method );
+                       if ( $parent === $ancestor ) {
+                               return true;
+                       }
+                       $method = $parent;
+               } while ( $parent );
+
+               return false;
+       }
+
+       /**
+        * @return the most general ancestor of a given payment $method
+        */
+       static public function getFamily( $method ) {
+               while ( $parent = PaymentMethod::getParent( $method ) ) {
+                       $method = $parent;
+               }
+               return $method;
+       }
+
+       //TODO: polyhierarchy?
+       static protected function getParent( $method ) {
+               $meta = PaymentMethod::getMethodMeta( $method );
+               if ( $meta and array_key_exists( 'group', $meta ) ) {
+                       return $meta['group'];
+               }
+               return null;
+       }
+
+       /**
+        * @return normalized utm_source payment method component
+        */
+       static public function getUtmSourceName( $method, $recurring ) {
+               $source = PaymentMethod::getFamily( $method );
+               if ( $recurring ) {
+                       $source = "r" . $source;
+               }
+               return $source;
+       }
+}
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index a79b044..fdca0d3 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -264,6 +264,7 @@
                $this->findAccount();
                $this->defineAccountInfo();
                $this->defineTransactions();
+               $this->definePaymentMethods();
                $this->defineErrorMap();
                $this->defineVarMap();
                $this->defineDataConstraints();
@@ -1208,7 +1209,7 @@
         * @return      string
         */
        public function getPaymentMethod() {
-
+               //FIXME: this should return the final calculated method
                return $this->getData_Unstaged_Escaped('payment_method');
        }
 
@@ -1220,13 +1221,6 @@
         * @return      array   Returns the available payment methods for the 
specific adapter
         */
        public function getPaymentMethods() {
-
-               // Define the payment methods if they have not been set yet.
-               if ( empty( $this->payment_methods ) ) {
-
-                       $this->definePaymentMethods();
-               }
-
                return $this->payment_methods;
        }
 
@@ -1251,13 +1245,6 @@
         * @return      array   Returns the available payment submethods for 
the specific adapter
         */
        public function getPaymentSubmethods() {
-
-               // Define the payment methods if they have not been set yet.
-               if ( empty( $this->payment_submethods ) ) {
-
-                       $this->definePaymentSubmethods();
-               }
-
                return $this->payment_submethods;
        }
 
diff --git a/globalcollect_gateway/globalcollect.adapter.php 
b/globalcollect_gateway/globalcollect.adapter.php
index ba75348..b66788a 100644
--- a/globalcollect_gateway/globalcollect.adapter.php
+++ b/globalcollect_gateway/globalcollect.adapter.php
@@ -402,11 +402,6 @@
         * - GET_ORDERSTATUS
         */
        public function defineTransactions() {
-
-               // Define the transaction types and groups
-               $this->definePaymentMethods();
-               $this->definePaymentSubmethods();
-               
                $this->transactions = array( );
 
                $this->transactions['DO_BANKVALIDATION'] = array(
@@ -675,14 +670,9 @@
                        'additional_success_status' => array( 55 ), //PENDING 
AT CUSTOMER - denotes they need to go to the bank, but we've done all we can.
                );
 
-       }
+               // *** Define payment submethods ***
+               //TODO: deprecate submethod, everything is a first-class method.
 
-       /**
-        * Define payment submethods
-        *
-        */
-       protected function definePaymentSubmethods() {
-               
                $this->payment_submethods = array();
 
                /*
@@ -1034,6 +1024,9 @@
                        'group' => 'cash',
                        'keys' => array(),
                );
+
+               PaymentMethod::registerMethods( $this->payment_methods );
+               PaymentMethod::registerMethods( $this->payment_submethods );
        }
 
        /**
@@ -2097,6 +2090,7 @@
                        $trythis = $payment_method . '_' . strtolower( 
$this->getData_Unstaged_Escaped('country') );
                        if ( array_key_exists( $trythis, 
$this->payment_submethods ) ){
                                $payment_submethod = $trythis;
+                               $this->staged_data['payment_submethod'] = 
$payment_submethod;
                        }
                }
 
@@ -2378,5 +2372,4 @@
                        return false;
                }
        }
-       
 }
diff --git a/paypal_gateway/paypal.adapter.php 
b/paypal_gateway/paypal.adapter.php
index 30fee8d..48d9c41 100644
--- a/paypal_gateway/paypal.adapter.php
+++ b/paypal_gateway/paypal.adapter.php
@@ -151,6 +151,13 @@
                }
        }
 
+       public function definePaymentMethods() {
+               $this->payment_methods = array_flip( array(
+                       'paypal',
+               ) );
+               PaymentMethod::registerMethods( $this->payment_methods );
+       }
+
        static function getCurrencies() {
                // see 
https://www.x.com/developers/paypal/documentation-tools/api/currency-codes
                return array(

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I34602faa9d8cca99132aa3b8067247e0f0a58b9e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Adamw <awi...@wikimedia.org>

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

Reply via email to