jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/374391 )

Change subject: Update SmashPig library
......................................................................


Update SmashPig library

Change-Id: I2766da66bf6ace0301f42a1984438a5bcf63ff22
---
M composer/installed.json
M wikimedia/smash-pig/Core/DataStores/PaymentsInitialDatabase.php
M wikimedia/smash-pig/Core/DataStores/PendingDatabase.php
A wikimedia/smash-pig/Core/Http/EnumValidator.php
M wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/amznipnfake.js
M wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/index.html
M wikimedia/smash-pig/PaymentProviders/Ingenico/Audit/IngenicoAudit.php
A wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/Data/sparseRefund.xml.gz
M wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/phpunit/AuditTest.php
M wikimedia/smash-pig/PaymentProviders/PayPal/PayPalPaymentsAPI.php
M wikimedia/smash-pig/PaymentProviders/PayPal/RefundMessage.php
M 
wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement.json
A 
wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec.json
A 
wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec_transformed.json
A 
wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_transformed.json
D wikimedia/smash-pig/PaymentProviders/PayPal/Tests/MockPayPalPaymentsAPI.php
D wikimedia/smash-pig/PaymentProviders/PayPal/Tests/PayPalTestConfiguration.php
D wikimedia/smash-pig/PaymentProviders/PayPal/Tests/config_test.yaml
M 
wikimedia/smash-pig/PaymentProviders/PayPal/Tests/phpunit/CaptureIncomingMessageTest.php
M wikimedia/smash-pig/Tests/PendingDatabaseTest.php
M wikimedia/smash-pig/config/paypal/main.yaml
21 files changed, 205 insertions(+), 124 deletions(-)

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



diff --git a/composer/installed.json b/composer/installed.json
index d027eef..9035ed4 100644
--- a/composer/installed.json
+++ b/composer/installed.json
@@ -1891,7 +1891,7 @@
         "source": {
             "type": "git",
             "url": 
"https://gerrit.wikimedia.org/r/wikimedia/fundraising/SmashPig.git";,
-            "reference": "b1822378fb781a510058c9b620258e0fafedada4"
+            "reference": "cd2e8d28911bdd2e02ebc78d480bf1d706cfb577"
         },
         "require": {
             "amzn/login-and-pay-with-amazon-sdk-php": "dev-master",
@@ -1909,7 +1909,7 @@
             "jakub-onderka/php-parallel-lint": "^0.9",
             "phpunit/phpunit": "^4.8"
         },
-        "time": "2017-08-22T18:13:48+00:00",
+        "time": "2017-08-28T16:03:45+00:00",
         "type": "library",
         "installation-source": "source",
         "autoload": {
diff --git a/wikimedia/smash-pig/Core/DataStores/PaymentsInitialDatabase.php 
b/wikimedia/smash-pig/Core/DataStores/PaymentsInitialDatabase.php
index d40238c..5b10cb9 100644
--- a/wikimedia/smash-pig/Core/DataStores/PaymentsInitialDatabase.php
+++ b/wikimedia/smash-pig/Core/DataStores/PaymentsInitialDatabase.php
@@ -73,6 +73,8 @@
 
         $sql = "INSERT INTO payments_initial ( $fieldList ) VALUES ( 
$paramList )";
                $this->prepareAndExecute( $sql, $message );
