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-1188: Add applicationInfo into terminal API requests #141

Merged
merged 10 commits into from
Dec 23, 2019
11 changes: 11 additions & 0 deletions src/Adyen/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ public function setAdyenPaymentSource($name, $version)
$this->config->set('adyenPaymentSource', array('name' => $name, 'version' => $version));
}

/**
* Set merchant application name and version
*
* @param string $name
* @param string $version
*/
public function setMerchantApplication($name, $version)
{
$this->config->set('merchantApplication', array('name' => $name, 'version' => $version));
}

/**
* Type can be json or array
*
Expand Down
8 changes: 8 additions & 0 deletions src/Adyen/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public function getAdyenPaymentSource()
return isset($this->data['adyenPaymentSource']) ? $this->data['adyenPaymentSource'] : null;
}

/**
* @return array|null an array with 'name' and 'version'
*/
public function getMerchantApplication()
{
return isset($this->data['merchantApplication']) ? $this->data['merchantApplication'] : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's slowly migrate off of the data field and use proper string value for this. protected $merchantApplication;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore it as this will mean for every param we added it will be a major release for our merchants what is not ideal

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can deprecate the interface so we are more flexible in the future.

}

/**
* @return mixed|null
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Adyen/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Adyen;

/**
* Interface ConfigInterface
* @deprecated Please do not use your own interface as we will deprecate this in the future for improvements on the library
* @package Adyen
*/
Interface ConfigInterface
{

Expand Down
136 changes: 104 additions & 32 deletions src/Adyen/Service/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ abstract class AbstractResource
*/
protected $allowApplicationInfo;

/**
* AbstractResource constructor.
*
* @param \Adyen\Service $service
* @param $endpoint
* @param bool $allowApplicationInfo
*/
public function __construct(\Adyen\Service $service, $endpoint, $allowApplicationInfo = false)
/**
* @var bool
*/
protected $allowApplicationInfoPOS;

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

/**
Expand Down Expand Up @@ -61,7 +68,16 @@ public function request($params, $requestOptions = null)

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

$params = $this->handleApplicationInfoInRequest($params);
if ($this->allowApplicationInfo) {
$params = $this->handleApplicationInfoInRequest($params);
} elseif ($this->allowApplicationInfoPOS) {
$params = $this->handleApplicationInfoInRequestPOS($params);
} else {
// remove if exists
if (isset($params['applicationInfo'])) {
unset($params['applicationInfo']);
}
}

$curlClient = $this->service->getClient()->getHttpClient();
return $curlClient->requestJson($this->service, $this->endpoint, $params, $requestOptions);
Expand Down Expand Up @@ -110,33 +126,89 @@ private function addDefaultParametersToRequest($params)
*/
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'];
}
// 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 ($externalPlatform = $this->service->getClient()->getConfig()->getExternalPlatform()) {
$params['applicationInfo']['externalPlatform']['version'] = $externalPlatform['version'];
$params['applicationInfo']['externalPlatform']['name'] = $externalPlatform['name'];
if ($adyenPaymentSource = $this->service->getClient()->getConfig()->getAdyenPaymentSource()) {
$params['applicationInfo']['adyenPaymentSource']['version'] = $adyenPaymentSource['version'];
$params['applicationInfo']['adyenPaymentSource']['name'] = $adyenPaymentSource['name'];
}

if (!empty($externalPlatform['integrator'])) {
$params['applicationInfo']['externalPlatform']['integrator'] = $externalPlatform['integrator'];
}
}
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'];
}
}

if ($merchantApplication = $this->service->getClient()->getConfig()->getMerchantApplication()) {
$params['applicationInfo']['merchantApplication']['version'] = $merchantApplication['version'];
$params['applicationInfo']['merchantApplication']['name'] = $merchantApplication['name'];
}

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

return $params;
}

/**
* If allowApplicationInfoPOS is true, this function does the following:
* 1) Converts SaleToAcquirerData to array(from querystring or base64encoded)
* 2) Adds ApplicationInfo to the array
* 3) Base64 encodes SaleToAcquirerData
*
* @param $params
* @return array|null
*/
private function handleApplicationInfoInRequestPOS(array $params)
{
//If the POS request is not a payment request, do not add application info
if (empty($params['SaleToPOIRequest']['PaymentRequest'])) {
return $params;
}

// Initialize $saleToAcquirerData
$saleToAcquirerData = array();

if (!empty($params['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'])) {
$saleToAcquirerData = $params['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'];

//If SaleToAcquirerData is a querystring convert it to array
parse_str($saleToAcquirerData, $queryString);
$queryStringValues = array_values($queryString);

//check if querystring is nonempty and contains a value
if (!empty($queryString) && !empty($queryStringValues[0])) {
$saleToAcquirerData = $queryString;
}

//If SaleToAcquirerData is a base64encoded string decode it and convert it to array
elseif ($this->isBase64Encoded($saleToAcquirerData)) {
$saleToAcquirerData = json_decode(base64_decode($saleToAcquirerData, true), true);
}
}

//add Application Information
$saleToAcquirerData = $this->handleApplicationInfoInRequest($saleToAcquirerData);
$saleToAcquirerData = base64_encode(json_encode($saleToAcquirerData));
$params['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'] = $saleToAcquirerData;

return $params;

}

/**
* @param $data
* @return bool
*/
private function isBase64Encoded($data)
{
if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $data) && !empty($data)) {
return true;
} else {
return false;
}
}
}
16 changes: 15 additions & 1 deletion src/Adyen/Service/ResourceModel/Payment/TerminalCloudAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ class TerminalCloudAPI extends \Adyen\Service\AbstractResource
*/
protected $endpoint;

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

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

/**
* TerminalCloudAPI constructor.
*
Expand All @@ -22,6 +36,6 @@ public function __construct($service, $asynchronous)
} else {
$this->endpoint = $service->getClient()->getConfig()->get('endpointTerminalCloud') . '/sync';
}
parent::__construct($service, $this->endpoint);
parent::__construct($service, $this->endpoint, $this->allowApplicationInfo, $this->allowApplicationInfoPOS);
}
}
Loading