jenkins-bot has submitted this change and it was merged. Change subject: Make paymentmethod non-static ......................................................................
Make paymentmethod non-static There was no reason to register methods in a central place. Now the payment method specs are fetched from an instantiated adapter object. Bug: T86256 Change-Id: I5d57d598c9b19f6695007f5ffd75a090ef302b24 --- M adyen_gateway/adyen.adapter.php M amazon_gateway/amazon.adapter.php M gateway_common/DataValidator.php M gateway_common/DonationData.php M gateway_common/PaymentMethod.php M gateway_common/gateway.adapter.php M globalcollect_gateway/globalcollect.adapter.php M paypal_gateway/paypal.adapter.php M tests/DonationDataTest.php M worldpay_gateway/worldpay.adapter.php 10 files changed, 100 insertions(+), 110 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 b2641aa..9fae997 100644 --- a/adyen_gateway/adyen.adapter.php +++ b/adyen_gateway/adyen.adapter.php @@ -168,7 +168,6 @@ $this->payment_methods = array( 'cc' => array(), ); - PaymentMethod::registerMethods( $this->payment_methods ); } protected function getAllowedPaymentMethods() { diff --git a/amazon_gateway/amazon.adapter.php b/amazon_gateway/amazon.adapter.php index 3517738..75047df 100644 --- a/amazon_gateway/amazon.adapter.php +++ b/amazon_gateway/amazon.adapter.php @@ -174,7 +174,11 @@ $this->payment_methods = array( 'amazon' => array(), ); - PaymentMethod::registerMethods( $this->payment_methods ); + + $this->payment_submethods = array( + 'amazon_cc' => array(), + 'amazon_wallet' => array(), + ); } protected function buildRequestParams() { diff --git a/gateway_common/DataValidator.php b/gateway_common/DataValidator.php index 87555c6..c6e31ea 100644 --- a/gateway_common/DataValidator.php +++ b/gateway_common/DataValidator.php @@ -749,7 +749,7 @@ /** * Calculates and returns the card type for a given credit card number. * @param numeric $card_num A credit card number. - * @return mixed 'american', 'mastercard', 'visa', 'discover', or false. + * @return string|bool 'amex', 'mc', 'visa', 'discover', or false. */ public static function getCardType( $card_num ) { // validate that credit card number entered is correct and set the card type diff --git a/gateway_common/DonationData.php b/gateway_common/DonationData.php index a8f664a..49b0fd6 100644 --- a/gateway_common/DonationData.php +++ b/gateway_common/DonationData.php @@ -744,17 +744,23 @@ * the utm_source is structured as: banner.landing_page.payment_method_family */ protected function setUtmSource() { - $utm_source = $this->getVal( 'utm_source' ); $utm_source_id = $this->getVal( 'utm_source_id' ); - - $payment_method_family = PaymentMethod::getUtmSourceName( - $this->getVal( 'payment_method' ), - $this->getVal( 'recurring' ) - ); - $recurring = ($this->getVal( 'recurring' ) ? 'true' : 'false'); - $this->log( __FUNCTION__ . ": Payment method is {$this->getVal( 'payment_method' )}, recurring = {$recurring}, utm_source = {$payment_method_family}", LOG_DEBUG ); + if ( $this->getVal( 'payment_method' ) ) { + $method_object = PaymentMethod::newFromCompoundName( + $this->gateway, + $this->getVal( 'payment_method' ), + $this->getVal( 'payment_submethod' ), + $this->getVal( 'recurring' ) + ); + $utm_payment_method_family = $method_object->getUtmSourceName(); + } else { + $utm_payment_method_family = ''; + } + + $recurring_str = var_export( $this->getVal( 'recurring' ), true ); + $this->log( __FUNCTION__ . ": Payment method is {$this->getVal( 'payment_method' )}, recurring = {$recurring_str}, utm_source = {$utm_payment_method_family}", LOG_DEBUG ); // split the utm_source into its parts for easier manipulation $source_parts = explode( ".", $utm_source ); @@ -767,14 +773,14 @@ // If the utm_source_id is set, include that in the landing page // portion of the string. if ( $utm_source_id ) { - $source_parts[1] = $payment_method_family . $utm_source_id; + $source_parts[1] = $utm_payment_method_family . $utm_source_id; } else { if ( empty( $source_parts[1] ) ) { $source_parts[1] = ''; } } - $source_parts[2] = $payment_method_family; + $source_parts[2] = $utm_payment_method_family; if ( empty( $source_parts[2] ) ) { $source_parts[2] = ''; } diff --git a/gateway_common/PaymentMethod.php b/gateway_common/PaymentMethod.php index 57d62e3..46f1323 100644 --- a/gateway_common/PaymentMethod.php +++ b/gateway_common/PaymentMethod.php @@ -19,72 +19,48 @@ * for most reporting. */ class PaymentMethod { - static protected $specs = array(); + /** + * @var GatewayAdapter $gateway + */ + protected $gateway; + + protected $name; + + protected $is_recurring; /** - * Register a list of payment methods - * - * FIXME: static ownership of gateway specs is bad - * - * @param array $methods_meta map of name => method specification + * Gateway definition for this payment method */ - static public function registerMethods( $methods_meta ) { - // TODO: The registration needs to be reworked. One of the more - // important issues is that several processors implement similar-enough - // methods (eg, "cc"), and they should each maintain separate metadata. - - foreach ( $methods_meta as $name => $meta ) { - if ( !array_key_exists( $name, self::$specs ) ) { - self::$specs[$name] = array(); - } - self::$specs[$name] = $meta + self::$specs[$name]; - } - } + protected $spec; /** - * Get the specification for this payment method + * Build a new PaymentMethod object from an name pair * - * @param string $method + * @param GatewayAdapter $gateway + * @param string $method_name + * @param string $submethod_name + * @param bool $is_recurring * - * @return array|null method specification data + * @return PaymentMethod */ - static protected function getMethodMeta( $method ) { - if ( array_key_exists( $method, self::$specs ) ) { - return self::$specs[$method]; - } - return null; - } + public static function newFromCompoundName( + GatewayAdapter $gateway, $method_name, $submethod_name, $is_recurring + ) { + $method = new PaymentMethod(); + $method->gateway = $gateway; + $method->name = PaymentMethod::parseCompoundMethod( $method_name, $submethod_name ); + $method->is_recurring = $is_recurring; - /** - * Convert a unique payment method name into the method/submethod form - * - * The compound form should be deprecated in favor of - * - * @param string $id unique method identifier - * @return list( $payment_method, $payment_submethod ) - */ - static public function getCompoundMethod( $id ) { - if ( !PaymentMethod::isCompletelySpecified( $id ) ) { - $payment_method = $id; - $payment_submethod = null; - } elseif ( strpos( "_", $id ) !== false ) { - // Use the first segment as the method, and the remainder as submethod - $segments = explode( "_", $id ); - $payment_method = array_shift( $segments ); - - // If the remainder is a valid method, use it as the submethod. - // Otherwise, we want something like (dd, dd_fr), so reuse the whole id. - $remainder = implode( "_", $segments ); - if ( PaymentMethod::getMethodMeta( $remainder ) ) { - $payment_submethod = $remainder; - } else { - $payment_submethod = $id; - } - } else { - $payment_method = $id; - $payment_submethod = $id; + $spec = null; + if ( $submethod_name ) { + $spec = $gateway->getPaymentSubmethodMeta( $submethod_name ); } - return array( $payment_method, $payment_submethod ); + if ( $spec === null ) { + $spec = $gateway->getPaymentMethodMeta( $method_name ); + } + $method->spec = $spec; + + return $method; } /** @@ -98,34 +74,48 @@ * @return string unique method id */ static public function parseCompoundMethod( $bareMethod, $subMethod ) { - $parts = explode( '_', $subMethod ); + $parts = array(); + if ( $subMethod ) { + $parts = explode( '_', $subMethod ); + } array_unshift( $parts, $bareMethod ); - if ( $parts[0] === $parts[1] ) { + if ( count( $parts ) > 1 && $parts[0] === $parts[1] ) { array_shift( $parts ); } return implode( '_', $parts ); + } + + + /** + * Get the gateway's specification for this payment method + * + * @return array method specification data + */ + public function getMethodMeta() { + return $this->spec; } /** * TODO: implement this function * @return true if this payment method is complete enough to begin a transaction */ - static public function isCompletelySpecified( $id ) { - if ( $id === 'cc' ) return false; + public function isCompletelySpecified() { + if ( $this->name === 'cc' ) return false; return true; } /** * @return true if the $method descends from a more general $ancestor method, or if they are equal. */ - static public function isInstanceOf( $method, $ancestor ) { + public function isInstanceOf( $ancestor ) { + $method = $this; do { - if ( $method === $ancestor ) { + if ( $method->name === $ancestor ) { return true; } - } while ( $method = PaymentMethod::getParent( $method ) ); + } while ( $method = $method->getParent() ); return false; } @@ -133,37 +123,32 @@ /** * Get the high-level family for this method * - * @return string the most general ancestor of a given payment $method + * @return PaymentMethod the most general ancestor of this method */ - static public function getFamily( $method ) { - while ( $parent = PaymentMethod::getParent( $method ) ) { + public function getFamily() { + $method = $this; + while ( $parent = $method->getParent() ) { $method = $parent; } return $method; } /** - * @param string $method - * - * @return string|null parent method name + * @return PaymentMethod|null parent method or null if there is no parent */ - static protected function getParent( $method ) { - $meta = PaymentMethod::getMethodMeta( $method ); - if ( $meta and array_key_exists( 'group', $meta ) ) { - return $meta['group']; + protected function getParent() { + if ( array_key_exists( 'group', $this->spec ) ) { + return PaymentMethod::newFromCompoundName( $this->gateway, $this->spec['group'], null, $this->is_recurring ); } return null; } /** - * @param string $method - * @param boolean $recurring - * - * @return normalized utm_source payment method component + * @return string normalized utm_source payment method component */ - static public function getUtmSourceName( $method, $recurring ) { - $source = PaymentMethod::getFamily( $method ); - if ( $recurring ) { + public function getUtmSourceName() { + $source = $this->getFamily()->name; + if ( $this->is_recurring ) { $source = "r" . $source; } return $source; diff --git a/gateway_common/gateway.adapter.php b/gateway_common/gateway.adapter.php index 5afc88c..b001b6e 100644 --- a/gateway_common/gateway.adapter.php +++ b/gateway_common/gateway.adapter.php @@ -335,8 +335,12 @@ $this->setApiRequest(); } - $this->defineOrderIDMeta(); //must happen before we go to DonationData. - $this->defineDataConstraints(); //must also happen before we go to DonationData. + // The following needs to be set up before we initialize DonationData. + // TODO: move the rest of the initialization here + $this->defineOrderIDMeta(); + $this->defineDataConstraints(); + $this->definePaymentMethods(); + $this->session_resetOnGatewaySwitch(); //clear out the old stuff before DD snarfs it. $this->dataObj = new DonationData( $this, $options['external_data'] ); @@ -351,7 +355,6 @@ $this->findAccount(); $this->defineAccountInfo(); $this->defineTransactions(); - $this->definePaymentMethods(); $this->defineErrorMap(); $this->defineVarMap(); $this->defineReturnValueMap(); diff --git a/globalcollect_gateway/globalcollect.adapter.php b/globalcollect_gateway/globalcollect.adapter.php index de70aa6..b5200a0 100644 --- a/globalcollect_gateway/globalcollect.adapter.php +++ b/globalcollect_gateway/globalcollect.adapter.php @@ -1062,9 +1062,6 @@ 'group' => 'cash', 'keys' => array(), ); - - PaymentMethod::registerMethods( $this->payment_methods ); - PaymentMethod::registerMethods( $this->payment_submethods ); } /** diff --git a/paypal_gateway/paypal.adapter.php b/paypal_gateway/paypal.adapter.php index 7017308..b3e98a6 100644 --- a/paypal_gateway/paypal.adapter.php +++ b/paypal_gateway/paypal.adapter.php @@ -191,7 +191,6 @@ $this->payment_methods = array( 'paypal' => array(), ); - PaymentMethod::registerMethods( $this->payment_methods ); } static function getCurrencies() { diff --git a/tests/DonationDataTest.php b/tests/DonationDataTest.php index 4b2a39f..bc8dc9a 100644 --- a/tests/DonationDataTest.php +++ b/tests/DonationDataTest.php @@ -51,8 +51,8 @@ 'expiration' => '1138', 'cvv' => '665', 'currency_code' => 'USD', - 'payment_method' => '', - 'payment_submethod' => '', + 'payment_method' => 'cc', + 'payment_submethod' => 'visa', 'numAttempt' => '5', 'referrer' => 'http://www.testing.com/', 'utm_source' => '..cc', @@ -90,7 +90,7 @@ 'country' => 'XX', 'payment_method' => '', 'referrer' => '', - 'utm_source' => '..cc', + 'utm_source' => '..', 'language' => $wgLanguageCode, 'gateway' => 'globalcollect', 'payment_submethod' => '', @@ -120,7 +120,7 @@ 'zip' => '94104', 'country' => 'US', 'card_num' => '378282246310005', - 'card_type' => 'american', + 'card_type' => 'amex', 'expiration' => '0415', 'cvv' => '001', 'currency_code' => 'USD', @@ -137,7 +137,7 @@ 'owa_session' => '', 'owa_ref' => 'http://localhost/defaultTestData', 'street_supplemental' => '3rd floor', - 'payment_submethod' => 'american', + 'payment_submethod' => 'amex', 'issuer_id' => '', 'utm_source_id' => '', 'user_ip' => '12.12.12.12', @@ -175,7 +175,7 @@ 'zip' => '94104', 'country' => 'US', 'card_num' => '378282246310005', - 'card_type' => 'american', + 'card_type' => 'amex', 'expiration' => '0415', 'cvv' => '001', 'currency_code' => 'USD', @@ -188,7 +188,7 @@ 'gateway' => 'globalcollect', 'owa_ref' => 'http://localhost/getTestData', 'street_supplemental' => '3rd floor', - 'payment_submethod' => 'american', + 'payment_submethod' => 'amex', 'user_ip' => $wgRequest->getIP(), 'server_ip' => $wgRequest->getIP(), 'recurring' => '', diff --git a/worldpay_gateway/worldpay.adapter.php b/worldpay_gateway/worldpay.adapter.php index 7d30a17..3f2bb5b 100644 --- a/worldpay_gateway/worldpay.adapter.php +++ b/worldpay_gateway/worldpay.adapter.php @@ -288,9 +288,6 @@ ), ); } - - PaymentMethod::registerMethods( $this->payment_methods ); - PaymentMethod::registerMethods( $this->payment_submethods ); } function defineOrderIDMeta() { -- To view, visit https://gerrit.wikimedia.org/r/183630 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I5d57d598c9b19f6695007f5ffd75a090ef302b24 Gerrit-PatchSet: 8 Gerrit-Project: mediawiki/extensions/DonationInterface Gerrit-Branch: master Gerrit-Owner: Awight <awi...@wikimedia.org> Gerrit-Reviewer: Awight <awi...@wikimedia.org> Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org> Gerrit-Reviewer: Ssmith <ssm...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits