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

Terminal API (Cloud) support #51

Merged
merged 14 commits into from
Apr 30, 2018
14 changes: 14 additions & 0 deletions src/Adyen/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Client
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";

/**
* @var Adyen_Config $config
Expand Down Expand Up @@ -71,6 +73,16 @@ public function setPassword($password)
$this->_config->set('password', $password);
}

/**
* Set x-api-key for Web Service Client
*
* @param $xapikey
*/
public function setXApiKey($xApiKey)
{
$this->_config->set('x-api-key', $xApiKey);
}

/**
* Set environment to connect to test or live platform of Adyen
*
Expand All @@ -83,10 +95,12 @@ public function setEnvironment($environment)
$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('endpointTerminalCloud', self::TERMINAL_CLOUD_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('endpointTerminalCloud', self::TERMINAL_CLOUD_LIVE);
} else {
// environment does not exists
$msg = "This environment does not exists use " . \Adyen\Environment::TEST . ' or ' . \Adyen\Environment::LIVE;
Expand Down
20 changes: 20 additions & 0 deletions src/Adyen/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ public function getPassword()
return isset($this->data['password']) ? $this->data['password'] : null;
}

/**
* Get the Checkout API Key from the Adyen Customer Area
*
* @return mixed|null
*/
public function getXApiKey()
{
return !empty($this->data['x-api-key']) ? $this->data['x-api-key'] : null;
}

/**
* Get the Point of Interest Terminal ID, used for POS Transactions with Terminal API
*
* @return mixed|null
*/
public function getPOIID()
{
return !empty($this->data['POIID']) ? $this->data['POIID'] : null;
}

public function getInputType()
{
if(isset($this->data['inputType']) && in_array($this->data['inputType'], $this->allowedInput)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Adyen/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

public function getUsername();
public function getPassword();
public function getXApiKey();
public function get($param);
public function getInputType();
public function getOutputType();
public function getMerchantAccount();
public function getPOIID();

}
23 changes: 18 additions & 5 deletions src/Adyen/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
$logger = $client->getLogger();
$username = $config->getUsername();
$password = $config->getPassword();
$xApiKey = $config->getXApiKey();

$jsonRequest = json_encode($params);

// log the request
Expand All @@ -32,14 +34,10 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

// set authorisation
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonRequest);

// set a custom User-Agent
//create a custom User-Agent
$userAgent = $config->get('applicationName') . " " . \Adyen\Client::USER_AGENT_SUFFIX . $client->getLibraryVersion();

//Set the content type to application/json and use the defined userAgent
Expand All @@ -48,6 +46,21 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
'User-Agent: ' . $userAgent
);

// set authorisation credentials according to support & availability
if ($service->supportsXAPIKey() && !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";
throw new \Adyen\AdyenException($msg);
} else {

//Set the basic auth credentials
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
}

//Set the headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// return the result
Expand Down
6 changes: 6 additions & 0 deletions src/Adyen/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Service
{
private $client;
protected $_supportsXAPIKey = false;

public function __construct(\Adyen\Client $client)
{
Expand All @@ -25,4 +26,9 @@ public function getClient()
return $this->client;
}

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

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

namespace Adyen\Service;

class PosPayment extends \Adyen\Service
{

protected $_runTenderSync;
protected $_runTenderAsync;
protected $_txType;

public function __construct(\Adyen\Client $client)
{
parent::__construct($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)
{
$result = $this->_runTenderSync->request($params);
return $result;
}

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

public function getServiceId($request)
{
if (isset($request['SaleToPOIRequest']['MessageHeader']['ServiceID'])) {
return $request['SaleToPOIRequest']['MessageHeader']['ServiceID'];
}
return null;
}

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

namespace Adyen\Service\ResourceModel\Payment;

class TerminalCloudAPI extends \Adyen\Service\AbstractResource
{

protected $_requiredFields = array(
'SaleToPOIRequest.MessageHeader.POIID',
'SaleToPOIRequest.MessageHeader.ServiceID',
//TODO fix: actually works with only first two levels
'SaleToPOIRequest.PaymentRequest.SaleData.SaleTransactionID.TransactionID', //reference
'SaleToPOIRequest.PaymentRequest.PaymentTransaction.AmountsReq.Currency',
'SaleToPOIRequest.PaymentRequest.PaymentTransaction.AmountsReq.RequestedAmount',
//PaymentData is optional, if not provided it will perform an authorisation(no refunds)
'SaleToPOIRequest.PaymentRequest.PaymentData.PaymentType',
);

protected $_endpoint;

public function __construct($service, $asynchronous)
{
if ($asynchronous) {
$this->_endpoint = $service->getClient()->getConfig()->get('endpointTerminalCloud') . '/async';
} else {
$this->_endpoint = $service->getClient()->getConfig()->get('endpointTerminalCloud') . '/sync';
}
parent::__construct($service, $this->_endpoint, $this->_requiredFields);
}

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


namespace Adyen;


class TransactionType
{
const NORMAL = 'Normal';
const REFUND = 'Refund';

}
Loading