1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352:
<?php
/**
* Base payment gateway class
*/
namespace Omnipay\Common;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\Client as HttpClient;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
/**
* Base payment gateway class
*
* This abstract class should be extended by all payment gateways
* throughout the Omnipay system. It enforces implementation of
* the GatewayInterface interface and defines various common attibutes
* and methods that all gateways should have.
*
* Example:
*
* <code>
* // 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()');
* }
* </code>
*
* For further code examples see the *omnipay-example* repository on github.
*
* @see GatewayInterface
*/
abstract class AbstractGateway implements GatewayInterface
{
/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $parameters;
/**
* @var \Guzzle\Http\ClientInterface
*/
protected $httpClient;
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
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
*/
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 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
*/
public function getTestMode()
{
return $this->getParameter('testMode');
}
/**
* @param boolean $value
* @return $this
*/
public function setTestMode($value)
{
return $this->setParameter('testMode', $value);
}
/**
* @return string
*/
public function getCurrency()
{
return strtoupper($this->getParameter('currency'));
}
/**
* @param string $value
* @return $this
*/
public function setCurrency($value)
{
return $this->setParameter('currency', $value);
}
/**
* Supports Authorize
*
* @return boolean True if this gateway supports the authorize() method
*/
public function supportsAuthorize()
{
return method_exists($this, 'authorize');
}
/**
* Supports Complete Authorize
*
* @return boolean True if this gateway supports the completeAuthorize() method
*/
public function supportsCompleteAuthorize()
{
return method_exists($this, 'completeAuthorize');
}
/**
* Supports Capture
*
* @return boolean True if this gateway supports the capture() method
*/
public function supportsCapture()
{
return method_exists($this, 'capture');
}
/**
* Supports Purchase
*
* @return boolean True if this gateway supports the purchase() method
*/
public function supportsPurchase()
{
return method_exists($this, 'purchase');
}
/**
* Supports Complete Purchase
*
* @return boolean True if this gateway supports the completePurchase() method
*/
public function supportsCompletePurchase()
{
return method_exists($this, 'completePurchase');
}
/**
* Supports Refund
*
* @return boolean True if this gateway supports the refund() method
*/
public function supportsRefund()
{
return method_exists($this, 'refund');
}
/**
* Supports Void
*
* @return boolean True if this gateway supports the void() method
*/
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
*
* @return boolean True if this gateway supports the create() method
*/
public function supportsCreateCard()
{
return method_exists($this, 'createCard');
}
/**
* Supports DeleteCard
*
* @return boolean True if this gateway supports the delete() method
*/
public function supportsDeleteCard()
{
return method_exists($this, 'deleteCard');
}
/**
* Supports UpdateCard
*
* @return boolean True if this gateway supports the update() method
*/
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>
*
* @see \Omnipay\Common\Message\AbstractRequest
* @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 HttpClient
*/
protected function getDefaultHttpClient()
{
return new HttpClient(
'',
array(
'curl.options' => array(CURLOPT_CONNECTTIMEOUT => 60),
)
);
}
/**
* Get the global default HTTP request.
*
* @return HttpRequest
*/
protected function getDefaultHttpRequest()
{
return HttpRequest::createFromGlobals();
}
}