Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for user and account gateways #209

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Common/AbstractAccountGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Base Account Gateway class
*/
namespace Omnipay\Common;

/**
* Base user gateway class
*
* This abstract class should be extended by all account gatways
* throughout the Omnipay system. It enforces implementation of
* the GatewayInterface interface and defines a few common attributes
* and methods that all gateways should have.
*
* Example:
*
* <code>
* // Initialize the gateway
* $gateway->initialize(...);
*
* // Get the gateway parameters
* $parameters = $gateway->getParameters();
*
* // lookup a user by access token
* if ($gateway->supportsFind()) {
*
* }
* </code>
*/
abstract class AbstractAccountGateway extends AbstractGateway
{
/**
* Supports Find
*
* @return string
*/
public function supportsFind()
{
return method_exists($this, 'find');
}

/**
* Supports create
*
* @return string
*/
public function supportsCreate()
{
return method_exists($this, 'create');
}

/**
* Supports Modify
*
* @return string
*/
public function supportsModify()
{
return method_exists($this, 'modify');
}

/**
* Supports Delete
*
* @return string
*/
public function supportsDelete()
{
return method_exists($this, 'delete');
}
}
157 changes: 1 addition & 156 deletions src/Common/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

namespace Omnipay\Common;

use Omnipay\Common\Http\Client;
use Omnipay\Common\Http\ClientInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

/**
* Base payment gateway class
*
Expand Down Expand Up @@ -41,92 +36,8 @@
* For further code examples see the *omnipay-example* repository on github.
*
*/
abstract class AbstractGateway implements GatewayInterface
abstract class AbstractGateway extends AbstractGenericGateway
{
use ParametersTrait;

/**
* @var ClientInterface
*/
protected $httpClient;

/**
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $httpRequest;

/**
* Create a new gateway instance
*
* @param ClientInterface $httpClient A HTTP client to make API calls with
* @param HttpRequest $httpRequest A Symfony HTTP request object
*/
public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
{
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient();
$this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest();
$this->initialize();
}

/**
* Get the short name of the Gateway
*
* @return string
*/
public function getShortName()
{
return Helper::getGatewayShortName(get_class($this));
}

/**
* Initialize this gateway with default parameters
*
* @param array $parameters
* @return $this
*/
public function initialize(array $parameters = array())
{
$this->parameters = new ParameterBag;

// set default parameters
foreach ($this->getDefaultParameters() as $key => $value) {
if (is_array($value)) {
$this->parameters->set($key, reset($value));
} else {
$this->parameters->set($key, $value);
}
}

Helper::initialize($this, $parameters);

return $this;
}

/**
* @return array
*/
public function getDefaultParameters()
{
return array();
}

/**
* @return boolean
*/
public function getTestMode()
{
return $this->getParameter('testMode');
}

/**
* @param boolean $value
* @return $this
*/
public function setTestMode($value)
{
return $this->setParameter('testMode', $value);
}

/**
* @return string
*/
Expand Down Expand Up @@ -224,16 +135,6 @@ public function supportsVoid()
return method_exists($this, 'void');
}

/**
* Supports AcceptNotification
*
* @return boolean True if this gateway supports the acceptNotification() method
*/
public function supportsAcceptNotification()
{
return method_exists($this, 'acceptNotification');
}

/**
* Supports CreateCard
*
Expand Down Expand Up @@ -263,60 +164,4 @@ public function supportsUpdateCard()
{
return method_exists($this, 'updateCard');
}

/**
* Create and initialize a request object
*
* This function is usually used to create objects of type
* Omnipay\Common\Message\AbstractRequest (or a non-abstract subclass of it)
* and initialise them with using existing parameters from this gateway.
*
* Example:
*
* <code>
* class MyRequest extends \Omnipay\Common\Message\AbstractRequest {};
*
* class MyGateway extends \Omnipay\Common\AbstractGateway {
* function myRequest($parameters) {
* $this->createRequest('MyRequest', $parameters);
* }
* }
*
* // Create the gateway object
* $gw = Omnipay::create('MyGateway');
*
* // Create the request object
* $myRequest = $gw->myRequest($someParameters);
* </code>
*
* @param string $class The request class name
* @param array $parameters
* @return \Omnipay\Common\Message\AbstractRequest
*/
protected function createRequest($class, array $parameters)
{
$obj = new $class($this->httpClient, $this->httpRequest);

return $obj->initialize(array_replace($this->getParameters(), $parameters));
}

/**
* Get the global default HTTP client.
*
* @return ClientInterface
*/
protected function getDefaultHttpClient()
{
return new Client();
}

/**
* Get the global default HTTP request.
*
* @return HttpRequest
*/
protected function getDefaultHttpRequest()
{
return HttpRequest::createFromGlobals();
}
}
Loading