Ejegg has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/345696 )

Change subject: Break out unofficial iDEAL status lookup
......................................................................

Break out unofficial iDEAL status lookup

Maybe someone else can use this, but we can't!

Bug: T128692
Change-Id: Ib2475f7140abee78d9e89351465d4975507df363
---
M PaymentProviders/Ingenico/BankPaymentProvider.php
A PaymentProviders/Ingenico/IdealStatusProvider.php
M PaymentProviders/Ingenico/Tests/Data/availability.response
M PaymentProviders/Ingenico/Tests/phpunit/BankPaymentProviderTest.php
A PaymentProviders/Ingenico/Tests/phpunit/IdealStatusProviderTest.php
M SmashPig.yaml
6 files changed, 182 insertions(+), 44 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig 
refs/changes/96/345696/1

diff --git a/PaymentProviders/Ingenico/BankPaymentProvider.php 
b/PaymentProviders/Ingenico/BankPaymentProvider.php
index 4844bbc..87c034d 100644
--- a/PaymentProviders/Ingenico/BankPaymentProvider.php
+++ b/PaymentProviders/Ingenico/BankPaymentProvider.php
@@ -4,7 +4,6 @@
 
 use SmashPig\Core\Context;
 use Psr\Cache\CacheItemPoolInterface;
-use SmashPig\Core\Http\OutboundRequest;
 
 /**
  * Handle bank payments via Ingenico
@@ -24,15 +23,9 @@
         */
        protected $cache;
 
