Awight has uploaded a new change for review.

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

Change subject: [WIP] Prototype encapsulation for staging functions
......................................................................

[WIP] Prototype encapsulation for staging functions

Bug: T130075
Change-Id: Ifab742e712a6f6f732bbf3d9a7acd21add9628f6
---
M DonationInterface.php
A gateway_common/AmountInCents.php
M gateway_common/GatewayType.php
A gateway_common/StagingHelper.php
M gateway_common/gateway.adapter.php
5 files changed, 71 insertions(+), 27 deletions(-)


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

diff --git a/DonationInterface.php b/DonationInterface.php
index fb0d627..40f09d3 100644
--- a/DonationInterface.php
+++ b/DonationInterface.php
@@ -37,6 +37,7 @@
 /**
  * CLASSES
  */
+$wgAutoloadClasses['AmountInCents'] = __DIR__ . 
'/gateway_common/AmountInCents.php';
 $wgAutoloadClasses['CurrencyRates'] = __DIR__ . 
'/gateway_common/CurrencyRates.php';
 $wgAutoloadClasses['CurrencyRatesModule'] = __DIR__ . 
'/modules/CurrencyRatesModule.php';
 $wgAutoloadClasses['CyclicalArray'] = __DIR__ . 
'/globalcollect_gateway/CyclicalArray.php';
@@ -60,6 +61,7 @@
 $wgAutoloadClasses['ResponseCodes'] = __DIR__ . 
'/gateway_common/ResponseCodes.php';
 $wgAutoloadClasses['ResponseProcessingException'] = __DIR__ . 
'/gateway_common/ResponseProcessingException.php';
 $wgAutoloadClasses['ResultPages'] = __DIR__ . 
'/gateway_common/ResultPages.php';
+$wgAutoloadClasses['StagingHelper'] = __DIR__ . 
'/gateway_common/StagingHelper.php';
 $wgAutoloadClasses['WmfFramework_Mediawiki'] = __DIR__ . 
'/gateway_common/WmfFramework.mediawiki.php';
 $wgAutoloadClasses['WmfFrameworkLogHandler'] = __DIR__ . 
'/gateway_common/WmfFrameworkLogHandler.php';
 
