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

Change subject: AstroPay signature as staging helper
......................................................................


AstroPay signature as staging helper

Start getting rid of some abuses of getTransactionSpecificValue

Bug: T137164
Change-Id: Ic64242201c4dc2250ba5b38f0ab97dbcdddedb8d
---
A astropay_gateway/AstroPaySignature.php
M astropay_gateway/astropay.adapter.php
M astropay_gateway/config/transformers.yaml
M astropay_gateway/config/var_map.yaml
M extension.json
M tests/Adapter/AstroPay/AstroPayTest.php
6 files changed, 53 insertions(+), 39 deletions(-)

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



diff --git a/astropay_gateway/AstroPaySignature.php 
b/astropay_gateway/AstroPaySignature.php
new file mode 100644
index 0000000..ec97d7c
--- /dev/null
+++ b/astropay_gateway/AstroPaySignature.php
@@ -0,0 +1,43 @@
+<?php
+
+class AstroPaySignature implements StagingHelper {
+       /*
+        * Sign an AstroPay NewInvoice request
+        * TODO: switch on transaction, build correct message for refund
+        */
+       public function stage( GatewayType $adapter, $normalized, &$stagedData 
) {
+               $message = self::getNewInvoiceMessage( $stagedData );
+               $stagedData['control'] = self::calculateSignature( $adapter, 
$message );
+       }
+
+       public static function getNewInvoiceMessage( $stagedData ) {
+               $requiredKeys = array(
+                       'order_id', 'amount', 'donor_id', 'bank_code', 
'fiscal_number', 'email'
+               );
+               $stagedKeys = array_keys( $stagedData );
+               if ( array_intersect( $requiredKeys, $stagedKeys ) != 
$requiredKeys ) {
+                       return '';
+               } else {
+                       return str_replace( '+', ' ',
+                               $stagedData['order_id'] . 'V'
+                               . $stagedData['amount'] . 'I'
+                               . $stagedData['donor_id'] . '2'
+                               . $stagedData['bank_code'] . '1'
+                               . $stagedData['fiscal_number'] . 'H'
+                               . /* bdate omitted */ 'G'
+                               . $stagedData['email'] .'Y'
+                               . /* zip omitted */ 'A'
+                               . /* street omitted */ 'P'
+                               . /* city omitted */ 'S'
+                               . /* state omitted */ 'P'
+                       );
+               }
+       }
+
+       public static function calculateSignature( GatewayType $adapter, 
$message ) {
+               $key = $adapter->getAccountConfig( 'SecretKey' );
+               return strtoupper(
+                       hash_hmac( 'sha256', pack( 'A*', $message ), pack( 
'A*', $key ) )
+               );
+       }
+}
diff --git a/astropay_gateway/astropay.adapter.php 
b/astropay_gateway/astropay.adapter.php
index e79b88f..311b4fd 100644
--- a/astropay_gateway/astropay.adapter.php
+++ b/astropay_gateway/astropay.adapter.php
@@ -239,35 +239,6 @@
                $fields[] = 'payment_submethod';
                return $fields;
        }