-       /**
-        * @var array()
-        */
-       protected $availabilityParameters;
-
        public function __construct( array $options = array() ) {
                parent::__construct( $options );
                $this->cacheParameters = $options['cache-parameters'];
-               $this->availabilityParameters = 
$options['availability-parameters'];
                // FIXME: provide objects in constructor
                $config = Context::get()->getConfiguration();
                $this->cache = $config->object( 'cache' );
@@ -52,7 +45,7 @@
                $cacheItem = $this->cache->getItem( $cacheKey );
 
                if ( !$cacheItem->isHit() || $this->shouldBeExpired( $cacheItem 
) ) {
-                       /*$query = array(
+                       $query = array(
                                'countryCode' => $country,
                                'currencyCode' => $currency
                        );
@@ -63,28 +56,7 @@
 
                        foreach ( $response['entries'] as $entry ) {
                                $banks[$entry['issuerId']] = 
$entry['issuerName'];
-                       }*/
-
-                       $banks = array();
-
-                       // HAAACK!
-                       // Use undocumented API to get availability straight 
from iDEAL,
-                       // until Ingenico can incorporate this into their 
directory
-                       if ( $country === 'NL' && $currency === 'EUR' ) {
-                               $url = $this->availabilityParameters['url'];
-                               $threshold = 
$this->availabilityParameters['threshold'];
-
-                               $request = new OutboundRequest( $url );
-                               $rawResponse = $request->execute();
-                               $response = json_decode( $rawResponse['body'], 
true );
-
-                               foreach ( $response['Issuers'] as $issuer ) {
-                                       if ( $issuer['Percent'] >= $threshold ) 
{
-                                               $banks[$issuer['BankId']] = 
$issuer['BankName'];
-                                       }
-                               }
                        }
-
                        $duration = $this->cacheParameters['duration'];
                        $cacheItem->set( array(
                                'value' => $banks,
diff --git a/PaymentProviders/Ingenico/IdealStatusProvider.php 
b/PaymentProviders/Ingenico/IdealStatusProvider.php
new file mode 100644
index 0000000..637207d
--- /dev/null
+++ b/PaymentProviders/Ingenico/IdealStatusProvider.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace SmashPig\PaymentProviders\Ingenico;
+
+use Psr\Cache\CacheItemPoolInterface;
+use SmashPig\Core\Context;
+use SmashPig\Core\Http\OutboundRequest;
+
+/**
+ * Uses an unofficial API to look up status of iDEAL banks
+ * see https://availability.ideal.nl
+ */
+class IdealStatusProvider {
+
+       /**
+        * @var array()
+        */
+       protected $cacheParameters;
+
+       /**
+        * @var CacheItemPoolInterface
+        */
+       protected $cache;
+
+       /**
+        * @var string
+        */
+       protected $availabilityUrl;
+
+       public function __construct( array $options = array() ) {
+               $this->cacheParameters = $options['cache-parameters'];
+               $this->availabilityUrl = $options['availability-url'];
+               // FIXME: provide objects in constructor
+               $config = Context::get()->getConfiguration();
+               $this->cache = $config->object( 'cache' );
+       }
+
+       /**
+        * Look up bank status
+        * @return array Keys are bank codes, values are names
+        */
+       public function getBankStatus() {
+               $cacheKey = $this->cacheParameters['key'];
+               $cacheItem = $this->cache->getItem( $cacheKey );
+
+               if ( !$cacheItem->isHit() ) {
+
+                       $banks = array();
+
+                       $url = $this->availabilityUrl;
+
+                       $request = new OutboundRequest( $url );
+                       $rawResponse = $request->execute();
+                       $response = json_decode( $rawResponse['body'], true );
+
+                       foreach ( $response['Issuers'] as $issuer ) {
+                               $banks[$issuer['BankId']] = array(
+                                       'name' => $issuer['BankName'],
+                                       'availability' => $issuer['Percent']
+                               );
+                       }
+
+                       $duration = $this->cacheParameters['duration'];
+
+                       $cacheItem->set( array(
+                               'value' => $banks,
+                               # TODO: determine timezone and parse this 
format: '22-3-2017, 23:40'
+                               'lastupdate' => $response['LastUpdate'],
+                               'expiration' => time() + $duration
+                       ) );
+                       $cacheItem->expiresAfter( $duration );
+                       $this->cache->save( $cacheItem );
+               }
+               $cached = $cacheItem->get();
+               return $cached['value'];
+       }
+}
diff --git a/PaymentProviders/Ingenico/Tests/Data/availability.response 
b/PaymentProviders/Ingenico/Tests/Data/availability.response
index 74f0def..349fb7c 100644
--- a/PaymentProviders/Ingenico/Tests/Data/availability.response
+++ b/PaymentProviders/Ingenico/Tests/Data/availability.response
@@ -11,4 +11,4 @@
 X-Powered-By: ASP.NET
 Date: Wed, 22 Mar 2017 22:42:05 GMT
 
-{"Issuers":[{"BankId":"ABNANL2A","BankName":"ABN 
AMRO","LogoPath":"/Content/icons/banks/abn.png","Status":"Green","Percent":"40","Details":"","MoreDetails":"","LogoStatus":"/Content/icons/green.png","URL":"/History?bank=ABN_AMRO"},{"BankId":"INGBNL2A","BankName":"Issuer
 Simulation V3 - 
ING","LogoPath":"/Content/icons/banks/sim.png","Status":"Green","Percent":"100","Details":"","MoreDetails":"","LogoStatus":"/Content/icons/green.png","URL":"/History?bank=Simulated_Bank"}],"Message":"","IssuerMessageCalculationFailed":false,"LastUpdate":"22-3-2017,
 23:40","AllGreen":true}
+{"Issuers":[{"BankId":"ABNANL2A","BankName":"ABN 
AMRO","LogoPath":"/Content/icons/banks/abn.png","Status":"Yellow","Percent":"40","Details":"","MoreDetails":"","LogoStatus":"/Content/icons/green.png","URL":"/History?bank=ABN_AMRO"},{"BankId":"INGBNL2A","BankName":"Issuer
 Simulation V3 - 
ING","LogoPath":"/Content/icons/banks/sim.png","Status":"Green","Percent":"100","Details":"","MoreDetails":"","LogoStatus":"/Content/icons/green.png","URL":"/History?bank=Simulated_Bank"}],"Message":"","IssuerMessageCalculationFailed":false,"LastUpdate":"22-3-2017,
 23:40","AllGreen":true}
diff --git 
a/PaymentProviders/Ingenico/Tests/phpunit/BankPaymentProviderTest.php 
b/PaymentProviders/Ingenico/Tests/phpunit/BankPaymentProviderTest.php
index 18ef9e7..d9a2e3e 100644
--- a/PaymentProviders/Ingenico/Tests/phpunit/BankPaymentProviderTest.php
+++ b/PaymentProviders/Ingenico/Tests/phpunit/BankPaymentProviderTest.php
@@ -38,18 +38,13 @@
                        'cache-parameters' => array(
                                'duration' => 10,
                                'key-base' => 'BLAH_BLAH'
-                       ),
-                       'availability-parameters' => array(
-                               'url' => 
'http://example.org/undocumented/api/GetIssuers',
-                               'threshold' => 60
                        )
                ) );
                parent::setUp();
        }
 
        public function testGetBankList() {
-               //$this->setUpResponse( 'productDirectory', 200 );
-               $this->setUpResponse( 'availability', 200 );
+               $this->setUpResponse( 'productDirectory', 200 );
                $results = $this->provider->getBankList( 'NL', 'EUR' );
                $this->assertEquals(
                        array(
@@ -60,8 +55,7 @@
        }
 
        public function testCacheBankList() {
-               //$this->setUpResponse( 'productDirectory', 200 );
-               $this->setUpResponse( 'availability', 200 );
+               $this->setUpResponse( 'productDirectory', 200 );
                $this->curlWrapper->expects( $this->once() )
                        ->method( 'execute' );
                $results = $this->provider->getBankList( 'NL', 'EUR' );
@@ -85,8 +79,7 @@
                        true
                );
                $this->cache->save( $cacheItem );
-               //$this->setUpResponse( 'productDirectory', 200 );
-               $this->setUpResponse( 'availability', 200 );
+               $this->setUpResponse( 'productDirectory', 200 );
                $this->curlWrapper->expects( $this->once() )
                        ->method( 'execute' );
                $results = $this->provider->getBankList( 'NL', 'EUR' );
diff --git 
a/PaymentProviders/Ingenico/Tests/phpunit/IdealStatusProviderTest.php 
b/PaymentProviders/Ingenico/Tests/phpunit/IdealStatusProviderTest.php
new file mode 100644
index 0000000..e99e6d7
--- /dev/null
+++ b/PaymentProviders/Ingenico/Tests/phpunit/IdealStatusProviderTest.php
@@ -0,0 +1,90 @@
+<?php
+namespace SmashPig\PaymentProviders\Ingenico\Tests;
+
+use PHPUnit_Framework_MockObject_MockObject;
+use Psr\Cache\CacheItemPoolInterface;
+use SmashPig\Core\Http\CurlWrapper;
+use SmashPig\PaymentProviders\Ingenico\BankPaymentProvider;
+use SmashPig\PaymentProviders\Ingenico\IdealStatusProvider;
+use SmashPig\Tests\BaseSmashPigUnitTestCase;
+
+/**
+ * @group Ingenico
+ */
+class IdealStatusProviderTest extends BaseSmashPigUnitTestCase {
+
+       /**
+        * @var PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $curlWrapper;
+
+       /**
+        * @var IdealStatusProvider
+        */
+       protected $provider;
+
+       /**
+        * @var CacheItemPoolInterface
+        */
+       protected $cache;
+
+       public function setUp() {
+
+               $config = $this->setConfig( 'ingenico' );
+               $this->curlWrapper = $this->getMock( 
'\SmashPig\Core\Http\CurlWrapper' );
+               $this->cache = $config->object( 'cache', true );
+               $config->overrideObjectInstance( 'curl/wrapper', 
$this->curlWrapper );
+               $config->object( 'cache' )->clear();
+               $this->provider = new IdealStatusProvider( array(
+                       'cache-parameters' => array(
+                               'duration' => 10,
+                               'key' => 'BLAH_BLAH'
+                       ),
+                       'availability-url' => 
'http://example.org/undocumented/api/GetIssuers'
+               ) );
+               $contents = file_get_contents( __DIR__ . 
"/../Data/availability.response" );
+               $parsed = CurlWrapper::parseResponse(
+                       $contents, array( 'http_code' => 200 )
+               );
+               $this->curlWrapper->method( 'execute' )->willReturn( $parsed );
+               parent::setUp();
+       }
+
+       public function testGetBankStatus() {
+               $results = $this->provider->getBankStatus();
+               $this->assertEquals(
+                       array(
+                               'ABNANL2A' => array(
+                                       'name' => 'ABN AMRO',
+                                       'availability' => '40',
+                               ),
+                               'INGBNL2A' => array(
+                                       'name' => 'Issuer Simulation V3 - ING',
+                                       'availability' => '100',
+                               )
+                       ),
+                       $results
+               );
+       }
+
+       public function testCacheBankStatus() {
+               $this->curlWrapper->expects( $this->once() )
+                       ->method( 'execute' );
+               $results = $this->provider->getBankStatus();
+               $this->assertEquals(
+                       array(
+                               'ABNANL2A' => array(
+                                       'name' => 'ABN AMRO',
+                                       'availability' => '40',
+                               ),
+                               'INGBNL2A' => array(
+                                       'name' => 'Issuer Simulation V3 - ING',
+                                       'availability' => '100',
+                               )
+                       ),
+                       $results
+               );
+               $cachedResults = $this->provider->getBankStatus();
+               $this->assertEquals( $results, $cachedResults );
+       }
+}
diff --git a/SmashPig.yaml b/SmashPig.yaml
index 1688b5e..19e2f1b 100644
--- a/SmashPig.yaml
+++ b/SmashPig.yaml
@@ -467,10 +467,16 @@
                     cache-parameters:
                         duration: 900
                         key-base: SMASHPIG_INGENICO_IDEAL_BANK_LIST
-                    availability-parameters:
-                        url: https://availability.ideal.nl/api/api/GetIssuers
-                        # percentage availability below which issuers are 
omitted
-                        threshold: 50
+
+    ideal-status-provider:
+        class: SmashPig\PaymentProviders\Ingenico\BankPaymentProvider
+        constructor-parameters:
+            -
+                cache-parameters:
+                    duration: 900
+                    key: SMASHPIG_IDEAL_BANK_STATUS
+                availability-url: 
https://availability.ideal.nl/api/api/GetIssuers
+
 
 # deprecated, delete when projects using SmashPig rename adaptors
 globalcollect:

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib2475f7140abee78d9e89351465d4975507df363
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>

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

Reply via email to