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

PW-562 Add applicationInformation into PHP API library #77

Merged
merged 7 commits into from
Oct 19, 2018
39 changes: 36 additions & 3 deletions src/Adyen/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Client
{
const LIB_VERSION = "1.5.1";
const LIB_NAME = "adyen-php-api-library";
const USER_AGENT_SUFFIX = "adyen-php-api-library/";
const ENDPOINT_TEST = "https://pal-test.adyen.com";
const ENDPOINT_LIVE = "https://pal-live.adyen.com";
Expand All @@ -26,13 +27,13 @@ class Client
const ENDPOINT_PROTOCOL = "https://";

/**
* @var Adyen_Config $config
* @var \Adyen\Config $config
*/
private $_config;
private $_httpClient;

/**
* @var Adyen_Logger_Abstract $logger
* @var Logger $logger
*/
private $logger;

Expand Down Expand Up @@ -93,7 +94,7 @@ public function setXApiKey($xApiKey)
* Set environment to connect to test or live platform of Adyen
* For live please specify the unique identifier.
*
* @param $environment test
* @param string $environment
* @param null $liveEndpointUrlPrefix Provide the unique live url prefix from the "API URLs and Response" menu in the Adyen Customer Area
* @throws AdyenException
*/
Expand Down Expand Up @@ -154,6 +155,29 @@ public function setApplicationName($applicationName)
$this->_config->set('applicationName', $applicationName);
}

/**
* Set external platform name, version and integrator
*
* @param string $name
* @param string $version
* @param string $integrator
*/
public function setExternalPlatform($name, $version, $integrator = "")
{
$this->_config->set('externalPlatform', array('name' => $name, 'version' => $version, 'integrator' => $integrator));
}

/**
* Set Adyen payment source name and version
*
* @param string $name
* @param string $version
*/
public function setAdyenPaymentSource($name, $version)
{
$this->_config->set('adyenPaymentSource', array('name' => $name, 'version' => $version));
}

/**
* Type can be json or array
*
Expand All @@ -179,6 +203,15 @@ public function setTimeout($value)
$this->_config->set('timeout', $value);
}

/**
* Get the library name
*
* @return string
*/
public function getLibraryName()
{
return self::LIB_NAME;
}

/**
* Get the library version
Expand Down
16 changes: 16 additions & 0 deletions src/Adyen/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@ public function getMerchantAccount()
{
return isset($this->data['merchantAccount']) ? $this->data['merchantAccount'] : null;
}

/**
* @return mixed|null
*/
public function getAdyenPaymentSource()
{
return isset($this->data['adyenPaymentSource']) ? $this->data['adyenPaymentSource'] : null;
}

/**
* @return mixed|null
*/
public function getExternalPlatform()
{
return isset($this->data['externalPlatform']) ? $this->data['externalPlatform'] : null;
}
}
115 changes: 102 additions & 13 deletions src/Adyen/Service/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,74 @@

class AbstractResource
{

/**
* @var \Adyen\Service
*/
protected $_service;

/**
* @var string
*/
protected $_endpoint;

public function __construct(\Adyen\Service $service, $endpoint)
/**
* @var bool
*/
protected $allowApplicationInfo;

/**
* AbstractResource constructor.
*
* @param \Adyen\Service $service
* @param $endpoint
* @param bool $allowApplicationInfo
*/
public function __construct(\Adyen\Service $service, $endpoint, $allowApplicationInfo = false)
{
$this->_service = $service;
$this->_endpoint = $endpoint;
$this->allowApplicationInfo = $allowApplicationInfo;
}

/**
* Do the request to the Http Client
*
* @param $params
* @return mixed
*/
/**
* Do the request to the Http Client
*
* @param $params
* @return mixed
* @throws \Adyen\AdyenException
*/
public function request($params)
{
// convert to PHP Array if type is inputType is json
if($this->_service->getClient()->getConfig()->getInputType() == 'json')
{
$params = json_decode($params, true);
if ($params === null && json_last_error() !== JSON_ERROR_NONE) {
$msg = 'The parameters in the request expect valid JSON but JSON error code found: ' . json_last_error();
$this->_service->getClient()->getLogger()->error($msg);
throw new \Adyen\AdyenException($msg);
}
}

// check if merchantAccount is setup in client and request is missing merchantAccount then add it
if(!isset($params['merchantAccount']) && $this->_service->getClient()->getConfig()->getMerchantAccount()) {
$params['merchantAccount'] = $this->_service->getClient()->getConfig()->getMerchantAccount();
}
if (!is_array($params)) {
$msg = 'The parameter is not valid array';
$this->_service->getClient()->getLogger()->error($msg);
throw new \Adyen\AdyenException($msg);
}

$params = $this->addDefaultParametersToRequest($params);

$params = $this->handleApplicationInfoInRequest($params);

$curlClient = $this->_service->getClient()->getHttpClient();
return $curlClient->requestJson($this->_service, $this->_endpoint, $params);
}

/**
* @param $params
* @return mixed
* @throws \Adyen\AdyenException
*/
public function requestPost($params)
{
// check if paramenters has a value
Expand All @@ -50,4 +85,58 @@ public function requestPost($params)
return $curlClient->requestPost($this->_service, $this->_endpoint, $params);
}

}
/**
* Fill expected but missing parameters with default data
*
* @param $params
* @return mixed
*/
private function addDefaultParametersToRequest($params)
{
// check if merchantAccount is setup in client and request is missing merchantAccount then add it
if(!isset($params['merchantAccount']) && $this->_service->getClient()->getConfig()->getMerchantAccount()) {
$params['merchantAccount'] = $this->_service->getClient()->getConfig()->getMerchantAccount();
}

return $params;
}

