diff --git a/src/Adyen/Client.php b/src/Adyen/Client.php index 4075af53a..fa2a43fc1 100644 --- a/src/Adyen/Client.php +++ b/src/Adyen/Client.php @@ -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"; @@ -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; @@ -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 */ @@ -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 * @@ -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 diff --git a/src/Adyen/Config.php b/src/Adyen/Config.php index a807cc338..4d0d44021 100644 --- a/src/Adyen/Config.php +++ b/src/Adyen/Config.php @@ -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; + } } \ No newline at end of file diff --git a/src/Adyen/Service/AbstractResource.php b/src/Adyen/Service/AbstractResource.php index 9c67a01ac..32d9ec4c7 100644 --- a/src/Adyen/Service/AbstractResource.php +++ b/src/Adyen/Service/AbstractResource.php @@ -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 @@ -50,4 +85,58 @@ public function requestPost($params) return $curlClient->requestPost($this->_service, $this->_endpoint, $params); } -} \ No newline at end of file + /** + * 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; + } +} diff --git a/src/Adyen/Service/ResourceModel/Checkout/PaymentMethods.php b/src/Adyen/Service/ResourceModel/Checkout/PaymentMethods.php index 6e0b8933a..2470d73fb 100644 --- a/src/Adyen/Service/ResourceModel/Checkout/PaymentMethods.php +++ b/src/Adyen/Service/ResourceModel/Checkout/PaymentMethods.php @@ -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); } - } diff --git a/src/Adyen/Service/ResourceModel/Checkout/PaymentSession.php b/src/Adyen/Service/ResourceModel/Checkout/PaymentSession.php index 0de8c8885..d0f617c2a 100644 --- a/src/Adyen/Service/ResourceModel/Checkout/PaymentSession.php +++ b/src/Adyen/Service/ResourceModel/Checkout/PaymentSession.php @@ -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); } -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Checkout/Payments.php b/src/Adyen/Service/ResourceModel/Checkout/Payments.php index b23713b3b..68dde8fa0 100644 --- a/src/Adyen/Service/ResourceModel/Checkout/Payments.php +++ b/src/Adyen/Service/ResourceModel/Checkout/Payments.php @@ -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); } -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Checkout/PaymentsDetails.php b/src/Adyen/Service/ResourceModel/Checkout/PaymentsDetails.php index 97354a50b..482c24dc5 100644 --- a/src/Adyen/Service/ResourceModel/Checkout/PaymentsDetails.php +++ b/src/Adyen/Service/ResourceModel/Checkout/PaymentsDetails.php @@ -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); } - } diff --git a/src/Adyen/Service/ResourceModel/Checkout/PaymentsResult.php b/src/Adyen/Service/ResourceModel/Checkout/PaymentsResult.php index 04af85399..51cc7c808 100644 --- a/src/Adyen/Service/ResourceModel/Checkout/PaymentsResult.php +++ b/src/Adyen/Service/ResourceModel/Checkout/PaymentsResult.php @@ -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); } - } diff --git a/src/Adyen/Service/ResourceModel/CheckoutUtility/OriginKeys.php b/src/Adyen/Service/ResourceModel/CheckoutUtility/OriginKeys.php index 571244e82..1fbac8cf5 100644 --- a/src/Adyen/Service/ResourceModel/CheckoutUtility/OriginKeys.php +++ b/src/Adyen/Service/ResourceModel/CheckoutUtility/OriginKeys.php @@ -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); } - } diff --git a/src/Adyen/Service/ResourceModel/DirectoryLookup/Directory.php b/src/Adyen/Service/ResourceModel/DirectoryLookup/Directory.php index b139b2cf0..52d35d32d 100644 --- a/src/Adyen/Service/ResourceModel/DirectoryLookup/Directory.php +++ b/src/Adyen/Service/ResourceModel/DirectoryLookup/Directory.php @@ -2,17 +2,23 @@ //https://test.adyen.com/hpp/directory.shtml - namespace Adyen\Service\ResourceModel\DirectoryLookup; class Directory extends \Adyen\Service\AbstractResource { + /** + * @var + */ protected $_endpoint; + /** + * Directory constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpointDirectorylookup'); parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Modification/AdjustAuthorisation.php b/src/Adyen/Service/ResourceModel/Modification/AdjustAuthorisation.php index 372595153..e9db79a1c 100644 --- a/src/Adyen/Service/ResourceModel/Modification/AdjustAuthorisation.php +++ b/src/Adyen/Service/ResourceModel/Modification/AdjustAuthorisation.php @@ -4,12 +4,26 @@ class AdjustAuthorisation extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; + + /** + * AdjustAuthorisation constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint').'/pal/servlet/Payment/'.$service->getClient()->getApiVersion().'/adjustAuthorisation'; - parent::__construct($service, $this->_endpoint); + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Modification/Cancel.php b/src/Adyen/Service/ResourceModel/Modification/Cancel.php index 735444319..f203bbecf 100644 --- a/src/Adyen/Service/ResourceModel/Modification/Cancel.php +++ b/src/Adyen/Service/ResourceModel/Modification/Cancel.php @@ -4,12 +4,26 @@ class Cancel extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; - public function __construct($service) - { - $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/' . $service->getClient()->getApiVersion() . '/cancel'; - parent::__construct($service, $this->_endpoint); - } + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; -} \ No newline at end of file + /** + * Cancel constructor. + * + * @param \Adyen\Service $service + */ + public function __construct($service) + { + $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/' . $service->getClient()->getApiVersion() . '/cancel'; + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); + } +} diff --git a/src/Adyen/Service/ResourceModel/Modification/CancelOrRefund.php b/src/Adyen/Service/ResourceModel/Modification/CancelOrRefund.php index cf4c550b2..0778d99bb 100644 --- a/src/Adyen/Service/ResourceModel/Modification/CancelOrRefund.php +++ b/src/Adyen/Service/ResourceModel/Modification/CancelOrRefund.php @@ -4,12 +4,26 @@ class CancelOrRefund extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; + + /** + * CancelOrRefund constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/'. $service->getClient()->getApiVersion() . '/cancelOrRefund'; - parent::__construct($service, $this->_endpoint); + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Modification/Capture.php b/src/Adyen/Service/ResourceModel/Modification/Capture.php index 5287d8906..15440a854 100644 --- a/src/Adyen/Service/ResourceModel/Modification/Capture.php +++ b/src/Adyen/Service/ResourceModel/Modification/Capture.php @@ -4,12 +4,26 @@ class Capture extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; + + /** + * Capture constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/' . $service->getClient()->getApiVersion() . '/capture'; - parent::__construct($service, $this->_endpoint); + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Modification/Refund.php b/src/Adyen/Service/ResourceModel/Modification/Refund.php index 95e91de85..b17e58639 100644 --- a/src/Adyen/Service/ResourceModel/Modification/Refund.php +++ b/src/Adyen/Service/ResourceModel/Modification/Refund.php @@ -4,12 +4,26 @@ class Refund extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; + + /** + * Refund constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/' . $service->getClient()->getApiVersion() . '/refund'; - parent::__construct($service, $this->_endpoint); + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payment/Authorise.php b/src/Adyen/Service/ResourceModel/Payment/Authorise.php index 09d649eac..ce2afd982 100644 --- a/src/Adyen/Service/ResourceModel/Payment/Authorise.php +++ b/src/Adyen/Service/ResourceModel/Payment/Authorise.php @@ -4,12 +4,26 @@ class Authorise extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; + + /** + * Authorise constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/' . $service->getClient()->getApiVersion() . '/authorise'; - parent::__construct($service, $this->_endpoint); + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payment/Authorise3D.php b/src/Adyen/Service/ResourceModel/Payment/Authorise3D.php index 52d9fd103..c505376c0 100644 --- a/src/Adyen/Service/ResourceModel/Payment/Authorise3D.php +++ b/src/Adyen/Service/ResourceModel/Payment/Authorise3D.php @@ -4,12 +4,26 @@ class Authorise3D extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * Include applicationInfo key in the request parameters + * + * @var bool + */ + protected $allowApplicationInfo = true; + + /** + * Authorise3D constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payment/' . $service->getClient()->getApiVersion() . '/authorise3d'; - parent::__construct($service, $this->_endpoint); + parent::__construct($service, $this->_endpoint, $this->allowApplicationInfo); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payment/TerminalCloudAPI.php b/src/Adyen/Service/ResourceModel/Payment/TerminalCloudAPI.php index 90b6095f9..870181e56 100644 --- a/src/Adyen/Service/ResourceModel/Payment/TerminalCloudAPI.php +++ b/src/Adyen/Service/ResourceModel/Payment/TerminalCloudAPI.php @@ -4,8 +4,17 @@ class TerminalCloudAPI extends \Adyen\Service\AbstractResource { - protected $_endpoint; + /** + * @var string + */ + protected $_endpoint; + /** + * TerminalCloudAPI constructor. + * + * @param \Adyen\Service $service + * @param bool $asynchronous + */ public function __construct($service, $asynchronous) { if ($asynchronous) { @@ -15,5 +24,4 @@ public function __construct($service, $asynchronous) } parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/Confirm.php b/src/Adyen/Service/ResourceModel/Payout/Confirm.php index 6e3a87aad..1699d90a4 100644 --- a/src/Adyen/Service/ResourceModel/Payout/Confirm.php +++ b/src/Adyen/Service/ResourceModel/Payout/Confirm.php @@ -4,12 +4,19 @@ class Confirm extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * Confirm constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/confirm'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/Decline.php b/src/Adyen/Service/ResourceModel/Payout/Decline.php index 1ed095d25..f82b6ca4f 100644 --- a/src/Adyen/Service/ResourceModel/Payout/Decline.php +++ b/src/Adyen/Service/ResourceModel/Payout/Decline.php @@ -4,12 +4,19 @@ class Decline extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * Decline constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/decline'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/StoreDetailsAndSubmit.php b/src/Adyen/Service/ResourceModel/Payout/StoreDetailsAndSubmit.php index 7d2dcbc30..1491d0274 100644 --- a/src/Adyen/Service/ResourceModel/Payout/StoreDetailsAndSubmit.php +++ b/src/Adyen/Service/ResourceModel/Payout/StoreDetailsAndSubmit.php @@ -4,12 +4,19 @@ class StoreDetailsAndSubmit extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * StoreDetailsAndSubmit constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/storeDetailAndSubmit'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/Submit.php b/src/Adyen/Service/ResourceModel/Payout/Submit.php index 0d1268292..81be2d7d3 100644 --- a/src/Adyen/Service/ResourceModel/Payout/Submit.php +++ b/src/Adyen/Service/ResourceModel/Payout/Submit.php @@ -4,12 +4,19 @@ class Submit extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * Submit constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/submit'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/ConfirmThirdParty.php b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/ConfirmThirdParty.php index 37e19251b..6c9cf850f 100644 --- a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/ConfirmThirdParty.php +++ b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/ConfirmThirdParty.php @@ -4,12 +4,19 @@ class ConfirmThirdParty extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * ConfirmThirdParty constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/confirmThirdParty'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/DeclineThirdParty.php b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/DeclineThirdParty.php index 07f681c02..162b3e14d 100644 --- a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/DeclineThirdParty.php +++ b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/DeclineThirdParty.php @@ -4,12 +4,19 @@ class DeclineThirdParty extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * DeclineThirdParty constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/declineThirdParty'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetail.php b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetail.php index aced34bb4..9f6ef242d 100644 --- a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetail.php +++ b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetail.php @@ -4,12 +4,19 @@ class StoreDetail extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * StoreDetail constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/storeDetail'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetailsAndSubmitThirdParty.php b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetailsAndSubmitThirdParty.php index f7cb4f86f..6be93f413 100644 --- a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetailsAndSubmitThirdParty.php +++ b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/StoreDetailsAndSubmitThirdParty.php @@ -4,12 +4,19 @@ class StoreDetailsAndSubmitThirdParty extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * StoreDetailsAndSubmitThirdParty constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/storeDetailAndSubmitThirdParty'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/SubmitThirdParty.php b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/SubmitThirdParty.php index a49d5dcc2..393c38ad8 100644 --- a/src/Adyen/Service/ResourceModel/Payout/ThirdParty/SubmitThirdParty.php +++ b/src/Adyen/Service/ResourceModel/Payout/ThirdParty/SubmitThirdParty.php @@ -4,12 +4,19 @@ class SubmitThirdParty extends \Adyen\Service\AbstractResource { + /** + * @var string + */ protected $_endpoint; + /** + * SubmitThirdParty constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { $this->_endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Payout/' . $service->getClient()->getApiVersion() . '/submitThirdParty'; parent::__construct($service, $this->_endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Recurring/Disable.php b/src/Adyen/Service/ResourceModel/Recurring/Disable.php index 3d86256c6..2f2978e32 100644 --- a/src/Adyen/Service/ResourceModel/Recurring/Disable.php +++ b/src/Adyen/Service/ResourceModel/Recurring/Disable.php @@ -4,10 +4,19 @@ class Disable extends \Adyen\Service\AbstractResource { + /** + * @var string + */ + protected $endpoint; + + /** + * Disable constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { - $endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Recurring/' . $service->getClient()->getApiRecurringVersion() . '/disable'; - parent::__construct($service, $endpoint); + $this->endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Recurring/' . $service->getClient()->getApiRecurringVersion() . '/disable'; + parent::__construct($service, $this->endpoint); } - -} \ No newline at end of file +} diff --git a/src/Adyen/Service/ResourceModel/Recurring/ListRecurringDetails.php b/src/Adyen/Service/ResourceModel/Recurring/ListRecurringDetails.php index 0b2d1910a..650554280 100644 --- a/src/Adyen/Service/ResourceModel/Recurring/ListRecurringDetails.php +++ b/src/Adyen/Service/ResourceModel/Recurring/ListRecurringDetails.php @@ -4,10 +4,19 @@ class ListRecurringDetails extends \Adyen\Service\AbstractResource { + /** + * @var string + */ + protected $endpoint; + + /** + * ListRecurringDetails constructor. + * + * @param \Adyen\Service $service + */ public function __construct($service) { - $endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Recurring/' . $service->getClient()->getApiRecurringVersion() . '/listRecurringDetails'; - parent::__construct($service, $endpoint); + $this->endpoint = $service->getClient()->getConfig()->get('endpoint') . '/pal/servlet/Recurring/' . $service->getClient()->getApiRecurringVersion() . '/listRecurringDetails'; + parent::__construct($service, $this->endpoint); } - -} \ No newline at end of file +} diff --git a/tests/TestCase.php b/tests/TestCase.php index ccfc194d6..680fd16c8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -56,6 +56,24 @@ protected function createClient() } } + /** + * Mock client object without configuring the config/test.ini + * + * @return \Adyen\Client + */ + protected function createClientWithoutTestIni() + { + try { + $client = new \Adyen\Client(); + $client->setApplicationName("My Test Application"); + $client->setEnvironment(\Adyen\Environment::TEST); + } catch (\Adyen\AdyenException $exception) { + $this->_skipTest($exception->getMessage()); + } + + return $client; + } + /** * Mock client for payout * @@ -241,4 +259,22 @@ public function validateApiPermission($e) } } + /** + * Get reflection class method set to public to make it testable + * + * @param string $class full path + * @param string $name + * @return mixed + */ + protected function getMethod($class, $name) { + try { + $class = new \ReflectionClass($class); + } catch (\ReflectionException $exception) { + $this->_skipTest($exception->getMessage()); + } + + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } } diff --git a/tests/Unit/AbstractResourceTest.php b/tests/Unit/AbstractResourceTest.php new file mode 100644 index 000000000..80269eae2 --- /dev/null +++ b/tests/Unit/AbstractResourceTest.php @@ -0,0 +1,167 @@ + array( + "adyenLibrary" => array( + "name" => "test", + "version" => "test" + ) + ) + ); + + // Mock client without the Test ini settings + $mockedClient = $this->createClientWithoutTestIni(); + + // Mock abstract class with mocked client and $paramsToFilter parameters + $mockedClass = $this->getMockForAbstractClass($this->className, array((new \Adyen\Service($mockedClient)), "", false)); + + // Get private method as testable public method + $method = $this->getMethod($this->className, "handleApplicationInfoInRequest"); + + // Test against function + $result = $method->invokeArgs($mockedClass, array($params)); + $this->assertArrayNotHasKey("applicationInfo", $result); + } + + /** + * If the allowApplicationInfo is true the applicationInfo Adyen Library should be overwritten with the real values + * + * @covers \Adyen\Service\AbstractResource::handleApplicationInfoInRequest + */ + public function testHandleApplicationInfoInRequestShouldOverwriteApplicationInfoAdyenLibraryParams() + { + $params = array( + "applicationInfo" => array( + "adyenLibrary" => array( + "name" => "test", + "version" => "test" + ) + ) + ); + + // Mock client without the Test ini settings + $mockedClient = $this->createClientWithoutTestIni(); + + // Mock abstract class with mocked client and $paramsToFilter parameters + $mockedClass = $this->getMockForAbstractClass($this->className, array((new \Adyen\Service($mockedClient)), "", true)); + + // Get private method as testable public method + $method = $this->getMethod($this->className, "handleApplicationInfoInRequest"); + + // Test against function + $result = $method->invokeArgs($mockedClass, array($params)); + $this->assertSame($mockedClient->getLibraryName(), $result['applicationInfo']['adyenLibrary']['name']); + $this->assertSame($mockedClient->getLibraryVersion(), $result['applicationInfo']['adyenLibrary']['version']); + } + + /** + * If the config adyenPaymentSource is set the applicationInfo adyenPaymentSource should be added to the params + * If the config externalPlatform is not set the applicationInfo externalPlatform should not be added to the params + * + * @covers \Adyen\Service\AbstractResource::handleApplicationInfoInRequest + */ + public function testHandleApplicationInfoInRequestShouldAddApplicationInfoAdyenPaymentSourceToParams() + { + $params = array(); + + $expectedArraySubset = array( + "applicationInfo" => array( + "adyenPaymentSource" => array( + "name" => "name-test", + "version" => "version-test" + ) + ) + ); + + // Mock client without the Test ini settings + $mockedClient = $this->createClientWithoutTestIni(); + + $mockedClient->setAdyenPaymentSource("name-test", "version-test"); + + // Mock abstract class with mocked client and $paramsToFilter parameters + $mockedClass = $this->getMockForAbstractClass($this->className, array((new \Adyen\Service($mockedClient)), "", true)); + + // Get private method as testable public method + $method = $this->getMethod($this->className, "handleApplicationInfoInRequest"); + + // Test against function + $result = $method->invokeArgs($mockedClass, array($params)); + + $this->assertArrayHasKey("applicationInfo", $result); + $this->assertArraySubset($expectedArraySubset, $result); + } + + /** + * If the config adyenPaymentSource integrator is set, the applicationInfo adyenPaymentSource integrator should be + * added to the params. + * + * @covers \Adyen\Service\AbstractResource::handleApplicationInfoInRequest + */ + public function testHandleApplicationInfoInRequestShouldAddApplicationInfoAdyenPaymentSourceIntegratorToParams() + { + $params = array(); + + // Mock client without the Test ini settings + $mockedClient = $this->createClientWithoutTestIni(); + + $mockedClient->setExternalPlatform("name-test", "version-test", "integrator-test"); + + // Mock abstract class with mocked client and $paramsToFilter parameters + $mockedClass = $this->getMockForAbstractClass($this->className, array((new \Adyen\Service($mockedClient)), "", true)); + + // Get private method as testable public method + $method = $this->getMethod($this->className, "handleApplicationInfoInRequest"); + + // Test against function + $result = $method->invokeArgs($mockedClass, array($params)); + + $this->assertArrayHasKey("applicationInfo", $result); + $this->assertArrayHasKey("externalPlatform", $result["applicationInfo"]); + $this->assertArrayHasKey("integrator", $result["applicationInfo"]["externalPlatform"]); + } + + /** + * If the config adyenPaymentSource integrator is not set, the applicationInfo adyenPaymentSource integrator should + * not be added to the params. + * + * @covers \Adyen\Service\AbstractResource::handleApplicationInfoInRequest + */ + public function testHandleApplicationInfoInRequestShouldNotAddApplicationInfoAdyenPaymentSourceIntegratorToParams() + { + $params = array(); + + // Mock client without the Test ini settings + $mockedClient = $this->createClientWithoutTestIni(); + + $mockedClient->setExternalPlatform("name-test", "version-test"); + + // Mock abstract class with mocked client and $paramsToFilter parameters + $mockedClass = $this->getMockForAbstractClass($this->className, array((new \Adyen\Service($mockedClient)), "", true)); + + // Get private method as testable public method + $method = $this->getMethod($this->className, "handleApplicationInfoInRequest"); + + // Test against function + $result = $method->invokeArgs($mockedClass, array($params)); + + $this->assertArrayHasKey("applicationInfo", $result); + $this->assertArrayHasKey("externalPlatform", $result["applicationInfo"]); + $this->assertArrayNotHasKey("integrator", $result["applicationInfo"]["externalPlatform"]); + } +}