diff --git a/gateway_common/AmountInCents.php b/gateway_common/AmountInCents.php
new file mode 100644
index 0000000..c99d4ed
--- /dev/null
+++ b/gateway_common/AmountInCents.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Stage: amount
+ *
+ * Amounts are usually passed as an integer, and usually x100 rather than
+ * using the currency's true fractional denomination ("cents").  Currencies
+ * without a fractional unit are still multiplied, so we have to floor to
+ * avoid killing the payment processor.
+ * For example: JPY 1000.05 would be changed to 100005, but should be 100000.
+ */
+class AmountInCents implements StagingHelper {
+       public function stage( GatewayType $adapter, $unstagedData, 
&$stagedData ) {
+               if ( empty( $unstagedData['amount'] ) || empty( 
$unstagedData['currency_code'] ) {
+                       //can't do anything with amounts at all. Just go home.
+                       unset( $stagedData['amount'] );
+                       return;
+               }
+
+               $amount = $unstagedData['amount'];
+               if ( !DataValidator::is_fractional_currency( 
$unstagedData['currency_code'] ) ) {
+                       $amount = floor( $amount );
+               }
+
+               $stagedData['amount'] = $amount * 100;
+       }
+
+       public function unstage( GatewayType $adapter, $stagedData, 
&$unstagedData ) {
+               $unstagedData['amount'] = $stagedData['amount'] / 100;
+       }
+}
diff --git a/gateway_common/GatewayType.php b/gateway_common/GatewayType.php
index 0a8679a..000e5b2 100644
--- a/gateway_common/GatewayType.php
+++ b/gateway_common/GatewayType.php
@@ -119,6 +119,11 @@
        function definePaymentMethods();
 
        /**
+        * Sets up the $staging_helpers array.
+        */
+       function defineStagingHelpers();
+
+       /**
         * Sets up the $order_id_meta array.
         * @TODO: Data Item Class. There should be a class that keeps track of
         * the metadata for every field we use (everything that currently comes
diff --git a/gateway_common/StagingHelper.php b/gateway_common/StagingHelper.php
new file mode 100644
index 0000000..c6b4dec
--- /dev/null
+++ b/gateway_common/StagingHelper.php
@@ -0,0 +1,6 @@
+<?php
+
+interface StagingHelper {
+       function stage( GatewayType $adapter, $unstagedData, &$stagedData );
+       function unstage( GatewayType $adapter, $stagedData, &$unstagedData );
+}
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index e57cc4f..ddb1b68 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -22,6 +22,7 @@
 interface LogPrefixProvider {
        function getLogMessagePrefix();
 }
+
 /**
  * GatewayAdapter
  *
@@ -102,6 +103,11 @@
        protected $return_value_map;
        protected $staged_data;
        protected $unstaged_data;
+
+       /**
+        * Staging helper objects.  These implement the StagingHelper interface.
+        */
+       protected $staging_helpers = array();
 
        /**
         * For gateways that speak XML, we use this variable to hold the 
document
@@ -246,6 +252,8 @@
                $this->defineDataConstraints();
                $this->definePaymentMethods();
 
+               $this->defineStagingHelpers();
+
                $this->session_resetOnSwitch(); // Need to do this before 
creating DonationData
 
                // FIXME: this should not have side effects like setting 
order_id_meta['final']
@@ -363,6 +371,15 @@
                                return null;
                        }
                }
+       }
+
+       /**
+        * FIXME: sloppy.  Not sure what we want to do instead.
+        */
+       public function getCoreStagingHelpers() {
+               return array(
+                       new AmountInCents(),
+               );
        }
 
        /**
@@ -1681,10 +1698,16 @@
                $this->staged_data = $this->unstaged_data;
 
                // This allows transactions to each stage different data.
+               // FIXME: These are actually constant.  We should move this to
+               // 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 );
+               }
 
                foreach ( $this->staged_vars as $field ) {
                        $function_name = 'stage_' . $field;
@@ -1701,6 +1724,10 @@
         * @param array $data response data
         */
        protected function unstageData( $data ) {
+               foreach ( $this->staging_helpers as $staging_helper ) {
+                       $staging_helper->unstage( $this, $this->staged_data, 
$this->unstaged_data );
+               }
+
                foreach ( $data as $field => $value ) {
                        // Run custom unstaging function if available.
                        $function_name = 'unstage_' . $field;
@@ -1747,33 +1774,6 @@
 
                        $this->staged_data[ $field ] = $value;
                }
-       }
-
-       /**
-        * Stage: amount
-        *
-        * For example: JPY 1000.05 get changed to 100005. This need to be 
100000.
-        * For example: JPY 1000.95 get changed to 100095. This need to be 
100000.
-        */
-       protected function stage_amount() {
-               if ( !$this->getData_Unstaged_Escaped( 'amount' )
-                       || !$this->getData_Unstaged_Escaped( 'currency_code' )
-               ) {
-                       //can't do anything with amounts at all. Just go home.
-                       unset( $this->staged_data['amount'] );
-                       return;
-               }
-
-               $amount = $this->getData_Unstaged_Escaped( 'amount' );
-               if ( !DataValidator::is_fractional_currency( 
$this->getData_Unstaged_Escaped( 'currency_code' ) ) ) {
-                       $amount = floor( $amount );
-               }
-
-               $this->staged_data['amount'] = $amount * 100;
-       }
-
-       protected function unstage_amount() {
-               $this->unstaged_data['amount'] = $this->getData_Staged( 
'amount' ) / 100;
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifab742e712a6f6f732bbf3d9a7acd21add9628f6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>

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

Reply via email to