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-421: Adding Checkout and Checkout Utility support, modified CurlCl… #62

Merged
merged 11 commits into from
May 14, 2018
9 changes: 9 additions & 0 deletions src/Adyen/ApiKeyAuthenticatedService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Adyen;


class ApiKeyAuthenticatedService extends Service
{
protected $_requiresApiKey = true;
}
62 changes: 44 additions & 18 deletions src/Adyen/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

class Client
{
const LIB_VERSION = "1.4.2";
const USER_AGENT_SUFFIX = "adyen-php-api-library/";
const ENDPOINT_TEST = "https://pal-test.adyen.com";
const ENDPOINT_LIVE = "https://pal-live.adyen.com";
const ENPOINT_TEST_DIRECTORY_LOOKUP = "https://test.adyen.com/hpp/directory/v2.shtml";
const ENPOINT_LIVE_DIRECTORY_LOOKUP = "https://live.adyen.com/hpp/directory/v2.shtml";
const API_VERSION = "v30";
const API_RECURRING_VERSION = "v25";
const TERMINAL_CLOUD_TEST = "https://terminal-api-test.adyen.com";
const TERMINAL_CLOUD_LIVE = "https://terminal-api-live.adyen.com";
const LIB_VERSION = "1.4.2";
const USER_AGENT_SUFFIX = "adyen-php-api-library/";
const ENDPOINT_TEST = "https://pal-test.adyen.com";
const ENDPOINT_LIVE = "https://pal-live.adyen.com";
const ENDPOINT_TEST_DIRECTORY_LOOKUP = "https://test.adyen.com/hpp/directory/v2.shtml";
const ENDPOINT_LIVE_DIRECTORY_LOOKUP = "https://live.adyen.com/hpp/directory/v2.shtml";
const API_VERSION = "v30";
const API_RECURRING_VERSION = "v25";
const API_CHECKOUT_VERSION = "v32";
const API_CHECKOUT_UTILITY_VERSION = "v1";
const TERMINAL_CLOUD_TEST = "https://terminal-api-test.adyen.com";
const TERMINAL_CLOUD_LIVE = "https://terminal-api-live.adyen.com";
const ENDPOINT_CHECKOUT_TEST = "https://checkout-test.adyen.com";
const ENDPOINT_CHECKOUT_LIVE = "https://checkout-live.adyen.com";

/**
* @var Adyen_Config $config
Expand All @@ -40,7 +44,7 @@ public function __construct($config = null)
if (!$config) {
// create config
$this->_config = new \Adyen\Config();
}elseif ($config instanceof \Adyen\ConfigInterface) {
} elseif ($config instanceof \Adyen\ConfigInterface) {
$this->_config = $config;
} else {
throw new \Adyen\AdyenException("This config object is not supported, you need to implement the ConfigInterface");
Expand Down Expand Up @@ -91,19 +95,21 @@ public function setXApiKey($xApiKey)
*/
public function setEnvironment($environment)
{
if($environment == \Adyen\Environment::TEST) {
if ($environment == \Adyen\Environment::TEST) {
$this->_config->set('environment', \Adyen\Environment::TEST);
$this->_config->set('endpoint', self::ENDPOINT_TEST);
$this->_config->set('endpointDirectorylookup', self::ENPOINT_TEST_DIRECTORY_LOOKUP);
$this->_config->set('endpointDirectorylookup', self::ENDPOINT_TEST_DIRECTORY_LOOKUP);
$this->_config->set('endpointTerminalCloud', self::TERMINAL_CLOUD_TEST);
} elseif($environment == \Adyen\Environment::LIVE) {
$this->_config->set('endpointCheckout', self::ENDPOINT_CHECKOUT_TEST);
} elseif ($environment == \Adyen\Environment::LIVE) {
$this->_config->set('environment', \Adyen\Environment::LIVE);
$this->_config->set('endpoint', self::ENDPOINT_LIVE);
$this->_config->set('endpointDirectorylookup', self::ENPOINT_LIVE_DIRECTORY_LOOKUP);
$this->_config->set('endpointDirectorylookup', self::ENDPOINT_LIVE_DIRECTORY_LOOKUP);
$this->_config->set('endpointTerminalCloud', self::TERMINAL_CLOUD_LIVE);
$this->_config->set('endpointCheckout', self::ENDPOINT_CHECKOUT_LIVE);
} else {
// environment does not exists
$msg = "This environment does not exists use " . \Adyen\Environment::TEST . ' or ' . \Adyen\Environment::LIVE;
// environment does not exist
$msg = "This environment does not exist, use " . \Adyen\Environment::TEST . ' or ' . \Adyen\Environment::LIVE;
throw new \Adyen\AdyenException($msg);
}
}
Expand Down Expand Up @@ -133,7 +139,8 @@ public function setMerchantAccount($merchantAccount)
$this->_config->set('merchantAccount', $merchantAccount);
}

public function setApplicationName($applicationName) {
public function setApplicationName($applicationName)
{
$this->_config->set('applicationName', $applicationName);
}

Expand Down Expand Up @@ -188,6 +195,25 @@ public function getApiRecurringVersion()
return self::API_RECURRING_VERSION;
}

/**
* Get the version of the Checkout API endpoint
*
* @return string
*/
public function getApiCheckoutVersion()
{
return self::API_CHECKOUT_VERSION;
}

/**
* Get the version of the Checkout Utility API endpoint
*
* @return string
*/
public function getApiCheckoutUtilityVersion()
{
return self::API_CHECKOUT_UTILITY_VERSION;
}

/**
* @param HttpClient\ClientInterface $httpClient
Expand Down
17 changes: 11 additions & 6 deletions src/Adyen/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
);

// set authorisation credentials according to support & availability
if ($service->supportsXAPIKey() && !empty($xApiKey)) {
if ($service->requiresApiKey() && !empty($xApiKey)) {
//Set the content type to application/json and use the defined userAgent along with the x-api-key
$headers[] = 'x-api-key: ' . $xApiKey;
} elseif ($service->supportsXAPIKey() && empty($xApiKey)) {
$msg = "Please insert a valid Checkout API Key in your test.ini file";
} elseif ($service->requiresApiKey() && empty($xApiKey)) {
$msg = "Please provide a valid Checkout API Key";
throw new \Adyen\AdyenException($msg);
} else {

Expand All @@ -67,8 +67,7 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Execute the request
$result = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
list($result, $httpStatus) = $this->curlRequest($ch);

// log the raw response
$logger->info("JSON Response is: " . $result);
Expand Down Expand Up @@ -231,7 +230,6 @@ protected function handleCurlError($url, $errno, $message, $logger)
protected function handleResultError($result, $logger)
{
$decodeResult = json_decode($result, true);

if (isset($decodeResult['message']) && isset($decodeResult['errorCode'])) {
$logger->error($decodeResult['errorCode'] . ': ' . $decodeResult['message']);
throw new \Adyen\AdyenException($decodeResult['message'], $decodeResult['errorCode'], null,
Expand Down Expand Up @@ -261,4 +259,11 @@ private function logRequest(\Psr\Log\LoggerInterface $logger, $requestUrl, $para
}
$logger->info('JSON Request to Adyen:' . json_encode($params));
}

protected function curlRequest($ch)
{
$result = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($result, $httpStatus);
}
}
8 changes: 4 additions & 4 deletions src/Adyen/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Service
{
private $client;
protected $_supportsXAPIKey = false;
protected $_requiresApiKey = false;

public function __construct(\Adyen\Client $client)
{
Expand All @@ -14,7 +14,7 @@ public function __construct(\Adyen\Client $client)
// validate if client has all the configuration we need
if(!$client->getConfig()->get('environment')) {
// throw exception
$msg = "The Client does not have a corect environment. use " . \Adyen\Environment::TEST . ' or ' . \Adyen\Environment::LIVE;
$msg = "The Client does not have a correct environment, use " . \Adyen\Environment::TEST . ' or ' . \Adyen\Environment::LIVE;
throw new \Adyen\AdyenException($msg);
}

Expand All @@ -26,9 +26,9 @@ public function getClient()
return $this->client;
}

public function supportsXAPIKey()
public function requiresApiKey()
{
return $this->_supportsXAPIKey;
return $this->_requiresApiKey;
}

}
57 changes: 57 additions & 0 deletions src/Adyen/Service/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Adyen\Service;


class Checkout extends \Adyen\ApiKeyAuthenticatedService
{
protected $_setup;
protected $_verify;
protected $_paymentMethods;
protected $_payments;
protected $_paymentsDetails;

public function __construct(\Adyen\Client $client)
{
parent::__construct($client);

$this->_setup = new \Adyen\Service\ResourceModel\Checkout\Setup($this);
$this->_verify = new \Adyen\Service\ResourceModel\Checkout\Verify($this);
$this->_paymentMethods = new \Adyen\Service\ResourceModel\Checkout\PaymentMethods($this);
$this->_payments = new \Adyen\Service\ResourceModel\Checkout\Payments($this);
$this->_paymentsDetails = new \Adyen\Service\ResourceModel\Checkout\PaymentsDetails($this);

}

public function setup($params)
{
$result = $this->_setup->request($params);
return $result;
}

public function verify($params)
{
$result = $this->_verify->request($params);
return $result;
}

public function paymentMethods($params)
{
$result = $this->_paymentMethods->request($params);
return $result;
}

public function payments($params)
{
$result = $this->_payments->request($params);
return $result;
}

public function paymentsDetails($params)
{
$result = $this->_paymentsDetails->request($params);
return $result;
}


}
24 changes: 24 additions & 0 deletions src/Adyen/Service/CheckoutUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Adyen\Service;


class CheckoutUtility extends \Adyen\ApiKeyAuthenticatedService
{
protected $_originKeys;

public function __construct(\Adyen\Client $client)
{
parent::__construct($client);

$this->_originKeys = new \Adyen\Service\ResourceModel\CheckoutUtility\OriginKeys($this);

}

public function originKeys($params)
{
$result = $this->_originKeys->request($params);
return $result;
}

}
3 changes: 1 addition & 2 deletions src/Adyen/Service/PosPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Adyen\Service;

class PosPayment extends \Adyen\Service
class PosPayment extends \Adyen\ApiKeyAuthenticatedService
{

protected $_runTenderSync;
Expand All @@ -15,7 +15,6 @@ public function __construct(\Adyen\Client $client)

$this->_runTenderSync = new \Adyen\Service\ResourceModel\Payment\TerminalCloudAPI($this, false);
$this->_runTenderAsync = new \Adyen\Service\ResourceModel\Payment\TerminalCloudAPI($this, true);
$this->_supportsXAPIKey = true;
}

public function runTenderSync($params)
Expand Down
20 changes: 20 additions & 0 deletions src/Adyen/Service/ResourceModel/Checkout/PaymentMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Adyen\Service\ResourceModel\Checkout;

class PaymentMethods extends \Adyen\Service\AbstractResource
{

protected $_requiredFields = array(
'merchantAccount'
);

protected $_endpoint;

public function __construct($service)
{
$this->_endpoint = $service->getClient()->getConfig()->get('endpointCheckout') .'/'. $service->getClient()->getApiCheckoutVersion() . '/paymentMethods';
parent::__construct($service, $this->_endpoint, $this->_requiredFields);
}

}
25 changes: 25 additions & 0 deletions src/Adyen/Service/ResourceModel/Checkout/Payments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Adyen\Service\ResourceModel\Checkout;

class Payments extends \Adyen\Service\AbstractResource
{

protected $_requiredFields = array(
'amount.value',
'amount.currency',
'merchantAccount',
'paymentMethod',
'reference',
'returnUrl'
);

protected $_endpoint;

public function __construct($service)
{
$this->_endpoint = $service->getClient()->getConfig()->get('endpointCheckout') .'/'. $service->getClient()->getApiCheckoutVersion() . '/payments';
parent::__construct($service, $this->_endpoint, $this->_requiredFields);
}

}
21 changes: 21 additions & 0 deletions src/Adyen/Service/ResourceModel/Checkout/PaymentsDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Adyen\Service\ResourceModel\Checkout;

class PaymentsDetails extends \Adyen\Service\AbstractResource
{

protected $_requiredFields = array(
'paymentData',
'details'
);

protected $_endpoint;

public function __construct($service)
{
$this->_endpoint = $service->getClient()->getConfig()->get('endpointCheckout') .'/'. $service->getClient()->getApiCheckoutVersion() . '/payments/details';
parent::__construct($service, $this->_endpoint, $this->_requiredFields);
}

}
23 changes: 23 additions & 0 deletions src/Adyen/Service/ResourceModel/Checkout/Setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Adyen\Service\ResourceModel\Checkout;

class Setup extends \Adyen\Service\AbstractResource
{

protected $_requiredFields = array(
'amount.value',
'amount.currency',
'countryCode',
'merchantAccount'
);

protected $_endpoint;

public function __construct($service)
{
$this->_endpoint = $service->getClient()->getConfig()->get('endpointCheckout') .'/'. $service->getClient()->getApiCheckoutVersion() . '/setup';
parent::__construct($service, $this->_endpoint, $this->_requiredFields);
}

}
Loading