+
+               return $this->getDatabase()->lastInsertId();
        }
 
        protected function getConfigKey() {
diff --git a/wikimedia/smash-pig/Core/DataStores/PendingDatabase.php 
b/wikimedia/smash-pig/Core/DataStores/PendingDatabase.php
index dde9bd5..781aaa7 100644
--- a/wikimedia/smash-pig/Core/DataStores/PendingDatabase.php
+++ b/wikimedia/smash-pig/Core/DataStores/PendingDatabase.php
@@ -28,6 +28,7 @@
         * Build and insert a database record from a pending queue message
         *
         * @param array $message
+        * @return int ID of message in pending database
         */
        public function storeMessage( $message ) {
                $this->validateMessage( $message );
@@ -57,6 +58,8 @@
                        $sql = $this->getInsertStatement( $dbRecord );
                }
                $this->prepareAndExecute( $sql, $dbRecord );
+
+               return $this->getDatabase()->lastInsertId();
        }
 
        /**
diff --git a/wikimedia/smash-pig/Core/Http/EnumValidator.php 
b/wikimedia/smash-pig/Core/Http/EnumValidator.php
new file mode 100644
index 0000000..1bd89a3
--- /dev/null
+++ b/wikimedia/smash-pig/Core/Http/EnumValidator.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace SmashPig\Core\Http;
+
+/**
+ * Validates HTTP responses based on a limited set of acceptable responses
+ */
+class EnumValidator implements ResponseValidator {
+
+       /**
+        * @var array
+        */
+       protected $validValues;
+
+       /**
+        * EnumValidator constructor.
+        * @param array $validValues
+        */
+       public function __construct( $validValues ) {
+               $this->validValues = $validValues;
+       }
+
+       /**
+        * @param array $parsedResponse with keys 'status', 'headers', and 
'body'
+        * @return bool Whether to retry the request
+        */
+       public function shouldRetry( $parsedResponse ) {
+               return !in_array( $parsedResponse['body'], $this->validValues );
+       }
+}
diff --git 
a/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/amznipnfake.js 
b/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/amznipnfake.js
index 7907a88..026fa22 100644
--- a/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/amznipnfake.js
+++ b/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/amznipnfake.js
@@ -10,7 +10,7 @@
        $.getJSON( 'RawPosts/' + notificationType + '.json',
                function( postJson ) {
                        $.ajax({
-                               type: 'POST',
+                               method: 'POST',
                                url: listenerUrl,
                                contentType: 'application/json; charset=utf-8',
                                headers: postJson.headers,
diff --git 
a/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/index.html 
b/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/index.html
index b9b07a2..fd057ed 100644
--- a/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/index.html
+++ b/wikimedia/smash-pig/PaymentProviders/Amazon/Tests/manual/index.html
@@ -7,7 +7,7 @@
        <h3>Amazon listener test posts</h3>
        <p>Send some convincing-looking posts (including valid signatures) to 
your listener</p>
        <p>If you have an externally-accessible https URL and an Amazon 
merchant account, you can test more varieties at <a 
href="https://sellercentral.amazon.com/hz/me/hmi/ipn/main";>https://sellercentral.amazon.com/hz/me/hmi/ipn/main</a></p>
-Listener URL: <input type='text' id='listener_url' /><br/>
+Listener URL (must be on the same domain as this page): <input type='text' 
id='listener_url' /><br/>
 <button id='capture_completed'>Capture completed</button><br/>
 <button id='refund_completed'>Refund completed</button><br/>
 <button id='capture_declined'>Capture declined</button><br/>
diff --git 
a/wikimedia/smash-pig/PaymentProviders/Ingenico/Audit/IngenicoAudit.php 
b/wikimedia/smash-pig/PaymentProviders/Ingenico/Audit/IngenicoAudit.php
index 0ec05d2..09000ec 100644
--- a/wikimedia/smash-pig/PaymentProviders/Ingenico/Audit/IngenicoAudit.php
+++ b/wikimedia/smash-pig/PaymentProviders/Ingenico/Audit/IngenicoAudit.php
@@ -40,6 +40,8 @@
                'OrderID' => 'gateway_parent_id',
                'EffortID' => 'installment',
                'DebitedCurrency' => 'gross_currency',
+               'DateDue' => 'date',
+               // Order matters. Prefer TransactionDateTime if it is present.
                'TransactionDateTime' => 'date',
        );
 
diff --git 
a/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/Data/sparseRefund.xml.gz 
b/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/Data/sparseRefund.xml.gz
new file mode 100644
index 0000000..0b29176
--- /dev/null
+++ 
b/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/Data/sparseRefund.xml.gz
Binary files differ
diff --git 
a/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/phpunit/AuditTest.php 
b/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/phpunit/AuditTest.php
index e63f1a3..3ef9999 100644
--- a/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/phpunit/AuditTest.php
+++ b/wikimedia/smash-pig/PaymentProviders/Ingenico/Tests/phpunit/AuditTest.php
@@ -114,4 +114,27 @@
                );
                $this->assertEquals( $expected, $actual, 'Did not parse 
chargeback correctly' );
        }
+
+       /**
+        * We get some refunds in a weird sparse format with OrderID zero and no
+        * TransactionDateTime. At least get the ct_id and a date out of them.
+        */
+       public function testProcessSparseRefund() {
+               $processor = new IngenicoAudit();
+               $output = $processor->parseFile( __DIR__ . 
'/../Data/sparseRefund.xml.gz' );
+               $this->assertEquals( 1, count( $output ), 'Should have found 
one refund' );
+               $actual = $output[0];
+               $expected = array(
+                       'gateway' => 'globalcollect', // TODO: switch to 
ingenico for Connect
+                       'contribution_tracking_id' => '48987654',
+                       'date' => 1503964800,
+                       'gross' => 15,
+                       'gateway_parent_id' => '0', // We'll need to find it by 
ct_id
+                       'gateway_refund_id' => '0', // And we'll need to fill 
in this field
+                       'installment' => '', // EffortID came in blank too
+                       'gross_currency' => 'EUR',
+                       'type' => 'refund',
+               );
+               $this->assertEquals( $expected, $actual, 'Did not parse refund 
correctly' );
+       }
 }
diff --git a/wikimedia/smash-pig/PaymentProviders/PayPal/PayPalPaymentsAPI.php 
b/wikimedia/smash-pig/PaymentProviders/PayPal/PayPalPaymentsAPI.php
index d955bec..1da6e23 100644
--- a/wikimedia/smash-pig/PaymentProviders/PayPal/PayPalPaymentsAPI.php
+++ b/wikimedia/smash-pig/PaymentProviders/PayPal/PayPalPaymentsAPI.php
@@ -1,17 +1,10 @@
 <?php namespace SmashPig\PaymentProviders\PayPal;
 
-use RuntimeException;
+use LogicException;
 use SmashPig\Core\Context;
-use SmashPig\Core\Logging\Logger;
+use SmashPig\Core\Http\OutboundRequest;
 
 class PayPalPaymentsAPI {
-
-       // Simply a function to override in testing.
-       protected function curl( $ch, $post_fields ) {
-               $post_fields['cmd'] = '_notify-validate';
-               curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );
-               return curl_exec( $ch );
-       }
 
        /**
         * @param array $post_fields Associative array of fields posted to 
listener
@@ -19,43 +12,23 @@
         */
        public function validate( $post_fields = array() ) {
 
-               // 
https://www.paypal-knowledge.com/infocenter/index?page=content&id=FAQ1336&actp=LIST
-               // PayPal randomly fails to validate messages, so try a few 
times.
-               $max_attempts = 7;
+               $post_fields['cmd'] = '_notify-validate';
 
-               for ( $i = 0; $i < $max_attempts; $i++ ) {
-                       $url = Context::get()->getProviderConfiguration()->val( 
'postback-url' );
-                       $ch = curl_init();
-                       curl_setopt( $ch, CURLOPT_URL, $url );
-                       curl_setopt( $ch, CURLOPT_HEADER, 0 );
-                       curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 );
-                       curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
-                       curl_setopt( $ch, CURLOPT_POST, 1 );
-                       // TODO we can put VERIFIED in config and generalize 
this
+               $url = Context::get()->getProviderConfiguration()->val( 
'postback-url' );
+               $request = new OutboundRequest( $url, 'POST' );
+               $request->setBody( $post_fields );
 
-                       // Always capture the cURL output
-                       $curlDebugLog = fopen( 'php://temp', 'r+' );
-                       curl_setopt( $ch, CURLOPT_VERBOSE, true );
-                       curl_setopt( $ch, CURLOPT_STDERR, $curlDebugLog );
+               $response = $request->execute();
 
-                       $response = $this->curl( $ch, $post_fields );
-
-                       // Read the logging output
-                       rewind( $curlDebugLog );
-                       $logged = fread( $curlDebugLog, 8192 );
-                       fclose( $curlDebugLog );
-                       Logger::debug( "cURL verbose logging: $logged" );
-
-                       if ( $response === 'VERIFIED' ) {
-                               return true;
-                       } elseif ( $response === 'INVALID' ) {
-                               return false;
-                       }
-                       // Must be an HTML page, keep trying.
+               if ( $response['body'] === 'VERIFIED' ) {
+                       return true;
+               } elseif ( $response['body'] === 'INVALID' ) {
+                       return false;
                }
 
-               throw new RuntimeException( 'Failed to validate message after ' 
.
-                       $max_attempts . ' attempts: ' . print_r( $post_fields, 
true ) );
+               throw new LogicException(
+                       'EnumValidator should not allow reaching this point!'
+               );
        }
 
 }
diff --git a/wikimedia/smash-pig/PaymentProviders/PayPal/RefundMessage.php 
b/wikimedia/smash-pig/PaymentProviders/PayPal/RefundMessage.php
index 98227a6..1520594 100644
--- a/wikimedia/smash-pig/PaymentProviders/PayPal/RefundMessage.php
+++ b/wikimedia/smash-pig/PaymentProviders/PayPal/RefundMessage.php
@@ -7,20 +7,25 @@
        public static function normalizeMessage( &$message, $ipnMessage ) {
                $message['gateway_refund_id'] = $ipnMessage['txn_id'];
                $message['gross_currency'] = $ipnMessage['mc_currency'];
-               if ( isset( $message['type'] ) &&
-                       $message['type'] === 'chargeback_settlement' ) {
+               if ( isset( $message['txn_type'] ) && $message['txn_type'] === 
'adjustment' ) {
                        $message['type'] = 'chargeback';
-               } else {
+
+                       // For chargebacks, express checkout sets the 'invoice' 
field
+                       if ( isset( $ipnMessage['invoice'] ) ) {
+                               $message['gateway'] = 'paypal_ec';
+                       } else {
+                               $message['gateway'] = 'paypal';
+                       }
+               } elseif ( isset( $ipnMessage['reason_code'] ) && 
$ipnMessage['reason_code'] === 'refund' ) {
                        $message['type'] = 'refund';
-               }
 
-               // Express checkout puts a description in transaction_subject, 
Legacy puts a contribution
-               // tracking ID there. Chargebacks don't set the field at all.
-               if ( isset( $ipnMessage['transaction_subject'] ) && 
!is_numeric( $ipnMessage['transaction_subject'] ) ) {
-                       $message['gateway'] = 'paypal_ec';
-               } else {
-                       $message['gateway'] = 'paypal';
+                       // For refunds, express checkout puts a description in 
transaction_subject,
+                       // but legacy puts a contribution tracking ID there.
+                       if ( isset( $ipnMessage['transaction_subject'] ) && 
!is_numeric( $ipnMessage['transaction_subject'] ) ) {
+                               $message['gateway'] = 'paypal_ec';
+                       } else {
+                               $message['gateway'] = 'paypal';
+                       }
                }
-
        }
 }
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement.json
 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement.json
index d96bde3..bfdbb40 100644
--- 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement.json
+++ 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement.json
@@ -1,22 +1,18 @@
 {
-       "payment_date": "11:55:22 Sep 28, 2016 PDT",
+       "payment_date": "05:32:08 Jun 30, 2017 PDT",
+       "txn_id": "91N12345TB8654321",
        "payment_status": "Completed",
-       "txn_type": "adjustment",
-       "parent_txn_id": "XXXXXXXXXXXXXXXXX",
-       "txn_id": "YYYYYYYYYYYYYYYYY",
        "mc_currency": "USD",
-       "reason_code": "chargeback_settlement",
-       "email": "[email protected]",
-       "mc_gross": "-10.00",
-       "gateway": "paypal",
-       "notify_version": "3.8",
-       "payment_gross": "-10.00",
+       "parent_txn_id": "2JP12345X654321V",
+       "notify_version": 3.8,
+       "custom": 47470083,
+       "txn_type": "adjustment",
+       "charset": "UTF-8",
+       "mc_gross": -1.00,
+       "payment_gross": -1.00,
+       "payer_email": "[email protected]",
+       "verify_sign": 
"AZfnGop49sf6bIkvk-UIIwAT3vE6AqUlokuReG4Znr85ICxgRDn1rO8s",
+       "payer_id": "AA987ASDFGHUW8",
        "payer_status": "verified",
-       "payment_fee": "-20.00",
-       "source_name": "SmashPig",
-       "source_type": "listener",
-       "source_host": "secrethost",
-       "source_run_id": "12423",
-       "source_version": "9999999999999999999999999999999999999999",
-       "source_enqueued_time": "1476796977"
+       "ipn_track_id": "28eec9b3c7857"
 }
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec.json
 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec.json
new file mode 100644
index 0000000..daef6ad
--- /dev/null
+++ 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec.json
@@ -0,0 +1,19 @@
+{
+       "txn_type" : "adjustment",
+       "payment_date" : "06:25:22 Jul 10, 2017 PDT",
+       "payment_gross" : -5.00,
+       "mc_currency" : "USD",
+       "verify_sign" : 
"A21kmBLyRcQpDSjYvmswlm3ckjahAZ-SbzbHuzDA0wERKwHSrBlTr9cY",
+       "payer_status" : "verified",
+       "payer_email" : "[email protected]",
+       "txn_id" : "91N12345TB8654321",
+       "parent_txn_id" : "2JP12345X654321V",
+       "payer_id" : "AA987ASDFGHUW8",
+       "invoice" : 47896777.0,
+       "payment_status" : "Completed",
+       "mc_gross" : -5.00,
+       "custom" : 47896777,
+       "charset" : "UTF-8",
+       "notify_version" : 3.8,
+       "ipn_track_id" : "f008d5768b47c"
+}
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec_transformed.json
 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec_transformed.json
new file mode 100644
index 0000000..16ace8a
--- /dev/null
+++ 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_ec_transformed.json
@@ -0,0 +1,15 @@
+{
+       "date": 1499693122,
+       "gateway_parent_id": "2JP12345X654321V",
+       "gateway_txn_id": "91N12345TB8654321",
+       "currency": "USD",
+       "type": "chargeback",
+       "contribution_tracking_id": 47896777,
+       "gross": -5,
+       "order_id": 47896777,
+       "gateway_refund_id": "91N12345TB8654321",
+       "gross_currency": "USD",
+       "gateway": "paypal_ec",
+       "txn_type": "adjustment",
+       "email": "[email protected]"
+}
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_transformed.json
 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_transformed.json
new file mode 100644
index 0000000..73d8416
--- /dev/null
+++ 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/Data/chargeback_settlement_transformed.json
@@ -0,0 +1,15 @@
+{
+       "date": 1498825928,
+       "gateway_parent_id": "2JP12345X654321V",
+       "gateway_txn_id": "91N12345TB8654321",
+       "currency": "USD",
+       "type": "chargeback",
+       "contribution_tracking_id": 47470083,
+       "gross": -1,
+       "order_id": 47470083,
+       "gateway_refund_id": "91N12345TB8654321",
+       "gross_currency": "USD",
+       "gateway": "paypal",
+       "txn_type": "adjustment",
+       "email": "[email protected]"
+}
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/MockPayPalPaymentsAPI.php 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/MockPayPalPaymentsAPI.php
deleted file mode 100644
index e35a52c..0000000
--- 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/MockPayPalPaymentsAPI.php
+++ /dev/null
@@ -1,15 +0,0 @@
-<?php namespace SmashPig\PaymentProviders\PayPal\Tests;
-
-use SmashPig\PaymentProviders\PayPal\PayPalPaymentsAPI;
-
-class MockPayPalPaymentsAPI extends PayPalPaymentsAPI {
-       protected function curl ( $ch, $post_fields ) {
-               if ( CaptureIncomingMessageTest::$fail_verification ) {
-                       return 'INVALID';
-               }
-               if ( CaptureIncomingMessageTest::$paypal_is_broken ) {
-                       return 'lkjasjdhfiuasdgjgbasdd';
-               }
-               return 'VERIFIED';
-       }
-}
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/PayPalTestConfiguration.php 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/PayPalTestConfiguration.php
deleted file mode 100644
index ca0b15d..0000000
--- 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/PayPalTestConfiguration.php
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php
-namespace SmashPig\PaymentProviders\PayPal\Tests;
-
-use SmashPig\Core\GlobalConfiguration;
-use SmashPig\Tests\TestingProviderConfiguration;
-
-class PayPalTestConfiguration extends TestingProviderConfiguration {
-
-       public static function get( GlobalConfiguration $globalConfig ) {
-               return self::createForProviderWithOverrideFile(
-                       'paypal',
-                       __DIR__ . '/config_test.yaml',
-                       $globalConfig
-               );
-       }
-
-}
diff --git a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/config_test.yaml 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/config_test.yaml
deleted file mode 100644
index fc0fdf7..0000000
--- a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/config_test.yaml
+++ /dev/null
@@ -1,2 +0,0 @@
-api:
-    class: SmashPig\PaymentProviders\PayPal\Tests\MockPayPalPaymentsAPI
diff --git 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/phpunit/CaptureIncomingMessageTest.php
 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/phpunit/CaptureIncomingMessageTest.php
index 2b08ef4..1cfa9db 100644
--- 
a/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/phpunit/CaptureIncomingMessageTest.php
+++ 
b/wikimedia/smash-pig/PaymentProviders/PayPal/Tests/phpunit/CaptureIncomingMessageTest.php
@@ -2,6 +2,7 @@
 namespace SmashPig\PaymentProviders\PayPal\Tests;
 
 use SmashPig\Core\Context;
+use SmashPig\Core\GlobalConfiguration;
 use SmashPig\Core\ProviderConfiguration;
 use SmashPig\CrmLink\Messages\SourceFields;
 use SmashPig\PaymentProviders\PayPal\Listener;
@@ -9,6 +10,7 @@
 use SmashPig\Core\Http\Response;
 use SmashPig\Core\Http\Request;
 use SmashPig\Core\DataStores\JsonSerializableObject;
+use SmashPig\Tests\TestingProviderConfiguration;
 
 /**
  * Test the IPN listener which receives messages, stores and processes them.
@@ -17,13 +19,18 @@
 class CaptureIncomingMessageTest extends BaseSmashPigUnitTestCase {
 
        /**
-        * @var ProviderConfiguration
+        * @var GlobalConfiguration
         */
        public $config;
 
-       static $fail_verification = false;
-       static $paypal_is_broken = false;
+       /**
+        * @var ProviderConfiguration
+        */
+       public $providerConfig;
 
+       /**
+        * @var array
+        */
        // filename and the queue it should get dropped in
        static $message_data = array(
                'web_accept.json' => 'donations',
@@ -36,6 +43,7 @@
                'refund_ec.json' => 'refund',
                'refund_recurring_ec.json' => 'refund',
                'chargeback_settlement.json' => 'refund',
+               'chargeback_settlement_ec.json' => 'refund',
                // this should not actually get written to
                // TODO 'new_case.json' => 'no-op',
        );
@@ -43,14 +51,11 @@
        public function setUp() {
                parent::setUp();
                $this->config = Context::get()->getGlobalConfiguration();
-               Context::get()->setProviderConfiguration(
-                       PayPalTestConfiguration::get( $this->config )
-               );
+               $this->providerConfig = $this->setProviderConfiguration( 
'paypal' );
        }
 
        public function tearDown() {
-               self::$fail_verification = false;
-               self::$paypal_is_broken = false;
+               $this->providerConfig->overrideObjectInstance( 'curl/wrapper', 
null );
                parent::tearDown();
        }
 
@@ -84,10 +89,21 @@
                return $listener->execute( $request, $response );
        }
 
+       protected function getCurlMock( $returnString ) {
+               $wrapper = $this->getMock( 'SmashPig\Core\Http\CurlWrapper' );
+               $wrapper->method( 'execute' )
+                       ->willReturn( array(
+                               'body' => $returnString
+                       ) );
+               $this->providerConfig->overrideObjectInstance( 'curl/wrapper', 
$wrapper );
+               return $wrapper;
+       }
+
        /**
         * @dataProvider messageProvider
         */
        public function testCapture( $msg ) {
+               $this->getCurlMock( 'VERIFIED' );
                $this->capture( $msg['payload'] );
 
                $jobQueue = $this->config->object( 'data-store/jobs-paypal' );
@@ -109,6 +125,7 @@
         * @dataProvider messageProvider
         */
        public function testConsume( $msg ) {
+               $this->getCurlMock( 'VERIFIED' );
                $this->capture( $msg['payload'] );
 
                $jobQueue = $this->config->object( 'data-store/jobs-paypal' );
@@ -146,16 +163,23 @@
        }
 
        public function testFailedVerification() {
-               self::$fail_verification = true;
+               $this->getCurlMock( 'INVALID' );
                $jobMessage = array( 'txn_type' => 'fail' );
                $this->assertFalse( $this->capture( $jobMessage ) );
        }
 
-       // FIXME: not really testing anything. Would like to verify that it 
tried
-       // N times. Bubble that information up somehow.
-       public function testPayPalIsBroken() {
-               self::$paypal_is_broken = true;
-               $jobMessage = array( 'txn_type' => 'fail' );
-               $this->assertFalse( $this->capture( $jobMessage ) );
+       public function testRetryValidator() {
+               $validator = $this->providerConfig->object( 'curl/validator' );
+               $response = array(
+                       'status' => 200,
+                       'headers' => array(),
+                       'body' => 
'<html><head><title>Fail</title></head><body>Oops</body></html>'
+               );
+               $this->assertTrue( $validator->shouldRetry( $response ) );
+               $response['body'] = 'INVALID';
+               $this->assertFalse( $validator->shouldRetry( $response ) );
+               $response['body'] = 'VERIFIED';
+               $this->assertFalse( $validator->shouldRetry( $response ) );
        }
+
 }
diff --git a/wikimedia/smash-pig/Tests/PendingDatabaseTest.php 
b/wikimedia/smash-pig/Tests/PendingDatabaseTest.php
index 8382eeb..cea7ffd 100644
--- a/wikimedia/smash-pig/Tests/PendingDatabaseTest.php
+++ b/wikimedia/smash-pig/Tests/PendingDatabaseTest.php
@@ -41,7 +41,7 @@
 
        public function testStoreMessage() {
                $message = self::getTestMessage();
-               $this->db->storeMessage( $message );
+               $id = $this->db->storeMessage( $message );
 
                // Confirm work without using the API.
                $pdo = $this->db->getDatabase();
@@ -53,7 +53,7 @@
                $this->assertEquals( 1, count( $rows ),
                        'One row stored and retrieved.' );
                $expected = array(
-                       'id' => '1',
+                       'id' => $id,
                        // NOTE: This is a db-specific string, sqlite3 in this 
case, and
                        // you'll have different formatting if using any other 
database.
                        'date' => '20160720001408',
diff --git a/wikimedia/smash-pig/config/paypal/main.yaml 
b/wikimedia/smash-pig/config/paypal/main.yaml
index 8ee159f..c95ebe1 100644
--- a/wikimedia/smash-pig/config/paypal/main.yaml
+++ b/wikimedia/smash-pig/config/paypal/main.yaml
@@ -109,3 +109,11 @@
             - refund
             # FIXME: if case_type=chargeback, then txn_type might be blank
             # and the message should go to the refund queue.
+curl:
+    validator:
+        class: SmashPig\Core\Http\EnumValidator
+        constructor-parameters:
+            -
+                - INVALID
+                - VERIFIED
+    retries: 7

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2766da66bf6ace0301f42a1984438a5bcf63ff22
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/vendor
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to