From dcbf8eb2fd488c02eab8fde69b14470aa9505d3a Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 19 Jan 2016 22:01:01 +0100 Subject: [PATCH 01/51] Initial upgrade to guzzle 6 --- composer.json | 12 +++-- src/Omnipay/Common/AbstractGateway.php | 29 +++++----- src/Omnipay/Common/GatewayFactory.php | 10 ++-- src/Omnipay/Common/Http/Client.php | 54 +++++++++++++++++++ src/Omnipay/Common/Http/ClientInterface.php | 40 ++++++++++++++ .../Common/Message/AbstractRequest.php | 14 ++--- src/Omnipay/Omnipay.php | 5 +- tests/Omnipay/Common/AbstractGatewayTest.php | 4 +- 8 files changed, 134 insertions(+), 34 deletions(-) create mode 100644 src/Omnipay/Common/Http/Client.php create mode 100644 src/Omnipay/Common/Http/ClientInterface.php diff --git a/composer.json b/composer.json index cb6cabd7..c82785d7 100644 --- a/composer.json +++ b/composer.json @@ -28,15 +28,19 @@ }, "require": { "php": ">=5.3.2", - "guzzle/guzzle": "~3.9", - "symfony/http-foundation": "~2.1" + "symfony/http-foundation": "~2.1|~3.0", + "psr/http-message": "~1.0", + "zendframework/zend-diactoros": "1.x", + "guzzlehttp/guzzle": "^6.1" }, "require-dev": { - "omnipay/tests": "~2.0" + "omnipay/tests": "~3.0" }, + "minimum-stability": "dev", + "prefer-stable": true, "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "3.0.x-dev" }, "gateways": [ "AuthorizeNet_AIM", diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index 97014a0f..4a5ed8d7 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -5,11 +5,15 @@ namespace Omnipay\Common; -use Guzzle\Http\ClientInterface; -use Guzzle\Http\Client as HttpClient; +use GuzzleHttp\Client as GuzzleClient; +use Psr\Http\Message\ServerRequestInterface; +use Zend\Diactoros\ServerRequestFactory; +use Omnipay\Common\Http\ClientInterface; +use Omnipay\Common\Http\Client as HttpClient; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request as HttpRequest; + /** * Base payment gateway class * @@ -50,22 +54,22 @@ abstract class AbstractGateway implements GatewayInterface protected $parameters; /** - * @var \Guzzle\Http\ClientInterface + * @var ClientInterface */ protected $httpClient; /** - * @var \Symfony\Component\HttpFoundation\Request + * @var ServerRequestInterface */ protected $httpRequest; /** * Create a new gateway instance * - * @param ClientInterface $httpClient A Guzzle client to make API calls with - * @param HttpRequest $httpRequest A Symfony HTTP request object + * @param ClientInterface $httpClient A HTTP client to make API calls with + * @param ServerRequestInterface $httpRequest A HTTP request object */ - public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null) + public function __construct(ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) { $this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); $this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); @@ -331,21 +335,16 @@ protected function createRequest($class, array $parameters) */ protected function getDefaultHttpClient() { - return new HttpClient( - '', - array( - 'curl.options' => array(CURLOPT_CONNECTTIMEOUT => 60), - ) - ); + return new HttpClient(); } /** * Get the global default HTTP request. * - * @return HttpRequest + * @return ServerRequestInterface */ protected function getDefaultHttpRequest() { - return HttpRequest::createFromGlobals(); + return ServerRequestFactory::fromGlobals(); } } diff --git a/src/Omnipay/Common/GatewayFactory.php b/src/Omnipay/Common/GatewayFactory.php index f81ba667..e7dd2331 100644 --- a/src/Omnipay/Common/GatewayFactory.php +++ b/src/Omnipay/Common/GatewayFactory.php @@ -5,9 +5,9 @@ namespace Omnipay\Common; -use Guzzle\Http\ClientInterface; +use Omnipay\Common\Http\ClientInterface; use Omnipay\Common\Exception\RuntimeException; -use Symfony\Component\HttpFoundation\Request as HttpRequest; +use Psr\Http\Message\ServerRequestInterface; /** * Omnipay Gateway Factory class @@ -92,12 +92,12 @@ public function find() * Create a new gateway instance * * @param string $class Gateway name - * @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation - * @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation + * @param ClientInterface|null $httpClient A HTTP Client implementation + * @param ServerRequestInterface|null $httpRequest A HTTP Request implementation * @throws RuntimeException If no such gateway is found * @return GatewayInterface An object of class $class is created and returned */ - public function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null) + public function create($class, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) { $class = Helper::getGatewayClassName($class); diff --git a/src/Omnipay/Common/Http/Client.php b/src/Omnipay/Common/Http/Client.php new file mode 100644 index 00000000..e7032c61 --- /dev/null +++ b/src/Omnipay/Common/Http/Client.php @@ -0,0 +1,54 @@ +guzzle = $client ?: new GuzzleClient(); + } + + /** + * @param RequestInterface $request + * @return ResponseInterface + */ + public function send(RequestInterface $request) + { + return $this->guzzle->send($request); + } + + /** + * @param $method + * @param $uri + * @param array $headers + * @param null $body + * @return ResponseInterface + */ + public function request($method, $uri, array $headers = [], $body = null) + { + $request = $this->createRequest($method, $uri, $headers, $body); + + return $this->send($request); + } + + /** + * @param string $method + * @param string $uri + * @param array $headers + * @param null $body + * @return RequestInterface + */ + public function createRequest($method, $uri, array $headers = [], $body = null) + { + return new Request($method, $uri, $headers, $body); + } +} \ No newline at end of file diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php new file mode 100644 index 00000000..02fed8b9 --- /dev/null +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -0,0 +1,40 @@ +httpClient = $httpClient; $this->httpRequest = $httpRequest; diff --git a/src/Omnipay/Omnipay.php b/src/Omnipay/Omnipay.php index fb064e7e..2094df55 100644 --- a/src/Omnipay/Omnipay.php +++ b/src/Omnipay/Omnipay.php @@ -6,6 +6,9 @@ namespace Omnipay; use Omnipay\Common\GatewayFactory; +use Omnipay\Common\GatewayInterface; +use Omnipay\Common\Http\ClientInterface; +use Psr\Http\Message\ServerRequestInterface; /** * Omnipay class @@ -46,7 +49,7 @@ * @method static array find() * @method static array getSupportedGateways() * @codingStandardsIgnoreStart - * @method static \Omnipay\Common\GatewayInterface create(string $class, \Guzzle\Http\ClientInterface $httpClient = null, \Symfony\Component\HttpFoundation\Request $httpRequest = null) + * @method static GatewayInterface create(string $class, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) * @codingStandardsIgnoreEnd * * @see Omnipay\Common\GatewayFactory diff --git a/tests/Omnipay/Common/AbstractGatewayTest.php b/tests/Omnipay/Common/AbstractGatewayTest.php index 71cb83fc..dca67c26 100755 --- a/tests/Omnipay/Common/AbstractGatewayTest.php +++ b/tests/Omnipay/Common/AbstractGatewayTest.php @@ -18,8 +18,8 @@ public function setUp() public function testConstruct() { $this->gateway = new AbstractGatewayTest_MockAbstractGateway; - $this->assertInstanceOf('\Guzzle\Http\Client', $this->gateway->getProtectedHttpClient()); - $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Request', $this->gateway->getProtectedHttpRequest()); + $this->assertInstanceOf('\Omnipay\Common\Http\Client', $this->gateway->getProtectedHttpClient()); + $this->assertInstanceOf('\Psr\Http\Message\ServerRequestInterface', $this->gateway->getProtectedHttpRequest()); $this->assertSame(array(), $this->gateway->getParameters()); } From 9b156bf709d716742e32e9134e91e908edb36a60 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 20 Jan 2016 09:10:35 +0100 Subject: [PATCH 02/51] Bump composer and travis to php 5.5.9 --- .travis.yml | 24 ++++++++++++++---------- composer.json | 12 ++++++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9adbf173..764a9904 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ language: php php: - - 5.3 - - 5.4 + - 5.5.9 - 5.5 - 5.6 - 7.0 @@ -13,14 +12,19 @@ php: sudo: false env: - - SYMFONY_VERSION="2.1" GUZZLE_VERSION="3.1" - - SYMFONY_VERSION="2.*" GUZZLE_VERSION="3.1" - - SYMFONY_VERSION="2.1" GUZZLE_VERSION="3.*" - - SYMFONY_VERSION="2.*" GUZZLE_VERSION="3.*" + global: + - setup=basic -before_script: - - composer require symfony/http-foundation:${SYMFONY_VERSION} --no-update - - composer require guzzle/guzzle:${GUZZLE_VERSION} --no-update - - composer install -n --dev --prefer-source +matrix: + include: + - php: 5.5.9 + env: setup=lowest + - php: 5.5.9 + env: setup=stable + +install: + - if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-source; fi + - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-stable; fi + - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-lowest --prefer-stable; fi script: vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpunit --coverage-text diff --git a/composer.json b/composer.json index c82785d7..076fcd82 100644 --- a/composer.json +++ b/composer.json @@ -27,14 +27,14 @@ "classmap": ["src/Omnipay/Omnipay.php"] }, "require": { - "php": ">=5.3.2", - "symfony/http-foundation": "~2.1|~3.0", - "psr/http-message": "~1.0", - "zendframework/zend-diactoros": "1.x", - "guzzlehttp/guzzle": "^6.1" + "php": ">=5.5.9", + "symfony/http-foundation": "^2.1|^3.0", + "psr/http-message": "^1.0", + "zendframework/zend-diactoros": "^1.0", + "guzzlehttp/guzzle": "^6.0" }, "require-dev": { - "omnipay/tests": "~3.0" + "omnipay/tests": "^3.0" }, "minimum-stability": "dev", "prefer-stable": true, From d79f83c5f4914f25b1e4c87140cb38373ed98eb5 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 20 Jan 2016 10:04:52 +0100 Subject: [PATCH 03/51] Add stream/uri methods --- src/Omnipay/Common/AbstractGateway.php | 3 -- src/Omnipay/Common/Http/Client.php | 38 +++++++++++++++++---- src/Omnipay/Common/Http/ClientInterface.php | 24 ++++++++++--- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index 4a5ed8d7..7de1ca11 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -5,14 +5,11 @@ namespace Omnipay\Common; -use GuzzleHttp\Client as GuzzleClient; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\ServerRequestFactory; use Omnipay\Common\Http\ClientInterface; use Omnipay\Common\Http\Client as HttpClient; use Symfony\Component\HttpFoundation\ParameterBag; -use Symfony\Component\HttpFoundation\Request as HttpRequest; - /** * Base payment gateway class diff --git a/src/Omnipay/Common/Http/Client.php b/src/Omnipay/Common/Http/Client.php index e7032c61..05632e8e 100644 --- a/src/Omnipay/Common/Http/Client.php +++ b/src/Omnipay/Common/Http/Client.php @@ -6,7 +6,15 @@ use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\UriInterface; +/** + * Default Http Client + * + * Implementation of the Http ClientInterface by using Guzzle. + * + */ class Client implements ClientInterface { /** @var \GuzzleHttp\Client */ @@ -27,10 +35,10 @@ public function send(RequestInterface $request) } /** - * @param $method - * @param $uri + * @param string $method + * @param string|UriInterface $uri * @param array $headers - * @param null $body + * @param string|resource|StreamInterface $body * @return ResponseInterface */ public function request($method, $uri, array $headers = [], $body = null) @@ -42,13 +50,31 @@ public function request($method, $uri, array $headers = [], $body = null) /** * @param string $method - * @param string $uri + * @param string|UriInterface $uri * @param array $headers - * @param null $body + * @param string|resource|StreamInterface $body * @return RequestInterface */ public function createRequest($method, $uri, array $headers = [], $body = null) { return new Request($method, $uri, $headers, $body); } -} \ No newline at end of file + + /** + * @param string|UriInterface $uri + * @return UriInterface + */ + public function createUri($uri) + { + return \GuzzleHttp\Psr7\uri_for($uri); + } + + /** + * @param mixed $resource + * @return StreamInterface + */ + public function createStream($resource) + { + \GuzzleHttp\Psr7\stream_for($resource); + } +} diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php index 02fed8b9..4c9a93d0 100644 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -4,6 +4,8 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\UriInterface; /** * Http Interface @@ -21,20 +23,32 @@ interface ClientInterface public function send(RequestInterface $request); /** - * @param $method - * @param $uri + * @param string $method + * @param string|UriInterface $uri * @param array $headers - * @param null $body + * @param string|resource|StreamInterface $body * @return ResponseInterface */ public function request($method, $uri, array $headers = [], $body = null); /** * @param string $method - * @param string $uri + * @param string|UriInterface $uri * @param array $headers - * @param null $body + * @param string|resource|StreamInterface $body * @return RequestInterface */ public function createRequest($method, $uri, array $headers = [], $body = null); + + /** + * @param string|UriInterface $uri + * @return UriInterface + */ + public function createUri($uri); + + /** + * @param mixed $resource + * @return StreamInterface + */ + public function createStream($resource); } From 48e2d4f4f6e89d9ee8525a378e29c86b6bd5781b Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 20 Jan 2016 10:54:52 +0100 Subject: [PATCH 04/51] Add xml/json decode helpers --- src/Omnipay/Common/Helper.php | 59 +++++++++++++++++++ .../Common/Message/MessageInterface.php | 2 +- src/Omnipay/Omnipay.php | 2 +- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Omnipay/Common/Helper.php b/src/Omnipay/Common/Helper.php index 13637378..9ccc7de2 100644 --- a/src/Omnipay/Common/Helper.php +++ b/src/Omnipay/Common/Helper.php @@ -6,6 +6,7 @@ namespace Omnipay\Common; use InvalidArgumentException; +use Omnipay\Common\Exception\RuntimeException; /** * Helper class @@ -164,4 +165,62 @@ public static function toFloat($value) return (float)$value; } + + /** + * Parse the JSON response body and return an array + * + * Copied from Response->json() in Guzzle3 (copyright @mtdowling) + * @link https://github.com/guzzle/guzzle3/blob/v3.9.3/src/Guzzle/Http/Message/Response.php + * + * @param string $body + * @throws RuntimeException if the response body is not in JSON format + * @return array|string|int|bool|float + */ + public static function jsonDecode($body) + { + $data = json_decode((string) $body, true); + if (JSON_ERROR_NONE !== json_last_error()) { + throw new RuntimeException('Unable to parse response body into JSON: ' . json_last_error()); + } + return $data === null ? array() : $data; + } + + /** + * Parse the XML response body and return a \SimpleXMLElement. + * + * In order to prevent XXE attacks, this method disables loading external + * entities. If you rely on external entities, then you must parse the + * XML response manually by accessing the response body directly. + * + * Copied from Response->xml() in Guzzle3 (copyright @mtdowling) + * @link https://github.com/guzzle/guzzle3/blob/v3.9.3/src/Guzzle/Http/Message/Response.php + * + * @param string $body + * @return \SimpleXMLElement + * @throws RuntimeException if the response body is not in XML format + * @link http://websec.io/2012/08/27/Preventing-XXE-in-PHP.html + * + */ + public static function xmlDecode($body) + { + $errorMessage = null; + $internalErrors = libxml_use_internal_errors(true); + $disableEntities = libxml_disable_entity_loader(true); + libxml_clear_errors(); + try { + $xml = new \SimpleXMLElement((string) $body ?: '', LIBXML_NONET); + if ($error = libxml_get_last_error()) { + $errorMessage = $error->message; + } + } catch (\Exception $e) { + $errorMessage = $e->getMessage(); + } + libxml_clear_errors(); + libxml_use_internal_errors($internalErrors); + libxml_disable_entity_loader($disableEntities); + if ($errorMessage) { + throw new RuntimeException('Unable to parse response body into XML: ' . $errorMessage); + } + return $xml; + } } diff --git a/src/Omnipay/Common/Message/MessageInterface.php b/src/Omnipay/Common/Message/MessageInterface.php index f472e2d0..44bb5e1d 100644 --- a/src/Omnipay/Common/Message/MessageInterface.php +++ b/src/Omnipay/Common/Message/MessageInterface.php @@ -9,7 +9,7 @@ * Message Interface * * This interface class defines the standard functions that any Omnipay message - * interface needs to be able to provide. + * interface needs to be able to provide. */ interface MessageInterface { diff --git a/src/Omnipay/Omnipay.php b/src/Omnipay/Omnipay.php index 2094df55..9eb990a1 100644 --- a/src/Omnipay/Omnipay.php +++ b/src/Omnipay/Omnipay.php @@ -108,7 +108,7 @@ public static function setFactory(GatewayFactory $factory = null) * * @param string $method The factory method to invoke. * @param array $parameters Parameters passed to the factory method. - * + * * @return mixed */ public static function __callStatic($method, $parameters) From 00aad3474060dd3b50c3ed6bf7078928e4dbe308 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 20 Jan 2016 13:51:09 +0100 Subject: [PATCH 05/51] Use PHP Http --- composer.json | 9 +- src/Omnipay/Common/AbstractGateway.php | 13 ++- src/Omnipay/Common/GatewayFactory.php | 6 +- src/Omnipay/Common/Http/Client.php | 88 +++++++++---------- src/Omnipay/Common/Http/ClientInterface.php | 54 ------------ .../Common/Message/AbstractRequest.php | 6 +- 6 files changed, 63 insertions(+), 113 deletions(-) delete mode 100644 src/Omnipay/Common/Http/ClientInterface.php diff --git a/composer.json b/composer.json index 076fcd82..e4e56e3c 100644 --- a/composer.json +++ b/composer.json @@ -31,10 +31,15 @@ "symfony/http-foundation": "^2.1|^3.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.0", - "guzzlehttp/guzzle": "^6.0" + "php-http/client-common": "^0.2.0", + "php-http/message": "^0.2.1" }, "require-dev": { - "omnipay/tests": "^3.0" + "omnipay/tests": "^3.0", + "php-http/guzzle6-adapter": "^0.4" + }, + "suggest": { + "php-http/guzzle6-adapter": "^0.4 for Guzzle as Http Client" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index 7de1ca11..abe1ad2a 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -7,8 +7,7 @@ use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\ServerRequestFactory; -use Omnipay\Common\Http\ClientInterface; -use Omnipay\Common\Http\Client as HttpClient; +use Omnipay\Common\Http\Client; use Symfony\Component\HttpFoundation\ParameterBag; /** @@ -51,7 +50,7 @@ abstract class AbstractGateway implements GatewayInterface protected $parameters; /** - * @var ClientInterface + * @var Client */ protected $httpClient; @@ -63,10 +62,10 @@ abstract class AbstractGateway implements GatewayInterface /** * Create a new gateway instance * - * @param ClientInterface $httpClient A HTTP client to make API calls with + * @param Client $httpClient A HTTP client to make API calls with * @param ServerRequestInterface $httpRequest A HTTP request object */ - public function __construct(ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) + public function __construct(Client $httpClient = null, ServerRequestInterface $httpRequest = null) { $this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); $this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); @@ -328,11 +327,11 @@ protected function createRequest($class, array $parameters) /** * Get the global default HTTP client. * - * @return HttpClient + * @return Client */ protected function getDefaultHttpClient() { - return new HttpClient(); + return new Client(); } /** diff --git a/src/Omnipay/Common/GatewayFactory.php b/src/Omnipay/Common/GatewayFactory.php index e7dd2331..1da52319 100644 --- a/src/Omnipay/Common/GatewayFactory.php +++ b/src/Omnipay/Common/GatewayFactory.php @@ -5,7 +5,7 @@ namespace Omnipay\Common; -use Omnipay\Common\Http\ClientInterface; +use Omnipay\Common\Http\Client; use Omnipay\Common\Exception\RuntimeException; use Psr\Http\Message\ServerRequestInterface; @@ -92,12 +92,12 @@ public function find() * Create a new gateway instance * * @param string $class Gateway name - * @param ClientInterface|null $httpClient A HTTP Client implementation + * @param Client|null $httpClient A HTTP Client implementation * @param ServerRequestInterface|null $httpRequest A HTTP Request implementation * @throws RuntimeException If no such gateway is found * @return GatewayInterface An object of class $class is created and returned */ - public function create($class, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) + public function create($class, Client $httpClient = null, ServerRequestInterface $httpRequest = null) { $class = Helper::getGatewayClassName($class); diff --git a/src/Omnipay/Common/Http/Client.php b/src/Omnipay/Common/Http/Client.php index 05632e8e..70b2af08 100644 --- a/src/Omnipay/Common/Http/Client.php +++ b/src/Omnipay/Common/Http/Client.php @@ -2,8 +2,10 @@ namespace Omnipay\Common\Http; -use GuzzleHttp\Client as GuzzleClient; -use GuzzleHttp\Psr7\Request; +use Http\Client\HttpClient; +use Http\Message\MessageFactory; +use Http\Client\Common\HttpMethodsClient; +use Http\Adapter\Guzzle6\Client as GuzzleClient; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -15,66 +17,64 @@ * Implementation of the Http ClientInterface by using Guzzle. * */ -class Client implements ClientInterface +class Client extends HttpMethodsClient implements HttpClient, MessageFactory { - /** @var \GuzzleHttp\Client */ - public $guzzle; - - public function __construct(GuzzleClient $client = null) - { - $this->guzzle = $client ?: new GuzzleClient(); - } - /** - * @param RequestInterface $request - * @return ResponseInterface + * @var HttpClient */ - public function send(RequestInterface $request) - { - return $this->guzzle->send($request); - } + private $httpClient; /** - * @param string $method - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body - * @return ResponseInterface + * @var MessageFactory */ - public function request($method, $uri, array $headers = [], $body = null) - { - $request = $this->createRequest($method, $uri, $headers, $body); - - return $this->send($request); - } + private $messageFactory; /** - * @param string $method - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body - * @return RequestInterface + * @param HttpClient $httpClient The client to send requests with. + * @param MessageFactory $messageFactory The message factory to create requests. */ - public function createRequest($method, $uri, array $headers = [], $body = null) + public function __construct(HttpClient $httpClient = null, MessageFactory $messageFactory = null) { - return new Request($method, $uri, $headers, $body); + $this->httpClient = $httpClient ?: new GuzzleClient; + $this->messageFactory = $messageFactory ?: new MessageFactory\DiactorosMessageFactory(); + + parent::__construct($this->httpClient, $this->messageFactory); } /** - * @param string|UriInterface $uri - * @return UriInterface + * Creates a new PSR-7 request. + * + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param resource|string|StreamInterface|null $body + * @param string $protocolVersion + * + * @return RequestInterface */ - public function createUri($uri) + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') { - return \GuzzleHttp\Psr7\uri_for($uri); + return $this->messageFactory->createRequest($method, $uri, $headers, $body, $protocolVersion); } /** - * @param mixed $resource - * @return StreamInterface + * Creates a new PSR-7 response. + * + * @param int $statusCode + * @param string|null $reasonPhrase + * @param array $headers + * @param resource|string|StreamInterface|null $body + * @param string $protocolVersion + * + * @return ResponseInterface */ - public function createStream($resource) - { - \GuzzleHttp\Psr7\stream_for($resource); + public function createResponse( + $statusCode = 200, + $reasonPhrase = null, + array $headers = [], + $body = null, + $protocolVersion = '1.1' + ) { + return $this->messageFactory->createResponse($statusCode, $reasonPhrase, $headers, $body, $protocolVersion); } } diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php deleted file mode 100644 index 4c9a93d0..00000000 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ /dev/null @@ -1,54 +0,0 @@ -httpClient = $httpClient; $this->httpRequest = $httpRequest; From 6ee08e45cd046cb2fcfa89f1ad2f6b2a5f6266c4 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 09:12:58 +0100 Subject: [PATCH 06/51] Revert "Use PHP Http" This reverts commit 00aad3474060dd3b50c3ed6bf7078928e4dbe308. --- composer.json | 9 +- src/Omnipay/Common/AbstractGateway.php | 13 +-- src/Omnipay/Common/GatewayFactory.php | 6 +- src/Omnipay/Common/Http/Client.php | 88 +++++++++---------- src/Omnipay/Common/Http/ClientInterface.php | 54 ++++++++++++ .../Common/Message/AbstractRequest.php | 6 +- 6 files changed, 113 insertions(+), 63 deletions(-) create mode 100644 src/Omnipay/Common/Http/ClientInterface.php diff --git a/composer.json b/composer.json index e4e56e3c..076fcd82 100644 --- a/composer.json +++ b/composer.json @@ -31,15 +31,10 @@ "symfony/http-foundation": "^2.1|^3.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.0", - "php-http/client-common": "^0.2.0", - "php-http/message": "^0.2.1" + "guzzlehttp/guzzle": "^6.0" }, "require-dev": { - "omnipay/tests": "^3.0", - "php-http/guzzle6-adapter": "^0.4" - }, - "suggest": { - "php-http/guzzle6-adapter": "^0.4 for Guzzle as Http Client" + "omnipay/tests": "^3.0" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index abe1ad2a..7de1ca11 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -7,7 +7,8 @@ use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\ServerRequestFactory; -use Omnipay\Common\Http\Client; +use Omnipay\Common\Http\ClientInterface; +use Omnipay\Common\Http\Client as HttpClient; use Symfony\Component\HttpFoundation\ParameterBag; /** @@ -50,7 +51,7 @@ abstract class AbstractGateway implements GatewayInterface protected $parameters; /** - * @var Client + * @var ClientInterface */ protected $httpClient; @@ -62,10 +63,10 @@ abstract class AbstractGateway implements GatewayInterface /** * Create a new gateway instance * - * @param Client $httpClient A HTTP client to make API calls with + * @param ClientInterface $httpClient A HTTP client to make API calls with * @param ServerRequestInterface $httpRequest A HTTP request object */ - public function __construct(Client $httpClient = null, ServerRequestInterface $httpRequest = null) + public function __construct(ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) { $this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); $this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); @@ -327,11 +328,11 @@ protected function createRequest($class, array $parameters) /** * Get the global default HTTP client. * - * @return Client + * @return HttpClient */ protected function getDefaultHttpClient() { - return new Client(); + return new HttpClient(); } /** diff --git a/src/Omnipay/Common/GatewayFactory.php b/src/Omnipay/Common/GatewayFactory.php index 1da52319..e7dd2331 100644 --- a/src/Omnipay/Common/GatewayFactory.php +++ b/src/Omnipay/Common/GatewayFactory.php @@ -5,7 +5,7 @@ namespace Omnipay\Common; -use Omnipay\Common\Http\Client; +use Omnipay\Common\Http\ClientInterface; use Omnipay\Common\Exception\RuntimeException; use Psr\Http\Message\ServerRequestInterface; @@ -92,12 +92,12 @@ public function find() * Create a new gateway instance * * @param string $class Gateway name - * @param Client|null $httpClient A HTTP Client implementation + * @param ClientInterface|null $httpClient A HTTP Client implementation * @param ServerRequestInterface|null $httpRequest A HTTP Request implementation * @throws RuntimeException If no such gateway is found * @return GatewayInterface An object of class $class is created and returned */ - public function create($class, Client $httpClient = null, ServerRequestInterface $httpRequest = null) + public function create($class, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) { $class = Helper::getGatewayClassName($class); diff --git a/src/Omnipay/Common/Http/Client.php b/src/Omnipay/Common/Http/Client.php index 70b2af08..05632e8e 100644 --- a/src/Omnipay/Common/Http/Client.php +++ b/src/Omnipay/Common/Http/Client.php @@ -2,10 +2,8 @@ namespace Omnipay\Common\Http; -use Http\Client\HttpClient; -use Http\Message\MessageFactory; -use Http\Client\Common\HttpMethodsClient; -use Http\Adapter\Guzzle6\Client as GuzzleClient; +use GuzzleHttp\Client as GuzzleClient; +use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -17,64 +15,66 @@ * Implementation of the Http ClientInterface by using Guzzle. * */ -class Client extends HttpMethodsClient implements HttpClient, MessageFactory +class Client implements ClientInterface { - /** - * @var HttpClient - */ - private $httpClient; + /** @var \GuzzleHttp\Client */ + public $guzzle; + + public function __construct(GuzzleClient $client = null) + { + $this->guzzle = $client ?: new GuzzleClient(); + } /** - * @var MessageFactory + * @param RequestInterface $request + * @return ResponseInterface */ - private $messageFactory; + public function send(RequestInterface $request) + { + return $this->guzzle->send($request); + } /** - * @param HttpClient $httpClient The client to send requests with. - * @param MessageFactory $messageFactory The message factory to create requests. + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|resource|StreamInterface $body + * @return ResponseInterface */ - public function __construct(HttpClient $httpClient = null, MessageFactory $messageFactory = null) + public function request($method, $uri, array $headers = [], $body = null) { - $this->httpClient = $httpClient ?: new GuzzleClient; - $this->messageFactory = $messageFactory ?: new MessageFactory\DiactorosMessageFactory(); + $request = $this->createRequest($method, $uri, $headers, $body); - parent::__construct($this->httpClient, $this->messageFactory); + return $this->send($request); } /** - * Creates a new PSR-7 request. - * - * @param string $method - * @param string|UriInterface $uri - * @param array $headers - * @param resource|string|StreamInterface|null $body - * @param string $protocolVersion - * + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|resource|StreamInterface $body * @return RequestInterface */ - public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') + public function createRequest($method, $uri, array $headers = [], $body = null) { - return $this->messageFactory->createRequest($method, $uri, $headers, $body, $protocolVersion); + return new Request($method, $uri, $headers, $body); } /** - * Creates a new PSR-7 response. - * - * @param int $statusCode - * @param string|null $reasonPhrase - * @param array $headers - * @param resource|string|StreamInterface|null $body - * @param string $protocolVersion - * - * @return ResponseInterface + * @param string|UriInterface $uri + * @return UriInterface + */ + public function createUri($uri) + { + return \GuzzleHttp\Psr7\uri_for($uri); + } + + /** + * @param mixed $resource + * @return StreamInterface */ - public function createResponse( - $statusCode = 200, - $reasonPhrase = null, - array $headers = [], - $body = null, - $protocolVersion = '1.1' - ) { - return $this->messageFactory->createResponse($statusCode, $reasonPhrase, $headers, $body, $protocolVersion); + public function createStream($resource) + { + \GuzzleHttp\Psr7\stream_for($resource); } } diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php new file mode 100644 index 00000000..4c9a93d0 --- /dev/null +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -0,0 +1,54 @@ +httpClient = $httpClient; $this->httpRequest = $httpRequest; From 003360a3ed231aac2e9c36cbe5a33eff3f8fc5f8 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 09:36:25 +0100 Subject: [PATCH 07/51] Use Guzzle as default Client, with get/post helpers --- src/Omnipay/Common/AbstractGateway.php | 6 +- src/Omnipay/Common/Http/ClientInterface.php | 26 ++++++--- .../Http/{Client.php => GuzzleClient.php} | 58 ++++++++++++------- tests/Omnipay/Common/AbstractGatewayTest.php | 2 +- 4 files changed, 58 insertions(+), 34 deletions(-) rename src/Omnipay/Common/Http/{Client.php => GuzzleClient.php} (58%) diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index 7de1ca11..90385c3a 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -5,10 +5,10 @@ namespace Omnipay\Common; +use Omnipay\Common\Http\GuzzleClient; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\ServerRequestFactory; use Omnipay\Common\Http\ClientInterface; -use Omnipay\Common\Http\Client as HttpClient; use Symfony\Component\HttpFoundation\ParameterBag; /** @@ -328,11 +328,11 @@ protected function createRequest($class, array $parameters) /** * Get the global default HTTP client. * - * @return HttpClient + * @return ClientInterface */ protected function getDefaultHttpClient() { - return new HttpClient(); + return new GuzzleClient(); } /** diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php index 4c9a93d0..b07d5e7d 100644 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -17,28 +17,37 @@ interface ClientInterface { /** - * @param RequestInterface $request + * Send a GET request + * + * @param string|UriInterface $uri + * @param array $headers * @return ResponseInterface */ - public function send(RequestInterface $request); + public function get($uri, array $headers = []); /** - * @param string $method - * @param string|UriInterface $uri + * @param string|UriInterface$uri * @param array $headers * @param string|resource|StreamInterface $body * @return ResponseInterface */ - public function request($method, $uri, array $headers = [], $body = null); + public function post($uri, array $headers = [], $body = null); + + /** + * @param RequestInterface $request + * @return ResponseInterface + */ + public function sendRequest(RequestInterface $request); /** * @param string $method * @param string|UriInterface $uri * @param array $headers * @param string|resource|StreamInterface $body + * @param string $protocolVersion * @return RequestInterface */ - public function createRequest($method, $uri, array $headers = [], $body = null); + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'); /** * @param string|UriInterface $uri @@ -47,8 +56,9 @@ public function createRequest($method, $uri, array $headers = [], $body = null); public function createUri($uri); /** - * @param mixed $resource + * @param mixed $body * @return StreamInterface */ - public function createStream($resource); + public function createStream($body); + } diff --git a/src/Omnipay/Common/Http/Client.php b/src/Omnipay/Common/Http/GuzzleClient.php similarity index 58% rename from src/Omnipay/Common/Http/Client.php rename to src/Omnipay/Common/Http/GuzzleClient.php index 05632e8e..a6383087 100644 --- a/src/Omnipay/Common/Http/Client.php +++ b/src/Omnipay/Common/Http/GuzzleClient.php @@ -2,7 +2,7 @@ namespace Omnipay\Common\Http; -use GuzzleHttp\Client as GuzzleClient; +use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -15,21 +15,21 @@ * Implementation of the Http ClientInterface by using Guzzle. * */ -class Client implements ClientInterface +class GuzzleClient implements ClientInterface { /** @var \GuzzleHttp\Client */ public $guzzle; - public function __construct(GuzzleClient $client = null) + public function __construct(Client $client = null) { - $this->guzzle = $client ?: new GuzzleClient(); + $this->guzzle = $client ?: new Client(); } /** * @param RequestInterface $request * @return ResponseInterface */ - public function send(RequestInterface $request) + public function sendRequest(RequestInterface $request) { return $this->guzzle->send($request); } @@ -39,25 +39,12 @@ public function send(RequestInterface $request) * @param string|UriInterface $uri * @param array $headers * @param string|resource|StreamInterface $body - * @return ResponseInterface - */ - public function request($method, $uri, array $headers = [], $body = null) - { - $request = $this->createRequest($method, $uri, $headers, $body); - - return $this->send($request); - } - - /** - * @param string $method - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body + * @param string $protocolVersion * @return RequestInterface */ - public function createRequest($method, $uri, array $headers = [], $body = null) + public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') { - return new Request($method, $uri, $headers, $body); + return new Request($method, $uri, $headers, $body, $protocolVersion); } /** @@ -75,6 +62,33 @@ public function createUri($uri) */ public function createStream($resource) { - \GuzzleHttp\Psr7\stream_for($resource); + return \GuzzleHttp\Psr7\stream_for($resource); + } + + /** + * Send a GET request + * + * @param string|UriInterface $uri + * @param array $headers + * @return ResponseInterface + */ + public function get($uri, array $headers = []) + { + $request = $this->createRequest('GET', $uri, $headers); + + return $this->sendRequest($request); + } + + /** + * @param string|UriInterface $uri + * @param array $headers + * @param string|resource|StreamInterface $body + * @return ResponseInterface + */ + public function post($uri, array $headers = [], $body = null) + { + $request = $this->createRequest('GET', $uri, $headers, $body); + + return $this->sendRequest($request); } } diff --git a/tests/Omnipay/Common/AbstractGatewayTest.php b/tests/Omnipay/Common/AbstractGatewayTest.php index dca67c26..5797cae6 100755 --- a/tests/Omnipay/Common/AbstractGatewayTest.php +++ b/tests/Omnipay/Common/AbstractGatewayTest.php @@ -18,7 +18,7 @@ public function setUp() public function testConstruct() { $this->gateway = new AbstractGatewayTest_MockAbstractGateway; - $this->assertInstanceOf('\Omnipay\Common\Http\Client', $this->gateway->getProtectedHttpClient()); + $this->assertInstanceOf('\Omnipay\Common\Http\ClientInterface', $this->gateway->getProtectedHttpClient()); $this->assertInstanceOf('\Psr\Http\Message\ServerRequestInterface', $this->gateway->getProtectedHttpRequest()); $this->assertSame(array(), $this->gateway->getParameters()); } From bcb90e1351951bea524c4b58d059690e35aaa35c Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 09:42:27 +0100 Subject: [PATCH 08/51] Get->post --- src/Omnipay/Common/Http/GuzzleClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Omnipay/Common/Http/GuzzleClient.php b/src/Omnipay/Common/Http/GuzzleClient.php index a6383087..4715db6a 100644 --- a/src/Omnipay/Common/Http/GuzzleClient.php +++ b/src/Omnipay/Common/Http/GuzzleClient.php @@ -87,7 +87,7 @@ public function get($uri, array $headers = []) */ public function post($uri, array $headers = [], $body = null) { - $request = $this->createRequest('GET', $uri, $headers, $body); + $request = $this->createRequest('POST', $uri, $headers, $body); return $this->sendRequest($request); } From 133f0f750e651862ddc2cc18f05852cec99bafa0 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 09:48:09 +0100 Subject: [PATCH 09/51] CS fix --- src/Omnipay/Common/Http/ClientInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php index b07d5e7d..5d09c9de 100644 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -60,5 +60,4 @@ public function createUri($uri); * @return StreamInterface */ public function createStream($body); - } From 144712b08c01b11c4789faabec94c16e94db1a65 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 10:29:58 +0100 Subject: [PATCH 10/51] Add some tests --- src/Omnipay/Common/Http/GuzzleClient.php | 2 +- .../Omnipay/Common/Http/GuzzleClientTest.php | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/Omnipay/Common/Http/GuzzleClientTest.php diff --git a/src/Omnipay/Common/Http/GuzzleClient.php b/src/Omnipay/Common/Http/GuzzleClient.php index 4715db6a..280afe50 100644 --- a/src/Omnipay/Common/Http/GuzzleClient.php +++ b/src/Omnipay/Common/Http/GuzzleClient.php @@ -18,7 +18,7 @@ class GuzzleClient implements ClientInterface { /** @var \GuzzleHttp\Client */ - public $guzzle; + protected $guzzle; public function __construct(Client $client = null) { diff --git a/tests/Omnipay/Common/Http/GuzzleClientTest.php b/tests/Omnipay/Common/Http/GuzzleClientTest.php new file mode 100644 index 00000000..471fd913 --- /dev/null +++ b/tests/Omnipay/Common/Http/GuzzleClientTest.php @@ -0,0 +1,99 @@ +guzzle = m::mock(Guzzle::class)->makePartial(); + $this->client = new GuzzleClient($this->guzzle); + } + + public function testEmptyConstruct() + { + $client = new GuzzleClientTest_MockGuzzleClient(); + $this->assertInstanceOf(ClientInterface::class, $client); + $this->assertInstanceOf(Guzzle::class, $client->guzzle); + $this->assertNotEquals($this->guzzle, $client->guzzle); + } + + public function testGuzzleConstruct() + { + $client = new GuzzleClientTest_MockGuzzleClient($this->guzzle); + $this->assertInstanceOf(ClientInterface::class, $client); + $this->assertInstanceOf(Guzzle::class, $client->guzzle); + $this->assertEquals($this->guzzle, $client->guzzle); + } + + public function testSendRequest() + { + $request = m::mock(RequestInterface::class); + $response = m::mock(ResponseInterface::class); + + $this->guzzle->shouldReceive('send')->once()->with($request)->andReturn($response); + + $this->assertSame($response, $this->client->sendRequest($request)); + } + + public function testCreateRequest() + { + $request = $this->client->createRequest('GET', 'https://thephpleague.com/', ['key' => 'value'], 'my-body'); + + $this->assertInstanceOf(RequestInterface::class, $request); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('https://thephpleague.com/', $request->getUri()); + $this->assertEquals('value', $request->getHeaderLine('key')); + $this->assertEquals('my-body', $request->getBody()); + } + + public function testCreateUri() + { + $uri = $this->client->createUri('https://thephpleague.com/'); + + $this->assertInstanceOf(UriInterface::class, $uri); + $this->assertEquals('https://thephpleague.com/', (string) $uri); + } + + public function testCreateStream() + { + $stream = $this->client->createStream('my-body'); + + $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertEquals('my-body', (string) $stream); + } + + public function getGet() + { + $response = m::mock(ResponseInterface::class); + + $this->guzzle->shouldReceive('send')->once()->andReturn($response); + + $this->assertSame($response, $this->client->get('https://thephpleague.com/')); + } + + public function getPost() + { + $response = m::mock(ResponseInterface::class); + + $this->guzzle->shouldReceive('send')->once()->andReturn($response); + + $this->assertSame($response, $this->client->post('https://thephpleague.com/', [], 'my-body')); + } +} + +class GuzzleClientTest_MockGuzzleClient extends GuzzleClient +{ + public $guzzle; +} \ No newline at end of file From f2624e184851d4c807f363730bad3a6c9df5f786 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 13:58:33 +0100 Subject: [PATCH 11/51] Bring ParameterBag in as local Symfony copy --- src/Omnipay/Common/AbstractGateway.php | 3 +- src/Omnipay/Common/CreditCard.php | 3 +- src/Omnipay/Common/Item.php | 4 +- .../Common/Message/AbstractRequest.php | 4 +- src/Omnipay/Common/ParameterBag.php | 240 ++++++++++++++++++ tests/Omnipay/Common/AbstractGatewayTest.php | 1 - .../Omnipay/Common/Http/GuzzleClientTest.php | 3 - 7 files changed, 245 insertions(+), 13 deletions(-) create mode 100644 src/Omnipay/Common/ParameterBag.php diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index 90385c3a..d87669b6 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -9,7 +9,6 @@ use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\ServerRequestFactory; use Omnipay\Common\Http\ClientInterface; -use Symfony\Component\HttpFoundation\ParameterBag; /** * Base payment gateway class @@ -46,7 +45,7 @@ abstract class AbstractGateway implements GatewayInterface { /** - * @var \Symfony\Component\HttpFoundation\ParameterBag + * @var ParameterBag */ protected $parameters; diff --git a/src/Omnipay/Common/CreditCard.php b/src/Omnipay/Common/CreditCard.php index 683695fd..c6943dc7 100644 --- a/src/Omnipay/Common/CreditCard.php +++ b/src/Omnipay/Common/CreditCard.php @@ -8,7 +8,6 @@ use DateTime; use DateTimeZone; use Omnipay\Common\Exception\InvalidCreditCardException; -use Symfony\Component\HttpFoundation\ParameterBag; /** * Credit Card class @@ -107,7 +106,7 @@ class CreditCard /** * Internal storage of all of the card parameters. * - * @var \Symfony\Component\HttpFoundation\ParameterBag + * @var ParameterBag */ protected $parameters; diff --git a/src/Omnipay/Common/Item.php b/src/Omnipay/Common/Item.php index 02d0652d..6fbb6fb5 100644 --- a/src/Omnipay/Common/Item.php +++ b/src/Omnipay/Common/Item.php @@ -5,8 +5,6 @@ namespace Omnipay\Common; -use Symfony\Component\HttpFoundation\ParameterBag; - /** * Cart Item * @@ -17,7 +15,7 @@ class Item implements ItemInterface { /** - * @var \Symfony\Component\HttpFoundation\ParameterBag + * @var ParameterBag */ protected $parameters; diff --git a/src/Omnipay/Common/Message/AbstractRequest.php b/src/Omnipay/Common/Message/AbstractRequest.php index 2da986ac..be35cb02 100644 --- a/src/Omnipay/Common/Message/AbstractRequest.php +++ b/src/Omnipay/Common/Message/AbstractRequest.php @@ -12,8 +12,8 @@ use Omnipay\Common\Exception\RuntimeException; use Omnipay\Common\Helper; use Omnipay\Common\ItemBag; +use Omnipay\Common\ParameterBag; use Psr\Http\Message\ServerRequestInterface; -use Symfony\Component\HttpFoundation\ParameterBag; use InvalidArgumentException; /** @@ -64,7 +64,7 @@ abstract class AbstractRequest implements RequestInterface /** * The request parameters * - * @var \Symfony\Component\HttpFoundation\ParameterBag + * @var ParameterBag */ protected $parameters; diff --git a/src/Omnipay/Common/ParameterBag.php b/src/Omnipay/Common/ParameterBag.php new file mode 100644 index 00000000..89528489 --- /dev/null +++ b/src/Omnipay/Common/ParameterBag.php @@ -0,0 +1,240 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Omnipay\Common; + +/** + * ParameterBag is a container for key/value pairs. + * + * @author Fabien Potencier + */ +class ParameterBag implements \IteratorAggregate, \Countable +{ + /** + * Parameter storage. + * + * @var array + */ + protected $parameters; + + /** + * Constructor. + * + * @param array $parameters An array of parameters + */ + public function __construct(array $parameters = array()) + { + $this->parameters = $parameters; + } + + /** + * Returns the parameters. + * + * @return array An array of parameters + */ + public function all() + { + return $this->parameters; + } + + /** + * Returns the parameter keys. + * + * @return array An array of parameter keys + */ + public function keys() + { + return array_keys($this->parameters); + } + + /** + * Replaces the current parameters by a new set. + * + * @param array $parameters An array of parameters + */ + public function replace(array $parameters = array()) + { + $this->parameters = $parameters; + } + + /** + * Adds parameters. + * + * @param array $parameters An array of parameters + */ + public function add(array $parameters = array()) + { + $this->parameters = array_replace($this->parameters, $parameters); + } + + /** + * Returns a parameter by name. + * + * @param string $key The key + * @param mixed $default The default value if the parameter key does not exist + * + * @return mixed + * + * @throws \InvalidArgumentException + */ + public function get($key, $default = null) + { + return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; + } + + /** + * Sets a parameter by name. + * + * @param string $key The key + * @param mixed $value The value + */ + public function set($key, $value) + { + $this->parameters[$key] = $value; + } + + /** + * Returns true if the parameter is defined. + * + * @param string $key The key + * + * @return bool true if the parameter exists, false otherwise + */ + public function has($key) + { + return array_key_exists($key, $this->parameters); + } + + /** + * Removes a parameter. + * + * @param string $key The key + */ + public function remove($key) + { + unset($this->parameters[$key]); + } + + /** + * Returns the alphabetic characters of the parameter value. + * + * @param string $key The parameter key + * @param string $default The default value if the parameter key does not exist + * + * @return string The filtered value + */ + public function getAlpha($key, $default = '') + { + return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default)); + } + + /** + * Returns the alphabetic characters and digits of the parameter value. + * + * @param string $key The parameter key + * @param string $default The default value if the parameter key does not exist + * + * @return string The filtered value + */ + public function getAlnum($key, $default = '') + { + return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default)); + } + + /** + * Returns the digits of the parameter value. + * + * @param string $key The parameter key + * @param string $default The default value if the parameter key does not exist + * + * @return string The filtered value + */ + public function getDigits($key, $default = '') + { + // we need to remove - and + because they're allowed in the filter + return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT)); + } + + /** + * Returns the parameter value converted to integer. + * + * @param string $key The parameter key + * @param int $default The default value if the parameter key does not exist + * + * @return int The filtered value + */ + public function getInt($key, $default = 0) + { + return (int) $this->get($key, $default); + } + + /** + * Returns the parameter value converted to boolean. + * + * @param string $key The parameter key + * @param mixed $default The default value if the parameter key does not exist + * + * @return bool The filtered value + */ + public function getBoolean($key, $default = false) + { + return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN); + } + + /** + * Filter key. + * + * @param string $key Key. + * @param mixed $default Default = null. + * @param int $filter FILTER_* constant. + * @param mixed $options Filter options. + * + * @see http://php.net/manual/en/function.filter-var.php + * + * @return mixed + */ + public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array()) + { + $value = $this->get($key, $default); + + // Always turn $options into an array - this allows filter_var option shortcuts. + if (!is_array($options) && $options) { + $options = array('flags' => $options); + } + + // Add a convenience check for arrays. + if (is_array($value) && !isset($options['flags'])) { + $options['flags'] = FILTER_REQUIRE_ARRAY; + } + + return filter_var($value, $filter, $options); + } + + /** + * Returns an iterator for parameters. + * + * @return \ArrayIterator An \ArrayIterator instance + */ + public function getIterator() + { + return new \ArrayIterator($this->parameters); + } + + /** + * Returns the number of parameters. + * + * @return int The number of parameters + */ + public function count() + { + return count($this->parameters); + } +} diff --git a/tests/Omnipay/Common/AbstractGatewayTest.php b/tests/Omnipay/Common/AbstractGatewayTest.php index 5797cae6..26d25ba2 100755 --- a/tests/Omnipay/Common/AbstractGatewayTest.php +++ b/tests/Omnipay/Common/AbstractGatewayTest.php @@ -5,7 +5,6 @@ use Mockery as m; use Omnipay\Common\Message\AbstractRequest; use Omnipay\Tests\TestCase; -use Symfony\Component\HttpFoundation\ParameterBag; class AbstractGatewayTest extends TestCase { diff --git a/tests/Omnipay/Common/Http/GuzzleClientTest.php b/tests/Omnipay/Common/Http/GuzzleClientTest.php index 471fd913..f09349c9 100644 --- a/tests/Omnipay/Common/Http/GuzzleClientTest.php +++ b/tests/Omnipay/Common/Http/GuzzleClientTest.php @@ -3,10 +3,7 @@ namespace Omnipay\Common\Http; use Mockery as m; -use Omnipay\Common\CreditCard; -use Omnipay\Common\ItemBag; use Omnipay\Tests\TestCase; -use ReflectionClass; use GuzzleHttp\Client as Guzzle; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; From f785808a4d9984d6d3717c13f2636977a6b58ef2 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 14:17:04 +0100 Subject: [PATCH 12/51] Use PSR-7 RedirectResponse --- composer.json | 1 - src/Omnipay/Common/Message/AbstractResponse.php | 12 +++++++----- .../Omnipay/Common/Message/AbstractResponseTest.php | 8 ++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 076fcd82..e0bd8134 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,6 @@ }, "require": { "php": ">=5.5.9", - "symfony/http-foundation": "^2.1|^3.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.0", "guzzlehttp/guzzle": "^6.0" diff --git a/src/Omnipay/Common/Message/AbstractResponse.php b/src/Omnipay/Common/Message/AbstractResponse.php index 27b63be5..e544f057 100644 --- a/src/Omnipay/Common/Message/AbstractResponse.php +++ b/src/Omnipay/Common/Message/AbstractResponse.php @@ -6,8 +6,9 @@ namespace Omnipay\Common\Message; use Omnipay\Common\Exception\RuntimeException; -use Symfony\Component\HttpFoundation\RedirectResponse as HttpRedirectResponse; -use Symfony\Component\HttpFoundation\Response as HttpResponse; +use Psr\Http\Message\ResponseInterface as HttpResponseInterface; +use Zend\Diactoros\Response\HtmlResponse; +use Zend\Diactoros\Response\RedirectResponse; /** * Abstract Response @@ -174,7 +175,7 @@ public function redirect() } /** - * @return HttpRedirectResponse + * @return HttpResponseInterface */ public function getRedirectResponse() { @@ -182,8 +183,9 @@ public function getRedirectResponse() throw new RuntimeException('This response does not support redirection.'); } + /** @var $this RedirectResponseInterface */ if ('GET' === $this->getRedirectMethod()) { - return HttpRedirectResponse::create($this->getRedirectUrl()); + return new RedirectResponse($this->getRedirectUrl()); } elseif ('POST' === $this->getRedirectMethod()) { $hiddenFields = ''; foreach ($this->getRedirectData() as $key => $value) { @@ -216,7 +218,7 @@ public function getRedirectResponse() $hiddenFields ); - return HttpResponse::create($output); + return new HtmlResponse($output); } throw new RuntimeException('Invalid redirect method "'.$this->getRedirectMethod().'".'); diff --git a/tests/Omnipay/Common/Message/AbstractResponseTest.php b/tests/Omnipay/Common/Message/AbstractResponseTest.php index 513eeae4..d3a7db71 100644 --- a/tests/Omnipay/Common/Message/AbstractResponseTest.php +++ b/tests/Omnipay/Common/Message/AbstractResponseTest.php @@ -60,7 +60,7 @@ public function testGetRedirectResponseGet() $httpResponse = $this->response->getRedirectResponse(); $this->assertSame(302, $httpResponse->getStatusCode()); - $this->assertSame('https://example.com/redirect?a=1&b=2', $httpResponse->getTargetUrl()); + $this->assertSame('https://example.com/redirect?a=1&b=2', $httpResponse->getHeaderLine('Location')); } public function testGetRedirectResponsePost() @@ -72,9 +72,9 @@ public function testGetRedirectResponsePost() $httpResponse = $this->response->getRedirectResponse(); $this->assertSame(200, $httpResponse->getStatusCode()); - $this->assertContains('
', $httpResponse->getContent()); - $this->assertContains('', $httpResponse->getContent()); - $this->assertContains('', $httpResponse->getContent()); + $this->assertContains('', (string) $httpResponse->getBody()); + $this->assertContains('', (string) $httpResponse->getBody()); + $this->assertContains('', (string) $httpResponse->getBody()); } /** From 2f983470835c2a054b58023ef7a9dd2c7f3b7f94 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Thu, 21 Jan 2016 14:22:08 +0100 Subject: [PATCH 13/51] Bump diactoros to include RedirectResponse --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e0bd8134..74f82a24 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "require": { "php": ">=5.5.9", "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.0", + "zendframework/zend-diactoros": "^1.1.0", "guzzlehttp/guzzle": "^6.0" }, "require-dev": { From 763f23e60192ec6f3bbc43cea14110e205475a0a Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 6 Feb 2016 23:51:29 +0100 Subject: [PATCH 14/51] Remove get/post, add request method to HTTP interface --- src/Omnipay/Common/Http/ClientInterface.php | 13 ++----- src/Omnipay/Common/Http/GuzzleClient.php | 41 +++++++-------------- 2 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php index 5d09c9de..745e2cca 100644 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -17,21 +17,13 @@ interface ClientInterface { /** - * Send a GET request - * - * @param string|UriInterface $uri - * @param array $headers - * @return ResponseInterface - */ - public function get($uri, array $headers = []); - - /** + * @param string * @param string|UriInterface$uri * @param array $headers * @param string|resource|StreamInterface $body * @return ResponseInterface */ - public function post($uri, array $headers = [], $body = null); + public function request($method, $uri, array $headers = [], $body = null); /** * @param RequestInterface $request @@ -60,4 +52,5 @@ public function createUri($uri); * @return StreamInterface */ public function createStream($body); + } diff --git a/src/Omnipay/Common/Http/GuzzleClient.php b/src/Omnipay/Common/Http/GuzzleClient.php index 280afe50..89f6ab17 100644 --- a/src/Omnipay/Common/Http/GuzzleClient.php +++ b/src/Omnipay/Common/Http/GuzzleClient.php @@ -25,6 +25,20 @@ public function __construct(Client $client = null) $this->guzzle = $client ?: new Client(); } + /** + * @param string + * @param string|UriInterface $uri + * @param array $headers + * @param string|resource|StreamInterface $body + * @return ResponseInterface + */ + public function request($method, $uri, array $headers = [], $body = null) + { + $request = $this->createRequest($method, $uri, $headers, $body); + + return $this->sendRequest($request); + } + /** * @param RequestInterface $request * @return ResponseInterface @@ -64,31 +78,4 @@ public function createStream($resource) { return \GuzzleHttp\Psr7\stream_for($resource); } - - /** - * Send a GET request - * - * @param string|UriInterface $uri - * @param array $headers - * @return ResponseInterface - */ - public function get($uri, array $headers = []) - { - $request = $this->createRequest('GET', $uri, $headers); - - return $this->sendRequest($request); - } - - /** - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body - * @return ResponseInterface - */ - public function post($uri, array $headers = [], $body = null) - { - $request = $this->createRequest('POST', $uri, $headers, $body); - - return $this->sendRequest($request); - } } From 58f9f12c6d96cd317f9f2055c4cc11d003696dc4 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 6 Feb 2016 23:59:42 +0100 Subject: [PATCH 15/51] Fix newline --- src/Omnipay/Common/Http/ClientInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Omnipay/Common/Http/ClientInterface.php index 745e2cca..4f776e25 100644 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ b/src/Omnipay/Common/Http/ClientInterface.php @@ -52,5 +52,4 @@ public function createUri($uri); * @return StreamInterface */ public function createStream($body); - } From af429996b24cb8543d057c05841b2205208af98e Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 7 Feb 2016 00:25:20 +0100 Subject: [PATCH 16/51] Use alcohol/4217 currency list --- composer.json | 5 ++- src/Omnipay/Common/Currency.php | 75 +++++++++------------------------ 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/composer.json b/composer.json index 74f82a24..be0927ec 100644 --- a/composer.json +++ b/composer.json @@ -28,9 +28,10 @@ }, "require": { "php": ">=5.5.9", + "alcohol/iso4217": "^3.1", + "guzzlehttp/guzzle": "^6.0", "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.1.0", - "guzzlehttp/guzzle": "^6.0" + "zendframework/zend-diactoros": "^1.1.0" }, "require-dev": { "omnipay/tests": "^3.0" diff --git a/src/Omnipay/Common/Currency.php b/src/Omnipay/Common/Currency.php index 23c81b54..0f915595 100644 --- a/src/Omnipay/Common/Currency.php +++ b/src/Omnipay/Common/Currency.php @@ -5,6 +5,8 @@ namespace Omnipay\Common; +use Alcohol\ISO4217; + /** * Currency class * @@ -66,12 +68,15 @@ public function getDecimals() */ public static function find($code) { - $code = strtoupper($code); - $currencies = static::all(); + $iso4217 = new ISO4217(); - if (isset($currencies[$code])) { - return new static($code, $currencies[$code]['numeric'], $currencies[$code]['decimals']); + try { + $currency = $iso4217->getByCode($code); + } catch (\OutOfBoundsException $e) { + return null; } + + return new static($currency['alpha3'], $currency['numeric'], $currency['exp']); } /** @@ -81,56 +86,16 @@ public static function find($code) */ public static function all() { - return array( - 'ARS' => array('numeric' => '032', 'decimals' => 2), - 'AUD' => array('numeric' => '036', 'decimals' => 2), - 'BOB' => array('numeric' => '068', 'decimals' => 2), - 'BRL' => array('numeric' => '986', 'decimals' => 2), - 'BTC' => array('numeric' => null, 'decimals' => 8), - 'CAD' => array('numeric' => '124', 'decimals' => 2), - 'CHF' => array('numeric' => '756', 'decimals' => 2), - 'CLP' => array('numeric' => '152', 'decimals' => 0), - 'CNY' => array('numeric' => '156', 'decimals' => 2), - 'COP' => array('numeric' => '170', 'decimals' => 2), - 'CRC' => array('numeric' => '188', 'decimals' => 2), - 'CZK' => array('numeric' => '203', 'decimals' => 2), - 'DKK' => array('numeric' => '208', 'decimals' => 2), - 'DOP' => array('numeric' => '214', 'decimals' => 2), - 'EUR' => array('numeric' => '978', 'decimals' => 2), - 'FJD' => array('numeric' => '242', 'decimals' => 2), - 'GBP' => array('numeric' => '826', 'decimals' => 2), - 'GTQ' => array('numeric' => '320', 'decimals' => 2), - 'HKD' => array('numeric' => '344', 'decimals' => 2), - 'HUF' => array('numeric' => '348', 'decimals' => 2), - 'ILS' => array('numeric' => '376', 'decimals' => 2), - 'INR' => array('numeric' => '356', 'decimals' => 2), - 'JPY' => array('numeric' => '392', 'decimals' => 0), - 'KRW' => array('numeric' => '410', 'decimals' => 0), - 'LAK' => array('numeric' => '418', 'decimals' => 0), - 'MXN' => array('numeric' => '484', 'decimals' => 2), - 'MYR' => array('numeric' => '458', 'decimals' => 2), - 'NOK' => array('numeric' => '578', 'decimals' => 2), - 'NZD' => array('numeric' => '554', 'decimals' => 2), - 'OMR' => array('numeric' => '512', 'decimals' => 3), - 'PEN' => array('numeric' => '604', 'decimals' => 2), - 'PGK' => array('numeric' => '598', 'decimals' => 2), - 'PHP' => array('numeric' => '608', 'decimals' => 2), - 'PLN' => array('numeric' => '985', 'decimals' => 2), - 'PYG' => array('numeric' => '600', 'decimals' => 0), - 'SBD' => array('numeric' => '090', 'decimals' => 2), - 'SEK' => array('numeric' => '752', 'decimals' => 2), - 'SGD' => array('numeric' => '702', 'decimals' => 2), - 'THB' => array('numeric' => '764', 'decimals' => 2), - 'TOP' => array('numeric' => '776', 'decimals' => 2), - 'TRY' => array('numeric' => '949', 'decimals' => 2), - 'TWD' => array('numeric' => '901', 'decimals' => 2), - 'USD' => array('numeric' => '840', 'decimals' => 2), - 'UYU' => array('numeric' => '858', 'decimals' => 2), - 'VEF' => array('numeric' => '937', 'decimals' => 2), - 'VND' => array('numeric' => '704', 'decimals' => 0), - 'VUV' => array('numeric' => '548', 'decimals' => 0), - 'WST' => array('numeric' => '882', 'decimals' => 2), - 'ZAR' => array('numeric' => '710', 'decimals' => 2), - ); + $currencies = []; + + $iso4217 = new ISO4217(); + foreach ($iso4217->getAll() as $currency) { + $currencies[$currency['alpha3']] = [ + 'numeric' => $currency['numeric'], + 'decimals' => $currency['exp'], + ]; + } + + return $currencies; } } From 26e974e1de25e4b4f14ded566b59488c3f049e13 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 7 Feb 2016 19:52:58 +0100 Subject: [PATCH 17/51] Bump to php 5.6 --- .travis.yml | 6 ++---- composer.json | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 764a9904..aed35471 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: php php: - - 5.5.9 - - 5.5 - 5.6 - 7.0 - hhvm @@ -17,9 +15,9 @@ env: matrix: include: - - php: 5.5.9 + - php: 5.6 env: setup=lowest - - php: 5.5.9 + - php: 5.6 env: setup=stable install: diff --git a/composer.json b/composer.json index be0927ec..1f680de6 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "classmap": ["src/Omnipay/Omnipay.php"] }, "require": { - "php": ">=5.5.9", + "php": ">=5.6", "alcohol/iso4217": "^3.1", "guzzlehttp/guzzle": "^6.0", "psr/http-message": "^1.0", From b2d05fe77b5fb7450ff9886d3eb52c8be77e3c01 Mon Sep 17 00:00:00 2001 From: Kayla Daniels Date: Mon, 8 Feb 2016 22:29:48 -0500 Subject: [PATCH 18/51] abstract parameters into trait --- src/Omnipay/Common/AbstractGateway.php | 35 ++-------- src/Omnipay/Common/HasParametersTrait.php | 64 +++++++++++++++++++ src/Omnipay/Common/Helper.php | 6 +- src/Omnipay/Common/Item.php | 36 +---------- .../Common/Message/AbstractRequest.php | 12 ++-- src/Omnipay/Common/ParameterizedInterface.php | 20 ++++++ tests/Omnipay/Common/HelperTest.php | 8 +-- 7 files changed, 106 insertions(+), 75 deletions(-) create mode 100644 src/Omnipay/Common/HasParametersTrait.php create mode 100644 src/Omnipay/Common/ParameterizedInterface.php diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index d87669b6..6ecde32c 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -42,8 +42,10 @@ * * @see GatewayInterface */ -abstract class AbstractGateway implements GatewayInterface +abstract class AbstractGateway implements GatewayInterface, ParameterizedInterface { + use HasParametersTrait; + /** * @var ParameterBag */ @@ -101,7 +103,7 @@ public function initialize(array $parameters = array()) } } - Helper::initialize($this, $parameters); + Helper::initializeParameters($this, $parameters); return $this; } @@ -114,35 +116,6 @@ public function getDefaultParameters() return array(); } - /** - * @return array - */ - public function getParameters() - { - return $this->parameters->all(); - } - - /** - * @param string $key - * @return mixed - */ - public function getParameter($key) - { - return $this->parameters->get($key); - } - - /** - * @param string $key - * @param mixed $value - * @return $this - */ - public function setParameter($key, $value) - { - $this->parameters->set($key, $value); - - return $this; - } - /** * @return boolean */ diff --git a/src/Omnipay/Common/HasParametersTrait.php b/src/Omnipay/Common/HasParametersTrait.php new file mode 100644 index 00000000..d71a6dfc --- /dev/null +++ b/src/Omnipay/Common/HasParametersTrait.php @@ -0,0 +1,64 @@ +parameters->set($key, $value); + + return $this; + } + + /** + * Get one parameter. + * + * @return mixed A single parameter value. + */ + public function getParameter($key) + { + return $this->parameters->get($key); + } + + /** + * Get all parameters. + * + * @return array An associative array of parameters. + */ + public function getParameters() + { + return $this->parameters->all(); + } + + /** + * Initialize the object with parameters. + * + * If any unknown parameters passed, they will be ignored. + * + * @param array $parameters An associative array of parameters + * @return $this. + */ + public function initialize(array $parameters = []) + { + $this->parameters = new ParameterBag; + + Helper::initializeParameters($this, $parameters); + + return $this; + } +} diff --git a/src/Omnipay/Common/Helper.php b/src/Omnipay/Common/Helper.php index 9ccc7de2..7577ca4d 100644 --- a/src/Omnipay/Common/Helper.php +++ b/src/Omnipay/Common/Helper.php @@ -76,16 +76,18 @@ public static function validateLuhn($number) * Parameters are automatically converted to camelCase. Any parameters which do * not match a setter on the target object are ignored. * - * @param mixed $target The object to set parameters on + * @param ParameterizedInterface $target The object to set parameters on * @param array $parameters An array of parameters to set */ - public static function initialize($target, $parameters) + public static function initializeParameters(ParameterizedInterface $target, array $parameters = []) { if (is_array($parameters)) { foreach ($parameters as $key => $value) { $method = 'set'.ucfirst(static::camelCase($key)); if (method_exists($target, $method)) { $target->$method($value); + } else { + $target->setParameter($key, $value); } } } diff --git a/src/Omnipay/Common/Item.php b/src/Omnipay/Common/Item.php index 6fbb6fb5..476504bc 100644 --- a/src/Omnipay/Common/Item.php +++ b/src/Omnipay/Common/Item.php @@ -12,8 +12,10 @@ * * @see ItemInterface */ -class Item implements ItemInterface +class Item implements ItemInterface, ParameterizedInterface { + use HasParametersTrait; + /** * @var ParameterBag */ @@ -29,38 +31,6 @@ public function __construct($parameters = null) $this->initialize($parameters); } - /** - * Initialize this item with the specified parameters - * - * @param array|null $parameters An array of parameters to set on this object - * @return $this Item - */ - public function initialize($parameters = null) - { - $this->parameters = new ParameterBag; - - Helper::initialize($this, $parameters); - - return $this; - } - - public function getParameters() - { - return $this->parameters->all(); - } - - protected function getParameter($key) - { - return $this->parameters->get($key); - } - - protected function setParameter($key, $value) - { - $this->parameters->set($key, $value); - - return $this; - } - /** * {@inheritDoc} */ diff --git a/src/Omnipay/Common/Message/AbstractRequest.php b/src/Omnipay/Common/Message/AbstractRequest.php index aec0f946..6eea7c77 100644 --- a/src/Omnipay/Common/Message/AbstractRequest.php +++ b/src/Omnipay/Common/Message/AbstractRequest.php @@ -5,6 +5,7 @@ namespace Omnipay\Common\Message; +use Omnipay\Common\HasParametersTrait; use Omnipay\Common\Http\ClientInterface; use Omnipay\Common\CreditCard; use Omnipay\Common\Currency; @@ -13,6 +14,7 @@ use Omnipay\Common\Helper; use Omnipay\Common\ItemBag; use Omnipay\Common\ParameterBag; +use Omnipay\Common\ParameterizedInterface; use Psr\Http\Message\ServerRequestInterface; use InvalidArgumentException; @@ -59,7 +61,7 @@ * @see RequestInterface * @see AbstractResponse */ -abstract class AbstractRequest implements RequestInterface +abstract class AbstractRequest implements RequestInterface, ParameterizedInterface { /** * The request parameters @@ -122,7 +124,7 @@ public function __construct(ClientInterface $httpClient, ServerRequestInterface * @return $this * @throws RuntimeException */ - public function initialize(array $parameters = array()) + public function initialize(array $parameters = []) { if (null !== $this->response) { throw new RuntimeException('Request cannot be modified after it has been sent!'); @@ -130,7 +132,7 @@ public function initialize(array $parameters = array()) $this->parameters = new ParameterBag; - Helper::initialize($this, $parameters); + Helper::initializeParameters($this, $parameters); return $this; } @@ -151,7 +153,7 @@ public function getParameters() * @param string $key The parameter key * @return mixed */ - protected function getParameter($key) + public function getParameter($key) { return $this->parameters->get($key); } @@ -164,7 +166,7 @@ protected function getParameter($key) * @return AbstractRequest Provides a fluent interface * @throws RuntimeException if a request parameter is modified after the request has been sent. */ - protected function setParameter($key, $value) + public function setParameter($key, $value) { if (null !== $this->response) { throw new RuntimeException('Request cannot be modified after it has been sent!'); diff --git a/src/Omnipay/Common/ParameterizedInterface.php b/src/Omnipay/Common/ParameterizedInterface.php new file mode 100644 index 00000000..b4624a6d --- /dev/null +++ b/src/Omnipay/Common/ParameterizedInterface.php @@ -0,0 +1,20 @@ +shouldReceive('setName')->once()->with('adrian'); $target->shouldReceive('setNumber')->once()->with('1234'); - Helper::initialize($target, array('name' => 'adrian', 'number' => '1234')); + Helper::initializeParameters($target, ['name' => 'adrian', 'number' => '1234']); } public function testInitializeIgnoresInvalidParameters() @@ -69,7 +69,7 @@ public function testInitializeIgnoresInvalidParameters() $target = m::mock('\Omnipay\Common\CreditCard'); $target->shouldReceive('setName')->once()->with('adrian'); - Helper::initialize($target, array('name' => 'adrian', 'extra' => 'invalid')); + Helper::initializeParameters($target, ['name' => 'adrian', 'extra' => 'invalid']); } public function testGetGatewayShortNameSimple() From 3b563273dd3836571f6782d5d2186247cdba28ed Mon Sep 17 00:00:00 2001 From: Kayla Daniels Date: Mon, 8 Feb 2016 22:30:09 -0500 Subject: [PATCH 19/51] abstract Customer object out of CreditCard object --- src/Omnipay/Common/CreditCard.php | 1067 +---------------------- src/Omnipay/Common/Customer.php | 414 +++++++++ tests/Omnipay/Common/CreditCardTest.php | 355 +------- tests/Omnipay/Common/CustomerTest.php | 161 ++++ 4 files changed, 613 insertions(+), 1384 deletions(-) create mode 100644 src/Omnipay/Common/Customer.php create mode 100644 tests/Omnipay/Common/CustomerTest.php diff --git a/src/Omnipay/Common/CreditCard.php b/src/Omnipay/Common/CreditCard.php index 7ae1b2fe..136b5923 100644 --- a/src/Omnipay/Common/CreditCard.php +++ b/src/Omnipay/Common/CreditCard.php @@ -89,8 +89,10 @@ * * If any unknown parameters are passed in, they will be ignored. No error is thrown. */ -class CreditCard +class CreditCard implements ParameterizedInterface { + use HasParametersTrait; + const BRAND_VISA = 'visa'; const BRAND_MASTERCARD = 'mastercard'; const BRAND_DISCOVER = 'discover'; @@ -115,7 +117,7 @@ class CreditCard * @link https://github.com/Shopify/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb * @var array */ - protected $supported_cards = array( + protected $supported_cards = [ self::BRAND_VISA => '/^4\d{12}(\d{3})?$/', self::BRAND_MASTERCARD => '/^(5[1-5]\d{4}|677189)\d{10}$/', self::BRAND_DISCOVER => '/^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/', @@ -128,21 +130,24 @@ class CreditCard self::BRAND_MAESTRO => '/^(5[06-8]|6\d)\d{10,17}$/', self::BRAND_FORBRUGSFORENINGEN => '/^600722\d{10}$/', self::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/', - ); + ]; /** - * Internal storage of all of the card parameters. - * - * @var ParameterBag + * @var Customer + */ + private $shippingCustomer; + + /** + * @var Customer */ - protected $parameters; + private $billingCustomer; /** * Create a new CreditCard object using the specified parameters * * @param array $parameters An array of parameters to set on the new object */ - public function __construct($parameters = null) + public function __construct(array $parameters = []) { $this->initialize($parameters); } @@ -182,58 +187,8 @@ public function addSupportedBrand($name, $expression) } $this->supported_cards[$name] = $expression; - return true; - } - - /** - * Initialize the object with parameters. - * - * If any unknown parameters passed, they will be ignored. - * - * @param array $parameters An associative array of parameters - * @return CreditCard provides a fluent interface. - */ - public function initialize($parameters = null) - { - $this->parameters = new ParameterBag; - - Helper::initialize($this, $parameters); - - return $this; - } - - /** - * Get all parameters. - * - * @return array An associative array of parameters. - */ - public function getParameters() - { - return $this->parameters->all(); - } - - /** - * Get one parameter. - * - * @return mixed A single parameter value. - */ - protected function getParameter($key) - { - return $this->parameters->get($key); - } - - /** - * Set one parameter. - * - * @param string $key Parameter key - * @param mixed $value Parameter value - * @return CreditCard provides a fluent interface. - */ - protected function setParameter($key, $value) - { - $this->parameters->set($key, $value); - return $this; + return true; } /** @@ -271,7 +226,7 @@ protected function setYearParameter($key, $value) */ public function validate() { - foreach (array('number', 'expiryMonth', 'expiryYear') as $key) { + foreach (['number', 'expiryMonth', 'expiryYear'] as $key) { if (!$this->getParameter($key)) { throw new InvalidCreditCardException("The $key parameter is required"); } @@ -290,102 +245,6 @@ public function validate() } } - /** - * Get Card Title. - * - * @return string - */ - public function getTitle() - { - return $this->getBillingTitle(); - } - - /** - * Set Card Title. - * - * @param string $value Parameter value - * @return CreditCard provides a fluent interface. - */ - public function setTitle($value) - { - $this->setBillingTitle($value); - $this->setShippingTitle($value); - - return $this; - } - - /** - * Get Card First Name. - * - * @return string - */ - public function getFirstName() - { - return $this->getBillingFirstName(); - } - - /** - * Set Card First Name (Billing and Shipping). - * - * @param string $value Parameter value - * @return CreditCard provides a fluent interface. - */ - public function setFirstName($value) - { - $this->setBillingFirstName($value); - $this->setShippingFirstName($value); - - return $this; - } - - /** - * Get Card Last Name. - * - * @return string - */ - public function getLastName() - { - return $this->getBillingLastName(); - } - - /** - * Set Card Last Name (Billing and Shipping). - * - * @param string $value Parameter value - * @return CreditCard provides a fluent interface. - */ - public function setLastName($value) - { - $this->setBillingLastName($value); - $this->setShippingLastName($value); - - return $this; - } - - /** - * Get Card Name. - * - * @return string - */ - public function getName() - { - return $this->getBillingName(); - } - - /** - * Set Card Name (Billing and Shipping). - * - * @param string $value Parameter value - * @return CreditCard provides a fluent interface. - */ - public function setName($value) - { - $this->setBillingName($value); - $this->setShippingName($value); - - return $this; - } - /** * Get Card Number. * @@ -601,909 +460,51 @@ public function setIssueNumber($value) } /** - * Get the card billing title. - * - * @return string - */ - public function getBillingTitle() - { - return $this->getParameter('billingTitle'); - } - - /** - * Sets the card billing title. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingTitle($value) - { - return $this->setParameter('billingTitle', $value); - } - - /** - * Get the card billing name. - * - * @return string - */ - public function getBillingName() - { - return trim($this->getBillingFirstName() . ' ' . $this->getBillingLastName()); - } - - /** - * Sets the card billing name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingName($value) - { - $names = explode(' ', $value, 2); - $this->setBillingFirstName($names[0]); - $this->setBillingLastName(isset($names[1]) ? $names[1] : null); - - return $this; - } - - /** - * Get the first part of the card billing name. - * - * @return string - */ - public function getBillingFirstName() - { - return $this->getParameter('billingFirstName'); - } - - /** - * Sets the first part of the card billing name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingFirstName($value) - { - return $this->setParameter('billingFirstName', $value); - } - - /** - * Get the last part of the card billing name. - * - * @return string - */ - public function getBillingLastName() - { - return $this->getParameter('billingLastName'); - } - - /** - * Sets the last part of the card billing name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingLastName($value) - { - return $this->setParameter('billingLastName', $value); - } - - /** - * Get the billing company name. - * - * @return string - */ - public function getBillingCompany() - { - return $this->getParameter('billingCompany'); - } - - /** - * Sets the billing company name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingCompany($value) - { - return $this->setParameter('billingCompany', $value); - } - - /** - * Get the billing address, line 1. - * - * @return string - */ - public function getBillingAddress1() - { - return $this->getParameter('billingAddress1'); - } - - /** - * Sets the billing address, line 1. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingAddress1($value) - { - return $this->setParameter('billingAddress1', $value); - } - - /** - * Get the billing address, line 2. - * - * @return string - */ - public function getBillingAddress2() - { - return $this->getParameter('billingAddress2'); - } - - /** - * Sets the billing address, line 2. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingAddress2($value) - { - return $this->setParameter('billingAddress2', $value); - } - - /** - * Get the billing city. - * - * @return string - */ - public function getBillingCity() - { - return $this->getParameter('billingCity'); - } - - /** - * Sets billing city. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingCity($value) - { - return $this->setParameter('billingCity', $value); - } - - /** - * Get the billing postcode. - * - * @return string + * @param Customer $customer */ - public function getBillingPostcode() + public function setCustomer(Customer $customer) { - return $this->getParameter('billingPostcode'); + $this->setBillingCustomer($customer); + $this->setShippingCustomer($customer); } /** - * Sets the billing postcode. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingPostcode($value) - { - return $this->setParameter('billingPostcode', $value); - } - - /** - * Get the billing state. - * - * @return string - */ - public function getBillingState() - { - return $this->getParameter('billingState'); - } - - /** - * Sets the billing state. - * - * @param string $value - * @return CreditCard provides a fluent interface. + * @param Customer $customer */ - public function setBillingState($value) + private function setBillingCustomer(Customer $customer) { - return $this->setParameter('billingState', $value); + $this->billingCustomer = $customer; } /** - * Get the billing country name. - * - * @return string + * @param Customer $customer */ - public function getBillingCountry() + private function setShippingCustomer(Customer $customer) { - return $this->getParameter('billingCountry'); + $this->shippingCustomer = $customer; } /** - * Sets the billing country name. - * - * @param string $value - * @return CreditCard provides a fluent interface. + * @return Customer */ - public function setBillingCountry($value) + public function getShippingCustomer() { - return $this->setParameter('billingCountry', $value); + return $this->shippingCustomer; } /** - * Get the billing phone number. - * - * @return string + * @return Customer */ - public function getBillingPhone() + public function getBillingCustomer() { - return $this->getParameter('billingPhone'); + return $this->billingCustomer; } /** - * Sets the billing phone number. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingPhone($value) - { - return $this->setParameter('billingPhone', $value); - } - - /** - * Get the billing phone number extension. - * - * @return string - */ - public function getBillingPhoneExtension() - { - return $this->getParameter('billingPhoneExtension'); - } - - /** - * Sets the billing phone number extension. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingPhoneExtension($value) - { - return $this->setParameter('billingPhoneExtension', $value); - } - - /** - * Get the billing fax number. - * - * @return string - */ - public function getBillingFax() - { - return $this->getParameter('billingFax'); - } - - /** - * Sets the billing fax number. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBillingFax($value) - { - return $this->setParameter('billingFax', $value); - } - - /** - * Get the title of the card shipping name. - * - * @return string - */ - public function getShippingTitle() - { - return $this->getParameter('shippingTitle'); - } - - /** - * Sets the title of the card shipping name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingTitle($value) - { - return $this->setParameter('shippingTitle', $value); - } - - /** - * Get the card shipping name. - * - * @return string - */ - public function getShippingName() - { - return trim($this->getShippingFirstName() . ' ' . $this->getShippingLastName()); - } - - /** - * Sets the card shipping name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingName($value) - { - $names = explode(' ', $value, 2); - $this->setShippingFirstName($names[0]); - $this->setShippingLastName(isset($names[1]) ? $names[1] : null); - - return $this; - } - - /** - * Get the first part of the card shipping name. - * - * @return string - */ - public function getShippingFirstName() - { - return $this->getParameter('shippingFirstName'); - } - - /** - * Sets the first part of the card shipping name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingFirstName($value) - { - return $this->setParameter('shippingFirstName', $value); - } - - /** - * Get the last part of the card shipping name. - * - * @return string - */ - public function getShippingLastName() - { - return $this->getParameter('shippingLastName'); - } - - /** - * Sets the last part of the card shipping name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingLastName($value) - { - return $this->setParameter('shippingLastName', $value); - } - - /** - * Get the shipping company name. - * - * @return string - */ - public function getShippingCompany() - { - return $this->getParameter('shippingCompany'); - } - - /** - * Sets the shipping company name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingCompany($value) - { - return $this->setParameter('shippingCompany', $value); - } - - /** - * Get the shipping address, line 1. - * - * @return string - */ - public function getShippingAddress1() - { - return $this->getParameter('shippingAddress1'); - } - - /** - * Sets the shipping address, line 1. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingAddress1($value) - { - return $this->setParameter('shippingAddress1', $value); - } - - /** - * Get the shipping address, line 2. - * - * @return string - */ - public function getShippingAddress2() - { - return $this->getParameter('shippingAddress2'); - } - - /** - * Sets the shipping address, line 2. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingAddress2($value) - { - return $this->setParameter('shippingAddress2', $value); - } - - /** - * Get the shipping city. - * - * @return string - */ - public function getShippingCity() - { - return $this->getParameter('shippingCity'); - } - - /** - * Sets the shipping city. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingCity($value) - { - return $this->setParameter('shippingCity', $value); - } - - /** - * Get the shipping postcode. - * - * @return string - */ - public function getShippingPostcode() - { - return $this->getParameter('shippingPostcode'); - } - - /** - * Sets the shipping postcode. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingPostcode($value) - { - return $this->setParameter('shippingPostcode', $value); - } - - /** - * Get the shipping state. - * - * @return string - */ - public function getShippingState() - { - return $this->getParameter('shippingState'); - } - - /** - * Sets the shipping state. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingState($value) - { - return $this->setParameter('shippingState', $value); - } - - /** - * Get the shipping country. - * - * @return string - */ - public function getShippingCountry() - { - return $this->getParameter('shippingCountry'); - } - - /** - * Sets the shipping country. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingCountry($value) - { - return $this->setParameter('shippingCountry', $value); - } - - /** - * Get the shipping phone number. - * - * @return string - */ - public function getShippingPhone() - { - return $this->getParameter('shippingPhone'); - } - - /** - * Sets the shipping phone number. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingPhone($value) - { - return $this->setParameter('shippingPhone', $value); - } - - /** - * Get the shipping phone number extension. - * - * @return string - */ - public function getShippingPhoneExtension() - { - return $this->getParameter('shippingPhoneExtension'); - } - - /** - * Sets the shipping phone number extension. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingPhoneExtension($value) - { - return $this->setParameter('shippingPhoneExtension', $value); - } - - /** - * Get the shipping fax number. - * - * @return string - */ - public function getShippingFax() - { - return $this->getParameter('shippingFax'); - } - - /** - * Sets the shipping fax number. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setShippingFax($value) - { - return $this->setParameter('shippingFax', $value); - } - - /** - * Get the billing address, line 1. - * - * @return string - */ - public function getAddress1() - { - return $this->getParameter('billingAddress1'); - } - - /** - * Sets the billing and shipping address, line 1. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setAddress1($value) - { - $this->setParameter('billingAddress1', $value); - $this->setParameter('shippingAddress1', $value); - - return $this; - } - - /** - * Get the billing address, line 2. - * - * @return string - */ - public function getAddress2() - { - return $this->getParameter('billingAddress2'); - } - - /** - * Sets the billing and shipping address, line 2. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setAddress2($value) - { - $this->setParameter('billingAddress2', $value); - $this->setParameter('shippingAddress2', $value); - - return $this; - } - - /** - * Get the billing city. - * - * @return string - */ - public function getCity() - { - return $this->getParameter('billingCity'); - } - - /** - * Sets the billing and shipping city. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setCity($value) - { - $this->setParameter('billingCity', $value); - $this->setParameter('shippingCity', $value); - - return $this; - } - - /** - * Get the billing postcode. - * - * @return string - */ - public function getPostcode() - { - return $this->getParameter('billingPostcode'); - } - - /** - * Sets the billing and shipping postcode. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setPostcode($value) - { - $this->setParameter('billingPostcode', $value); - $this->setParameter('shippingPostcode', $value); - - return $this; - } - - /** - * Get the billing state. - * - * @return string - */ - public function getState() - { - return $this->getParameter('billingState'); - } - - /** - * Sets the billing and shipping state. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setState($value) - { - $this->setParameter('billingState', $value); - $this->setParameter('shippingState', $value); - - return $this; - } - - /** - * Get the billing country. - * - * @return string - */ - public function getCountry() - { - return $this->getParameter('billingCountry'); - } - - /** - * Sets the billing and shipping country. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setCountry($value) - { - $this->setParameter('billingCountry', $value); - $this->setParameter('shippingCountry', $value); - - return $this; - } - - /** - * Get the billing phone number. - * - * @return string - */ - public function getPhone() - { - return $this->getParameter('billingPhone'); - } - - /** - * Sets the billing and shipping phone number. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setPhone($value) - { - $this->setParameter('billingPhone', $value); - $this->setParameter('shippingPhone', $value); - - return $this; - } - - /** - * Get the billing phone number extension. - * - * @return string - */ - public function getPhoneExtension() - { - return $this->getParameter('billingPhoneExtension'); - } - - /** - * Sets the billing and shipping phone number extension. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setPhoneExtension($value) - { - $this->setParameter('billingPhoneExtension', $value); - $this->setParameter('shippingPhoneExtension', $value); - - return $this; - } - - /** - * Get the billing fax number.. - * - * @return string - */ - public function getFax() - { - return $this->getParameter('billingFax'); - } - - /** - * Sets the billing and shipping fax number. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setFax($value) - { - $this->setParameter('billingFax', $value); - $this->setParameter('shippingFax', $value); - - return $this; - } - - /** - * Get the card billing company name. - * - * @return string - */ - public function getCompany() - { - return $this->getParameter('billingCompany'); - } - - /** - * Sets the billing and shipping company name. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setCompany($value) - { - $this->setParameter('billingCompany', $value); - $this->setParameter('shippingCompany', $value); - - return $this; - } - - /** - * Get the cardholder's email address. - * - * @return string - */ - public function getEmail() - { - return $this->getParameter('email'); - } - - /** - * Sets the cardholder's email address. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setEmail($value) - { - return $this->setParameter('email', $value); - } - - /** - * Get the cardholder's birthday. - * - * @return string - */ - public function getBirthday($format = 'Y-m-d') - { - $value = $this->getParameter('birthday'); - - return $value ? $value->format($format) : null; - } - - /** - * Sets the cardholder's birthday. - * - * @param string $value - * @return CreditCard provides a fluent interface. - */ - public function setBirthday($value) - { - if ($value) { - $value = new DateTime($value, new DateTimeZone('UTC')); - } else { - $value = null; - } - - return $this->setParameter('birthday', $value); - } - - /** - * Get the cardholder's gender. - * - * @return string - */ - public function getGender() - { - return $this->getParameter('gender'); - } - - /** - * Sets the cardholder's gender. - * - * @param string $value - * @return CreditCard provides a fluent interface. + * @return Customer */ - public function setGender($value) + public function getCustomer() { - return $this->setParameter('gender', $value); + return $this->getBillingCustomer(); } } diff --git a/src/Omnipay/Common/Customer.php b/src/Omnipay/Common/Customer.php new file mode 100644 index 00000000..c0a7d191 --- /dev/null +++ b/src/Omnipay/Common/Customer.php @@ -0,0 +1,414 @@ +initialize($parameters); + } + + /** + * Get Card Title. + * + * @return string + */ + public function getTitle() + { + return $this->getParameter('title'); + } + + /** + * Set Card Title. + * + * @param string $value Parameter value + * @return CreditCard provides a fluent interface. + */ + public function setTitle($value) + { + $this->setParameter('title', $value); + + return $this; + } + + /** + * Get Card First Name. + * + * @return string + */ + public function getFirstName() + { + return $this->getParameter('firstName'); + } + + /** + * Set Card First Name (Billing and Shipping). + * + * @param string $value Parameter value + * @return CreditCard provides a fluent interface. + */ + public function setFirstName($value) + { + $this->setParameter('firstName', $value); + + return $this; + } + + /** + * Get Card Last Name. + * + * @return string + */ + public function getLastName() + { + return $this->getParameter('lastName'); + } + + /** + * Set Card Last Name (Billing and Shipping). + * + * @param string $value Parameter value + * @return CreditCard provides a fluent interface. + */ + public function setLastName($value) + { + $this->setParameter('lastName', $value); + + return $this; + } + + /** + * Get Card Name. + * + * @return string + */ + public function getName() + { + return trim($this->getFirstName() . ' ' . $this->getLastName()); + } + + /** + * Set Card Name (Billing and Shipping). + * + * @param string $value Parameter value + * @return CreditCard provides a fluent interface. + */ + public function setName($value) + { + $names = explode(' ', $value, 2); + $this->setFirstName($names[0]); + $this->setLastName(isset($names[1]) ? $names[1] : null); + + return $this; + } + + /** + * Get the address, line 1. + * + * @return string + */ + public function getAddress1() + { + return $this->getParameter('address1'); + } + + /** + * Sets the address, line 1. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setAddress1($value) + { + $this->setParameter('address1', $value); + + return $this; + } + + /** + * Get the address, line 2. + * + * @return string + */ + public function getAddress2() + { + return $this->getParameter('address2'); + } + + /** + * Sets the address, line 2. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setAddress2($value) + { + $this->setParameter('address2', $value); + + return $this; + } + + /** + * Get the city. + * + * @return string + */ + public function getCity() + { + return $this->getParameter('city'); + } + + /** + * Sets the city. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setCity($value) + { + $this->setParameter('city', $value); + + return $this; + } + + /** + * Get the postcode. + * + * @return string + */ + public function getPostcode() + { + return $this->getParameter('postcode'); + } + + /** + * Sets the postcode. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setPostcode($value) + { + $this->setParameter('postcode', $value); + + return $this; + } + + /** + * Get the state. + * + * @return string + */ + public function getState() + { + return $this->getParameter('state'); + } + + /** + * Sets the state. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setState($value) + { + $this->setParameter('state', $value); + + return $this; + } + + /** + * Get the country. + * + * @return string + */ + public function getCountry() + { + return $this->getParameter('country'); + } + + /** + * Sets the country. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setCountry($value) + { + $this->setParameter('country', $value); + + return $this; + } + + /** + * Get the phone number. + * + * @return string + */ + public function getPhone() + { + return $this->getParameter('phone'); + } + + /** + * Sets the phone number. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setPhone($value) + { + $this->setParameter('phone', $value); + + return $this; + } + + /** + * Get the phone number extension. + * + * @return string + */ + public function getPhoneExtension() + { + return $this->getParameter('phoneExtension'); + } + + /** + * Sets the phone number extension. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setPhoneExtension($value) + { + $this->setParameter('phoneExtension', $value); + + return $this; + } + + /** + * Get the fax number.. + * + * @return string + */ + public function getFax() + { + return $this->getParameter('fax'); + } + + /** + * Sets the fax number. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setFax($value) + { + $this->setParameter('fax', $value); + + return $this; + } + + /** + * Get the card company name. + * + * @return string + */ + public function getCompany() + { + return $this->getParameter('company'); + } + + /** + * Sets the company name. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setCompany($value) + { + $this->setParameter('company', $value); + + return $this; + } + + /** + * Get the cardholder's email address. + * + * @return string + */ + public function getEmail() + { + return $this->getParameter('email'); + } + + /** + * Sets the cardholder's email address. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setEmail($value) + { + return $this->setParameter('email', $value); + } + + /** + * Get the cardholder's birthday. + * + * @return string + */ + public function getBirthday($format = 'Y-m-d') + { + $value = $this->getParameter('birthday'); + + return $value ? $value->format($format) : null; + } + + /** + * Sets the cardholder's birthday. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setBirthday($value) + { + if ($value) { + $value = new DateTime($value, new DateTimeZone('UTC')); + } else { + $value = null; + } + + return $this->setParameter('birthday', $value); + } + + /** + * Get the cardholder's gender. + * + * @return string + */ + public function getGender() + { + return $this->getParameter('gender'); + } + + /** + * Sets the cardholder's gender. + * + * @param string $value + * @return CreditCard provides a fluent interface. + */ + public function setGender($value) + { + return $this->setParameter('gender', $value); + } +} diff --git a/tests/Omnipay/Common/CreditCardTest.php b/tests/Omnipay/Common/CreditCardTest.php index 47649de4..ea2c704b 100644 --- a/tests/Omnipay/Common/CreditCardTest.php +++ b/tests/Omnipay/Common/CreditCardTest.php @@ -10,8 +10,6 @@ public function setUp() { $this->card = new CreditCard; $this->card->setNumber('4111111111111111'); - $this->card->setFirstName('Example'); - $this->card->setLastName('Customer'); $this->card->setExpiryMonth('4'); $this->card->setExpiryYear(gmdate('Y')+2); $this->card->setCvv('123'); @@ -19,29 +17,26 @@ public function setUp() public function testConstructWithParams() { - $card = new CreditCard(array('name' => 'Test Customer')); - $this->assertSame('Test Customer', $card->getName()); + $card = new CreditCard(array('number' => '4111111111111111')); + $this->assertSame('4111111111111111', $card->getNumber()); } public function testInitializeWithParams() { $card = new CreditCard; - $card->initialize(array('name' => 'Test Customer')); - $this->assertSame('Test Customer', $card->getName()); + $card->initialize(array('number' => '4111111111111111')); + $this->assertSame('4111111111111111', $card->getNumber()); } public function testGetParamters() { $card = new CreditCard(array( - 'name' => 'Example Customer', 'number' => '1234', 'expiryMonth' => 6, 'expiryYear' => 2016, )); $parameters = $card->getParameters(); - $this->assertSame('Example', $parameters['billingFirstName']); - $this->assertSame('Customer', $parameters['billingLastName']); $this->assertSame('1234', $parameters['number']); $this->assertSame(6, $parameters['expiryMonth']); $this->assertSame(2016, $parameters['expiryYear']); @@ -124,52 +119,6 @@ public function testCustomBrandWorks() $this->assertEquals('omniexpress', $this->card->getBrand()); } - public function testTitle() - { - $this->card->setTitle('Mr.'); - $this->assertEquals('Mr.', $this->card->getTitle()); - } - - public function testFirstName() - { - $this->card->setFirstName('Bob'); - $this->assertEquals('Bob', $this->card->getFirstName()); - } - - public function testLastName() - { - $this->card->setLastName('Smith'); - $this->assertEquals('Smith', $this->card->getLastName()); - } - - public function testGetName() - { - $this->card->setFirstName('Bob'); - $this->card->setLastName('Smith'); - $this->assertEquals('Bob Smith', $this->card->getName()); - } - - public function testSetName() - { - $this->card->setName('Bob Smith'); - $this->assertEquals('Bob', $this->card->getFirstName()); - $this->assertEquals('Smith', $this->card->getLastName()); - } - - public function testSetNameWithOneName() - { - $this->card->setName('Bob'); - $this->assertEquals('Bob', $this->card->getFirstName()); - $this->assertEquals('', $this->card->getLastName()); - } - - public function testSetNameWithMultipleNames() - { - $this->card->setName('Bob John Smith'); - $this->assertEquals('Bob', $this->card->getFirstName()); - $this->assertEquals('John Smith', $this->card->getLastName()); - } - public function testNumber() { $this->card->setNumber('4000000000000000'); @@ -330,302 +279,6 @@ public function testIssueNumber() $this->assertSame('12', $this->card->getIssueNumber()); } - public function testBillingTitle() - { - $this->card->setBillingTitle('Mrs.'); - $this->assertEquals('Mrs.', $this->card->getBillingTitle()); - $this->assertEquals('Mrs.', $this->card->getTitle()); - } - - public function testBillingFirstName() - { - $this->card->setBillingFirstName('Bob'); - $this->assertEquals('Bob', $this->card->getBillingFirstName()); - $this->assertEquals('Bob', $this->card->getFirstName()); - } - - public function testBillingLastName() - { - $this->card->setBillingLastName('Smith'); - $this->assertEquals('Smith', $this->card->getBillingLastName()); - $this->assertEquals('Smith', $this->card->getLastName()); - } - - public function testBillingName() - { - $this->card->setBillingFirstName('Bob'); - $this->card->setBillingLastName('Smith'); - $this->assertEquals('Bob Smith', $this->card->getBillingName()); - - $this->card->setBillingName('John Foo'); - $this->assertEquals('John', $this->card->getBillingFirstName()); - $this->assertEquals('Foo', $this->card->getBillingLastName()); - } - - public function testBillingCompany() - { - $this->card->setBillingCompany('SuperSoft'); - $this->assertEquals('SuperSoft', $this->card->getBillingCompany()); - $this->assertEquals('SuperSoft', $this->card->getCompany()); - } - - public function testBillingAddress1() - { - $this->card->setBillingAddress1('31 Spooner St'); - $this->assertEquals('31 Spooner St', $this->card->getBillingAddress1()); - $this->assertEquals('31 Spooner St', $this->card->getAddress1()); - } - - public function testBillingAddress2() - { - $this->card->setBillingAddress2('Suburb'); - $this->assertEquals('Suburb', $this->card->getBillingAddress2()); - $this->assertEquals('Suburb', $this->card->getAddress2()); - } - - public function testBillingCity() - { - $this->card->setBillingCity('Quahog'); - $this->assertEquals('Quahog', $this->card->getBillingCity()); - $this->assertEquals('Quahog', $this->card->getCity()); - } - - public function testBillingPostcode() - { - $this->card->setBillingPostcode('12345'); - $this->assertEquals('12345', $this->card->getBillingPostcode()); - $this->assertEquals('12345', $this->card->getPostcode()); - } - - public function testBillingState() - { - $this->card->setBillingState('RI'); - $this->assertEquals('RI', $this->card->getBillingState()); - $this->assertEquals('RI', $this->card->getState()); - } - - public function testBillingCountry() - { - $this->card->setBillingCountry('US'); - $this->assertEquals('US', $this->card->getBillingCountry()); - $this->assertEquals('US', $this->card->getCountry()); - } - - public function testBillingPhone() - { - $this->card->setBillingPhone('12345'); - $this->assertSame('12345', $this->card->getBillingPhone()); - $this->assertSame('12345', $this->card->getPhone()); - } - - public function testBillingPhoneExtension() - { - $this->card->setBillingPhoneExtension('001'); - $this->assertSame('001', $this->card->getBillingPhoneExtension()); - $this->assertSame('001', $this->card->getPhoneExtension()); - } - - public function testBillingFax() - { - $this->card->setBillingFax('54321'); - $this->assertSame('54321', $this->card->getBillingFax()); - $this->assertSame('54321', $this->card->getFax()); - } - - public function testShippingTitle() - { - $this->card->setShippingTitle('Dr.'); - $this->assertEquals('Dr.', $this->card->getShippingTitle()); - } - - public function testShippingFirstName() - { - $this->card->setShippingFirstName('James'); - $this->assertEquals('James', $this->card->getShippingFirstName()); - } - - public function testShippingLastName() - { - $this->card->setShippingLastName('Doctor'); - $this->assertEquals('Doctor', $this->card->getShippingLastName()); - } - - public function testShippingName() - { - $this->card->setShippingFirstName('Bob'); - $this->card->setShippingLastName('Smith'); - $this->assertEquals('Bob Smith', $this->card->getShippingName()); - - $this->card->setShippingName('John Foo'); - $this->assertEquals('John', $this->card->getShippingFirstName()); - $this->assertEquals('Foo', $this->card->getShippingLastName()); - } - - public function testShippingCompany() - { - $this->card->setShippingCompany('SuperSoft'); - $this->assertEquals('SuperSoft', $this->card->getShippingCompany()); - } - - public function testShippingAddress1() - { - $this->card->setShippingAddress1('31 Spooner St'); - $this->assertEquals('31 Spooner St', $this->card->getShippingAddress1()); - } - - public function testShippingAddress2() - { - $this->card->setShippingAddress2('Suburb'); - $this->assertEquals('Suburb', $this->card->getShippingAddress2()); - } - - public function testShippingCity() - { - $this->card->setShippingCity('Quahog'); - $this->assertEquals('Quahog', $this->card->getShippingCity()); - } - - public function testShippingPostcode() - { - $this->card->setShippingPostcode('12345'); - $this->assertEquals('12345', $this->card->getShippingPostcode()); - } - - public function testShippingState() - { - $this->card->setShippingState('RI'); - $this->assertEquals('RI', $this->card->getShippingState()); - } - - public function testShippingCountry() - { - $this->card->setShippingCountry('US'); - $this->assertEquals('US', $this->card->getShippingCountry()); - } - - public function testShippingPhone() - { - $this->card->setShippingPhone('12345'); - $this->assertEquals('12345', $this->card->getShippingPhone()); - } - - public function testShippingPhoneExtension() - { - $this->card->setShippingPhoneExtension('001'); - $this->assertEquals('001', $this->card->getShippingPhoneExtension()); - } - - public function testShippingFax() - { - $this->card->setShippingFax('54321'); - $this->assertEquals('54321', $this->card->getShippingFax()); - } - - public function testCompany() - { - $this->card->setCompany('FooBar'); - $this->assertEquals('FooBar', $this->card->getCompany()); - $this->assertEquals('FooBar', $this->card->getBillingCompany()); - $this->assertEquals('FooBar', $this->card->getShippingCompany()); - } - - public function testAddress1() - { - $this->card->setAddress1('31 Spooner St'); - $this->assertEquals('31 Spooner St', $this->card->getAddress1()); - $this->assertEquals('31 Spooner St', $this->card->getBillingAddress1()); - $this->assertEquals('31 Spooner St', $this->card->getShippingAddress1()); - } - - public function testAddress2() - { - $this->card->setAddress2('Suburb'); - $this->assertEquals('Suburb', $this->card->getAddress2()); - $this->assertEquals('Suburb', $this->card->getBillingAddress2()); - $this->assertEquals('Suburb', $this->card->getShippingAddress2()); - } - - public function testCity() - { - $this->card->setCity('Quahog'); - $this->assertEquals('Quahog', $this->card->getCity()); - $this->assertEquals('Quahog', $this->card->getBillingCity()); - $this->assertEquals('Quahog', $this->card->getShippingCity()); - } - - public function testPostcode() - { - $this->card->setPostcode('12345'); - $this->assertEquals('12345', $this->card->getPostcode()); - $this->assertEquals('12345', $this->card->getBillingPostcode()); - $this->assertEquals('12345', $this->card->getShippingPostcode()); - } - - public function testState() - { - $this->card->setState('RI'); - $this->assertEquals('RI', $this->card->getState()); - $this->assertEquals('RI', $this->card->getBillingState()); - $this->assertEquals('RI', $this->card->getShippingState()); - } - - public function testCountry() - { - $this->card->setCountry('US'); - $this->assertEquals('US', $this->card->getCountry()); - $this->assertEquals('US', $this->card->getBillingCountry()); - $this->assertEquals('US', $this->card->getShippingCountry()); - } - - public function testPhone() - { - $this->card->setPhone('12345'); - $this->assertEquals('12345', $this->card->getPhone()); - $this->assertEquals('12345', $this->card->getBillingPhone()); - $this->assertEquals('12345', $this->card->getShippingPhone()); - } - - public function testPhoneExtension() - { - $this->card->setPhoneExtension('001'); - $this->assertEquals('001', $this->card->getPhoneExtension()); - $this->assertEquals('001', $this->card->getBillingPhoneExtension()); - $this->assertEquals('001', $this->card->getShippingPhoneExtension()); - } - - public function testFax() - { - $this->card->setFax('54321'); - $this->assertEquals('54321', $this->card->getFax()); - $this->assertEquals('54321', $this->card->getBillingFax()); - $this->assertEquals('54321', $this->card->getShippingFax()); - } - - public function testEmail() - { - $this->card->setEmail('adrian@example.com'); - $this->assertEquals('adrian@example.com', $this->card->getEmail()); - } - - public function testBirthday() - { - $this->card->setBirthday('01-02-2000'); - $this->assertEquals('2000-02-01', $this->card->getBirthday()); - $this->assertEquals('01/02/2000', $this->card->getBirthday('d/m/Y')); - } - - public function testBirthdayEmpty() - { - $this->card->setBirthday(''); - $this->assertNull($this->card->getBirthday()); - } - - public function testGender() - { - $this->card->setGender('female'); - $this->assertEquals('female', $this->card->getGender()); - } - /** * @expectedException Omnipay\Common\Exception\InvalidCreditCardException * @expectedExceptionMessage Card number is invalid diff --git a/tests/Omnipay/Common/CustomerTest.php b/tests/Omnipay/Common/CustomerTest.php new file mode 100644 index 00000000..229a8579 --- /dev/null +++ b/tests/Omnipay/Common/CustomerTest.php @@ -0,0 +1,161 @@ +setTitle('Mr.'); + $this->assertEquals('Mr.', $customer->getTitle()); + } + + public function testFirstName() + { + $customer = new Customer(); + $customer->setFirstName('Bob'); + $this->assertEquals('Bob', $customer->getFirstName()); + } + + public function testLastName() + { + $customer = new Customer(); + $customer->setLastName('Smith'); + $this->assertEquals('Smith', $customer->getLastName()); + } + + public function testGetName() + { + $customer = new Customer(); + $customer->setFirstName('Bob'); + $customer->setLastName('Smith'); + $this->assertEquals('Bob Smith', $customer->getName()); + } + + public function testSetName() + { + $customer = new Customer(); + $customer->setName('Bob Smith'); + $this->assertEquals('Bob', $customer->getFirstName()); + $this->assertEquals('Smith', $customer->getLastName()); + } + + public function testSetNameWithOneName() + { + $customer = new Customer(); + $customer->setName('Bob'); + $this->assertEquals('Bob', $customer->getFirstName()); + $this->assertEquals('', $customer->getLastName()); + } + + public function testSetNameWithMultipleNames() + { + $customer = new Customer(); + $customer->setName('Bob John Smith'); + $this->assertEquals('Bob', $customer->getFirstName()); + $this->assertEquals('John Smith', $customer->getLastName()); + } + + public function testCompany() + { + $customer = new Customer(); + $customer->setCompany('FooBar'); + $this->assertEquals('FooBar', $customer->getCompany()); + } + + public function testAddress1() + { + $customer = new Customer(); + $customer->setAddress1('31 Spooner St'); + $this->assertEquals('31 Spooner St', $customer->getAddress1()); + } + + public function testAddress2() + { + $customer = new Customer(); + $customer->setAddress2('Suburb'); + $this->assertEquals('Suburb', $customer->getAddress2()); + } + + public function testCity() + { + $customer = new Customer(); + $customer->setCity('Quahog'); + $this->assertEquals('Quahog', $customer->getCity()); + } + + public function testPostcode() + { + $customer = new Customer(); + $customer->setPostcode('12345'); + $this->assertEquals('12345', $customer->getPostcode()); + } + + public function testState() + { + $customer = new Customer(); + $customer->setState('RI'); + $this->assertEquals('RI', $customer->getState()); + } + + public function testCountry() + { + $customer = new Customer(); + $customer->setCountry('US'); + $this->assertEquals('US', $customer->getCountry()); + } + + public function testPhone() + { + $customer = new Customer(); + $customer->setPhone('12345'); + $this->assertEquals('12345', $customer->getPhone()); + } + + public function testPhoneExtension() + { + $customer = new Customer(); + $customer->setPhoneExtension('001'); + $this->assertEquals('001', $customer->getPhoneExtension()); + } + + public function testFax() + { + $customer = new Customer(); + $customer->setFax('54321'); + $this->assertEquals('54321', $customer->getFax()); + } + + public function testEmail() + { + $customer = new Customer(); + $customer->setEmail('adrian@example.com'); + $this->assertEquals('adrian@example.com', $customer->getEmail()); + } + + public function testBirthday() + { + $customer = new Customer(); + $customer->setBirthday('01-02-2000'); + $this->assertEquals('2000-02-01', $customer->getBirthday()); + $this->assertEquals('01/02/2000', $customer->getBirthday('d/m/Y')); + } + + public function testBirthdayEmpty() + { + $customer = new Customer(); + $customer->setBirthday(''); + $this->assertNull($customer->getBirthday()); + } + + public function testGender() + { + $customer = new Customer(); + $customer->setGender('female'); + $this->assertEquals('female', $customer->getGender()); + } +} From 83aecd0dc2ba51ba15b6846270aca44edb395859 Mon Sep 17 00:00:00 2001 From: Kayla Daniels Date: Mon, 8 Feb 2016 22:46:04 -0500 Subject: [PATCH 20/51] fix styling and tests --- composer.json | 1 + src/Omnipay/Common/AbstractGateway.php | 5 ---- src/Omnipay/Common/Item.php | 9 ++------ src/Omnipay/Common/ParameterizedInterface.php | 6 ++--- tests/Omnipay/Common/AbstractGatewayTest.php | 2 +- tests/Omnipay/Common/HelperTest.php | 23 +------------------ 6 files changed, 8 insertions(+), 38 deletions(-) diff --git a/composer.json b/composer.json index 1f680de6..e81c6866 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "require": { "php": ">=5.6", "alcohol/iso4217": "^3.1", + "beberlei/assert": "^2.4", "guzzlehttp/guzzle": "^6.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.1.0" diff --git a/src/Omnipay/Common/AbstractGateway.php b/src/Omnipay/Common/AbstractGateway.php index 6ecde32c..e69c0b78 100755 --- a/src/Omnipay/Common/AbstractGateway.php +++ b/src/Omnipay/Common/AbstractGateway.php @@ -46,11 +46,6 @@ abstract class AbstractGateway implements GatewayInterface, ParameterizedInterfa { use HasParametersTrait; - /** - * @var ParameterBag - */ - protected $parameters; - /** * @var ClientInterface */ diff --git a/src/Omnipay/Common/Item.php b/src/Omnipay/Common/Item.php index 476504bc..7c4e1c6d 100644 --- a/src/Omnipay/Common/Item.php +++ b/src/Omnipay/Common/Item.php @@ -16,17 +16,12 @@ class Item implements ItemInterface, ParameterizedInterface { use HasParametersTrait; - /** - * @var ParameterBag - */ - protected $parameters; - /** * Create a new item with the specified parameters * - * @param array|null $parameters An array of parameters to set on the new object + * @param array $parameters An array of parameters to set on the new object */ - public function __construct($parameters = null) + public function __construct(array $parameters = []) { $this->initialize($parameters); } diff --git a/src/Omnipay/Common/ParameterizedInterface.php b/src/Omnipay/Common/ParameterizedInterface.php index b4624a6d..e3a45d6e 100644 --- a/src/Omnipay/Common/ParameterizedInterface.php +++ b/src/Omnipay/Common/ParameterizedInterface.php @@ -2,19 +2,19 @@ interface ParameterizedInterface { - function setParameter($key, $value); + public function setParameter($key, $value); /** * Get one parameter. * * @return mixed A single parameter value. */ - function getParameter($key); + public function getParameter($key); /** * Get all parameters. * * @return array An associative array of parameters. */ - function getParameters(); + public function getParameters(); } diff --git a/tests/Omnipay/Common/AbstractGatewayTest.php b/tests/Omnipay/Common/AbstractGatewayTest.php index 26d25ba2..a94bf87e 100755 --- a/tests/Omnipay/Common/AbstractGatewayTest.php +++ b/tests/Omnipay/Common/AbstractGatewayTest.php @@ -55,7 +55,7 @@ public function testInitializeParameters() 'unknown' => '42', )); - $this->assertSame(array('currency' => 'USD'), $this->gateway->getParameters()); + $this->assertSame(array('currency' => 'USD', 'unknown' => '42'), $this->gateway->getParameters()); } public function testGetDefaultParameters() diff --git a/tests/Omnipay/Common/HelperTest.php b/tests/Omnipay/Common/HelperTest.php index 55b7a4e5..5325310b 100644 --- a/tests/Omnipay/Common/HelperTest.php +++ b/tests/Omnipay/Common/HelperTest.php @@ -43,33 +43,12 @@ public function testValidateLuhnNull() $this->assertTrue($result); } - public function testInitializeIgnoresNull() - { - $target = m::mock(); - Helper::initializeParameters($target, null); - } - - public function testInitializeIgnoresString() - { - $target = m::mock(); - Helper::initializeParameters($target, 'invalid'); - } - public function testInitializeCallsSetters() { $target = m::mock('\Omnipay\Common\CreditCard'); - $target->shouldReceive('setName')->once()->with('adrian'); $target->shouldReceive('setNumber')->once()->with('1234'); - Helper::initializeParameters($target, ['name' => 'adrian', 'number' => '1234']); - } - - public function testInitializeIgnoresInvalidParameters() - { - $target = m::mock('\Omnipay\Common\CreditCard'); - $target->shouldReceive('setName')->once()->with('adrian'); - - Helper::initializeParameters($target, ['name' => 'adrian', 'extra' => 'invalid']); + Helper::initializeParameters($target, ['number' => '1234']); } public function testGetGatewayShortNameSimple() From b57174d155ade495d25e8b183e7171bdc9728514 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 7 Feb 2016 20:05:56 +0100 Subject: [PATCH 21/51] Use Amount object with integer instead of float/string Make final, update tests, tweak decimal Fix tests Allow currency in request Make currency required Add negative float checks --- src/Omnipay/Common/Amount.php | 130 ++++++++++++ src/Omnipay/Common/Currency.php | 4 +- .../Common/Message/AbstractRequest.php | 141 +++---------- tests/Omnipay/Common/AmountTest.php | 182 ++++++++++++++++ tests/Omnipay/Common/CurrencyTest.php | 7 + .../Common/Message/AbstractRequestTest.php | 198 +++--------------- 6 files changed, 381 insertions(+), 281 deletions(-) create mode 100644 src/Omnipay/Common/Amount.php create mode 100644 tests/Omnipay/Common/AmountTest.php diff --git a/src/Omnipay/Common/Amount.php b/src/Omnipay/Common/Amount.php new file mode 100644 index 00000000..c441c207 --- /dev/null +++ b/src/Omnipay/Common/Amount.php @@ -0,0 +1,130 @@ +amount = (string) $amount; + $this->currency = self::findCurrency($currency); + } + + /** + * Get the amount in smallest units (eg. cents) + * + * @return string + */ + public function getAmount() + { + return $this->amount; + } + + /** + * Get the amount as decimal string + * + * @return string + */ + public function getFormatted() + { + $decimals = $this->currency->getDecimals(); + $amount = $this->amount / pow(10, $decimals); + + return number_format( + $amount, + $decimals, + '.', + '' + ); + } + + /** + * Get the currency + * + * @return Currency + */ + public function getCurrency() + { + return $this->currency; + } + + /** + * @return bool + */ + public function isNegative() + { + return $this->amount < 0; + } + + /** + * @return bool + */ + public function isZero() + { + return $this->amount == 0; + } + + /** + * Get the amount, based on a decimal string + * + * @param string|float $amount + * @param string|Currency $currency + * @return static + */ + public static function fromDecimal($amount, $currency) + { + $amount = Helper::toFloat($amount); + + $currency = self::findCurrency($currency); + $factor = pow(10, $currency->getDecimals()); + $amount = (int) round($amount * $factor); + + return new self($amount, $currency); + } + + /** + * @param string|Currency $currencyCode + * @return Currency + */ + private static function findCurrency($currencyCode) + { + if ($currencyCode instanceof Currency) { + return $currencyCode; + } elseif (is_string($currencyCode) || is_integer($currencyCode)) { + $currency = Currency::find($currencyCode); + if (is_null($currency)) { + throw new InvalidArgumentException('Invalid currency'); + } + return $currency; + } + + throw new InvalidArgumentException('Currency must be a string or Currency object'); + } +} diff --git a/src/Omnipay/Common/Currency.php b/src/Omnipay/Common/Currency.php index 0f915595..eb70432a 100644 --- a/src/Omnipay/Common/Currency.php +++ b/src/Omnipay/Common/Currency.php @@ -71,8 +71,8 @@ public static function find($code) $iso4217 = new ISO4217(); try { - $currency = $iso4217->getByCode($code); - } catch (\OutOfBoundsException $e) { + $currency = $iso4217->getByAlpha3($code); + } catch (\Exception $e) { return null; } diff --git a/src/Omnipay/Common/Message/AbstractRequest.php b/src/Omnipay/Common/Message/AbstractRequest.php index aec0f946..747ec873 100644 --- a/src/Omnipay/Common/Message/AbstractRequest.php +++ b/src/Omnipay/Common/Message/AbstractRequest.php @@ -5,6 +5,7 @@ namespace Omnipay\Common\Message; +use Omnipay\Common\Amount; use Omnipay\Common\Http\ClientInterface; use Omnipay\Common\CreditCard; use Omnipay\Common\Currency; @@ -283,155 +284,71 @@ public function setCardReference($value) } /** - * Convert an amount into a float. - * - * @var string|int|float $value The value to convert. - * @throws InvalidRequestException on any validation failure. - * @return float The amount converted to a float. + * @return string */ + protected function getCurrency() + { + return strtoupper($this->getParameter('currency')); + } - public function toFloat($value) + /** + * @param string $value + * @return $this + */ + public function setCurrency($value) { - try { - return Helper::toFloat($value); - } catch (InvalidArgumentException $e) { - // Throw old exception for legacy implementations. - throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e); - } + return $this->setParameter('currency', $value); } /** - * Validates and returns the formated amount. + * Validates and returns amount as integer. * * @throws InvalidRequestException on any validation failure. - * @return string The amount formatted to the correct number of decimal places for the selected currency. + * @return string The amount in smallest unit possible (eg. 'cents') */ public function getAmount() { $amount = $this->getParameter('amount'); if ($amount !== null) { - // Don't allow integers for currencies that support decimals. - // This is for legacy reasons - upgrades from v0.9 - if ($this->getCurrencyDecimalPlaces() > 0) { - if (is_int($amount) || (is_string($amount) && false === strpos((string) $amount, '.'))) { - throw new InvalidRequestException( - 'Please specify amount as a string or float, ' - . 'with decimal places (e.g. \'10.00\' to represent $10.00).' - ); - }; - } + if (!$amount instanceof Amount) { + + // Default currency when none set + $currency = $this->getCurrency(); + + if ($currency == null) { + throw new InvalidRequestException('A currency is required.'); + } - $amount = $this->toFloat($amount); + $amount = new Amount($amount, $currency); + } // Check for a negative amount. - if (!$this->negativeAmountAllowed && $amount < 0) { + if (!$this->negativeAmountAllowed && $amount->isNegative()) { throw new InvalidRequestException('A negative amount is not allowed.'); } // Check for a zero amount. - if (!$this->zeroAmountAllowed && $amount === 0.0) { + if (!$this->zeroAmountAllowed && $amount->isZero()) { throw new InvalidRequestException('A zero amount is not allowed.'); } - // Check for rounding that may occur if too many significant decimal digits are supplied. - $decimal_count = strlen(substr(strrchr(sprintf('%.8g', $amount), '.'), 1)); - if ($decimal_count > $this->getCurrencyDecimalPlaces()) { - throw new InvalidRequestException('Amount precision is too high for currency.'); - } - - return $this->formatCurrency($amount); + return $amount; } } /** * Sets the payment amount. * - * @param string $value + * @param string|Amount $value * @return AbstractRequest Provides a fluent interface + * @throws InvalidRequestException */ public function setAmount($value) { return $this->setParameter('amount', $value); } - /** - * Get the payment amount as an integer. - * - * @return integer - */ - public function getAmountInteger() - { - return (int) round($this->getAmount() * $this->getCurrencyDecimalFactor()); - } - - /** - * Get the payment currency code. - * - * @return string - */ - public function getCurrency() - { - return $this->getParameter('currency'); - } - - /** - * Sets the payment currency code. - * - * @param string $value - * @return AbstractRequest Provides a fluent interface - */ - public function setCurrency($value) - { - return $this->setParameter('currency', strtoupper($value)); - } - - /** - * Get the payment currency number. - * - * @return integer - */ - public function getCurrencyNumeric() - { - if ($currency = Currency::find($this->getCurrency())) { - return $currency->getNumeric(); - } - } - - /** - * Get the number of decimal places in the payment currency. - * - * @return integer - */ - public function getCurrencyDecimalPlaces() - { - if ($currency = Currency::find($this->getCurrency())) { - return $currency->getDecimals(); - } - - return 2; - } - - private function getCurrencyDecimalFactor() - { - return pow(10, $this->getCurrencyDecimalPlaces()); - } - - /** - * Format an amount for the payment currency. - * - * @return string - */ - public function formatCurrency($amount) - { - return number_format( - $amount, - $this->getCurrencyDecimalPlaces(), - '.', - '' - ); - } - /** * Get the request description. * diff --git a/tests/Omnipay/Common/AmountTest.php b/tests/Omnipay/Common/AmountTest.php new file mode 100644 index 00000000..3a5120b9 --- /dev/null +++ b/tests/Omnipay/Common/AmountTest.php @@ -0,0 +1,182 @@ +assertSame('1000', $amount->getAmount()); + } + + public function testConstructInteger() + { + $amount = new Amount(1000, 'USD'); + $this->assertSame('1000', $amount->getAmount()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testConstructFloat() + { + new Amount(10.00, 'USD'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testConstructDecimalString() + { + new Amount('10.00', 'USD'); + } + + public function testFromDecimal() + { + $amount = Amount::fromDecimal('10.00', 'USD'); + $this->assertSame('1000', $amount->getAmount()); + } + + public function testFromDecimalRounded() + { + $amount = Amount::fromDecimal('10', 'USD'); + $this->assertSame('1000', $amount->getAmount()); + + $amount = Amount::fromDecimal(10, 'USD'); + $this->assertSame('1000', $amount->getAmount()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testDecimalInvalid() + { + Amount::fromDecimal('1,234.00', 'USD'); + } + + public function testCurrencyString() + { + $amount = new Amount(1000, 'EUR'); + $this->assertSame('EUR', $amount->getCurrency()->getCode()); + } + + public function testCurrencyObject() + { + $currency = Currency::find('EUR'); + $amount = new Amount(1000, $currency); + + $this->assertSame('EUR', $amount->getCurrency()->getCode()); + } + + public function testFormatted() + { + $amount = new Amount(1000, 'USD'); + + $this->assertSame('10.00', $amount->getFormatted()); + } + + public function testGetAmountNoDecimals() + { + $amount = new Amount(1366, 'JPY'); + + $this->assertSame('JPY', $amount->getCurrency()->getCode()); + $this->assertSame('1366', $amount->getAmount()); + $this->assertSame('1366', $amount->getFormatted()); + } + + public function testFromDecimalNoDecimals() + { + $amount = Amount::fromDecimal('10', 'JPY'); + $this->assertSame('10', $amount->getAmount()); + } + + public function testIsNegative() + { + $amount = new Amount(-1, 'USD'); + $this->assertTrue($amount->isNegative()); + + $amount = new Amount(0, 'USD'); + $this->assertFalse($amount->isNegative()); + + $amount = new Amount(1, 'USD'); + $this->assertFalse($amount->isNegative()); + } + + public function testIsZero() + { + $amount = new Amount(-1, 'USD'); + $this->assertFalse($amount->isZero()); + + $amount = new Amount(0, 'USD'); + $this->assertTrue($amount->isZero()); + + $amount = new Amount('0', 'USD'); + $this->assertTrue($amount->isZero()); + + $amount = Amount::fromDecimal('0.00', 'USD'); + $this->assertTrue($amount->isZero()); + + $amount = new Amount(1, 'USD'); + $this->assertFalse($amount->isZero()); + } + + public function testAmountNegativeDecimalString() + { + $amount = Amount::fromDecimal('-123.00', 'USD'); + + $this->assertEquals('-12300', $amount->getAmount()); + $this->assertTrue($amount->isNegative()); + } + + public function testAmountNegativeDecimalFloat() + { + $amount = Amount::fromDecimal(-123.00, 'USD'); + + $this->assertEquals('-12300', $amount->getAmount()); + $this->assertTrue($amount->isNegative()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testAmountThousandsSepThrowsException() + { + Amount::fromDecimal('1,234', 'USD'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testAmountInvalidFormatThrowsException() + { + new Amount('1.234', 'USD'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testAmountInvalidTypeThrowsException() + { + new Amount(true, 'USD'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testAmountNegativeStringThrowsException() + { + new Amount('-123.00', 'USD'); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testAmountNegativeFloatThrowsException() + { + new Amount(-123.00, 'USD'); + } +} diff --git a/tests/Omnipay/Common/CurrencyTest.php b/tests/Omnipay/Common/CurrencyTest.php index 6eadeffc..11acf6ea 100644 --- a/tests/Omnipay/Common/CurrencyTest.php +++ b/tests/Omnipay/Common/CurrencyTest.php @@ -25,6 +25,13 @@ public function testFindLowercase() $this->assertSame(2, $currency->getDecimals()); } + public function testNumericCodeReturnsNull() + { + $currency = Currency::find('840'); + + $this->assertNull($currency); + } + public function testUnknownCurrencyReturnsNull() { $currency = Currency::find('XYZ'); diff --git a/tests/Omnipay/Common/Message/AbstractRequestTest.php b/tests/Omnipay/Common/Message/AbstractRequestTest.php index 13383584..052b58d9 100644 --- a/tests/Omnipay/Common/Message/AbstractRequestTest.php +++ b/tests/Omnipay/Common/Message/AbstractRequestTest.php @@ -3,6 +3,7 @@ namespace Omnipay\Common\Message; use Mockery as m; +use Omnipay\Common\Amount; use Omnipay\Common\CreditCard; use Omnipay\Common\ItemBag; use Omnipay\Tests\TestCase; @@ -12,7 +13,9 @@ class AbstractRequestTest extends TestCase public function setUp() { $this->request = m::mock('\Omnipay\Common\Message\AbstractRequest')->makePartial(); - $this->request->initialize(); + $this->request->initialize([ + 'currency' => 'USD', + ]); } /** @@ -39,8 +42,8 @@ public function testConstruct() public function testInitializeWithParams() { - $this->assertSame($this->request, $this->request->initialize(array('amount' => '1.23'))); - $this->assertSame('1.23', $this->request->getAmount()); + $this->assertSame($this->request, $this->request->initialize(array('amount' => '123', 'currency' => 'USD'))); + $this->assertSame('123', $this->request->getAmount()->getAmount()); } /** @@ -87,14 +90,14 @@ public function testCardReference() public function testAmount() { - $this->assertSame($this->request, $this->request->setAmount('2.00')); - $this->assertSame('2.00', $this->request->getAmount()); + $this->assertSame($this->request, $this->request->setAmount(2)); + $this->assertSame('2', $this->request->getAmount()->getAmount()); } - public function testAmountWithFloat() + public function testAmountWithInt() { - $this->assertSame($this->request, $this->request->setAmount(2.0)); - $this->assertSame('2.00', $this->request->getAmount()); + $this->assertSame($this->request, $this->request->setAmount(2)); + $this->assertSame('2', $this->request->getAmount()->getAmount()); } public function testAmountWithEmpty() @@ -105,14 +108,14 @@ public function testAmountWithEmpty() public function testAmountZeroFloat() { - $this->assertSame($this->request, $this->request->setAmount(0.0)); - $this->assertSame('0.00', $this->request->getAmount()); + $this->assertSame($this->request, $this->request->setAmount(0)); + $this->assertSame('0', $this->request->getAmount()->getAmount()); } public function testAmountZeroString() { - $this->assertSame($this->request, $this->request->setAmount('0.000000')); - $this->assertSame('0.00', $this->request->getAmount()); + $this->assertSame($this->request, $this->request->setAmount('0')); + $this->assertSame('0', $this->request->getAmount()->getAmount()); } /** @@ -122,175 +125,35 @@ public function testAmountZeroString() public function testAmountZeroNotAllowed() { $this->changeProtectedProperty('zeroAmountAllowed', false); - $this->request->setAmount('0.00'); - $this->request->getAmount(); - } - - // See https://github.com/thephpleague/omnipay-common/issues/69 - public function testAmountPrecision() - { - // The default precision for PHP is 6 decimal places. - ini_set('precision', 6); - $this->assertSame($this->request, $this->request->setAmount('67.10')); - $this->assertSame('67.10', $this->request->getAmount()); - - // At 17 decimal places, 67.10 will echo as 67.09999... - // This is *why* PHP sets the default precision at 6. - ini_set('precision', 17); - $this->assertSame('67.10', $this->request->getAmount()); - - $this->assertSame($this->request, $this->request->setAmount('67.01')); - $this->assertSame('67.01', $this->request->getAmount()); - - $this->assertSame($this->request, $this->request->setAmount('0.10')); - $this->assertSame('0.10', $this->request->getAmount()); - - $this->assertSame($this->request, $this->request->setAmount('0.01')); - $this->assertSame('0.01', $this->request->getAmount()); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - * - * We still want to catch obvious fractions of the minor units that are - * not precision errors at a much lower level. - */ - public function testAmountPrecisionTooHigh() - { - $this->assertSame($this->request, $this->request->setAmount('123.005')); - $this->assertSame('123.005', $this->request->getAmount()); - } - - public function testGetAmountNoDecimals() - { - $this->assertSame($this->request, $this->request->setCurrency('JPY')); - $this->assertSame($this->request, $this->request->setAmount('1366')); - $this->assertSame('1366', $this->request->getAmount()); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testGetAmountNoDecimalsRounding() - { - // There will not be any rounding; the amount is sent as requested or not at all. - $this->assertSame($this->request, $this->request->setAmount('136.5')); - $this->assertSame($this->request, $this->request->setCurrency('JPY')); + $this->request->setAmount('0'); $this->request->getAmount(); } /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException + * @expectedException InvalidArgumentException */ - public function testAmountWithIntThrowsException() + public function testAmountWithFloatStringThrowsException() { - // ambiguous value, avoid errors upgrading from v0.9 - $this->assertSame($this->request, $this->request->setAmount(10)); + $this->assertSame($this->request, $this->request->setAmount('10.00')); $this->request->getAmount(); } - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testAmountWithIntStringThrowsException() + public function testGetAmountFormatted() { - // ambiguous value, avoid errors upgrading from v0.9 - // Some currencies only take integers, so an integer (in a string) should be valid. - $this->assertSame($this->request, $this->request->setAmount('10')); - $this->request->getAmount(); - } - - public function testGetAmountInteger() - { - $this->assertSame($this->request, $this->request->setAmount('13.66')); - $this->assertSame(1366, $this->request->getAmountInteger()); - } - - public function testGetAmountIntegerNoDecimals() - { - $this->assertSame($this->request, $this->request->setCurrency('JPY')); - $this->assertSame($this->request, $this->request->setAmount('1366')); - $this->assertSame(1366, $this->request->getAmountInteger()); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testAmountThousandsSepThrowsException() - { - $this->assertSame($this->request, $this->request->setAmount('1,234.00')); - $this->request->getAmount(); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testAmountInvalidFormatThrowsException() - { - $this->assertSame($this->request, $this->request->setAmount('1.234.00')); - $this->request->getAmount(); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testAmountInvalidTypeThrowsException() - { - $this->assertSame($this->request, $this->request->setAmount(true)); - $this->request->getAmount(); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testAmountNegativeStringThrowsException() - { - $this->assertSame($this->request, $this->request->setAmount('-123.00')); - $this->request->getAmount(); - } - - /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException - */ - public function testAmountNegativeFloatThrowsException() - { - $this->assertSame($this->request, $this->request->setAmount(-123.00)); - $this->request->getAmount(); + $this->assertSame($this->request, $this->request->setAmount(1366)); + $this->assertSame('13.66', $this->request->getAmount()->getFormatted()); } public function testCurrency() { - $this->assertSame($this->request, $this->request->setCurrency('USD')); - $this->assertSame('USD', $this->request->getCurrency()); - } - - public function testCurrencyLowercase() - { - $this->assertSame($this->request, $this->request->setCurrency('usd')); - $this->assertSame('USD', $this->request->getCurrency()); - } - - public function testCurrencyNumeric() - { - $this->assertSame($this->request, $this->request->setCurrency('USD')); - $this->assertSame('840', $this->request->getCurrencyNumeric()); - } - - public function testCurrencyDecimals() - { - $this->assertSame($this->request, $this->request->setCurrency('JPY')); - $this->assertSame(0, $this->request->getCurrencyDecimalPlaces()); - } - - public function testFormatCurrency() - { - $this->assertSame('1234.00', $this->request->formatCurrency(1234)); + $this->assertSame($this->request, $this->request->setCurrency('EUR')->setAmount(1)); + $this->assertSame('EUR', $this->request->getAmount()->getCurrency()->getCode()); } - public function testFormatCurrencyNoDecimals() + public function testAmountWithCurrency() { - $this->request->setCurrency('JPY'); - $this->assertSame('1234', $this->request->formatCurrency(1234)); + $this->assertSame($this->request, $this->request->setCurrency('USD')->setAmount(new Amount(1, 'EUR'))); + $this->assertSame('EUR', $this->request->getAmount()->getCurrency()->getCode()); } public function testDescription() @@ -388,6 +251,7 @@ public function testGetParameters() $expected = array( 'testMode' => true, 'token' => 'asdf', + 'currency' => 'USD', ); $this->assertEquals($expected, $this->request->getParameters()); } @@ -401,7 +265,7 @@ public function testSetParameterAfterRequestSent() $this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->send(); - $this->request->setCurrency('PHP'); + $this->request->setAmount('100'); } public function testCanValidateExistingParameters() @@ -422,9 +286,9 @@ public function testInvalidParametersThrowsException() $this->request->validate('testMode', 'token'); } - public function testNoCurrencyReturnedIfCurrencyNotSet() + public function testNoAmountReturnedIfAmountNotSet() { - $this->assertNull($this->request->getCurrencyNumeric()); + $this->assertNull($this->request->getAmount()); } public function testSend() From 24325f1c50c63494ef4540b417e1adaa8ebaa6f6 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 13 Feb 2016 15:07:55 +0100 Subject: [PATCH 22/51] Fix composer, update phpdocs --- composer.json | 1 - src/Omnipay/Common/Customer.php | 78 ++++++++++++++++----------------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/composer.json b/composer.json index e81c6866..1f680de6 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "require": { "php": ">=5.6", "alcohol/iso4217": "^3.1", - "beberlei/assert": "^2.4", "guzzlehttp/guzzle": "^6.0", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.1.0" diff --git a/src/Omnipay/Common/Customer.php b/src/Omnipay/Common/Customer.php index c0a7d191..5d05078b 100644 --- a/src/Omnipay/Common/Customer.php +++ b/src/Omnipay/Common/Customer.php @@ -18,7 +18,7 @@ public function __construct(array $parameters = []) } /** - * Get Card Title. + * Get Customer Title. * * @return string */ @@ -28,7 +28,7 @@ public function getTitle() } /** - * Set Card Title. + * Set Customer Title. * * @param string $value Parameter value * @return CreditCard provides a fluent interface. @@ -41,7 +41,7 @@ public function setTitle($value) } /** - * Get Card First Name. + * Get Customer First Name. * * @return string */ @@ -51,10 +51,10 @@ public function getFirstName() } /** - * Set Card First Name (Billing and Shipping). + * Set Customer First Name (Billing and Shipping). * * @param string $value Parameter value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setFirstName($value) { @@ -64,7 +64,7 @@ public function setFirstName($value) } /** - * Get Card Last Name. + * Get Customer Last Name. * * @return string */ @@ -74,10 +74,10 @@ public function getLastName() } /** - * Set Card Last Name (Billing and Shipping). + * Set Customer Last Name (Billing and Shipping). * * @param string $value Parameter value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setLastName($value) { @@ -87,7 +87,7 @@ public function setLastName($value) } /** - * Get Card Name. + * Get Customer Name. * * @return string */ @@ -97,10 +97,10 @@ public function getName() } /** - * Set Card Name (Billing and Shipping). + * Set Customer Name (Billing and Shipping). * * @param string $value Parameter value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setName($value) { @@ -125,7 +125,7 @@ public function getAddress1() * Sets the address, line 1. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setAddress1($value) { @@ -135,7 +135,7 @@ public function setAddress1($value) } /** - * Get the address, line 2. + * Get the address, line 2. * * @return string */ @@ -148,7 +148,7 @@ public function getAddress2() * Sets the address, line 2. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setAddress2($value) { @@ -158,7 +158,7 @@ public function setAddress2($value) } /** - * Get the city. + * Get the city. * * @return string */ @@ -171,7 +171,7 @@ public function getCity() * Sets the city. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setCity($value) { @@ -181,7 +181,7 @@ public function setCity($value) } /** - * Get the postcode. + * Get the postcode. * * @return string */ @@ -194,7 +194,7 @@ public function getPostcode() * Sets the postcode. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setPostcode($value) { @@ -204,7 +204,7 @@ public function setPostcode($value) } /** - * Get the state. + * Get the state. * * @return string */ @@ -217,7 +217,7 @@ public function getState() * Sets the state. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setState($value) { @@ -227,7 +227,7 @@ public function setState($value) } /** - * Get the country. + * Get the country. * * @return string */ @@ -240,7 +240,7 @@ public function getCountry() * Sets the country. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setCountry($value) { @@ -250,7 +250,7 @@ public function setCountry($value) } /** - * Get the phone number. + * Get the phone number. * * @return string */ @@ -263,7 +263,7 @@ public function getPhone() * Sets the phone number. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setPhone($value) { @@ -273,7 +273,7 @@ public function setPhone($value) } /** - * Get the phone number extension. + * Get the phone number extension. * * @return string */ @@ -286,7 +286,7 @@ public function getPhoneExtension() * Sets the phone number extension. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setPhoneExtension($value) { @@ -296,7 +296,7 @@ public function setPhoneExtension($value) } /** - * Get the fax number.. + * Get the fax number.. * * @return string */ @@ -309,7 +309,7 @@ public function getFax() * Sets the fax number. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setFax($value) { @@ -319,7 +319,7 @@ public function setFax($value) } /** - * Get the card company name. + * Get the company name. * * @return string */ @@ -332,7 +332,7 @@ public function getCompany() * Sets the company name. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setCompany($value) { @@ -342,7 +342,7 @@ public function setCompany($value) } /** - * Get the cardholder's email address. + * Get the customer's email address. * * @return string */ @@ -352,10 +352,10 @@ public function getEmail() } /** - * Sets the cardholder's email address. + * Sets the customer's email address. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setEmail($value) { @@ -363,7 +363,7 @@ public function setEmail($value) } /** - * Get the cardholder's birthday. + * Get the customer's birthday. * * @return string */ @@ -375,10 +375,10 @@ public function getBirthday($format = 'Y-m-d') } /** - * Sets the cardholder's birthday. + * Sets the customer's birthday. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setBirthday($value) { @@ -392,7 +392,7 @@ public function setBirthday($value) } /** - * Get the cardholder's gender. + * Get the customer's gender. * * @return string */ @@ -402,10 +402,10 @@ public function getGender() } /** - * Sets the cardholder's gender. + * Sets the customer's gender. * * @param string $value - * @return CreditCard provides a fluent interface. + * @return $this provides a fluent interface. */ public function setGender($value) { From dd5af1d3500163d9becdef9d18427857fb136724 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 13 Feb 2016 23:32:32 +0100 Subject: [PATCH 23/51] Move to league/omniapy with namespace + psr4 --- README.md | 8 +- composer.json | 46 +-------- tests/bootstrap.php => phpunit.php | 4 +- phpunit.xml.dist | 2 +- src/{Omnipay => }/Common/AbstractGateway.php | 16 ++-- src/{Omnipay => }/Common/Amount.php | 2 +- src/{Omnipay => }/Common/CreditCard.php | 4 +- src/{Omnipay => }/Common/Currency.php | 2 +- src/{Omnipay => }/Common/Customer.php | 2 +- .../Exception/BadMethodCallException.php | 2 +- .../Exception/InvalidCreditCardException.php | 2 +- .../Exception/InvalidRequestException.php | 2 +- .../Exception/InvalidResponseException.php | 2 +- .../Common/Exception/OmnipayException.php | 2 +- .../Common/Exception/RuntimeException.php | 2 +- src/{Omnipay => }/Common/GatewayFactory.php | 37 +------- src/{Omnipay => }/Common/GatewayInterface.php | 22 ++--- .../Common/HasParametersTrait.php | 2 +- src/{Omnipay => }/Common/Helper.php | 18 ++-- .../Common/Http/ClientInterface.php | 2 +- .../Common/Http/GuzzleClient.php | 2 +- src/{Omnipay => }/Common/Issuer.php | 2 +- src/{Omnipay => }/Common/Item.php | 2 +- src/{Omnipay => }/Common/ItemBag.php | 2 +- src/{Omnipay => }/Common/ItemInterface.php | 2 +- .../Common/Message/AbstractRequest.php | 30 +++--- .../Common/Message/AbstractResponse.php | 4 +- .../Message/FetchIssuersResponseInterface.php | 6 +- .../FetchPaymentMethodsResponseInterface.php | 6 +- .../Common/Message/MessageInterface.php | 2 +- .../Common/Message/NotificationInterface.php | 2 +- .../Message/RedirectResponseInterface.php | 2 +- .../Common/Message/RequestInterface.php | 2 +- .../Common/Message/ResponseInterface.php | 2 +- src/{Omnipay => }/Common/ParameterBag.php | 2 +- .../Common/ParameterizedInterface.php | 2 +- src/{Omnipay => }/Common/PaymentMethod.php | 2 +- src/{Omnipay => }/Omnipay.php | 10 +- .../Common/AbstractGatewayTest.php | 12 +-- tests/{Omnipay => }/Common/AmountTest.php | 4 +- tests/{Omnipay => }/Common/CreditCardTest.php | 18 ++-- tests/{Omnipay => }/Common/CurrencyTest.php | 6 +- tests/{Omnipay => }/Common/CustomerTest.php | 2 +- .../Exception/BadMethodCallExceptionTest.php | 4 +- .../InvalidCreditCardExceptionTest.php | 4 +- .../Exception/InvalidRequestExceptionTest.php | 4 +- .../InvalidResponseExceptionTest.php | 4 +- .../Common/Exception/RuntimeExceptionTest.php | 4 +- tests/Common/GatewayFactoryTest.php | 64 +++++++++++++ tests/{Omnipay => }/Common/HelperTest.php | 20 ++-- .../Common/Http/GuzzleClientTest.php | 4 +- tests/{Omnipay => }/Common/IssuerTest.php | 4 +- tests/{Omnipay => }/Common/ItemBagTest.php | 6 +- tests/{Omnipay => }/Common/ItemTest.php | 4 +- .../Common/Message/AbstractRequestTest.php | 32 +++---- .../Common/Message/AbstractResponseTest.php | 22 ++--- .../Common/PaymentMethodTest.php | 4 +- tests/Omnipay/Common/GatewayFactoryTest.php | 95 ------------------- tests/{Omnipay => }/OmnipayTest.php | 10 +- 59 files changed, 243 insertions(+), 345 deletions(-) rename tests/bootstrap.php => phpunit.php (58%) rename src/{Omnipay => }/Common/AbstractGateway.php (93%) rename src/{Omnipay => }/Common/Amount.php (98%) rename src/{Omnipay => }/Common/CreditCard.php (99%) rename src/{Omnipay => }/Common/Currency.php (98%) rename src/{Omnipay => }/Common/Customer.php (99%) rename src/{Omnipay => }/Common/Exception/BadMethodCallException.php (76%) rename src/{Omnipay => }/Common/Exception/InvalidCreditCardException.php (82%) rename src/{Omnipay => }/Common/Exception/InvalidRequestException.php (81%) rename src/{Omnipay => }/Common/Exception/InvalidResponseException.php (90%) rename src/{Omnipay => }/Common/Exception/OmnipayException.php (66%) rename src/{Omnipay => }/Common/Exception/RuntimeException.php (73%) rename src/{Omnipay => }/Common/GatewayFactory.php (71%) rename src/{Omnipay => }/Common/GatewayInterface.php (59%) rename src/{Omnipay => }/Common/HasParametersTrait.php (97%) rename src/{Omnipay => }/Common/Helper.php (93%) rename src/{Omnipay => }/Common/Http/ClientInterface.php (97%) rename src/{Omnipay => }/Common/Http/GuzzleClient.php (98%) rename src/{Omnipay => }/Common/Issuer.php (97%) rename src/{Omnipay => }/Common/Item.php (98%) rename src/{Omnipay => }/Common/ItemBag.php (98%) rename src/{Omnipay => }/Common/ItemInterface.php (93%) rename src/{Omnipay => }/Common/Message/AbstractRequest.php (95%) rename src/{Omnipay => }/Common/Message/AbstractResponse.php (98%) rename src/{Omnipay => }/Common/Message/FetchIssuersResponseInterface.php (85%) rename src/{Omnipay => }/Common/Message/FetchPaymentMethodsResponseInterface.php (84%) rename src/{Omnipay => }/Common/Message/MessageInterface.php (92%) rename src/{Omnipay => }/Common/Message/NotificationInterface.php (95%) rename src/{Omnipay => }/Common/Message/RedirectResponseInterface.php (95%) rename src/{Omnipay => }/Common/Message/RequestInterface.php (96%) rename src/{Omnipay => }/Common/Message/ResponseInterface.php (97%) rename src/{Omnipay => }/Common/ParameterBag.php (99%) rename src/{Omnipay => }/Common/ParameterizedInterface.php (90%) rename src/{Omnipay => }/Common/PaymentMethod.php (97%) rename src/{Omnipay => }/Omnipay.php (93%) rename tests/{Omnipay => }/Common/AbstractGatewayTest.php (91%) rename tests/{Omnipay => }/Common/AmountTest.php (98%) rename tests/{Omnipay => }/Common/CreditCardTest.php (92%) rename tests/{Omnipay => }/Common/CurrencyTest.php (91%) rename tests/{Omnipay => }/Common/CustomerTest.php (99%) rename tests/{Omnipay => }/Common/Exception/BadMethodCallExceptionTest.php (74%) rename tests/{Omnipay => }/Common/Exception/InvalidCreditCardExceptionTest.php (75%) rename tests/{Omnipay => }/Common/Exception/InvalidRequestExceptionTest.php (74%) rename tests/{Omnipay => }/Common/Exception/InvalidResponseExceptionTest.php (85%) rename tests/{Omnipay => }/Common/Exception/RuntimeExceptionTest.php (73%) create mode 100644 tests/Common/GatewayFactoryTest.php rename tests/{Omnipay => }/Common/HelperTest.php (87%) rename tests/{Omnipay => }/Common/Http/GuzzleClientTest.php (97%) rename tests/{Omnipay => }/Common/IssuerTest.php (90%) rename tests/{Omnipay => }/Common/ItemBagTest.php (91%) rename tests/{Omnipay => }/Common/ItemTest.php (94%) rename tests/{Omnipay => }/Common/Message/AbstractRequestTest.php (90%) rename tests/{Omnipay => }/Common/Message/AbstractResponseTest.php (76%) rename tests/{Omnipay => }/Common/PaymentMethodTest.php (79%) delete mode 100644 tests/Omnipay/Common/GatewayFactoryTest.php rename tests/{Omnipay => }/OmnipayTest.php (72%) diff --git a/README.md b/README.md index 5c6f4127..975835fe 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ **Core components for the Omnipay PHP payment processing library** -[![Build Status](https://travis-ci.org/thephpleague/omnipay-common.png?branch=master)](https://travis-ci.org/thephpleague/omnipay-common) -[![Latest Stable Version](https://poser.pugx.org/omnipay/common/version.png)](https://packagist.org/packages/omnipay/common) -[![Total Downloads](https://poser.pugx.org/omnipay/common/d/total.png)](https://packagist.org/packages/omnipay/common) +[![Build Status](https://travis-ci.org/thephpleague/omnipay.png?branch=master)](https://travis-ci.org/thephpleague/omnipay) +[![Latest Stable Version](https://poser.pugx.org/league/omnipay/version.png)](https://packagist.org/packages/league/omnipay) +[![Total Downloads](https://poser.pugx.org/league/omnipay/d/total.png)](https://packagist.org/packages/league/omnipay) [Omnipay](https://github.com/thephpleague/omnipay) is a framework agnostic, multi-gateway payment processing library for PHP 5.3+. This package implements common classes required by Omnipay. @@ -19,5 +19,5 @@ If you want to keep up to date with release anouncements, discuss ideas for the or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which you can subscribe to. -If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/thephpleague/omnipay-common/issues), +If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/thephpleague/omnipay/issues), or better yet, fork the library and submit a pull request. diff --git a/composer.json b/composer.json index 1f680de6..995a4592 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "omnipay/common", + "name": "league/omnipay", "type": "library", "description": "Common components for Omnipay payment processing library", "keywords": [ @@ -23,8 +23,7 @@ } ], "autoload": { - "psr-0": { "Omnipay\\Common\\" : "src/" }, - "classmap": ["src/Omnipay/Omnipay.php"] + "psr-4": { "League\\Omnipay\\" : "src/" } }, "require": { "php": ">=5.6", @@ -37,44 +36,5 @@ "omnipay/tests": "^3.0" }, "minimum-stability": "dev", - "prefer-stable": true, - "extra": { - "branch-alias": { - "dev-master": "3.0.x-dev" - }, - "gateways": [ - "AuthorizeNet_AIM", - "AuthorizeNet_SIM", - "Buckaroo_Ideal", - "Buckaroo_PayPal", - "CardSave", - "Coinbase", - "Dummy", - "Eway_Rapid", - "FirstData_Connect", - "GoCardless", - "Manual", - "Migs_ThreeParty", - "Migs_TwoParty", - "Mollie", - "MultiSafepay", - "Netaxept", - "NetBanx", - "PayFast", - "Payflow_Pro", - "PaymentExpress_PxPay", - "PaymentExpress_PxPost", - "PayPal_Express", - "PayPal_Pro", - "Pin", - "SagePay_Direct", - "SagePay_Server", - "SecurePay_DirectPost", - "Stripe", - "TargetPay_Directebanking", - "TargetPay_Ideal", - "TargetPay_Mrcash", - "WorldPay" - ] - } + "prefer-stable": true } diff --git a/tests/bootstrap.php b/phpunit.php similarity index 58% rename from tests/bootstrap.php rename to phpunit.php index 847298ef..5a3ecaf8 100644 --- a/tests/bootstrap.php +++ b/phpunit.php @@ -4,7 +4,7 @@ date_default_timezone_set('UTC'); // include the composer autoloader -$autoloader = require __DIR__.'/../vendor/autoload.php'; +$autoloader = require __DIR__ . '/vendor/autoload.php'; // autoload abstract TestCase classes in test directory -$autoloader->add('Omnipay', __DIR__); +$autoloader->addPsr4('League\\Omnipay\\', __DIR__ . '/tests'); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a61160cd..8829cb3b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ - * class MyRequest extends \Omnipay\Common\Message\AbstractRequest {}; + * class MyRequest extends \League\Omnipay\Common\Message\AbstractRequest {}; * - * class MyGateway extends \Omnipay\Common\AbstractGateway { + * class MyGateway extends \League\Omnipay\Common\AbstractGateway { * function myRequest($parameters) { * $this->createRequest('MyRequest', $parameters); * } @@ -280,10 +280,10 @@ public function supportsUpdateCard() * $myRequest = $gw->myRequest($someParameters); * * - * @see \Omnipay\Common\Message\AbstractRequest + * @see \League\Omnipay\Common\Message\AbstractRequest * @param string $class The request class name * @param array $parameters - * @return \Omnipay\Common\Message\AbstractRequest + * @return \League\Omnipay\Common\Message\AbstractRequest */ protected function createRequest($class, array $parameters) { diff --git a/src/Omnipay/Common/Amount.php b/src/Common/Amount.php similarity index 98% rename from src/Omnipay/Common/Amount.php rename to src/Common/Amount.php index c441c207..f2eb8248 100644 --- a/src/Omnipay/Common/Amount.php +++ b/src/Common/Amount.php @@ -3,7 +3,7 @@ * Amount class */ -namespace Omnipay\Common; +namespace League\Omnipay\Common; use InvalidArgumentException; diff --git a/src/Omnipay/Common/CreditCard.php b/src/Common/CreditCard.php similarity index 99% rename from src/Omnipay/Common/CreditCard.php rename to src/Common/CreditCard.php index 136b5923..db5a80c5 100644 --- a/src/Omnipay/Common/CreditCard.php +++ b/src/Common/CreditCard.php @@ -3,11 +3,11 @@ * Credit Card class */ -namespace Omnipay\Common; +namespace League\Omnipay\Common; use DateTime; use DateTimeZone; -use Omnipay\Common\Exception\InvalidCreditCardException; +use League\Omnipay\Common\Exception\InvalidCreditCardException; /** * Credit Card class diff --git a/src/Omnipay/Common/Currency.php b/src/Common/Currency.php similarity index 98% rename from src/Omnipay/Common/Currency.php rename to src/Common/Currency.php index eb70432a..f9be7c62 100644 --- a/src/Omnipay/Common/Currency.php +++ b/src/Common/Currency.php @@ -3,7 +3,7 @@ * Currency class */ -namespace Omnipay\Common; +namespace League\Omnipay\Common; use Alcohol\ISO4217; diff --git a/src/Omnipay/Common/Customer.php b/src/Common/Customer.php similarity index 99% rename from src/Omnipay/Common/Customer.php rename to src/Common/Customer.php index 5d05078b..04c83ae2 100644 --- a/src/Omnipay/Common/Customer.php +++ b/src/Common/Customer.php @@ -1,4 +1,4 @@ -getSupportedGateways() as $gateway) { - $class = Helper::getGatewayClassName($gateway); - if (class_exists($class)) { - $this->register($gateway); - } - } - - ksort($this->gateways); - - return $this->all(); - } - /** * Create a new gateway instance * @@ -107,16 +88,4 @@ public function create($class, ClientInterface $httpClient = null, ServerRequest return new $class($httpClient, $httpRequest); } - - /** - * Get a list of supported gateways which may be available - * - * @return array - */ - public function getSupportedGateways() - { - $package = json_decode(file_get_contents(__DIR__.'/../../../composer.json'), true); - - return $package['extra']['gateways']; - } } diff --git a/src/Omnipay/Common/GatewayInterface.php b/src/Common/GatewayInterface.php similarity index 59% rename from src/Omnipay/Common/GatewayInterface.php rename to src/Common/GatewayInterface.php index 67783394..25fc4280 100644 --- a/src/Omnipay/Common/GatewayInterface.php +++ b/src/Common/GatewayInterface.php @@ -3,7 +3,7 @@ * Payment gateway interface */ -namespace Omnipay\Common; +namespace League\Omnipay\Common; /** * Payment gateway interface @@ -13,25 +13,25 @@ * * @see AbstractGateway * - * @method \Omnipay\Common\Message\ResponseInterface authorize(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface authorize(array $options = array()) (Optional method) * Authorize an amount on the customers card - * @method \Omnipay\Common\Message\ResponseInterface completeAuthorize(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface completeAuthorize(array $options = array()) (Optional method) * Handle return from off-site gateways after authorization - * @method \Omnipay\Common\Message\ResponseInterface capture(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface capture(array $options = array()) (Optional method) * Capture an amount you have previously authorized - * @method \Omnipay\Common\Message\ResponseInterface purchase(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface purchase(array $options = array()) (Optional method) * Authorize and immediately capture an amount on the customers card - * @method \Omnipay\Common\Message\ResponseInterface completePurchase(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface completePurchase(array $options = array()) (Optional method) * Handle return from off-site gateways after purchase - * @method \Omnipay\Common\Message\ResponseInterface refund(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface refund(array $options = array()) (Optional method) * Refund an already processed transaction - * @method \Omnipay\Common\Message\ResponseInterface void(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface void(array $options = array()) (Optional method) * Generally can only be called up to 24 hours after submitting a transaction - * @method \Omnipay\Common\Message\ResponseInterface createCard(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface createCard(array $options = array()) (Optional method) * The returned response object includes a cardReference, which can be used for future transactions - * @method \Omnipay\Common\Message\ResponseInterface updateCard(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface updateCard(array $options = array()) (Optional method) * Update a stored card - * @method \Omnipay\Common\Message\ResponseInterface deleteCard(array $options = array()) (Optional method) + * @method \League\Omnipay\Common\Message\ResponseInterface deleteCard(array $options = array()) (Optional method) * Delete a stored card */ interface GatewayInterface diff --git a/src/Omnipay/Common/HasParametersTrait.php b/src/Common/HasParametersTrait.php similarity index 97% rename from src/Omnipay/Common/HasParametersTrait.php rename to src/Common/HasParametersTrait.php index d71a6dfc..0aa8641b 100644 --- a/src/Omnipay/Common/HasParametersTrait.php +++ b/src/Common/HasParametersTrait.php @@ -1,6 +1,6 @@ \Custom\Gateway * \Custom_Gateway => \Custom_Gateway - * Stripe => \Omnipay\Stripe\Gateway - * PayPal\Express => \Omnipay\PayPal\ExpressGateway - * PayPal_Express => \Omnipay\PayPal\ExpressGateway + * Stripe => \League\Omnipay\Stripe\Gateway + * PayPal\Express => \League\Omnipay\PayPal\ExpressGateway + * PayPal_Express => \League\Omnipay\PayPal\ExpressGateway * * @param string $shortName The short gateway name * @return string The fully namespaced gateway class name @@ -139,7 +139,7 @@ public static function getGatewayClassName($shortName) $shortName .= '\\'; } - return '\\Omnipay\\'.$shortName.'Gateway'; + return '\\League\\Omnipay\\'.$shortName.'Gateway'; } /** @@ -184,7 +184,7 @@ public static function jsonDecode($body) if (JSON_ERROR_NONE !== json_last_error()) { throw new RuntimeException('Unable to parse response body into JSON: ' . json_last_error()); } - return $data === null ? array() : $data; + return $data === null ? [] : $data; } /** diff --git a/src/Omnipay/Common/Http/ClientInterface.php b/src/Common/Http/ClientInterface.php similarity index 97% rename from src/Omnipay/Common/Http/ClientInterface.php rename to src/Common/Http/ClientInterface.php index 4f776e25..800f3ee2 100644 --- a/src/Omnipay/Common/Http/ClientInterface.php +++ b/src/Common/Http/ClientInterface.php @@ -1,6 +1,6 @@ - * class MyRequest extends \Omnipay\Common\Message\AbstractRequest {}; + * class MyRequest extends \League\Omnipay\Common\Message\AbstractRequest {}; * - * class MyGateway extends \Omnipay\Common\AbstractGateway { + * class MyGateway extends \League\Omnipay\Common\AbstractGateway { * function myRequest($parameters) { * $this->createRequest('MyRequest', $parameters); * } diff --git a/src/Omnipay/Common/Message/AbstractResponse.php b/src/Common/Message/AbstractResponse.php similarity index 98% rename from src/Omnipay/Common/Message/AbstractResponse.php rename to src/Common/Message/AbstractResponse.php index e544f057..ba46437e 100644 --- a/src/Omnipay/Common/Message/AbstractResponse.php +++ b/src/Common/Message/AbstractResponse.php @@ -3,9 +3,9 @@ * Abstract Response */ -namespace Omnipay\Common\Message; +namespace League\Omnipay\Common\Message; -use Omnipay\Common\Exception\RuntimeException; +use League\Omnipay\Common\Exception\RuntimeException; use Psr\Http\Message\ResponseInterface as HttpResponseInterface; use Zend\Diactoros\Response\HtmlResponse; use Zend\Diactoros\Response\RedirectResponse; diff --git a/src/Omnipay/Common/Message/FetchIssuersResponseInterface.php b/src/Common/Message/FetchIssuersResponseInterface.php similarity index 85% rename from src/Omnipay/Common/Message/FetchIssuersResponseInterface.php rename to src/Common/Message/FetchIssuersResponseInterface.php index e86d6a4a..939b6f81 100644 --- a/src/Omnipay/Common/Message/FetchIssuersResponseInterface.php +++ b/src/Common/Message/FetchIssuersResponseInterface.php @@ -3,7 +3,7 @@ * Fetch Issuers Response interface */ -namespace Omnipay\Common\Message; +namespace League\Omnipay\Common\Message; /** * Fetch Issuers Response interface @@ -16,7 +16,7 @@ * card issuer. * * @see ResponseInterface - * @see Omnipay\Common\Issuer + * @see League\Omnipay\Common\Issuer */ interface FetchIssuersResponseInterface extends ResponseInterface { @@ -25,7 +25,7 @@ interface FetchIssuersResponseInterface extends ResponseInterface * * These represent banks which the user must choose between. * - * @return \Omnipay\Common\Issuer[] + * @return \League\Omnipay\Common\Issuer[] */ public function getIssuers(); } diff --git a/src/Omnipay/Common/Message/FetchPaymentMethodsResponseInterface.php b/src/Common/Message/FetchPaymentMethodsResponseInterface.php similarity index 84% rename from src/Omnipay/Common/Message/FetchPaymentMethodsResponseInterface.php rename to src/Common/Message/FetchPaymentMethodsResponseInterface.php index 57ca8800..46c289cb 100644 --- a/src/Omnipay/Common/Message/FetchPaymentMethodsResponseInterface.php +++ b/src/Common/Message/FetchPaymentMethodsResponseInterface.php @@ -3,7 +3,7 @@ * Fetch Payment Methods Response interface */ -namespace Omnipay\Common\Message; +namespace League\Omnipay\Common\Message; /** * Fetch Payment Methods Response interface @@ -16,7 +16,7 @@ * payment method. * * @see ResponseInterface - * @see Omnipay\Common\PaymentMethod + * @see League\Omnipay\Common\PaymentMethod */ interface FetchPaymentMethodsResponseInterface extends ResponseInterface { @@ -25,7 +25,7 @@ interface FetchPaymentMethodsResponseInterface extends ResponseInterface * * These represent separate payment methods which the user must choose between. * - * @return \Omnipay\Common\PaymentMethod[] + * @return \League\Omnipay\Common\PaymentMethod[] */ public function getPaymentMethods(); } diff --git a/src/Omnipay/Common/Message/MessageInterface.php b/src/Common/Message/MessageInterface.php similarity index 92% rename from src/Omnipay/Common/Message/MessageInterface.php rename to src/Common/Message/MessageInterface.php index 44bb5e1d..b5d33979 100644 --- a/src/Omnipay/Common/Message/MessageInterface.php +++ b/src/Common/Message/MessageInterface.php @@ -3,7 +3,7 @@ * Message Interface */ -namespace Omnipay\Common\Message; +namespace League\Omnipay\Common\Message; /** * Message Interface diff --git a/src/Omnipay/Common/Message/NotificationInterface.php b/src/Common/Message/NotificationInterface.php similarity index 95% rename from src/Omnipay/Common/Message/NotificationInterface.php rename to src/Common/Message/NotificationInterface.php index 14d53a90..731976d4 100755 --- a/src/Omnipay/Common/Message/NotificationInterface.php +++ b/src/Common/Message/NotificationInterface.php @@ -1,6 +1,6 @@ gateway = m::mock('\Omnipay\Common\AbstractGateway')->makePartial(); + $this->gateway = m::mock('\League\Omnipay\Common\AbstractGateway')->makePartial(); $this->gateway->initialize(); } public function testConstruct() { $this->gateway = new AbstractGatewayTest_MockAbstractGateway; - $this->assertInstanceOf('\Omnipay\Common\Http\ClientInterface', $this->gateway->getProtectedHttpClient()); + $this->assertInstanceOf('\League\Omnipay\Common\Http\ClientInterface', $this->gateway->getProtectedHttpClient()); $this->assertInstanceOf('\Psr\Http\Message\ServerRequestInterface', $this->gateway->getProtectedHttpRequest()); $this->assertSame(array(), $this->gateway->getParameters()); } @@ -141,7 +141,7 @@ public function testCreateRequest() { $this->gateway = new AbstractGatewayTest_MockAbstractGateway; $request = $this->gateway->callCreateRequest( - '\Omnipay\Common\AbstractGatewayTest_MockAbstractRequest', + '\League\Omnipay\Common\AbstractGatewayTest_MockAbstractRequest', array('currency' => 'THB') ); diff --git a/tests/Omnipay/Common/AmountTest.php b/tests/Common/AmountTest.php similarity index 98% rename from tests/Omnipay/Common/AmountTest.php rename to tests/Common/AmountTest.php index 3a5120b9..6af33021 100644 --- a/tests/Omnipay/Common/AmountTest.php +++ b/tests/Common/AmountTest.php @@ -1,8 +1,8 @@ factory = new GatewayFactory; + } + + public function testReplace() + { + $gateways = array('Foo'); + $this->factory->replace($gateways); + + $this->assertSame($gateways, $this->factory->all()); + } + + public function testRegister() + { + $this->factory->register('Bar'); + + $this->assertSame(array('Bar'), $this->factory->all()); + } + + public function testRegisterExistingGateway() + { + $this->factory->register('Milky'); + $this->factory->register('Bar'); + $this->factory->register('Bar'); + + $this->assertSame(array('Milky', 'Bar'), $this->factory->all()); + } + + public function testCreateShortName() + { + $gateway = $this->factory->create('SpareChange_Test'); + $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); + } + + public function testCreateFullyQualified() + { + $gateway = $this->factory->create('\\League\\Omnipay\\SpareChange\\TestGateway'); + $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); + } + + /** + * @expectedException \League\Omnipay\Common\Exception\RuntimeException + * @expectedExceptionMessage Class '\League\Omnipay\Invalid\Gateway' not found + */ + public function testCreateInvalid() + { + $gateway = $this->factory->create('Invalid'); + } +} diff --git a/tests/Omnipay/Common/HelperTest.php b/tests/Common/HelperTest.php similarity index 87% rename from tests/Omnipay/Common/HelperTest.php rename to tests/Common/HelperTest.php index 5325310b..52699790 100644 --- a/tests/Omnipay/Common/HelperTest.php +++ b/tests/Common/HelperTest.php @@ -1,9 +1,9 @@ shouldReceive('setNumber')->once()->with('1234'); Helper::initializeParameters($target, ['number' => '1234']); @@ -53,25 +53,25 @@ public function testInitializeCallsSetters() public function testGetGatewayShortNameSimple() { - $shortName = Helper::getGatewayShortName('Omnipay\\Stripe\\Gateway'); + $shortName = Helper::getGatewayShortName('League\\Omnipay\\Stripe\\Gateway'); $this->assertSame('Stripe', $shortName); } public function testGetGatewayShortNameSimpleLeadingSlash() { - $shortName = Helper::getGatewayShortName('\\Omnipay\\Stripe\\Gateway'); + $shortName = Helper::getGatewayShortName('\\League\\Omnipay\\Stripe\\Gateway'); $this->assertSame('Stripe', $shortName); } public function testGetGatewayShortNameUnderscore() { - $shortName = Helper::getGatewayShortName('Omnipay\\PayPal\\ExpressGateway'); + $shortName = Helper::getGatewayShortName('League\\Omnipay\\PayPal\\ExpressGateway'); $this->assertSame('PayPal_Express', $shortName); } public function testGetGatewayShortNameUnderscoreLeadingSlash() { - $shortName = Helper::getGatewayShortName('\\Omnipay\\PayPal\\ExpressGateway'); + $shortName = Helper::getGatewayShortName('\\League\\Omnipay\\PayPal\\ExpressGateway'); $this->assertSame('PayPal_Express', $shortName); } @@ -102,13 +102,13 @@ public function testGetGatewayClassNameExistingNamespaceUnderscore() public function testGetGatewayClassNameSimple() { $class = Helper::getGatewayClassName('Stripe'); - $this->assertEquals('\\Omnipay\\Stripe\\Gateway', $class); + $this->assertEquals('\\League\\Omnipay\\Stripe\\Gateway', $class); } public function testGetGatewayClassNamePartialNamespace() { $class = Helper::getGatewayClassName('PayPal\\Express'); - $this->assertEquals('\\Omnipay\\PayPal\\ExpressGateway', $class); + $this->assertEquals('\\League\\Omnipay\\PayPal\\ExpressGateway', $class); } /** @@ -117,7 +117,7 @@ public function testGetGatewayClassNamePartialNamespace() public function testGetGatewayClassNameUnderscoreNamespace() { $class = Helper::getGatewayClassName('PayPal_Express'); - $this->assertEquals('\\Omnipay\\PayPal\\ExpressGateway', $class); + $this->assertEquals('\\League\\Omnipay\\PayPal\\ExpressGateway', $class); } /** diff --git a/tests/Omnipay/Common/Http/GuzzleClientTest.php b/tests/Common/Http/GuzzleClientTest.php similarity index 97% rename from tests/Omnipay/Common/Http/GuzzleClientTest.php rename to tests/Common/Http/GuzzleClientTest.php index f09349c9..c2587c78 100644 --- a/tests/Omnipay/Common/Http/GuzzleClientTest.php +++ b/tests/Common/Http/GuzzleClientTest.php @@ -1,9 +1,9 @@ bag->add($item); $contents = $this->bag->all(); - $this->assertInstanceOf('\Omnipay\Common\Item', $contents[0]); + $this->assertInstanceOf('\League\Omnipay\Common\Item', $contents[0]); $this->assertSame('CD-ROM', $contents[0]->getName()); } diff --git a/tests/Omnipay/Common/ItemTest.php b/tests/Common/ItemTest.php similarity index 94% rename from tests/Omnipay/Common/ItemTest.php rename to tests/Common/ItemTest.php index 8c008b51..6647713f 100644 --- a/tests/Omnipay/Common/ItemTest.php +++ b/tests/Common/ItemTest.php @@ -1,8 +1,8 @@ request = m::mock('\Omnipay\Common\Message\AbstractRequest')->makePartial(); + $this->request = m::mock('\League\Omnipay\Common\Message\AbstractRequest')->makePartial(); $this->request->initialize([ 'currency' => 'USD', ]); @@ -47,7 +47,7 @@ public function testInitializeWithParams() } /** - * @expectedException \Omnipay\Common\Exception\RuntimeException + * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage Request cannot be modified after it has been sent! */ public function testInitializeAfterRequestSent() @@ -72,7 +72,7 @@ public function testSetCardWithArray() $this->assertSame($this->request, $this->request->setCard(array('number' => '1234'))); $card = $this->request->getCard(); - $this->assertInstanceOf('\Omnipay\Common\CreditCard', $card); + $this->assertInstanceOf('\League\Omnipay\Common\CreditCard', $card); $this->assertSame('1234', $card->getNumber()); } @@ -119,7 +119,7 @@ public function testAmountZeroString() } /** - * @expectedException Omnipay\Common\Exception\InvalidRequestException + * @expectedException League\Omnipay\Common\Exception\InvalidRequestException * @expectedExceptionMessage A zero amount is not allowed. */ public function testAmountZeroNotAllowed() @@ -182,7 +182,7 @@ public function testItemsArray() ))); $itemBag = $this->request->getItems(); - $this->assertInstanceOf('\Omnipay\Common\ItemBag', $itemBag); + $this->assertInstanceOf('\League\Omnipay\Common\ItemBag', $itemBag); $items = $itemBag->all(); $this->assertSame('Floppy Disk', $items[0]->getName()); @@ -257,7 +257,7 @@ public function testGetParameters() } /** - * @expectedException \Omnipay\Common\Exception\RuntimeException + * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage Request cannot be modified after it has been sent! */ public function testSetParameterAfterRequestSent() @@ -277,7 +277,7 @@ public function testCanValidateExistingParameters() } /** - * @expectedException \Omnipay\Common\Exception\InvalidRequestException + * @expectedException \League\Omnipay\Common\Exception\InvalidRequestException */ public function testInvalidParametersThrowsException() { @@ -293,7 +293,7 @@ public function testNoAmountReturnedIfAmountNotSet() public function testSend() { - $response = m::mock('\Omnipay\Common\Message\ResponseInterface'); + $response = m::mock('\League\Omnipay\Common\Message\ResponseInterface'); $data = array('request data'); $this->request->shouldReceive('getData')->once()->andReturn($data); @@ -303,7 +303,7 @@ public function testSend() } /** - * @expectedException \Omnipay\Common\Exception\RuntimeException + * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage You must call send() before accessing the Response! */ public function testGetResponseBeforeRequestSent() @@ -318,7 +318,7 @@ public function testGetResponseAfterRequestSent() $this->request->send(); $response = $this->request->getResponse(); - $this->assertInstanceOf('\Omnipay\Common\Message\ResponseInterface', $response); + $this->assertInstanceOf('\League\Omnipay\Common\Message\ResponseInterface', $response); } } @@ -328,6 +328,6 @@ public function getData() {} public function sendData($data) { - $this->response = m::mock('\Omnipay\Common\Message\AbstractResponse'); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponse'); } } diff --git a/tests/Omnipay/Common/Message/AbstractResponseTest.php b/tests/Common/Message/AbstractResponseTest.php similarity index 76% rename from tests/Omnipay/Common/Message/AbstractResponseTest.php rename to tests/Common/Message/AbstractResponseTest.php index 339f6253..d07c5a57 100644 --- a/tests/Omnipay/Common/Message/AbstractResponseTest.php +++ b/tests/Common/Message/AbstractResponseTest.php @@ -1,22 +1,22 @@ response = m::mock('\Omnipay\Common\Message\AbstractResponse')->makePartial(); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponse')->makePartial(); } public function testConstruct() { $data = array('foo' => 'bar'); $request = $this->getMockRequest(); - $this->response = m::mock('\Omnipay\Common\Message\AbstractResponse', array($request, $data))->makePartial(); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponse', array($request, $data))->makePartial(); $this->assertSame($request, $this->response->getRequest()); $this->assertSame($data, $this->response->getData()); @@ -35,7 +35,7 @@ public function testDefaultMethods() } /** - * @expectedException \Omnipay\Common\Exception\RuntimeException + * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage This response does not support redirection. */ public function testGetRedirectResponseNotImplemented() @@ -44,12 +44,12 @@ public function testGetRedirectResponseNotImplemented() } /** - * @expectedException \Omnipay\Common\Exception\RuntimeException + * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage This response does not support redirection. */ public function testGetRedirectResponseNotSupported() { - $this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); $this->response->shouldReceive('isRedirect')->once()->andReturn(false); $this->response->getRedirectResponse(); @@ -57,7 +57,7 @@ public function testGetRedirectResponseNotSupported() public function testGetRedirectResponseGet() { - $this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); $this->response->shouldReceive('getRedirectMethod')->andReturn('GET'); $httpResponse = $this->response->getRedirectResponse(); @@ -68,7 +68,7 @@ public function testGetRedirectResponseGet() public function testGetRedirectResponsePost() { $data = array('foo' => 'bar', 'key&"' => ''); - $this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); $this->response->shouldReceive('getRedirectMethod')->andReturn('POST'); $this->response->shouldReceive('getRedirectData')->andReturn($data); @@ -80,12 +80,12 @@ public function testGetRedirectResponsePost() } /** - * @expectedException \Omnipay\Common\Exception\RuntimeException + * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage Invalid redirect method "DELETE". */ public function testGetRedirectResponseInvalidMethod() { - $this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); + $this->response = m::mock('\League\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial(); $this->response->shouldReceive('getRedirectMethod')->andReturn('DELETE'); $this->response->getRedirectResponse(); diff --git a/tests/Omnipay/Common/PaymentMethodTest.php b/tests/Common/PaymentMethodTest.php similarity index 79% rename from tests/Omnipay/Common/PaymentMethodTest.php rename to tests/Common/PaymentMethodTest.php index 3d29aec6..de57cd77 100644 --- a/tests/Omnipay/Common/PaymentMethodTest.php +++ b/tests/Common/PaymentMethodTest.php @@ -1,8 +1,8 @@ factory = new GatewayFactory; - } - - public function testReplace() - { - $gateways = array('Foo'); - $this->factory->replace($gateways); - - $this->assertSame($gateways, $this->factory->all()); - } - - public function testRegister() - { - $this->factory->register('Bar'); - - $this->assertSame(array('Bar'), $this->factory->all()); - } - - public function testRegisterExistingGateway() - { - $this->factory->register('Milky'); - $this->factory->register('Bar'); - $this->factory->register('Bar'); - - $this->assertSame(array('Milky', 'Bar'), $this->factory->all()); - } - - public function testFindRegistersAvailableGateways() - { - $this->factory = m::mock('Omnipay\Common\GatewayFactory[getSupportedGateways]'); - $this->factory->shouldReceive('getSupportedGateways')->once() - ->andReturn(array('SpareChange_Test')); - - $gateways = $this->factory->find(); - - $this->assertContains('SpareChange_Test', $gateways); - $this->assertContains('SpareChange_Test', $this->factory->all()); - } - - public function testFindIgnoresUnavailableGateways() - { - $this->factory = m::mock('Omnipay\Common\GatewayFactory[getSupportedGateways]'); - $this->factory->shouldReceive('getSupportedGateways')->once() - ->andReturn(array('SpareChange_Gone')); - - $gateways = $this->factory->find(); - - $this->assertEmpty($gateways); - $this->assertEmpty($this->factory->all()); - } - - public function testCreateShortName() - { - $gateway = $this->factory->create('SpareChange_Test'); - $this->assertInstanceOf('\\Omnipay\\SpareChange\\TestGateway', $gateway); - } - - public function testCreateFullyQualified() - { - $gateway = $this->factory->create('\\Omnipay\\SpareChange\\TestGateway'); - $this->assertInstanceOf('\\Omnipay\\SpareChange\\TestGateway', $gateway); - } - - /** - * @expectedException \Omnipay\Common\Exception\RuntimeException - * @expectedExceptionMessage Class '\Omnipay\Invalid\Gateway' not found - */ - public function testCreateInvalid() - { - $gateway = $this->factory->create('Invalid'); - } - - public function testGetSupportedGateways() - { - $gateways = $this->factory->getSupportedGateways(); - - $this->assertContains('Stripe', $gateways); - } -} diff --git a/tests/Omnipay/OmnipayTest.php b/tests/OmnipayTest.php similarity index 72% rename from tests/Omnipay/OmnipayTest.php rename to tests/OmnipayTest.php index 2ad08124..a0743074 100644 --- a/tests/Omnipay/OmnipayTest.php +++ b/tests/OmnipayTest.php @@ -1,9 +1,9 @@ assertInstanceOf('Omnipay\Common\GatewayFactory', $factory); + $this->assertInstanceOf('League\Omnipay\Common\GatewayFactory', $factory); } public function testSetFactory() { - $factory = m::mock('Omnipay\Common\GatewayFactory'); + $factory = m::mock('League\Omnipay\Common\GatewayFactory'); Omnipay::setFactory($factory); @@ -31,7 +31,7 @@ public function testSetFactory() public function testCallStatic() { - $factory = m::mock('Omnipay\Common\GatewayFactory'); + $factory = m::mock('League\Omnipay\Common\GatewayFactory'); $factory->shouldReceive('testMethod')->with('some-argument')->once()->andReturn('some-result'); Omnipay::setFactory($factory); From eab4e24eb97f758bff189f705466076a2a2eb5c4 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 13 Feb 2016 23:39:02 +0100 Subject: [PATCH 24/51] Updat readme for 3.0 --- README.md | 403 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 394 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 975835fe..8ad8b588 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,388 @@ -# Omnipay Common +# Omnipay -**Core components for the Omnipay PHP payment processing library** +**An easy to use, consistent payment processing library for PHP 5.3+** -[![Build Status](https://travis-ci.org/thephpleague/omnipay.png?branch=master)](https://travis-ci.org/thephpleague/omnipay) -[![Latest Stable Version](https://poser.pugx.org/league/omnipay/version.png)](https://packagist.org/packages/league/omnipay) -[![Total Downloads](https://poser.pugx.org/league/omnipay/d/total.png)](https://packagist.org/packages/league/omnipay) +[![Build Status](https://travis-ci.org/thephpleague/omnipay.png?branch=3.0)](https://travis-ci.org/thephpleague/omnipay) +[![Latest Stable Version](https://poser.pugx.org/league/omnipay/version)](https://packagist.org/packages/league/omnipay) +[![Total Downloads](https://poser.pugx.org/league/omnipay/d/total)](https://packagist.org/packages/league/omnipay) -[Omnipay](https://github.com/thephpleague/omnipay) is a framework agnostic, multi-gateway payment -processing library for PHP 5.3+. This package implements common classes required by Omnipay. + +** Omnipay 3.x is Work in Progress, please do not use in production yet ** + +Omnipay is a payment processing library for PHP. It has been designed based on +ideas from [Active Merchant](http://activemerchant.org/), plus experience implementing +dozens of gateways for [CI Merchant]. It has a clear and consistent API, +is fully unit tested, and even comes with an example application to get you started. + +**Why use Omnipay instead of a gateway's official PHP package/example code?** + +* Because you can learn one API and use it in multiple projects using different payment gateways +* Because if you need to change payment gateways you won't need to rewrite your code +* Because most official PHP payment gateway libraries are a mess +* Because most payment gateways have exceptionally poor documentation +* Because you are writing a shopping cart and need to support multiple gateways + +**Important Note: Upgrading from < 2.0** + +If you are upgrading from a pre-2.0 version of Omnipay, please note that the +project has now been split into multiple packages. There have also been some +changes to how gateway instances are created. See the +[full release notes](https://github.com/thephpleague/omnipay/releases/tag/v2.0.0) +for more details. + +## TL;DR + +Just want to see some code? + +```php +use Omnipay\Omnipay; + +$gateway = Omnipay::create('Stripe'); +$gateway->setApiKey('abc123'); + +$formData = array('number' => '4242424242424242', 'expiryMonth' => '6', 'expiryYear' => '2016', 'cvv' => '123'); +$response = $gateway->purchase(array('amount' => '10.00', 'currency' => 'USD', 'card' => $formData))->send(); + +if ($response->isSuccessful()) { + // payment was successful: update database + print_r($response); +} elseif ($response->isRedirect()) { + // redirect to offsite payment gateway + $response->redirect(); +} else { + // payment failed: display message to customer + echo $response->getMessage(); +} +``` + +As you can see, Omnipay has a consistent, well thought out API. We try to abstract as much +as possible the differences between the various payments gateways. + +## Package Layout + +Omnipay is a collection of packages which all depend on the +[league/omnipay](https://github.com/thephpleague/omnipay) package to provide +a consistent interface. There are no dependencies on official payment gateway PHP packages - +we prefer to work with the HTTP API directly. Under the hood, we use the popular and powerful +[Guzzle](http://guzzlephp.org/) library to make HTTP requests. + +New gateways can be created by cloning the layout of an existing package. When choosing a +name for your package, please don't use the `omnipay` vendor prefix, as this implies that +it is officially supported. You should use your own username as the vendor prefix, and prepend +`omnipay-` to the package name to make it clear that your package works with Omnipay. +For example, if your GitHub username was `santa`, and you were implementing the `giftpay` +payment library, a good name for your composer package would be `santa/omnipay-giftpay`. + +If you want to add your gateway to the list of officially supported gateways, please open a pull request on the +[league/omnipay](https://github.com/thephpleague/omnipay) package. Before new gateways will +be accepted, they must have 100% unit test code coverage, and follow the conventions +and code style used in other Omnipay gateways. + +## Installation + +Omnipay is installed via [Composer](https://getcomposer.org/). For most uses, you will need to require an individual gateway: + +``` +composer require omnipay/paypal:~2.0 +``` + + + +## Payment Gateways + +All payment gateways must implement [GatewayInterface](https://github.com/thephpleague/omnipay/blob/3.0/src/Common/GatewayInterface.php), and will usually +extend [AbstractGateway](https://github.com/thephpleague/omnipay/blob/3.0/src/Common/AbstractGateway.php) for basic functionality. + +The available gateways can be viewed at http://omnipay.thephpleague.com/gateways/official/ + +Gateways are created and initialized like so: + +```php +use Omnipay\Omnipay; + +$gateway = Omnipay::create('PayPal_Express'); +$gateway->setUsername('adrian'); +$gateway->setPassword('12345'); +``` + +Most settings are gateway specific. If you need to query a gateway to get a list +of available settings, you can call `getDefaultParameters()`: + +```php +$settings = $gateway->getDefaultParameters(); +// default settings array format: +array( + 'username' => '', // string variable + 'testMode' => false, // boolean variable + 'landingPage' => array('billing', 'login'), // enum variable, first item should be treated as default +); +``` + +Generally most payment gateways can be classified as one of two types: + +* Off-site gateways such as PayPal Express, where the customer is redirected to a third party site to enter payment details +* On-site (merchant-hosted) gateways such as PayPal Pro, where the customer enters their credit card details on your site + +However, there are some gateways such as Sage Pay Direct, where you take credit card details on site, then optionally redirect +if the customer's card supports 3D Secure authentication. Therefore, there is no point differentiating between the two types of +gateway (other than by the methods they support). + +## Credit Card / Payment Form Input + +User form input is directed to an [CreditCard](https://github.com/thephpleague/omnipay/blob/3.0/src/Common/CreditCard.php) +object. This provides a safe way to accept user input. + +The `CreditCard` object has the following fields: + +* firstName +* lastName +* number +* expiryMonth +* expiryYear +* startMonth +* startYear +* cvv +* issueNumber +* type +* billingAddress1 +* billingAddress2 +* billingCity +* billingPostcode +* billingState +* billingCountry +* billingPhone +* shippingAddress1 +* shippingAddress2 +* shippingCity +* shippingPostcode +* shippingState +* shippingCountry +* shippingPhone +* company +* email + +Even off-site gateways make use of the `CreditCard` object, because often you need to pass +customer billing or shipping details through to the gateway. + +The `CreditCard` object can be initialized with untrusted user input via the constructor. +Any fields passed to the constructor which are not recognized will be ignored. + +```php +$formInputData = array( + 'firstName' => 'Bobby', + 'lastName' => 'Tables', + 'number' => '4111111111111111', +); +$card = new CreditCard($formInputData); +``` + +You can also just pass the form data array directly to the gateway, and a `CreditCard` object +will be created for you. + +CreditCard fields can be accessed using getters and setters: + +```php +$number = $card->getNumber(); +$card->setFirstName('Adrian'); +``` + +If you submit credit card details which are obviously invalid (missing required fields, or a number +which fails the Luhn check), [InvalidCreditCardException](https://github.com/thephpleague/omnipay/blob/3.0/src/Common/Exception/InvalidCreditCardException.php) +will be thrown. You should validate the card details using your framework's validation library +before submitting the details to your gateway, to avoid unnecessary API calls. + +For on-site payment gateways, the following card fields are generally required: + +* firstName +* lastName +* number +* expiryMonth +* expiryYear +* cvv + +You can also verify the card number using the Luhn algorithm by calling `Helper::validateLuhn($number)`. + +## Gateway Methods + +The main methods implemented by gateways are: + +* `authorize($options)` - authorize an amount on the customer's card +* `completeAuthorize($options)` - handle return from off-site gateways after authorization +* `capture($options)` - capture an amount you have previously authorized +* `purchase($options)` - authorize and immediately capture an amount on the customer's card +* `completePurchase($options)` - handle return from off-site gateways after purchase +* `refund($options)` - refund an already processed transaction +* `void($options)` - generally can only be called up to 24 hours after submitting a transaction +* `acceptNotification()` - convert an incoming request from an off-site gateway to a generic notification object + for further processing + +On-site gateways do not need to implement the `completeAuthorize` and `completePurchase` methods. Gateways that don't +receive payment notifications don't need to implement `acceptNotification`. If any gateway does not support certain +features (such as refunds), it will throw `BadMethodCallException`. + +All gateway methods except `acceptNotification` take an `$options` array as an argument. The `acceptNotification` method +does not take any parameters and will access the HTTP URL variables or POST data implicitly. Each gateway differs in +which parameters are required, and the gateway will throw `InvalidRequestException` if you omit any required parameters. +All gateways will accept a subset of these options: + +* card +* token +* amount +* currency +* description +* transactionId +* clientIp +* returnUrl +* cancelUrl + +Pass the options through to the method like so: + +```php +$card = new CreditCard($formData); +$request = $gateway->authorize(array( + 'amount' => '10.00', // this represents $10.00 + 'card' => $card, + 'returnUrl' => 'https://www.example.com/return', +)); +``` + +When calling the `completeAuthorize` or `completePurchase` methods, the exact same arguments should be provided as +when you made the initial `authorize` or `purchase` call (some gateways will need to verify for example the actual +amount paid equals the amount requested). The only parameter you can omit is `card`. + +To summarize the various parameters you have available to you: + +* Gateway settings (e.g. username and password) are set directly on the gateway. These settings apply to all payments, and generally you will store these in a configuration file or in the database. +* Method options are used for any payment-specific options, which are not set by the customer. For example, the payment `amount`, `currency`, `transactionId` and `returnUrl`. +* CreditCard parameters are data which the user supplies. For example, you want the user to specify their `firstName` and `billingCountry`, but you don't want a user to specify the payment `currency` or `returnUrl`. + +## The Payment Response + +The payment response must implement [ResponseInterface](https://github.com/thephpleague/omnipay/blob/3.0/src/Common/Message/ResponseInterface.php). There are two main types of response: + +* Payment was successful (standard response) +* Website requires redirect to off-site payment form (redirect response) + +### Successful Response + +For a successful responses, a reference will normally be generated, which can be used to capture or refund the transaction +at a later date. The following methods are always available: + +```php +$response = $gateway->purchase(array('amount' => '10.00', 'card' => $card))->send(); + +$response->isSuccessful(); // is the response successful? +$response->isRedirect(); // is the response a redirect? +$response->getTransactionReference(); // a reference generated by the payment gateway +$response->getTransactionId(); // the reference set by the originating website if available. +$response->getMessage(); // a message generated by the payment gateway +``` + +In addition, most gateways will override the response object, and provide access to any extra fields returned by the gateway. + +### Redirect Response + +The redirect response is further broken down by whether the customer's browser must redirect using GET (RedirectResponse object), or +POST (FormRedirectResponse). These could potentially be combined into a single response class, with a `getRedirectMethod()`. + +After processing a payment, the cart should check whether the response requires a redirect, and if so, redirect accordingly: + +```php +$response = $gateway->purchase(array('amount' => '10.00', 'card' => $card))->send(); +if ($response->isSuccessful()) { + // payment is complete +} elseif ($response->isRedirect()) { + $response->redirect(); // this will automatically forward the customer +} else { + // not successful +} +``` + +The customer isn't automatically forwarded on, because often the cart or developer will want to customize the redirect method +(or if payment processing is happening inside an AJAX call they will want to return JS to the browser instead). + +To display your own redirect page, simply call `getRedirectUrl()` on the response, then display it accordingly: + +```php +$url = $response->getRedirectUrl(); +// for a form redirect, you can also call the following method: +$data = $response->getRedirectData(); // associative array of fields which must be posted to the redirectUrl +``` + +## Error Handling + +You can test for a successful response by calling `isSuccessful()` on the response object. If there +was an error communicating with the gateway, or your request was obviously invalid, an exception +will be thrown. In general, if the gateway does not throw an exception, but returns an unsuccessful +response, it is a message you should display to the customer. If an exception is thrown, it is +either a bug in your code (missing required fields), or a communication error with the gateway. + +You can handle both scenarios by wrapping the entire request in a try-catch block: + +```php +try { + $response = $gateway->purchase(array('amount' => '10.00', 'card' => $card))->send(); + if ($response->isSuccessful()) { + // mark order as complete + } elseif ($response->isRedirect()) { + $response->redirect(); + } else { + // display error to customer + exit($response->getMessage()); + } +} catch (\Exception $e) { + // internal error, log exception and display a generic message to the customer + exit('Sorry, there was an error processing your payment. Please try again later.'); +} +``` + +## Token Billing + +Token billing allows you to store a credit card with your gateway, and charge it at a later date. +Token billing is not supported by all gateways. For supported gateways, the following methods +are available: + +* `createCard($options)` - returns a response object which includes a `cardReference`, which can be used for future transactions +* `updateCard($options)` - update a stored card, not all gateways support this method +* `deleteCard($options)` - remove a stored card, not all gateways support this method + +Once you have a `cardReference`, you can use it instead of the `card` parameter when creating a charge: + + $gateway->purchase(array('amount' => '10.00', 'cardReference' => 'abc')); + +## Recurring Billing + +At this stage, automatic recurring payments functionality is out of scope for this library. +This is because there is likely far too many differences between how each gateway handles +recurring billing profiles. Also in most cases token billing will cover your needs, as you can +store a credit card then charge it on whatever schedule you like. Feel free to get in touch if +you really think this should be a core feature and worth the effort. + +## Incoming Notifications + +Some gateways (e.g. Cybersource, GoPay) offer HTTP notifications to inform the merchant about the completion (or, in +general, status) of the payment. To assist with handling such notifications, the `acceptNotification()` method will +extract the transaction reference and payment status from the HTTP request and return a generic `NotificationInterface`. + +```php +$notification = $gateway->acceptNotification(); + +$notification->getTransactionReference(); // A reference provided by the gateway to represent this transaction +$notification->getTransactionStatus(); // Current status of the transaction, one of NotificationInterface::STATUS_* +$notification->getMessage(); // Additional message, if any, provided by the gateway + +// update the status of the corresponding transaction in your database +``` + +## Example Application + +An example application is provided in the [omnipay/example](https://github.com/thephpleague/omnipay-example) repo. +You can run it using PHP's built in web server (PHP 5.4+): + + $ php composer.phar update --dev + $ php -S localhost:8000 + +For more information, see the [Omnipay example application](https://github.com/thephpleague/omnipay-example). ## Support @@ -19,5 +394,15 @@ If you want to keep up to date with release anouncements, discuss ideas for the or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which you can subscribe to. -If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/thephpleague/omnipay/issues), -or better yet, fork the library and submit a pull request. +If you believe you have found a bug, please report it using the GitHub issue tracker +for the appropriate package, or better yet, fork the library and submit a pull request. + +## Security +If you discover any security related issues, please email kayladnls@gmail.com instead of using the issue tracker. + + +## Feedback + +**Please provide feedback!** We want to make this library useful in as many projects as possible. +Please head on over to the [mailing list](https://groups.google.com/forum/#!forum/omnipay) +and point out what you do and don't like, or fork the project and make suggestions. **No issue is too small.** \ No newline at end of file From b5420cc0502b1ebc2898324d6943f61711f6bf06 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 13 Feb 2016 23:53:34 +0100 Subject: [PATCH 25/51] Use local namespace --- src/Common/GatewayInterface.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Common/GatewayInterface.php b/src/Common/GatewayInterface.php index 25fc4280..392d2196 100644 --- a/src/Common/GatewayInterface.php +++ b/src/Common/GatewayInterface.php @@ -13,25 +13,25 @@ * * @see AbstractGateway * - * @method \League\Omnipay\Common\Message\ResponseInterface authorize(array $options = array()) (Optional method) + * @method Message\ResponseInterface authorize(array $options = array()) (Optional method) * Authorize an amount on the customers card - * @method \League\Omnipay\Common\Message\ResponseInterface completeAuthorize(array $options = array()) (Optional method) + * @method Message\ResponseInterface completeAuthorize(array $options = array()) (Optional method) * Handle return from off-site gateways after authorization - * @method \League\Omnipay\Common\Message\ResponseInterface capture(array $options = array()) (Optional method) + * @method Message\ResponseInterface capture(array $options = array()) (Optional method) * Capture an amount you have previously authorized - * @method \League\Omnipay\Common\Message\ResponseInterface purchase(array $options = array()) (Optional method) + * @method Message\ResponseInterface purchase(array $options = array()) (Optional method) * Authorize and immediately capture an amount on the customers card - * @method \League\Omnipay\Common\Message\ResponseInterface completePurchase(array $options = array()) (Optional method) + * @method Message\ResponseInterface completePurchase(array $options = array()) (Optional method) * Handle return from off-site gateways after purchase - * @method \League\Omnipay\Common\Message\ResponseInterface refund(array $options = array()) (Optional method) + * @method Message\ResponseInterface refund(array $options = array()) (Optional method) * Refund an already processed transaction - * @method \League\Omnipay\Common\Message\ResponseInterface void(array $options = array()) (Optional method) + * @method Message\ResponseInterface void(array $options = array()) (Optional method) * Generally can only be called up to 24 hours after submitting a transaction - * @method \League\Omnipay\Common\Message\ResponseInterface createCard(array $options = array()) (Optional method) + * @method Message\ResponseInterface createCard(array $options = array()) (Optional method) * The returned response object includes a cardReference, which can be used for future transactions - * @method \League\Omnipay\Common\Message\ResponseInterface updateCard(array $options = array()) (Optional method) + * @method Message\ResponseInterface updateCard(array $options = array()) (Optional method) * Update a stored card - * @method \League\Omnipay\Common\Message\ResponseInterface deleteCard(array $options = array()) (Optional method) + * @method Message\ResponseInterface deleteCard(array $options = array()) (Optional method) * Delete a stored card */ interface GatewayInterface From 87abfd9eb9f1514f0ae2a3badd33d08eee109706 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 13 Feb 2016 23:55:57 +0100 Subject: [PATCH 26/51] re-enable php5.5 --- .travis.yml | 6 ++++-- composer.json | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index aed35471..035cf971 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: php php: + - 5.5 + - 5.5 - 5.6 - 7.0 - hhvm @@ -15,9 +17,9 @@ env: matrix: include: - - php: 5.6 + - php: 5.5 env: setup=lowest - - php: 5.6 + - php: 5.5 env: setup=stable install: diff --git a/composer.json b/composer.json index 995a4592..440922f7 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "psr-4": { "League\\Omnipay\\" : "src/" } }, "require": { - "php": ">=5.6", + "php": ">=5.5", "alcohol/iso4217": "^3.1", "guzzlehttp/guzzle": "^6.0", "psr/http-message": "^1.0", From f8bab0db9dbc48782c1eee400244c100d32f9946 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 13 Feb 2016 23:58:39 +0100 Subject: [PATCH 27/51] Allow existing class to be used --- src/Common/GatewayFactory.php | 10 +++++++--- tests/Common/GatewayFactoryTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Common/GatewayFactory.php b/src/Common/GatewayFactory.php index 9775ee2e..72a7f657 100644 --- a/src/Common/GatewayFactory.php +++ b/src/Common/GatewayFactory.php @@ -78,12 +78,16 @@ public function register($className) * @throws RuntimeException If no such gateway is found * @return GatewayInterface An object of class $class is created and returned */ - public function create($class, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) + public function create($gateway, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) { - $class = Helper::getGatewayClassName($class); + $class = Helper::getGatewayClassName($gateway); if (!class_exists($class)) { - throw new RuntimeException("Class '$class' not found"); + if (class_exists($gateway)) { + $class = $gateway; + } else { + throw new RuntimeException("Class '$class' not found"); + } } return new $class($httpClient, $httpRequest); diff --git a/tests/Common/GatewayFactoryTest.php b/tests/Common/GatewayFactoryTest.php index 1bbcc16b..4671b3aa 100644 --- a/tests/Common/GatewayFactoryTest.php +++ b/tests/Common/GatewayFactoryTest.php @@ -53,6 +53,12 @@ public function testCreateFullyQualified() $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); } + public function testCreateExistingClass() + { + $gateway = $this->factory->create('League\\Omnipay\\SpareChange\\TestGateway'); + $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); + } + /** * @expectedException \League\Omnipay\Common\Exception\RuntimeException * @expectedExceptionMessage Class '\League\Omnipay\Invalid\Gateway' not found From 65bc7ce161bfd747aceb5d1a95acff7e07a9cc5d Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 14 Feb 2016 00:22:59 +0100 Subject: [PATCH 28/51] Mask sensitive creditcard data, fixes #355 --- src/Common/CreditCard.php | 45 ++++++++++++++++++++++++++++++++- tests/Common/CreditCardTest.php | 26 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/Common/CreditCard.php b/src/Common/CreditCard.php index db5a80c5..6316aa66 100644 --- a/src/Common/CreditCard.php +++ b/src/Common/CreditCard.php @@ -89,7 +89,7 @@ * * If any unknown parameters are passed in, they will be ignored. No error is thrown. */ -class CreditCard implements ParameterizedInterface +class CreditCard implements ParameterizedInterface, \JsonSerializable { use HasParametersTrait; @@ -507,4 +507,47 @@ public function getCustomer() { return $this->getBillingCustomer(); } + + /** + * Mask some parameters when debugging. + * + * @return array + */ + protected function toMaskedArray() + { + $params = $this->parameters->all(); + + if (isset($params['number'])) { + $params['number'] = $this->getNumberMasked(); + } + + $maskedKeys = ['expiryYear', 'expiryMonth', 'cvv', 'issueNumber']; + foreach ($maskedKeys as $key){ + if (isset($params[$key])) { + $params[$key] = str_pad('', strlen((string)$params[$key]), '*'); + } + } + + return $params; + } + + /** + * Serialize with sensitive data + * + * @return array + */ + public function jsonSerialize() + { + return $this->toMaskedArray(); + } + + /** + * var_dump or print_r without sensitive data + * + * @return array + */ + public function __debuginfo() + { + return $this->toMaskedArray(); + } } diff --git a/tests/Common/CreditCardTest.php b/tests/Common/CreditCardTest.php index 85b064f0..854fca24 100644 --- a/tests/Common/CreditCardTest.php +++ b/tests/Common/CreditCardTest.php @@ -298,4 +298,30 @@ public function testInvalidShortCard() $this->card->setNumber('4440'); $this->card->validate(); } + + public function testMaskedJson() + { + $card = json_decode(json_encode($this->card), true); + + $this->assertEquals('XXXXXXXXXXXX1111', $card['number']); + $this->assertEquals('****', $card['expiryYear']); + $this->assertEquals('*', $card['expiryMonth']); + $this->assertEquals('***', $card['cvv']); + } + + + public function testMaskedDebug() + { + if (version_compare(PHP_VERSION, '5.6.0', '<')) { + $this->markTestSkipped( + '__debugInfo is not available in PHP 5.6' + ); + } + + $output = print_r($this->card, true); + + $this->assertContains('XXXXXXXXXXXX1111', $output); + $this->assertNotContains('4111111111111111', $output); + $this->assertContains('****', $output); + } } From 053bf31fffe8c598d80c5132e826f20c6220a09d Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 14 Feb 2016 00:27:21 +0100 Subject: [PATCH 29/51] CS fix --- src/Common/CreditCard.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/CreditCard.php b/src/Common/CreditCard.php index 6316aa66..43df2fea 100644 --- a/src/Common/CreditCard.php +++ b/src/Common/CreditCard.php @@ -89,7 +89,7 @@ * * If any unknown parameters are passed in, they will be ignored. No error is thrown. */ -class CreditCard implements ParameterizedInterface, \JsonSerializable +class CreditCard implements ParameterizedInterface, \JsonSerializable { use HasParametersTrait; @@ -522,7 +522,7 @@ protected function toMaskedArray() } $maskedKeys = ['expiryYear', 'expiryMonth', 'cvv', 'issueNumber']; - foreach ($maskedKeys as $key){ + foreach ($maskedKeys as $key) { if (isset($params[$key])) { $params[$key] = str_pad('', strlen((string)$params[$key]), '*'); } From 59139cd0867a0f8aa1ed777b06de9daeeff7ff08 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 16 Feb 2016 21:38:28 +0100 Subject: [PATCH 30/51] Update CreditCard.php --- src/Common/CreditCard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/CreditCard.php b/src/Common/CreditCard.php index 43df2fea..24962550 100644 --- a/src/Common/CreditCard.php +++ b/src/Common/CreditCard.php @@ -546,7 +546,7 @@ public function jsonSerialize() * * @return array */ - public function __debuginfo() + public function __debugInfo() { return $this->toMaskedArray(); } From 09bfd2719cdd455be8288bd07e4ca36c2120e474 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 20 Mar 2016 18:03:11 +0100 Subject: [PATCH 31/51] Rename successful->completed, as statuses --- src/Common/Message/AbstractResponse.php | 23 ++++++++++++++++--- src/Common/Message/ResponseInterface.php | 19 +++++++++++++-- tests/Common/Message/AbstractResponseTest.php | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Common/Message/AbstractResponse.php b/src/Common/Message/AbstractResponse.php index ba46437e..b13ffb23 100644 --- a/src/Common/Message/AbstractResponse.php +++ b/src/Common/Message/AbstractResponse.php @@ -69,13 +69,30 @@ public function getRequest() } /** - * Is the response successful? + * Is the response completed? + * + * @return boolean + */ + abstract public function isCompleted(); + + /** + * Get the response status + * + * @return string + */ + public function getStatus() + { + return ResponseInterface::STATUS_UNDEFINED; + } + + /** + * Is the response pending? * * @return boolean */ public function isPending() { - return false; + return $this->getStatus() === ResponseInterface::STATUS_PENDING; } /** @@ -105,7 +122,7 @@ public function isTransparentRedirect() */ public function isCancelled() { - return false; + return $this->getStatus() === ResponseInterface::STATUS_CANCELLED; } /** diff --git a/src/Common/Message/ResponseInterface.php b/src/Common/Message/ResponseInterface.php index 4790bbea..2d32f5f3 100644 --- a/src/Common/Message/ResponseInterface.php +++ b/src/Common/Message/ResponseInterface.php @@ -15,6 +15,14 @@ */ interface ResponseInterface extends MessageInterface { + const STATUS_AUTHORIZED = 'authorized'; + const STATUS_CANCELLED = 'cancelled'; + const STATUS_CAPTURED = 'captured'; + const STATUS_EXPIRED = 'expired'; + const STATUS_PENDING = 'pending'; + const STATUS_REFUNDED = 'refunded'; + const STATUS_UNDEFINED = 'undefined'; + /** * Get the original request which generated this response * @@ -23,11 +31,11 @@ interface ResponseInterface extends MessageInterface public function getRequest(); /** - * Is the response successful? + * Is the transaction completed? * * @return boolean */ - public function isSuccessful(); + public function isCompleted(); /** * Does the response require a redirect? @@ -57,6 +65,13 @@ public function getMessage(); */ public function getCode(); + /** + * Status + * + * @return null|string The status of the response + */ + public function getStatus(); + /** * Gateway Reference * diff --git a/tests/Common/Message/AbstractResponseTest.php b/tests/Common/Message/AbstractResponseTest.php index d07c5a57..885b84e0 100644 --- a/tests/Common/Message/AbstractResponseTest.php +++ b/tests/Common/Message/AbstractResponseTest.php @@ -99,7 +99,7 @@ public function isPending() return false; } - public function isSuccessful() + public function isCompleted() { return false; } From 2245aa7f850ae9e06be9283ace37d6c9ef070d24 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 20 Mar 2016 18:15:17 +0100 Subject: [PATCH 32/51] Cache composer To speed up builds --- .travis.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 035cf971..6582e9e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,11 @@ php: # See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/ sudo: false +## Cache composer +cache: + directories: + - $HOME/.composer/cache + env: global: - setup=basic @@ -23,8 +28,8 @@ matrix: env: setup=stable install: - - if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-source; fi - - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-stable; fi - - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-lowest --prefer-stable; fi + - if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction; fi + - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable; fi + - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable; fi script: vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpunit --coverage-text From 0e749f9bedd4bd2269b3918295d625503a4b54d0 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 20 Mar 2016 18:36:27 +0100 Subject: [PATCH 33/51] Add amountinterface --- src/Common/Amount.php | 2 +- src/Common/AmountInterface.php | 47 ++++++++++++++++++++++++++ src/Common/Message/AbstractRequest.php | 5 +-- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/Common/AmountInterface.php diff --git a/src/Common/Amount.php b/src/Common/Amount.php index f2eb8248..65584a8a 100644 --- a/src/Common/Amount.php +++ b/src/Common/Amount.php @@ -12,7 +12,7 @@ * * This class abstracts certain functionality around amount and currencies in the Omnipay system. */ -final class Amount +final class Amount implements AmountInterface { /** @var string */ private $amount; diff --git a/src/Common/AmountInterface.php b/src/Common/AmountInterface.php new file mode 100644 index 00000000..b3844df4 --- /dev/null +++ b/src/Common/AmountInterface.php @@ -0,0 +1,47 @@ +getParameter('amount'); if ($amount !== null) { - if (!$amount instanceof Amount) { + if (!$amount instanceof AmountInterface) { // Default currency when none set $currency = $this->getCurrency(); @@ -338,7 +339,7 @@ public function getAmount() /** * Sets the payment amount. * - * @param string|Amount $value + * @param string|AmountInterface $value * @return AbstractRequest Provides a fluent interface * @throws InvalidRequestException */ From c645952ca01fafbe4b0ff40b3f05ff23752d39de Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 20 Mar 2016 22:39:53 +0100 Subject: [PATCH 34/51] Add locale --- src/Common/Locale.php | 89 ++++++++++++++++++++ src/Common/Message/AbstractRequest.php | 31 +++++++ tests/Common/LocaleTest.php | 54 ++++++++++++ tests/Common/Message/AbstractRequestTest.php | 9 ++ 4 files changed, 183 insertions(+) create mode 100644 src/Common/Locale.php create mode 100644 tests/Common/LocaleTest.php diff --git a/src/Common/Locale.php b/src/Common/Locale.php new file mode 100644 index 00000000..8741c704 --- /dev/null +++ b/src/Common/Locale.php @@ -0,0 +1,89 @@ +primaryLanguage = strtolower($primaryLanguage); + $this->region = strtolower($region); + } + + /** + * Get the full locale + * + * @return string + */ + public function getLocale() + { + return $this->primaryLanguage . ($this->region ? '-' . $this->region : ''); + } + + /** + * Get the primary language + * + * @return string + */ + public function getPrimaryLanguage() + { + return $this->primaryLanguage; + } + + /** + * Get the region + * + * @return string + */ + public function getRegion() + { + return $this->region ?: null; + } + + /** + * Get the locale, based on a string + * + * @param string $locale + * @return static + */ + public static function parse($locale) + { + $primaryLanguage = $locale; + $region = null; + + $locale = str_replace('_', '-', $locale); + if (strpos($locale, '-') !== false) { + list($primaryLanguage, $region) = explode('-', $locale); + } + + return new static($primaryLanguage, $region); + } + + public function __toString() + { + return $this->getLocale(); + } +} diff --git a/src/Common/Message/AbstractRequest.php b/src/Common/Message/AbstractRequest.php index 5cc1d174..aa3a5884 100644 --- a/src/Common/Message/AbstractRequest.php +++ b/src/Common/Message/AbstractRequest.php @@ -14,6 +14,7 @@ use League\Omnipay\Common\Exception\RuntimeException; use League\Omnipay\Common\Helper; use League\Omnipay\Common\ItemBag; +use League\Omnipay\Common\Locale; use League\Omnipay\Common\ParameterBag; use League\Omnipay\Common\ParameterizedInterface; use Psr\Http\Message\ServerRequestInterface; @@ -347,6 +348,36 @@ public function setAmount($value) return $this->setParameter('amount', $value); } + /** + * Get the request locale. + * + * @return Locale + */ + public function getLocale() + { + return $this->getParameter('locale'); + } + + /** + * Set the request Locale + * + * @param string|Locale $value + * @throws InvalidRequestException + * @return AbstractRequest Provides a fluent interface + */ + public function setLocale($value) + { + if (is_string($value)) { + $value = Locale::parse($value); + } + + if (! $value instanceof Locale) { + throw new InvalidRequestException('A valid Locale is required'); + } + + return $this->setParameter('locale', $value); + } + /** * Get the request description. * diff --git a/tests/Common/LocaleTest.php b/tests/Common/LocaleTest.php new file mode 100644 index 00000000..a88b6f1a --- /dev/null +++ b/tests/Common/LocaleTest.php @@ -0,0 +1,54 @@ +assertSame('en-us', $locale->getLocale()); + $this->assertSame('en', $locale->getPrimaryLanguage()); + $this->assertSame('us', $locale->getRegion()); + $this->assertSame('en-us', (string) $locale); + } + + public function testConstructWithoutRegion() + { + $locale = new Locale('en'); + $this->assertSame('en', $locale->getPrimaryLanguage()); + $this->assertNull($locale->getRegion()); + } + + public function testParse() + { + $locale = Locale::parse('en-us'); + $this->assertSame('en', $locale->getPrimaryLanguage()); + $this->assertSame('us', $locale->getRegion()); + } + + public function testParseUnderscore() + { + $locale = Locale::parse('en_us'); + $this->assertSame('en', $locale->getPrimaryLanguage()); + $this->assertSame('us', $locale->getRegion()); + } + + public function testParseWithoutRegion() + { + $locale = Locale::parse('en'); + $this->assertSame('en', $locale->getPrimaryLanguage()); + $this->assertNull($locale->getRegion()); + } + + public function testLowercase() + { + $locale = Locale::parse('En-US'); + $this->assertSame('en-us', $locale->getLocale()); + $this->assertSame('en', $locale->getPrimaryLanguage()); + $this->assertSame('us', $locale->getRegion()); + } +} diff --git a/tests/Common/Message/AbstractRequestTest.php b/tests/Common/Message/AbstractRequestTest.php index 9a75f075..449fce57 100644 --- a/tests/Common/Message/AbstractRequestTest.php +++ b/tests/Common/Message/AbstractRequestTest.php @@ -156,6 +156,15 @@ public function testAmountWithCurrency() $this->assertSame('EUR', $this->request->getAmount()->getCurrency()->getCode()); } + public function testLocale() + { + $this->assertSame($this->request, $this->request->setLocale('en-us')); + + $locale = $this->request->getLocale(); + $this->assertInstanceOf('\League\Omnipay\Common\Locale', $locale); + $this->assertSame('en-us', $locale->getLocale()); + } + public function testDescription() { $this->assertSame($this->request, $this->request->setDescription('Cool product')); From 6e0e68c9bddf5af9bf1aa75fcd436d30e71cd5ea Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 21 Jun 2016 21:16:09 +0200 Subject: [PATCH 35/51] CS fix --- src/Common/Message/AbstractRequest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Common/Message/AbstractRequest.php b/src/Common/Message/AbstractRequest.php index 60dc379b..c5e2ca0b 100644 --- a/src/Common/Message/AbstractRequest.php +++ b/src/Common/Message/AbstractRequest.php @@ -311,7 +311,6 @@ public function getAmount() if ($amount !== null) { if (!$amount instanceof AmountInterface) { - // Default currency when none set $currency = $this->getCurrency(); From 71476905de0b1fc13dc68603be4c1e016da8f7ea Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 21 Jun 2016 21:27:47 +0200 Subject: [PATCH 36/51] Bump to 5.6, test 7.1 alpha --- .travis.yml | 7 +++---- composer.json | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6582e9e9..c1e1c2ba 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php php: - - 5.5 - - 5.5 - 5.6 - 7.0 + - 7.1.0a1 - hhvm # This triggers builds to run on the new TravisCI infrastructure. @@ -22,9 +21,9 @@ env: matrix: include: - - php: 5.5 + - php: 5.6 env: setup=lowest - - php: 5.5 + - php: 5.6 env: setup=stable install: diff --git a/composer.json b/composer.json index 440922f7..995a4592 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "psr-4": { "League\\Omnipay\\" : "src/" } }, "require": { - "php": ">=5.5", + "php": ">=5.6", "alcohol/iso4217": "^3.1", "guzzlehttp/guzzle": "^6.0", "psr/http-message": "^1.0", From eff31cb9296b278ee014a03034685aecc61e8d94 Mon Sep 17 00:00:00 2001 From: Eduard Date: Wed, 20 Jul 2016 00:39:19 +0300 Subject: [PATCH 37/51] Fixed PHP version. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ad8b588..be5bf702 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Omnipay -**An easy to use, consistent payment processing library for PHP 5.3+** +**An easy to use, consistent payment processing library for PHP 5.6+** [![Build Status](https://travis-ci.org/thephpleague/omnipay.png?branch=3.0)](https://travis-ci.org/thephpleague/omnipay) [![Latest Stable Version](https://poser.pugx.org/league/omnipay/version)](https://packagist.org/packages/league/omnipay) From 03dd99b13862ebc3ad275b96dac94844a5505fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20T=C3=B3th?= Date: Fri, 22 Jul 2016 18:33:51 +0200 Subject: [PATCH 38/51] Upgrade to guzzle 6.2.1 For security reasons, see https://github.com/guzzle/guzzle/releases/tag/6.2.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 995a4592..c31315a4 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "require": { "php": ">=5.6", "alcohol/iso4217": "^3.1", - "guzzlehttp/guzzle": "^6.0", + "guzzlehttp/guzzle": "^6.2.1", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.1.0" }, From 3f652f54726530d50cfc0db404d94768698c6187 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 22 Oct 2016 20:45:27 +0200 Subject: [PATCH 39/51] Seperate gatewayfactory --- composer.json | 17 ++- phpunit.php | 3 - src/{Common => }/AbstractGateway.php | 26 +--- src/{Common => }/Amount.php | 0 src/{Common => }/AmountInterface.php | 0 src/Common/GatewayFactory.php | 95 -------------- src/{Common => }/CreditCard.php | 0 src/{Common => }/Currency.php | 0 src/{Common => }/Customer.php | 0 .../Exception/BadMethodCallException.php | 0 .../Exception/InvalidCreditCardException.php | 0 .../Exception/InvalidRequestException.php | 0 .../Exception/InvalidResponseException.php | 0 .../Exception/OmnipayException.php | 0 .../Exception/RuntimeException.php | 0 src/{Common => }/GatewayInterface.php | 0 src/{Common => }/HasParametersTrait.php | 0 src/{Common => }/Helper.php | 0 src/{Common => }/Http/ClientInterface.php | 0 src/{Common => }/Http/GuzzleClient.php | 0 src/{Common => }/Issuer.php | 0 src/{Common => }/Item.php | 0 src/{Common => }/ItemBag.php | 0 src/{Common => }/ItemInterface.php | 0 src/{Common => }/Locale.php | 0 src/{Common => }/Message/AbstractRequest.php | 0 src/{Common => }/Message/AbstractResponse.php | 0 .../Message/FetchIssuersResponseInterface.php | 0 .../FetchPaymentMethodsResponseInterface.php | 0 src/{Common => }/Message/MessageInterface.php | 0 .../Message/NotificationInterface.php | 0 .../Message/RedirectResponseInterface.php | 0 src/{Common => }/Message/RequestInterface.php | 0 .../Message/ResponseInterface.php | 0 src/Omnipay.php | 120 ------------------ src/{Common => }/ParameterBag.php | 0 src/{Common => }/ParameterizedInterface.php | 0 src/{Common => }/PaymentMethod.php | 0 tests/{Common => }/AbstractGatewayTest.php | 11 +- tests/{Common => }/AmountTest.php | 0 tests/Common/GatewayFactoryTest.php | 70 ---------- tests/{Common => }/CreditCardTest.php | 0 tests/{Common => }/CurrencyTest.php | 0 tests/{Common => }/CustomerTest.php | 0 .../Exception/BadMethodCallExceptionTest.php | 0 .../InvalidCreditCardExceptionTest.php | 0 .../Exception/InvalidRequestExceptionTest.php | 0 .../InvalidResponseExceptionTest.php | 0 .../Exception/RuntimeExceptionTest.php | 0 tests/{Common => }/HelperTest.php | 0 tests/{Common => }/Http/GuzzleClientTest.php | 0 tests/{Common => }/IssuerTest.php | 0 tests/{Common => }/ItemBagTest.php | 0 tests/{Common => }/ItemTest.php | 0 tests/{Common => }/LocaleTest.php | 0 .../Message/AbstractRequestTest.php | 0 .../Message/AbstractResponseTest.php | 0 tests/OmnipayTest.php | 42 ------ tests/{Common => }/PaymentMethodTest.php | 0 59 files changed, 24 insertions(+), 360 deletions(-) rename src/{Common => }/AbstractGateway.php (91%) rename src/{Common => }/Amount.php (100%) rename src/{Common => }/AmountInterface.php (100%) delete mode 100644 src/Common/GatewayFactory.php rename src/{Common => }/CreditCard.php (100%) rename src/{Common => }/Currency.php (100%) rename src/{Common => }/Customer.php (100%) rename src/{Common => }/Exception/BadMethodCallException.php (100%) rename src/{Common => }/Exception/InvalidCreditCardException.php (100%) rename src/{Common => }/Exception/InvalidRequestException.php (100%) rename src/{Common => }/Exception/InvalidResponseException.php (100%) rename src/{Common => }/Exception/OmnipayException.php (100%) rename src/{Common => }/Exception/RuntimeException.php (100%) rename src/{Common => }/GatewayInterface.php (100%) rename src/{Common => }/HasParametersTrait.php (100%) rename src/{Common => }/Helper.php (100%) rename src/{Common => }/Http/ClientInterface.php (100%) rename src/{Common => }/Http/GuzzleClient.php (100%) rename src/{Common => }/Issuer.php (100%) rename src/{Common => }/Item.php (100%) rename src/{Common => }/ItemBag.php (100%) rename src/{Common => }/ItemInterface.php (100%) rename src/{Common => }/Locale.php (100%) rename src/{Common => }/Message/AbstractRequest.php (100%) rename src/{Common => }/Message/AbstractResponse.php (100%) rename src/{Common => }/Message/FetchIssuersResponseInterface.php (100%) rename src/{Common => }/Message/FetchPaymentMethodsResponseInterface.php (100%) rename src/{Common => }/Message/MessageInterface.php (100%) rename src/{Common => }/Message/NotificationInterface.php (100%) rename src/{Common => }/Message/RedirectResponseInterface.php (100%) rename src/{Common => }/Message/RequestInterface.php (100%) rename src/{Common => }/Message/ResponseInterface.php (100%) delete mode 100644 src/Omnipay.php rename src/{Common => }/ParameterBag.php (100%) rename src/{Common => }/ParameterizedInterface.php (100%) rename src/{Common => }/PaymentMethod.php (100%) rename tests/{Common => }/AbstractGatewayTest.php (92%) rename tests/{Common => }/AmountTest.php (100%) delete mode 100644 tests/Common/GatewayFactoryTest.php rename tests/{Common => }/CreditCardTest.php (100%) rename tests/{Common => }/CurrencyTest.php (100%) rename tests/{Common => }/CustomerTest.php (100%) rename tests/{Common => }/Exception/BadMethodCallExceptionTest.php (100%) rename tests/{Common => }/Exception/InvalidCreditCardExceptionTest.php (100%) rename tests/{Common => }/Exception/InvalidRequestExceptionTest.php (100%) rename tests/{Common => }/Exception/InvalidResponseExceptionTest.php (100%) rename tests/{Common => }/Exception/RuntimeExceptionTest.php (100%) rename tests/{Common => }/HelperTest.php (100%) rename tests/{Common => }/Http/GuzzleClientTest.php (100%) rename tests/{Common => }/IssuerTest.php (100%) rename tests/{Common => }/ItemBagTest.php (100%) rename tests/{Common => }/ItemTest.php (100%) rename tests/{Common => }/LocaleTest.php (100%) rename tests/{Common => }/Message/AbstractRequestTest.php (100%) rename tests/{Common => }/Message/AbstractResponseTest.php (100%) delete mode 100644 tests/OmnipayTest.php rename tests/{Common => }/PaymentMethodTest.php (100%) diff --git a/composer.json b/composer.json index c31315a4..268f2a18 100644 --- a/composer.json +++ b/composer.json @@ -23,17 +23,24 @@ } ], "autoload": { - "psr-4": { "League\\Omnipay\\" : "src/" } + "psr-4": { + "League\\Omnipay\\Common\\" : "src/" + } + }, + "autoload-dev": { + "psr-4": { + "League\\Omnipay\\Common\\" : "tests/" + } }, "require": { "php": ">=5.6", "alcohol/iso4217": "^3.1", - "guzzlehttp/guzzle": "^6.2.1", - "psr/http-message": "^1.0", - "zendframework/zend-diactoros": "^1.1.0" + "psr/http-message": "^1.0" }, "require-dev": { - "omnipay/tests": "^3.0" + "omnipay/tests": "^3.0", + "guzzlehttp/guzzle": "^6.2.1", + "zendframework/zend-diactoros": "^1.1.0" }, "minimum-stability": "dev", "prefer-stable": true diff --git a/phpunit.php b/phpunit.php index 5a3ecaf8..b5947821 100644 --- a/phpunit.php +++ b/phpunit.php @@ -5,6 +5,3 @@ // include the composer autoloader $autoloader = require __DIR__ . '/vendor/autoload.php'; - -// autoload abstract TestCase classes in test directory -$autoloader->addPsr4('League\\Omnipay\\', __DIR__ . '/tests'); diff --git a/src/Common/AbstractGateway.php b/src/AbstractGateway.php similarity index 91% rename from src/Common/AbstractGateway.php rename to src/AbstractGateway.php index c045fe43..c48f121e 100755 --- a/src/Common/AbstractGateway.php +++ b/src/AbstractGateway.php @@ -62,10 +62,10 @@ abstract class AbstractGateway implements GatewayInterface, ParameterizedInterfa * @param ClientInterface $httpClient A HTTP client to make API calls with * @param ServerRequestInterface $httpRequest A HTTP request object */ - public function __construct(ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) + public function __construct(ClientInterface $httpClient, ServerRequestInterface $httpRequest) { - $this->httpClient = $httpClient ?: $this->getDefaultHttpClient(); - $this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest(); + $this->httpClient = $httpClient; + $this->httpRequest = $httpRequest; $this->initialize(); } @@ -291,24 +291,4 @@ protected function createRequest($class, array $parameters) return $obj->initialize(array_replace($this->getParameters(), $parameters)); } - - /** - * Get the global default HTTP client. - * - * @return ClientInterface - */ - protected function getDefaultHttpClient() - { - return new GuzzleClient(); - } - - /** - * Get the global default HTTP request. - * - * @return ServerRequestInterface - */ - protected function getDefaultHttpRequest() - { - return ServerRequestFactory::fromGlobals(); - } } diff --git a/src/Common/Amount.php b/src/Amount.php similarity index 100% rename from src/Common/Amount.php rename to src/Amount.php diff --git a/src/Common/AmountInterface.php b/src/AmountInterface.php similarity index 100% rename from src/Common/AmountInterface.php rename to src/AmountInterface.php diff --git a/src/Common/GatewayFactory.php b/src/Common/GatewayFactory.php deleted file mode 100644 index 72a7f657..00000000 --- a/src/Common/GatewayFactory.php +++ /dev/null @@ -1,95 +0,0 @@ - - * // Create a gateway for the PayPal ExpressGateway - * // (routes to GatewayFactory::create) - * $gateway = Omnipay::create('ExpressGateway'); - * - * - * @see Omnipay\Omnipay - */ -class GatewayFactory -{ - /** - * Internal storage for all available gateways - * - * @var array - */ - private $gateways = array(); - - /** - * All available gateways - * - * @return array An array of gateway names - */ - public function all() - { - return $this->gateways; - } - - /** - * Replace the list of available gateways - * - * @param array $gateways An array of gateway names - */ - public function replace(array $gateways) - { - $this->gateways = $gateways; - } - - /** - * Register a new gateway - * - * @param string $className Gateway name - */ - public function register($className) - { - if (!in_array($className, $this->gateways)) { - $this->gateways[] = $className; - } - } - - /** - * Create a new gateway instance - * - * @param string $class Gateway name - * @param ClientInterface|null $httpClient A HTTP Client implementation - * @param ServerRequestInterface|null $httpRequest A HTTP Request implementation - * @throws RuntimeException If no such gateway is found - * @return GatewayInterface An object of class $class is created and returned - */ - public function create($gateway, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) - { - $class = Helper::getGatewayClassName($gateway); - - if (!class_exists($class)) { - if (class_exists($gateway)) { - $class = $gateway; - } else { - throw new RuntimeException("Class '$class' not found"); - } - } - - return new $class($httpClient, $httpRequest); - } -} diff --git a/src/Common/CreditCard.php b/src/CreditCard.php similarity index 100% rename from src/Common/CreditCard.php rename to src/CreditCard.php diff --git a/src/Common/Currency.php b/src/Currency.php similarity index 100% rename from src/Common/Currency.php rename to src/Currency.php diff --git a/src/Common/Customer.php b/src/Customer.php similarity index 100% rename from src/Common/Customer.php rename to src/Customer.php diff --git a/src/Common/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php similarity index 100% rename from src/Common/Exception/BadMethodCallException.php rename to src/Exception/BadMethodCallException.php diff --git a/src/Common/Exception/InvalidCreditCardException.php b/src/Exception/InvalidCreditCardException.php similarity index 100% rename from src/Common/Exception/InvalidCreditCardException.php rename to src/Exception/InvalidCreditCardException.php diff --git a/src/Common/Exception/InvalidRequestException.php b/src/Exception/InvalidRequestException.php similarity index 100% rename from src/Common/Exception/InvalidRequestException.php rename to src/Exception/InvalidRequestException.php diff --git a/src/Common/Exception/InvalidResponseException.php b/src/Exception/InvalidResponseException.php similarity index 100% rename from src/Common/Exception/InvalidResponseException.php rename to src/Exception/InvalidResponseException.php diff --git a/src/Common/Exception/OmnipayException.php b/src/Exception/OmnipayException.php similarity index 100% rename from src/Common/Exception/OmnipayException.php rename to src/Exception/OmnipayException.php diff --git a/src/Common/Exception/RuntimeException.php b/src/Exception/RuntimeException.php similarity index 100% rename from src/Common/Exception/RuntimeException.php rename to src/Exception/RuntimeException.php diff --git a/src/Common/GatewayInterface.php b/src/GatewayInterface.php similarity index 100% rename from src/Common/GatewayInterface.php rename to src/GatewayInterface.php diff --git a/src/Common/HasParametersTrait.php b/src/HasParametersTrait.php similarity index 100% rename from src/Common/HasParametersTrait.php rename to src/HasParametersTrait.php diff --git a/src/Common/Helper.php b/src/Helper.php similarity index 100% rename from src/Common/Helper.php rename to src/Helper.php diff --git a/src/Common/Http/ClientInterface.php b/src/Http/ClientInterface.php similarity index 100% rename from src/Common/Http/ClientInterface.php rename to src/Http/ClientInterface.php diff --git a/src/Common/Http/GuzzleClient.php b/src/Http/GuzzleClient.php similarity index 100% rename from src/Common/Http/GuzzleClient.php rename to src/Http/GuzzleClient.php diff --git a/src/Common/Issuer.php b/src/Issuer.php similarity index 100% rename from src/Common/Issuer.php rename to src/Issuer.php diff --git a/src/Common/Item.php b/src/Item.php similarity index 100% rename from src/Common/Item.php rename to src/Item.php diff --git a/src/Common/ItemBag.php b/src/ItemBag.php similarity index 100% rename from src/Common/ItemBag.php rename to src/ItemBag.php diff --git a/src/Common/ItemInterface.php b/src/ItemInterface.php similarity index 100% rename from src/Common/ItemInterface.php rename to src/ItemInterface.php diff --git a/src/Common/Locale.php b/src/Locale.php similarity index 100% rename from src/Common/Locale.php rename to src/Locale.php diff --git a/src/Common/Message/AbstractRequest.php b/src/Message/AbstractRequest.php similarity index 100% rename from src/Common/Message/AbstractRequest.php rename to src/Message/AbstractRequest.php diff --git a/src/Common/Message/AbstractResponse.php b/src/Message/AbstractResponse.php similarity index 100% rename from src/Common/Message/AbstractResponse.php rename to src/Message/AbstractResponse.php diff --git a/src/Common/Message/FetchIssuersResponseInterface.php b/src/Message/FetchIssuersResponseInterface.php similarity index 100% rename from src/Common/Message/FetchIssuersResponseInterface.php rename to src/Message/FetchIssuersResponseInterface.php diff --git a/src/Common/Message/FetchPaymentMethodsResponseInterface.php b/src/Message/FetchPaymentMethodsResponseInterface.php similarity index 100% rename from src/Common/Message/FetchPaymentMethodsResponseInterface.php rename to src/Message/FetchPaymentMethodsResponseInterface.php diff --git a/src/Common/Message/MessageInterface.php b/src/Message/MessageInterface.php similarity index 100% rename from src/Common/Message/MessageInterface.php rename to src/Message/MessageInterface.php diff --git a/src/Common/Message/NotificationInterface.php b/src/Message/NotificationInterface.php similarity index 100% rename from src/Common/Message/NotificationInterface.php rename to src/Message/NotificationInterface.php diff --git a/src/Common/Message/RedirectResponseInterface.php b/src/Message/RedirectResponseInterface.php similarity index 100% rename from src/Common/Message/RedirectResponseInterface.php rename to src/Message/RedirectResponseInterface.php diff --git a/src/Common/Message/RequestInterface.php b/src/Message/RequestInterface.php similarity index 100% rename from src/Common/Message/RequestInterface.php rename to src/Message/RequestInterface.php diff --git a/src/Common/Message/ResponseInterface.php b/src/Message/ResponseInterface.php similarity index 100% rename from src/Common/Message/ResponseInterface.php rename to src/Message/ResponseInterface.php diff --git a/src/Omnipay.php b/src/Omnipay.php deleted file mode 100644 index 47471a38..00000000 --- a/src/Omnipay.php +++ /dev/null @@ -1,120 +0,0 @@ - - * // Create a gateway for the PayPal ExpressGateway - * // (routes to GatewayFactory::create) - * $gateway = Omnipay::create('ExpressGateway'); - * - * // Initialise the gateway - * $gateway->initialize(...); - * - * // Get the gateway parameters. - * $parameters = $gateway->getParameters(); - * - * // Create a credit card object - * $card = new CreditCard(...); - * - * // Do an authorisation transaction on the gateway - * if ($gateway->supportsAuthorize()) { - * $gateway->authorize(...); - * } else { - * throw new \Exception('Gateway does not support authorize()'); - * } - * - * - * For further code examples see the *omnipay-example* repository on github. - * - * @method static array all() - * @method static array replace(array $gateways) - * @method static string register(string $className) - * @method static array find() - * @method static array getSupportedGateways() - * @codingStandardsIgnoreStart - * @method static GatewayInterface create(string $class, ClientInterface $httpClient = null, ServerRequestInterface $httpRequest = null) - * @codingStandardsIgnoreEnd - * - * @see League\Omnipay\Common\GatewayFactory - */ -class Omnipay -{ - - /** - * Internal factory storage - * - * @var GatewayFactory - */ - private static $factory; - - /** - * Get the gateway factory - * - * Creates a new empty GatewayFactory if none has been set previously. - * - * @return GatewayFactory A GatewayFactory instance - */ - public static function getFactory() - { - if (is_null(static::$factory)) { - static::$factory = new GatewayFactory; - } - - return static::$factory; - } - - /** - * Set the gateway factory - * - * @param GatewayFactory $factory A GatewayFactory instance - */ - public static function setFactory(GatewayFactory $factory = null) - { - static::$factory = $factory; - } - - /** - * Static function call router. - * - * All other function calls to the Omnipay class are routed to the - * factory. e.g. Omnipay::getSupportedGateways(1, 2, 3, 4) is routed to the - * factory's getSupportedGateways method and passed the parameters 1, 2, 3, 4. - * - * Example: - * - * - * // Create a gateway for the PayPal ExpressGateway - * $gateway = Omnipay::create('ExpressGateway'); - * - * - * @see GatewayFactory - * - * @param string $method The factory method to invoke. - * @param array $parameters Parameters passed to the factory method. - * - * @return mixed - */ - public static function __callStatic($method, $parameters) - { - $factory = static::getFactory(); - - return call_user_func_array(array($factory, $method), $parameters); - } -} diff --git a/src/Common/ParameterBag.php b/src/ParameterBag.php similarity index 100% rename from src/Common/ParameterBag.php rename to src/ParameterBag.php diff --git a/src/Common/ParameterizedInterface.php b/src/ParameterizedInterface.php similarity index 100% rename from src/Common/ParameterizedInterface.php rename to src/ParameterizedInterface.php diff --git a/src/Common/PaymentMethod.php b/src/PaymentMethod.php similarity index 100% rename from src/Common/PaymentMethod.php rename to src/PaymentMethod.php diff --git a/tests/Common/AbstractGatewayTest.php b/tests/AbstractGatewayTest.php similarity index 92% rename from tests/Common/AbstractGatewayTest.php rename to tests/AbstractGatewayTest.php index be7e64c4..fd80a4c6 100755 --- a/tests/Common/AbstractGatewayTest.php +++ b/tests/AbstractGatewayTest.php @@ -2,9 +2,12 @@ namespace League\Omnipay\Common; +use GuzzleHttp\Client; +use League\Omnipay\Common\Http\GuzzleClient; use Mockery as m; use League\Omnipay\Common\Message\AbstractRequest; use League\Omnipay\Tests\TestCase; +use Zend\Diactoros\ServerRequestFactory; class AbstractGatewayTest extends TestCase { @@ -16,7 +19,9 @@ public function setUp() public function testConstruct() { - $this->gateway = new AbstractGatewayTest_MockAbstractGateway; + $httpClient = new GuzzleClient(new Client()); + $httpRequest = ServerRequestFactory::fromGlobals(); + $this->gateway = new AbstractGatewayTest_MockAbstractGateway($httpClient, $httpRequest); $this->assertInstanceOf('\League\Omnipay\Common\Http\ClientInterface', $this->gateway->getProtectedHttpClient()); $this->assertInstanceOf('\Psr\Http\Message\ServerRequestInterface', $this->gateway->getProtectedHttpRequest()); $this->assertSame(array(), $this->gateway->getParameters()); @@ -139,7 +144,9 @@ public function testSupportsAcceptNotification() public function testCreateRequest() { - $this->gateway = new AbstractGatewayTest_MockAbstractGateway; + $httpClient = new GuzzleClient(new Client()); + $httpRequest = ServerRequestFactory::fromGlobals(); + $this->gateway = new AbstractGatewayTest_MockAbstractGateway($httpClient, $httpRequest); $request = $this->gateway->callCreateRequest( '\League\Omnipay\Common\AbstractGatewayTest_MockAbstractRequest', array('currency' => 'THB') diff --git a/tests/Common/AmountTest.php b/tests/AmountTest.php similarity index 100% rename from tests/Common/AmountTest.php rename to tests/AmountTest.php diff --git a/tests/Common/GatewayFactoryTest.php b/tests/Common/GatewayFactoryTest.php deleted file mode 100644 index 4671b3aa..00000000 --- a/tests/Common/GatewayFactoryTest.php +++ /dev/null @@ -1,70 +0,0 @@ -factory = new GatewayFactory; - } - - public function testReplace() - { - $gateways = array('Foo'); - $this->factory->replace($gateways); - - $this->assertSame($gateways, $this->factory->all()); - } - - public function testRegister() - { - $this->factory->register('Bar'); - - $this->assertSame(array('Bar'), $this->factory->all()); - } - - public function testRegisterExistingGateway() - { - $this->factory->register('Milky'); - $this->factory->register('Bar'); - $this->factory->register('Bar'); - - $this->assertSame(array('Milky', 'Bar'), $this->factory->all()); - } - - public function testCreateShortName() - { - $gateway = $this->factory->create('SpareChange_Test'); - $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); - } - - public function testCreateFullyQualified() - { - $gateway = $this->factory->create('\\League\\Omnipay\\SpareChange\\TestGateway'); - $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); - } - - public function testCreateExistingClass() - { - $gateway = $this->factory->create('League\\Omnipay\\SpareChange\\TestGateway'); - $this->assertInstanceOf('\\League\\Omnipay\\SpareChange\\TestGateway', $gateway); - } - - /** - * @expectedException \League\Omnipay\Common\Exception\RuntimeException - * @expectedExceptionMessage Class '\League\Omnipay\Invalid\Gateway' not found - */ - public function testCreateInvalid() - { - $gateway = $this->factory->create('Invalid'); - } -} diff --git a/tests/Common/CreditCardTest.php b/tests/CreditCardTest.php similarity index 100% rename from tests/Common/CreditCardTest.php rename to tests/CreditCardTest.php diff --git a/tests/Common/CurrencyTest.php b/tests/CurrencyTest.php similarity index 100% rename from tests/Common/CurrencyTest.php rename to tests/CurrencyTest.php diff --git a/tests/Common/CustomerTest.php b/tests/CustomerTest.php similarity index 100% rename from tests/Common/CustomerTest.php rename to tests/CustomerTest.php diff --git a/tests/Common/Exception/BadMethodCallExceptionTest.php b/tests/Exception/BadMethodCallExceptionTest.php similarity index 100% rename from tests/Common/Exception/BadMethodCallExceptionTest.php rename to tests/Exception/BadMethodCallExceptionTest.php diff --git a/tests/Common/Exception/InvalidCreditCardExceptionTest.php b/tests/Exception/InvalidCreditCardExceptionTest.php similarity index 100% rename from tests/Common/Exception/InvalidCreditCardExceptionTest.php rename to tests/Exception/InvalidCreditCardExceptionTest.php diff --git a/tests/Common/Exception/InvalidRequestExceptionTest.php b/tests/Exception/InvalidRequestExceptionTest.php similarity index 100% rename from tests/Common/Exception/InvalidRequestExceptionTest.php rename to tests/Exception/InvalidRequestExceptionTest.php diff --git a/tests/Common/Exception/InvalidResponseExceptionTest.php b/tests/Exception/InvalidResponseExceptionTest.php similarity index 100% rename from tests/Common/Exception/InvalidResponseExceptionTest.php rename to tests/Exception/InvalidResponseExceptionTest.php diff --git a/tests/Common/Exception/RuntimeExceptionTest.php b/tests/Exception/RuntimeExceptionTest.php similarity index 100% rename from tests/Common/Exception/RuntimeExceptionTest.php rename to tests/Exception/RuntimeExceptionTest.php diff --git a/tests/Common/HelperTest.php b/tests/HelperTest.php similarity index 100% rename from tests/Common/HelperTest.php rename to tests/HelperTest.php diff --git a/tests/Common/Http/GuzzleClientTest.php b/tests/Http/GuzzleClientTest.php similarity index 100% rename from tests/Common/Http/GuzzleClientTest.php rename to tests/Http/GuzzleClientTest.php diff --git a/tests/Common/IssuerTest.php b/tests/IssuerTest.php similarity index 100% rename from tests/Common/IssuerTest.php rename to tests/IssuerTest.php diff --git a/tests/Common/ItemBagTest.php b/tests/ItemBagTest.php similarity index 100% rename from tests/Common/ItemBagTest.php rename to tests/ItemBagTest.php diff --git a/tests/Common/ItemTest.php b/tests/ItemTest.php similarity index 100% rename from tests/Common/ItemTest.php rename to tests/ItemTest.php diff --git a/tests/Common/LocaleTest.php b/tests/LocaleTest.php similarity index 100% rename from tests/Common/LocaleTest.php rename to tests/LocaleTest.php diff --git a/tests/Common/Message/AbstractRequestTest.php b/tests/Message/AbstractRequestTest.php similarity index 100% rename from tests/Common/Message/AbstractRequestTest.php rename to tests/Message/AbstractRequestTest.php diff --git a/tests/Common/Message/AbstractResponseTest.php b/tests/Message/AbstractResponseTest.php similarity index 100% rename from tests/Common/Message/AbstractResponseTest.php rename to tests/Message/AbstractResponseTest.php diff --git a/tests/OmnipayTest.php b/tests/OmnipayTest.php deleted file mode 100644 index a0743074..00000000 --- a/tests/OmnipayTest.php +++ /dev/null @@ -1,42 +0,0 @@ -assertInstanceOf('League\Omnipay\Common\GatewayFactory', $factory); - } - - public function testSetFactory() - { - $factory = m::mock('League\Omnipay\Common\GatewayFactory'); - - Omnipay::setFactory($factory); - - $this->assertSame($factory, Omnipay::getFactory()); - } - - public function testCallStatic() - { - $factory = m::mock('League\Omnipay\Common\GatewayFactory'); - $factory->shouldReceive('testMethod')->with('some-argument')->once()->andReturn('some-result'); - - Omnipay::setFactory($factory); - - $result = Omnipay::testMethod('some-argument'); - $this->assertSame('some-result', $result); - } -} diff --git a/tests/Common/PaymentMethodTest.php b/tests/PaymentMethodTest.php similarity index 100% rename from tests/Common/PaymentMethodTest.php rename to tests/PaymentMethodTest.php From 7d8940d1c48e97017e91711c6466cd05fc7340ad Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 22 Oct 2016 21:01:44 +0200 Subject: [PATCH 40/51] Change amount behavior --- src/Amount.php | 2 +- src/AmountInterface.php | 2 +- src/Message/AbstractRequest.php | 4 ++-- tests/AmountTest.php | 18 +++++++++--------- tests/Message/AbstractRequestTest.php | 25 ++++++++----------------- 5 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/Amount.php b/src/Amount.php index 65584a8a..28df4f20 100644 --- a/src/Amount.php +++ b/src/Amount.php @@ -42,7 +42,7 @@ public function __construct($amount, $currency) * * @return string */ - public function getAmount() + public function getInteger() { return $this->amount; } diff --git a/src/AmountInterface.php b/src/AmountInterface.php index b3844df4..d2276365 100644 --- a/src/AmountInterface.php +++ b/src/AmountInterface.php @@ -19,7 +19,7 @@ interface AmountInterface * * @return string */ - public function getAmount(); + public function getInteger(); /** * Get the amount as decimal string diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 281f63ba..56d58cfa 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -306,7 +306,7 @@ public function setCurrency($value) * Validates and returns amount as integer. * * @throws InvalidRequestException on any validation failure. - * @return string The amount in smallest unit possible (eg. 'cents') + * @return string The amount as decimal */ public function getAmount() { @@ -321,7 +321,7 @@ public function getAmount() throw new InvalidRequestException('A currency is required.'); } - $amount = new Amount($amount, $currency); + $amount = Amount::fromDecimal($amount, $currency); } // Check for a negative amount. diff --git a/tests/AmountTest.php b/tests/AmountTest.php index 6af33021..d2b59d14 100644 --- a/tests/AmountTest.php +++ b/tests/AmountTest.php @@ -10,13 +10,13 @@ class AmountTest extends TestCase public function testConstruct() { $amount = new Amount('1000', 'USD'); - $this->assertSame('1000', $amount->getAmount()); + $this->assertSame('1000', $amount->getInteger()); } public function testConstructInteger() { $amount = new Amount(1000, 'USD'); - $this->assertSame('1000', $amount->getAmount()); + $this->assertSame('1000', $amount->getInteger()); } /** @@ -38,16 +38,16 @@ public function testConstructDecimalString() public function testFromDecimal() { $amount = Amount::fromDecimal('10.00', 'USD'); - $this->assertSame('1000', $amount->getAmount()); + $this->assertSame('1000', $amount->getInteger()); } public function testFromDecimalRounded() { $amount = Amount::fromDecimal('10', 'USD'); - $this->assertSame('1000', $amount->getAmount()); + $this->assertSame('1000', $amount->getInteger()); $amount = Amount::fromDecimal(10, 'USD'); - $this->assertSame('1000', $amount->getAmount()); + $this->assertSame('1000', $amount->getInteger()); } /** @@ -84,14 +84,14 @@ public function testGetAmountNoDecimals() $amount = new Amount(1366, 'JPY'); $this->assertSame('JPY', $amount->getCurrency()->getCode()); - $this->assertSame('1366', $amount->getAmount()); + $this->assertSame('1366', $amount->getInteger()); $this->assertSame('1366', $amount->getFormatted()); } public function testFromDecimalNoDecimals() { $amount = Amount::fromDecimal('10', 'JPY'); - $this->assertSame('10', $amount->getAmount()); + $this->assertSame('10', $amount->getInteger()); } public function testIsNegative() @@ -128,7 +128,7 @@ public function testAmountNegativeDecimalString() { $amount = Amount::fromDecimal('-123.00', 'USD'); - $this->assertEquals('-12300', $amount->getAmount()); + $this->assertEquals('-12300', $amount->getInteger()); $this->assertTrue($amount->isNegative()); } @@ -136,7 +136,7 @@ public function testAmountNegativeDecimalFloat() { $amount = Amount::fromDecimal(-123.00, 'USD'); - $this->assertEquals('-12300', $amount->getAmount()); + $this->assertEquals('-12300', $amount->getInteger()); $this->assertTrue($amount->isNegative()); } diff --git a/tests/Message/AbstractRequestTest.php b/tests/Message/AbstractRequestTest.php index 449fce57..2b9437a4 100644 --- a/tests/Message/AbstractRequestTest.php +++ b/tests/Message/AbstractRequestTest.php @@ -42,8 +42,8 @@ public function testConstruct() public function testInitializeWithParams() { - $this->assertSame($this->request, $this->request->initialize(array('amount' => '123', 'currency' => 'USD'))); - $this->assertSame('123', $this->request->getAmount()->getAmount()); + $this->assertSame($this->request, $this->request->initialize(array('amount' => '1.23', 'currency' => 'USD'))); + $this->assertSame('123', $this->request->getAmount()->getInteger()); } /** @@ -90,14 +90,14 @@ public function testCardReference() public function testAmount() { - $this->assertSame($this->request, $this->request->setAmount(2)); - $this->assertSame('2', $this->request->getAmount()->getAmount()); + $this->assertSame($this->request, $this->request->setAmount('2')); + $this->assertSame('200', $this->request->getAmount()->getInteger()); } public function testAmountWithInt() { $this->assertSame($this->request, $this->request->setAmount(2)); - $this->assertSame('2', $this->request->getAmount()->getAmount()); + $this->assertSame('200', $this->request->getAmount()->getInteger()); } public function testAmountWithEmpty() @@ -109,13 +109,13 @@ public function testAmountWithEmpty() public function testAmountZeroFloat() { $this->assertSame($this->request, $this->request->setAmount(0)); - $this->assertSame('0', $this->request->getAmount()->getAmount()); + $this->assertSame('0', $this->request->getAmount()->getInteger()); } public function testAmountZeroString() { $this->assertSame($this->request, $this->request->setAmount('0')); - $this->assertSame('0', $this->request->getAmount()->getAmount()); + $this->assertSame('0', $this->request->getAmount()->getInteger()); } /** @@ -129,18 +129,9 @@ public function testAmountZeroNotAllowed() $this->request->getAmount(); } - /** - * @expectedException InvalidArgumentException - */ - public function testAmountWithFloatStringThrowsException() - { - $this->assertSame($this->request, $this->request->setAmount('10.00')); - $this->request->getAmount(); - } - public function testGetAmountFormatted() { - $this->assertSame($this->request, $this->request->setAmount(1366)); + $this->assertSame($this->request, $this->request->setAmount(13.66)); $this->assertSame('13.66', $this->request->getAmount()->getFormatted()); } From d159bb716e61a02ea9da15e338c006f610f1a3a3 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 22 Oct 2016 21:05:08 +0200 Subject: [PATCH 41/51] Fix composer.json --- composer.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 268f2a18..93a79d3b 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,9 @@ { - "name": "league/omnipay", + "name": "omnipay/common", "type": "library", "description": "Common components for Omnipay payment processing library", "keywords": [ + "league", "gateway", "merchant", "omnipay", @@ -17,6 +18,10 @@ "name": "Adrian Macneil", "email": "adrian@adrianmacneil.com" }, + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + }, { "name": "Omnipay Contributors", "homepage": "https://github.com/thephpleague/omnipay-common/contributors" From 28c951a5c31ce0973292984a5cd9a42420a3806c Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 23 Oct 2016 21:43:45 +0200 Subject: [PATCH 42/51] Add HTTP Factories for Guzzle --- composer.json | 6 +- src/Http/AbstractClient.php | 122 ++++++++++++++++++++++++++++++ src/Http/ClientInterface.php | 75 +++++++++++++----- src/Http/Factory.php | 81 ++++++++++++++++++++ src/Http/GuzzleClient.php | 49 +----------- src/Message/AbstractResponse.php | 9 ++- tests/AbstractGatewayTest.php | 6 +- tests/Http/AbstractClientTest.php | 82 ++++++++++++++++++++ tests/Http/FactoryTest.php | 41 ++++++++++ tests/Http/GuzzleClientTest.php | 48 +----------- 10 files changed, 399 insertions(+), 120 deletions(-) create mode 100644 src/Http/AbstractClient.php create mode 100644 src/Http/Factory.php create mode 100644 tests/Http/AbstractClientTest.php create mode 100644 tests/Http/FactoryTest.php diff --git a/composer.json b/composer.json index 93a79d3b..fbaf6d17 100644 --- a/composer.json +++ b/composer.json @@ -40,12 +40,12 @@ "require": { "php": ">=5.6", "alcohol/iso4217": "^3.1", - "psr/http-message": "^1.0" + "psr/http-message": "^1.0", + "guzzlehttp/psr7": "^1.3" }, "require-dev": { "omnipay/tests": "^3.0", - "guzzlehttp/guzzle": "^6.2.1", - "zendframework/zend-diactoros": "^1.1.0" + "guzzlehttp/guzzle": "^6.2.1" }, "minimum-stability": "dev", "prefer-stable": true diff --git a/src/Http/AbstractClient.php b/src/Http/AbstractClient.php new file mode 100644 index 00000000..7b53a12a --- /dev/null +++ b/src/Http/AbstractClient.php @@ -0,0 +1,122 @@ +sendRequest($request); + } + + /** + * Send a POST request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface + */ + public function post($uri, $headers = [], $body = null) + { + $request = Factory::createRequest('POST', $uri, $headers, $body); + + return $this->sendRequest($request); + } + + /** + * Send a PUT request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface + */ + public function put($uri, $headers = [], $body = null) + { + $request = Factory::createRequest('PUT', $uri, $headers, $body); + + return $this->sendRequest($request); + } + + /** + * Send a PATCH request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface + */ + public function patch($uri, $headers = [], $body = null) + { + $request = Factory::createRequest('PATCH', $uri, $headers, $body); + + return $this->sendRequest($request); + } + + /** + * Send a DELETE request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface + */ + public function delete($uri, $headers = [], $body = null) + { + $request = Factory::createRequest('DELETE', $uri, $headers, $body); + + return $this->sendRequest($request); + } + + /** + * Send a HEAD request. + * + * @param UriInterface|string $uri + * @param array $headers + * @return ResponseInterface + */ + public function head($uri, $headers = []) + { + $request = Factory::createRequest('HEAD', $uri, $headers); + + return $this->sendRequest($request); + } + + /** + * Send a OPTIONS request. + * + * @param UriInterface|string $uri + * @return ResponseInterface + */ + public function options($uri) + { + $request = Factory::createRequest('OPTIONS', $uri); + + return $this->sendRequest($request); + } +} diff --git a/src/Http/ClientInterface.php b/src/Http/ClientInterface.php index 800f3ee2..7686ea52 100644 --- a/src/Http/ClientInterface.php +++ b/src/Http/ClientInterface.php @@ -17,39 +17,74 @@ interface ClientInterface { /** - * @param string - * @param string|UriInterface$uri - * @param array $headers - * @param string|resource|StreamInterface $body + * @param RequestInterface $request * @return ResponseInterface */ - public function request($method, $uri, array $headers = [], $body = null); + public function sendRequest(RequestInterface $request); /** - * @param RequestInterface $request + * Send a GET request. + * + * @param UriInterface|string $uri + * @param array $headers * @return ResponseInterface */ - public function sendRequest(RequestInterface $request); + public function get($uri, $headers = []); + + /** + * Send a POST request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface + */ + public function post($uri, $headers = [], $body = null); /** - * @param string $method - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body - * @param string $protocolVersion - * @return RequestInterface + * Send a PUT request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface */ - public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'); + public function put($uri, $headers = [], $body = null); /** - * @param string|UriInterface $uri - * @return UriInterface + * Send a PATCH request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface */ - public function createUri($uri); + public function patch($uri, $headers = [], $body = null); /** - * @param mixed $body - * @return StreamInterface + * Send a DELETE request. + * + * @param UriInterface|string $uri + * @param array $headers + * @param string|null|resource|StreamInterface $body + * @return ResponseInterface + */ + public function delete($uri, $headers = [], $body = null); + + /** + * Send a HEAD request. + * + * @param UriInterface|string $uri + * @param array $headers + * @return ResponseInterface + */ + public function head($uri, $headers = []); + + /** + * Send a OPTIONS request. + * + * @param UriInterface|string $uri + * @return ResponseInterface */ - public function createStream($body); + public function options($uri); } diff --git a/src/Http/Factory.php b/src/Http/Factory.php new file mode 100644 index 00000000..963762b2 --- /dev/null +++ b/src/Http/Factory.php @@ -0,0 +1,81 @@ +guzzle = $client ?: new Client(); - } - - /** - * @param string - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body - * @return ResponseInterface - */ - public function request($method, $uri, array $headers = [], $body = null) - { - $request = $this->createRequest($method, $uri, $headers, $body); - - return $this->sendRequest($request); + $this->guzzle = $client ?: new Client(['a' => 'b']); } /** @@ -47,35 +33,4 @@ public function sendRequest(RequestInterface $request) { return $this->guzzle->send($request); } - - /** - * @param string $method - * @param string|UriInterface $uri - * @param array $headers - * @param string|resource|StreamInterface $body - * @param string $protocolVersion - * @return RequestInterface - */ - public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') - { - return new Request($method, $uri, $headers, $body, $protocolVersion); - } - - /** - * @param string|UriInterface $uri - * @return UriInterface - */ - public function createUri($uri) - { - return \GuzzleHttp\Psr7\uri_for($uri); - } - - /** - * @param mixed $resource - * @return StreamInterface - */ - public function createStream($resource) - { - return \GuzzleHttp\Psr7\stream_for($resource); - } } diff --git a/src/Message/AbstractResponse.php b/src/Message/AbstractResponse.php index b13ffb23..1e6ca16b 100644 --- a/src/Message/AbstractResponse.php +++ b/src/Message/AbstractResponse.php @@ -6,6 +6,7 @@ namespace League\Omnipay\Common\Message; use League\Omnipay\Common\Exception\RuntimeException; +use League\Omnipay\Common\Http\Factory; use Psr\Http\Message\ResponseInterface as HttpResponseInterface; use Zend\Diactoros\Response\HtmlResponse; use Zend\Diactoros\Response\RedirectResponse; @@ -202,7 +203,9 @@ public function getRedirectResponse() /** @var $this RedirectResponseInterface */ if ('GET' === $this->getRedirectMethod()) { - return new RedirectResponse($this->getRedirectUrl()); + return Factory::createResponse(302, [ + 'location' => $this->getRedirectUrl(), + ]); } elseif ('POST' === $this->getRedirectMethod()) { $hiddenFields = ''; foreach ($this->getRedirectData() as $key => $value) { @@ -235,7 +238,9 @@ public function getRedirectResponse() $hiddenFields ); - return new HtmlResponse($output); + return Factory::createResponse(200, [ + 'content-type' => 'text/html', + ], $output); } throw new RuntimeException('Invalid redirect method "'.$this->getRedirectMethod().'".'); diff --git a/tests/AbstractGatewayTest.php b/tests/AbstractGatewayTest.php index fd80a4c6..ffcd8af6 100755 --- a/tests/AbstractGatewayTest.php +++ b/tests/AbstractGatewayTest.php @@ -3,11 +3,11 @@ namespace League\Omnipay\Common; use GuzzleHttp\Client; +use League\Omnipay\Common\Http\Factory; use League\Omnipay\Common\Http\GuzzleClient; use Mockery as m; use League\Omnipay\Common\Message\AbstractRequest; use League\Omnipay\Tests\TestCase; -use Zend\Diactoros\ServerRequestFactory; class AbstractGatewayTest extends TestCase { @@ -20,7 +20,7 @@ public function setUp() public function testConstruct() { $httpClient = new GuzzleClient(new Client()); - $httpRequest = ServerRequestFactory::fromGlobals(); + $httpRequest = Factory::createServerRequestFromGlobals(); $this->gateway = new AbstractGatewayTest_MockAbstractGateway($httpClient, $httpRequest); $this->assertInstanceOf('\League\Omnipay\Common\Http\ClientInterface', $this->gateway->getProtectedHttpClient()); $this->assertInstanceOf('\Psr\Http\Message\ServerRequestInterface', $this->gateway->getProtectedHttpRequest()); @@ -145,7 +145,7 @@ public function testSupportsAcceptNotification() public function testCreateRequest() { $httpClient = new GuzzleClient(new Client()); - $httpRequest = ServerRequestFactory::fromGlobals(); + $httpRequest = Factory::createServerRequestFromGlobals(); $this->gateway = new AbstractGatewayTest_MockAbstractGateway($httpClient, $httpRequest); $request = $this->gateway->callCreateRequest( '\League\Omnipay\Common\AbstractGatewayTest_MockAbstractRequest', diff --git a/tests/Http/AbstractClientTest.php b/tests/Http/AbstractClientTest.php new file mode 100644 index 00000000..178ea21d --- /dev/null +++ b/tests/Http/AbstractClientTest.php @@ -0,0 +1,82 @@ +client = m::mock(AbstractClient::class)->makePartial(); + } + + public function testGet() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->get('https://thephpleague.com/')); + } + + public function testPost() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->post('https://thephpleague.com/', [], 'my-body')); + } + + public function testPut() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->put('https://thephpleague.com/', [], 'my-body')); + } + + public function testPatch() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->patch('https://thephpleague.com/', [], 'my-body')); + } + + public function testDelete() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->patch('https://thephpleague.com/', [], 'my-body')); + } + + public function testHead() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->head('https://thephpleague.com/', [])); + } + + public function testOptions() + { + $response = m::mock(ResponseInterface::class); + + $this->client->shouldReceive('sendRequest')->once()->andReturn($response); + + $this->assertSame($response, $this->client->options('https://thephpleague.com/')); + } +} diff --git a/tests/Http/FactoryTest.php b/tests/Http/FactoryTest.php new file mode 100644 index 00000000..1c303fd1 --- /dev/null +++ b/tests/Http/FactoryTest.php @@ -0,0 +1,41 @@ + 'value'], 'my-body'); + + $this->assertInstanceOf(RequestInterface::class, $request); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('https://thephpleague.com/', $request->getUri()); + $this->assertEquals('value', $request->getHeaderLine('key')); + $this->assertEquals('my-body', $request->getBody()); + } + + public function testCreateUri() + { + $uri = Factory::createUri('https://thephpleague.com/'); + + $this->assertInstanceOf(UriInterface::class, $uri); + $this->assertEquals('https://thephpleague.com/', (string) $uri); + } + + public function testCreateStream() + { + $stream = Factory::createStream('my-body'); + + $this->assertInstanceOf(StreamInterface::class, $stream); + $this->assertEquals('my-body', (string) $stream); + } +} diff --git a/tests/Http/GuzzleClientTest.php b/tests/Http/GuzzleClientTest.php index c2587c78..a7aeb85e 100644 --- a/tests/Http/GuzzleClientTest.php +++ b/tests/Http/GuzzleClientTest.php @@ -12,6 +12,9 @@ class GuzzleClientTest extends TestCase { + /** @var GuzzleClient */ + protected $client; + public function setUp() { $this->guzzle = m::mock(Guzzle::class)->makePartial(); @@ -43,51 +46,6 @@ public function testSendRequest() $this->assertSame($response, $this->client->sendRequest($request)); } - - public function testCreateRequest() - { - $request = $this->client->createRequest('GET', 'https://thephpleague.com/', ['key' => 'value'], 'my-body'); - - $this->assertInstanceOf(RequestInterface::class, $request); - $this->assertEquals('GET', $request->getMethod()); - $this->assertEquals('https://thephpleague.com/', $request->getUri()); - $this->assertEquals('value', $request->getHeaderLine('key')); - $this->assertEquals('my-body', $request->getBody()); - } - - public function testCreateUri() - { - $uri = $this->client->createUri('https://thephpleague.com/'); - - $this->assertInstanceOf(UriInterface::class, $uri); - $this->assertEquals('https://thephpleague.com/', (string) $uri); - } - - public function testCreateStream() - { - $stream = $this->client->createStream('my-body'); - - $this->assertInstanceOf(StreamInterface::class, $stream); - $this->assertEquals('my-body', (string) $stream); - } - - public function getGet() - { - $response = m::mock(ResponseInterface::class); - - $this->guzzle->shouldReceive('send')->once()->andReturn($response); - - $this->assertSame($response, $this->client->get('https://thephpleague.com/')); - } - - public function getPost() - { - $response = m::mock(ResponseInterface::class); - - $this->guzzle->shouldReceive('send')->once()->andReturn($response); - - $this->assertSame($response, $this->client->post('https://thephpleague.com/', [], 'my-body')); - } } class GuzzleClientTest_MockGuzzleClient extends GuzzleClient From 544ac50e3dcf076573522866275453c4d3306f3b Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 23 Oct 2016 21:53:40 +0200 Subject: [PATCH 43/51] Fix client options --- src/Http/GuzzleClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/GuzzleClient.php b/src/Http/GuzzleClient.php index c71d8a9b..3239356e 100644 --- a/src/Http/GuzzleClient.php +++ b/src/Http/GuzzleClient.php @@ -22,7 +22,7 @@ class GuzzleClient extends AbstractClient implements ClientInterface public function __construct(Client $client = null) { - $this->guzzle = $client ?: new Client(['a' => 'b']); + $this->guzzle = $client ?: new Client(); } /** From 5e8236689f27f396a72066558ef383e8693ac03e Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 23 Oct 2016 22:01:34 +0200 Subject: [PATCH 44/51] Add Amount to Response --- src/Message/AbstractResponse.php | 11 +++++++++++ src/Message/ResponseInterface.php | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Message/AbstractResponse.php b/src/Message/AbstractResponse.php index b13ffb23..6a855cca 100644 --- a/src/Message/AbstractResponse.php +++ b/src/Message/AbstractResponse.php @@ -5,6 +5,7 @@ namespace League\Omnipay\Common\Message; +use League\Omnipay\Common\AmountInterface; use League\Omnipay\Common\Exception\RuntimeException; use Psr\Http\Message\ResponseInterface as HttpResponseInterface; use Zend\Diactoros\Response\HtmlResponse; @@ -125,6 +126,16 @@ public function isCancelled() return $this->getStatus() === ResponseInterface::STATUS_CANCELLED; } + /** + * Get the amount of the transaction if returned by the Gateway + * + * @return null|AmountInterface + */ + public function getAmount() + { + return null; + } + /** * Get the response data. * diff --git a/src/Message/ResponseInterface.php b/src/Message/ResponseInterface.php index 2d32f5f3..86e4b441 100644 --- a/src/Message/ResponseInterface.php +++ b/src/Message/ResponseInterface.php @@ -5,6 +5,8 @@ namespace League\Omnipay\Common\Message; +use League\Omnipay\Common\AmountInterface; + /** * Response Interface * @@ -78,4 +80,11 @@ public function getStatus(); * @return null|string A reference provided by the gateway to represent this transaction */ public function getTransactionReference(); + + /** + * Get the amount of the transaction if returned by the Gateway + * + * @return null|AmountInterface + */ + public function getAmount(); } From 0afcefd5972068910c2ff9d0506a17b83d860dfa Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 7 Nov 2016 09:35:16 +0100 Subject: [PATCH 45/51] Add createServerRequest method --- src/Http/Factory.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Http/Factory.php b/src/Http/Factory.php index 963762b2..f9b5486f 100644 --- a/src/Http/Factory.php +++ b/src/Http/Factory.php @@ -14,7 +14,21 @@ class Factory { /** - * Create a new request. + * Create a new Response. + * + * @param int $statusCode + * @param array $headers + * @param null $body + * @return ResponseInterface + * + */ + public static function createResponse($statusCode = 200, $headers = [], $body = null) + { + return new Response($statusCode, $headers, $body); + } + + /** + * Create a new Request. * * @param string $method * @param UriInterface|string $uri @@ -29,21 +43,20 @@ public static function createRequest($method, $uri, $headers = [], $body = null, } /** - * Create a new response. - * - * @param int $statusCode - * @param array $headers - * @param null $body - * @return ResponseInterface + * Create a new ServerRequest. * + * @param string $method + * @param UriInterface|string $uri + * @param array $server + * @return ServerRequestInterface */ - public static function createResponse($statusCode = 200, $headers = [], $body = null) + public static function createServerRequest($method, $uri, array $server = null) { - return new Response($statusCode, $headers, $body); + return new ServerRequest($method, $uri, [], null, '1.1', $server); } /** - * Create a new server request from PHP globals. + * Create a new ServerRequest from PHP globals. * * @return ServerRequestInterface */ From 77773d809b7185ff0a0064328cfb79d68e4d67be Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 7 Nov 2016 22:06:20 +0100 Subject: [PATCH 46/51] Update AbstractRequest.php --- src/Message/AbstractRequest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 56d58cfa..f90585db 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -218,6 +218,31 @@ public function validate() } } + /** + * Get the customer. + * + * @return Customer + */ + public function getCustomer() + { + return $this->getParameter('customer'); + } + + /** + * Sets the customer. + * + * @param Customer $value + * @return AbstractRequest Provides a fluent interface + */ + public function setCustomer($value) + { + if ($value && !$value instanceof Customer) { + $value = new Customer($value); + } + + return $this->setParameter('customer', $value); + } + /** * Get the card. * From 9be5c1e87e90169817d5e3c385add89ca8ce48a3 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 8 Nov 2016 09:17:27 +0100 Subject: [PATCH 47/51] Allow replacing Guzzle --- src/Http/GuzzleClient.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Http/GuzzleClient.php b/src/Http/GuzzleClient.php index 3239356e..009469c0 100644 --- a/src/Http/GuzzleClient.php +++ b/src/Http/GuzzleClient.php @@ -25,6 +25,16 @@ public function __construct(Client $client = null) $this->guzzle = $client ?: new Client(); } + public function getGuzzleClient() + { + return $this->guzzle; + } + + public function setGuzzleClient(Client $client) + { + $this->guzzle = $client; + } + /** * @param RequestInterface $request * @return ResponseInterface From f9b753ac20ed39384f5d17821c8dc12e8fac4c2a Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 8 Nov 2016 09:30:42 +0100 Subject: [PATCH 48/51] Update AbstractRequest.php --- src/Message/AbstractRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index f90585db..927dc897 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -331,7 +331,7 @@ public function setCurrency($value) * Validates and returns amount as integer. * * @throws InvalidRequestException on any validation failure. - * @return string The amount as decimal + * @return Amount The amount as decimal */ public function getAmount() { From 3f0b85217cccb50a583f35cf01086875f3bff646 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 8 Nov 2016 10:09:43 +0100 Subject: [PATCH 49/51] Add query helper --- src/Message/AbstractRequest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 927dc897..85821049 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -661,4 +661,17 @@ public function getResponse() return $this->response; } + + /** + * Get a query parameter + * + * @param $key + * @param null $default + * @return mixed + */ + public function query($key, $default = null) + { + $params = $this->httpRequest->getQueryParams(); + return isset($params[$key]) ? $params[$key] : $default; + } } From dd368986af5410ad82ed1c245520acb6471cfdf7 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 8 Nov 2016 10:15:55 +0100 Subject: [PATCH 50/51] Update AbstractRequest.php --- src/Message/AbstractRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 85821049..d25250cc 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -664,7 +664,7 @@ public function getResponse() /** * Get a query parameter - * + * * @param $key * @param null $default * @return mixed From 10bee8025938d84183e7e469a80641d70e91a6e3 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 8 Nov 2016 11:09:45 +0100 Subject: [PATCH 51/51] Update AbstractRequest.php --- src/Message/AbstractRequest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index d25250cc..b63cab65 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -11,6 +11,7 @@ use League\Omnipay\Common\Http\ClientInterface; use League\Omnipay\Common\CreditCard; use League\Omnipay\Common\Currency; +use League\Omnipay\Common\Customer; use League\Omnipay\Common\Exception\InvalidRequestException; use League\Omnipay\Common\Exception\RuntimeException; use League\Omnipay\Common\Helper;