/**
* If allowApplicationInfo is true then it adds applicationInfo to request
* otherwise removes from the request
*
* @param $params
* @return mixed
*/
private function handleApplicationInfoInRequest($params)
{
// Only add if allowed
if ($this->allowApplicationInfo) {
// add/overwrite applicationInfo adyenLibrary even if it's already set
$params['applicationInfo']['adyenLibrary']['name'] = $this->_service->getClient()->getLibraryName();
$params['applicationInfo']['adyenLibrary']['version'] = $this->_service->getClient()->getLibraryVersion();

if ($adyenPaymentSource = $this->_service->getClient()->getConfig()->getAdyenPaymentSource()) {
$params['applicationInfo']['adyenPaymentSource']['version'] = $adyenPaymentSource['version'];
$params['applicationInfo']['adyenPaymentSource']['name'] = $adyenPaymentSource['name'];
}

if ($externalPlatform = $this->_service->getClient()->getConfig()->getExternalPlatform()) {
$params['applicationInfo']['externalPlatform']['version'] = $externalPlatform['version'];
$params['applicationInfo']['externalPlatform']['name'] = $externalPlatform['name'];

if (!empty($externalPlatform['integrator'])) {
$params['applicationInfo']['externalPlatform']['integrator'] = $externalPlatform['integrator'];
}
}

} else {
// remove if exists
if (isset($params['applicationInfo'])) {
unset($params['applicationInfo']);
}
}

return $params;
}
}
12 changes: 10 additions & 2 deletions src/Adyen/Service/ResourceModel/Checkout/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

class PaymentMethods extends \Adyen\Service\AbstractCheckoutResource
{
protected $_endpoint;
/**
* @var string
*/
protected $_endpoint;

/**
* PaymentMethods constructor.
*
* @param \Adyen\Service $service
* @throws \Adyen\AdyenException
*/
public function __construct($service)
{
$this->_endpoint = $this->getCheckoutEndpoint($service) .'/'. $service->getClient()->getApiCheckoutVersion() . '/paymentMethods';
parent::__construct($service, $this->_endpoint);
}

}
13 changes: 11 additions & 2 deletions src/Adyen/Service/ResourceModel/Checkout/PaymentSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

class PaymentSession extends \Adyen\Service\AbstractCheckoutResource
{
protected $_endpoint;
/**
* @var string
*/
protected $_endpoint;

/**
* PaymentSession constructor.
*
* @param \Adyen\Service $service
* @throws \Adyen\AdyenException
*/
public function __construct($service)
{
$this->_endpoint = $this->getCheckoutEndpoint($service) .'/'. $service->getClient()->getApiCheckoutVersion() . '/paymentSession';
parent::__construct($service, $this->_endpoint);
}
}
}
21 changes: 18 additions & 3 deletions src/Adyen/Service/ResourceModel/Checkout/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@

class Payments extends \Adyen\Service\AbstractCheckoutResource
{
/**
* @var string
*/
protected $_endpoint;

protected $_endpoint;
/**
* Include applicationInfo key in the request parameters
*
* @var bool
*/
protected $allowApplicationInfo = true;

/**
* Payments constructor.
*
* @param \Adyen\Service $service
* @throws \Adyen\AdyenException
*/
public function __construct($service)
{
$this->_endpoint = $this->getCheckoutEndpoint($service) .'/'. $service->getClient()->getApiCheckoutVersion() . '/payments';
parent::__construct($service, $this->_endpoint);
parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo);
}
}
}
12 changes: 10 additions & 2 deletions src/Adyen/Service/ResourceModel/Checkout/PaymentsDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

class PaymentsDetails extends \Adyen\Service\AbstractCheckoutResource
{
protected $_endpoint;
/**
* @var string
*/
protected $_endpoint;

/**
* PaymentsDetails constructor.
*
* @param \Adyen\Service $service
* @throws \Adyen\AdyenException
*/
public function __construct($service)
{
$this->_endpoint = $this->getCheckoutEndpoint($service) .'/'. $service->getClient()->getApiCheckoutVersion() . '/payments/details';
parent::__construct($service, $this->_endpoint);
}

}
12 changes: 10 additions & 2 deletions src/Adyen/Service/ResourceModel/Checkout/PaymentsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

class PaymentsResult extends \Adyen\Service\AbstractCheckoutResource
{
protected $_endpoint;
/**
* @var string
*/
protected $_endpoint;

/**
* PaymentsResult constructor.
*
* @param \Adyen\Service $service
* @throws \Adyen\AdyenException
*/
public function __construct($service)
{
$this->_endpoint = $this->getCheckoutEndpoint($service) .'/'. $service->getClient()->getApiCheckoutVersion() . '/payments/result';
parent::__construct($service, $this->_endpoint);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

class OriginKeys extends \Adyen\Service\AbstractCheckoutResource
{
/**
* @var string
*/
protected $_endpoint;

/**
* OriginKeys constructor.
*
* @param \Adyen\Service $service
* @throws \Adyen\AdyenException
*/
public function __construct($service)
{
$this->_endpoint = $this->getCheckoutEndpoint($service) .'/'. $service->getClient()->getApiCheckoutUtilityVersion() . '/originKeys';
parent::__construct($service, $this->_endpoint);
}

}
Loading