-       /**
-        * Overriding @see GatewayAdapter::getTransactionSpecificValue to add a
-        * calculated signature.
-        * @param string $gateway_field_name
-        * @param boolean $token
-        * @return mixed
-        */
-       protected function getTransactionSpecificValue( $gateway_field_name, 
$token = false ) {
-               if ( $gateway_field_name === 'control' ) {
-                       $message = $this->getMessageToSign();
-                       return $this->calculateSignature( $message );
-               }
-               return parent::getTransactionSpecificValue( 
$gateway_field_name, $token );
-       }
-
-       protected function getMessageToSign() {
-               return str_replace( '+', ' ',
-                       $this->getData_Staged( 'order_id' ) . 'V'
-                       . $this->getData_Staged( 'amount' ) . 'I'
-                       . $this->getData_Staged( 'donor_id' ) . '2'
-                       . $this->getData_Staged( 'bank_code' ) . '1'
-                       . $this->getData_Staged( 'fiscal_number' ) . 'H'
-                       . /* bdate omitted */ 'G'
-                       . $this->getData_Staged( 'email' ) .'Y'
-                       . /* zip omitted */ 'A'
-                       . /* street omitted */ 'P'
-                       . /* city omitted */ 'S'
-                       . /* state omitted */ 'P' );
-       }
 
        public function getCurrencies( $options = array() ) {
                $country = isset( $options['country'] ) ?
@@ -402,8 +373,10 @@
                                        $message = 
DataValidator::getErrorMessage( 'fiscal_number', 'calculated', $language, 
$country );
                                } else if ( preg_match( '/invalid control/i', 
$response['desc'] ) ) {
                                        // They think we screwed up the 
signature.  Log what we signed.
-                                       $signed = $this->getMessageToSign();
-                                       $signature = 
$this->getTransactionSpecificValue( 'control' );
+                                       $signed = 
AstroPaySignature::getNewInvoiceMessage(
+                                               $this->getData_Staged()
+                                       );
+                                       $signature = $this->getData_Staged( 
'control' );
                                        $this->logger->error( "$logme Signed 
message: '$signed' Signature: '$signature'" );
                                } else {
                                        // Some less common error.  Also log 
message at 'error' level
@@ -469,19 +442,12 @@
                        $data['result'] .
                        $data['x_amount'] .
                        $data['x_invoice'];
-               $signature = $this->calculateSignature( $message );
+               $signature = AstroPaySignature::calculateSignature( $this, 
$message );
 
                if ( $signature !== $data['x_control'] ) {
                        $message = 'Bad signature in transaction ' . 
$this->getCurrentTransaction();
                        $this->logger->error( $message );
                        throw new ResponseProcessingException( $message, 
ResponseCodes::BAD_SIGNATURE );
                }
-       }
-
-       protected function calculateSignature( $message ) {
-               $key = $this->accountInfo['SecretKey'];
-               return strtoupper(
-                       hash_hmac( 'sha256', pack( 'A*', $message ), pack( 
'A*', $key ) )
-               );
        }
 }
diff --git a/astropay_gateway/config/transformers.yaml 
b/astropay_gateway/config/transformers.yaml
index bae2a9c..acac9ca 100644
--- a/astropay_gateway/config/transformers.yaml
+++ b/astropay_gateway/config/transformers.yaml
@@ -7,3 +7,5 @@
 - DummyFiscalNumber # see class comment
 - DonorFullName
 - StreetAddress
+# Signature calculation must come last
+- AstroPaySignature
diff --git a/astropay_gateway/config/var_map.yaml 
b/astropay_gateway/config/var_map.yaml
index 1592144..ffecdc3 100644
--- a/astropay_gateway/config/var_map.yaml
+++ b/astropay_gateway/config/var_map.yaml
@@ -17,4 +17,5 @@
 x_city: city
 x_state: state
 x_document: gateway_txn_id
+control: control
 country_code: country
diff --git a/extension.json b/extension.json
index fc00d95..c940879 100644
--- a/extension.json
+++ b/extension.json
@@ -136,6 +136,7 @@
                "AstroPayAdapter": "astropay_gateway/astropay.adapter.php",
                "AstroPayFinancialNumbers": 
"astropay_gateway/AstroPayFinancialNumbers.php",
                "AstroPayMethodCodec": 
"astropay_gateway/AstroPayMethodCodec.php",
+               "AstroPaySignature": "astropay_gateway/AstroPaySignature.php",
                "DummyFiscalNumber": "astropay_gateway/DummyFiscalNumber.php",
                "CleanupRecurringLength": 
"paypal_gateway/CleanupRecurringLength.php",
                "PaypalExpressAdapter": 
"paypal_gateway/express_checkout/paypal_express.adapter.php",
diff --git a/tests/Adapter/AstroPay/AstroPayTest.php 
b/tests/Adapter/AstroPay/AstroPayTest.php
index 5f9f7ad..64ac0bf 100644
--- a/tests/Adapter/AstroPay/AstroPayTest.php
+++ b/tests/Adapter/AstroPay/AstroPayTest.php
@@ -582,6 +582,7 @@
 
        function testDummyFiscalNumber() {
                $init = $this->getDonorTestData( 'MX' );
+               $init['payment_submethod'] = 'visa';
                $gateway = $this->getFreshGatewayObject( $init );
                $gateway->doPayment();
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic64242201c4dc2250ba5b38f0ab97dbcdddedb8d
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Cdentinger <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to