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

Change subject: Move Ingenico base API wrapper functions to own class
......................................................................

Move Ingenico base API wrapper functions to own class

Tests for higher level functions shouldn't have to mock all the way
down at the cURL response level.

Bug: T158374
Change-Id: Icafa897a0a79430170a35b5c85427527132e5baf
---
A PaymentProviders/Ingenico/Api.php
M PaymentProviders/Ingenico/BankPaymentProvider.php
M PaymentProviders/Ingenico/IngenicoPaymentProvider.php
M SmashPig.yaml
4 files changed, 74 insertions(+), 47 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig 
refs/changes/03/338303/1

diff --git a/PaymentProviders/Ingenico/Api.php 
b/PaymentProviders/Ingenico/Api.php
new file mode 100644
index 0000000..2ef21d2
--- /dev/null
+++ b/PaymentProviders/Ingenico/Api.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace SmashPig\PaymentProviders\Ingenico;
+
+use DateTime;
+use DateTimeZone;
+use SmashPig\Core\Context;
+use SmashPig\Core\Http\OutboundRequest;
+
+/**
+ * Prepares and sends requests to the Ingenico Connect API.
+ */
+class Api {
+
+       const API_VERSION = 'v1';
+
+       /**
+        * @var Authenticator
+        */
+       protected $authenticator;
+       protected $baseUrl;
+       protected $merchantId;
+
+       public function __construct( $baseUrl, $merchantId ) {
+               // Ensure trailing slash
+               if ( substr( $baseUrl, -1 ) !== '/' ) {
+                       $baseUrl .= '/';
+               }
+               $this->baseUrl = $baseUrl;
+               $this->merchantId = $merchantId;
+               // FIXME: provide objects in constructor
+               $config = Context::get()->getConfiguration();
+               $this->authenticator = $config->object( 'authenticator' );
+       }
+
+       public function makeApiCall( $path, $method = 'POST', $data = null ) {
+               if ( is_array( $data ) ) {
+                       // FIXME: this is weird, maybe OutboundRequest should 
handle this part
+                       if ( $method === 'GET' ) {
+                               $path .= '?' . http_build_query( $data );
+                               $data = null;
+                       } else {
+                               $data = json_encode( $data );
+                       }
+               }
+               $url = $this->baseUrl . self::API_VERSION . 
"/{$this->merchantId}/$path";
+               $request = new OutboundRequest( $url, $method );
+               $request->setBody( $data );
+               if ( $method !== 'GET' ) {
+                       $request->setHeader( 'Content-Type', 'application/json' 
);
+               }
+               // Set date header manually so we can use it in signature 
generation
+               $date = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
+               $request->setHeader( 'Date', $date->format( 'D, d M Y H:i:s T' 
) );
+
+               // set more headers...
+
+               $this->authenticator->signRequest( $request );
+
+               $response = $request->execute();
+               // TODO error handling
+               return $response;
+       }
+}
diff --git a/PaymentProviders/Ingenico/BankPaymentProvider.php 
b/PaymentProviders/Ingenico/BankPaymentProvider.php
index 8803ae9..b7b0e05 100644
--- a/PaymentProviders/Ingenico/BankPaymentProvider.php
+++ b/PaymentProviders/Ingenico/BankPaymentProvider.php
@@ -30,7 +30,7 @@
                                'currencyCode' => $currency
                        );
                        $path = "products/$productId/directory";
-                       $response = $this->makeApiCall( $path, 'GET', $query );
+                       $response = $this->api->makeApiCall( $path, 'GET', 
$query );
 
                        // TODO: base class should probably decode
                        $decoded = json_decode( $response['body'] );
diff --git a/PaymentProviders/Ingenico/IngenicoPaymentProvider.php 
b/PaymentProviders/Ingenico/IngenicoPaymentProvider.php
index 3ad939a..9fe7ad2 100644
--- a/PaymentProviders/Ingenico/IngenicoPaymentProvider.php
+++ b/PaymentProviders/Ingenico/IngenicoPaymentProvider.php
@@ -2,12 +2,7 @@
 
 namespace SmashPig\PaymentProviders\Ingenico;
 
-use DateTime;
-use DateTimeZone;
 use SmashPig\Core\Context;
-use SmashPig\Core\Configuration;
-use SmashPig\Core\Http\OutboundRequest;
-use SmashPig\Core\UtcDate;
 
 /**
  * Base class for Ingenico payments. Each payment product group should get
@@ -15,48 +10,12 @@
  */
 abstract class IngenicoPaymentProvider {
 
-       const API_VERSION = 'v1';
-       /**
-        * @var Configuration
-        */
+       protected $api;
        protected $config;
 
-       public function __construct() {
+       public function __construct( $options = array() ) {
+               // FIXME: provide objects in constructor
                $this->config = Context::get()->getConfiguration();
-       }
-
-       protected function makeApiCall( $path, $method = 'POST', $data = null ) 
{
-               if ( is_array( $data ) ) {
-                       // FIXME: this is weird, maybe OutboundRequest should 
handle this part
-                       if ( $method === 'GET' ) {
-                               $path .= '?' . http_build_query( $data );
-                               $data = null;
-                       } else {
-                               $data = json_encode( $data );
-                       }
-               }
-               $base = $this->config->val( 'base-url' );
-               if ( substr( $base, -1 ) !== '/' ) {
-                       $base .= '/';
-               }
-               $merchantId = $this->config->val( 'credentials/merchant-id' );
-               $url = $base . self::API_VERSION . "/$merchantId/$path";
-               $request = new OutboundRequest( $url, $method );
-               $request->setBody( $data );
-               if ( $method !== 'GET' ) {
-                       $request->setHeader( 'Content-Type', 'application/json' 
);
-               }
-               // Set date header manually so we can use it in signature 
generation
-               $date = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
-               $request->setHeader( 'Date', $date->format( 'D, d M Y H:i:s T' 
) );
-
-               // set more headers...
-
-               $authenticator = $this->config->object( 'authenticator' );
-               $authenticator->signRequest( $request );
-
-               $response = $request->execute();
-               // TODO error handling
-               return $response;
+               $this->api = $this->config->object( 'api' );
        }
 }
diff --git a/SmashPig.yaml b/SmashPig.yaml
index ce84b07..229d7dc 100644
--- a/SmashPig.yaml
+++ b/SmashPig.yaml
@@ -442,7 +442,11 @@
         listener:
             class: 
SmashPig\PaymentProviders\GlobalCollect\GlobalCollectListener
 
-    base-url: 'https://api-sandbox.globalcollect.com/'
+    api:
+        class: SmashPig\PaymentProviders\Ingenico\Api
+        constructor-parameters:
+            - 'https://api-sandbox.globalcollect.com/'
+            - 1234 # numeric merchant ID
 
     credentials:
         merchant-id: 1234

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icafa897a0a79430170a35b5c85427527132e5baf
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