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

New release 1.4.1 #54

Merged
merged 19 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**PHP version**: x.y.z
**Library version**: x.y.z
**Description**
<!--
- please provide description of the issue. In case of bug report, please provide the necessary steps to reproduce.
- For merchant specific requests, please use https://support.adyen.com
-->
7 changes: 7 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**Description**
<!-- Please provide a description of the changes proposed in the Pull Request -->

**Tested scenarios**
<!-- Description of tested scenarios -->

**Fixed issue**: <!-- #-prefixed issue number -->
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "adyen/php-api-library",
"type": "library",
"description": "A PHP client library for accessing Adyen APIs",
"keywords": ["adyen"],
"keywords": [
"adyen"
],
"homepage": "https://github.com/Adyen/adyen-php-api-library",
"license": "Apache-2.0",
"require": {
Expand All @@ -19,7 +21,12 @@
"Adyen\\": "src/Adyen/"
},
"classmap": [
"src/Adyen/Service/"
]
"src/Adyen/Service/"
]
},
"autoload-dev": {
"psr-4": {
"Adyen\\Tests\\": "tests/"
}
}
}
2 changes: 1 addition & 1 deletion src/Adyen/AdyenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct($message = "", $code = 0, Exception $previous = null
{
$this->_status = $status;
$this->_errorType = $errorType;
parent::__construct($message, $code, $previous);
parent::__construct($message, (int) $code, $previous);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Adyen/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Client
{
const LIB_VERSION = "1.4.0";
const LIB_VERSION = "1.4.1";
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 Down Expand Up @@ -104,6 +104,16 @@ public function setRequestUrl($url)
$this->_config->set('endpoint', $url);
}

/**
* Set directory lookup URL
*
* @param $url
*/
public function setDirectoryLookupUrl($url)
{
$this->_config->set('endpointDirectorylookup', $url);
}

public function setMerchantAccount($merchantAccount)
{
$this->_config->set('merchantAccount', $merchantAccount);
Expand Down Expand Up @@ -217,4 +227,4 @@ protected function createDefaultLogger()

return $logger;
}
}
}
59 changes: 39 additions & 20 deletions src/Adyen/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class CurlClient implements ClientInterface
{

/**
* Json API request to Adyen
*
Expand All @@ -17,17 +16,15 @@ class CurlClient implements ClientInterface
*/
public function requestJson(\Adyen\Service $service, $requestUrl, $params)
{
$client = $service->getClient();
$client = $service->getClient();
$config = $client->getConfig();
$logger = $client->getLogger();
$username = $config->getUsername();
$password = $config->getPassword();
$jsonRequest = json_encode($params);

// log the requestUr, params and json request
$logger->info("Request url to Adyen: " . $requestUrl);
$logger->info('Params in request to Adyen:' . print_r($params, 1));
$logger->info('JSON Request to Adyen:' . $jsonRequest);
// log the request
$this->logRequest($logger, $requestUrl, $params);

//Initiate cURL.
$ch = curl_init($requestUrl);
Expand All @@ -37,7 +34,7 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)

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

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonRequest);
Expand Down Expand Up @@ -66,7 +63,7 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
// result not 200 throw error
if ($httpStatus != 200 && $result) {
$this->handleResultError($result, $logger);
} elseif(!$result) {
} elseif (!$result) {
$errno = curl_errno($ch);
$message = curl_error($ch);

Expand All @@ -77,7 +74,7 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
curl_close($ch);

// result in array or json
if($config->getOutputType() == 'array') {
if ($config->getOutputType() == 'array') {

// transform to PHP Array
$result = json_decode($result, true);
Expand All @@ -101,7 +98,7 @@ public function requestJson(\Adyen\Service $service, $requestUrl, $params)
*/
public function requestPost(\Adyen\Service $service, $requestUrl, $params)
{
$client = $service->getClient();
$client = $service->getClient();
$config = $client->getConfig();
$logger = $client->getLogger();
$username = $config->getUsername();
Expand All @@ -119,7 +116,7 @@ public function requestPost(\Adyen\Service $service, $requestUrl, $params)

// set authorisation
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

Expand All @@ -132,8 +129,10 @@ public function requestPost(\Adyen\Service $service, $requestUrl, $params)
'User-Agent: ' . $userAgent
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// return the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Execute the request
$result = curl_exec($ch);
Expand All @@ -144,10 +143,9 @@ public function requestPost(\Adyen\Service $service, $requestUrl, $params)

if ($httpStatus != 200 && $result) {
$this->handleResultError($result, $logger);
} elseif(!$result) {
} elseif (!$result) {
$errno = curl_errno($ch);
$message = curl_error($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
$this->handleCurlError($requestUrl, $errno, $message, $logger);
Expand All @@ -156,11 +154,11 @@ public function requestPost(\Adyen\Service $service, $requestUrl, $params)
curl_close($ch);

// result in array or json
if($config->getOutputType() == 'array') {
if ($config->getOutputType() == 'array') {
// transform to PHP Array
$result = json_decode($result, true);

if(!$result) {
if (!$result) {
$msg = "The result is empty, looks like your request is invalid";
$logger->error($msg);
throw new \Adyen\AdyenException($msg);
Expand All @@ -182,7 +180,7 @@ public function requestPost(\Adyen\Service $service, $requestUrl, $params)
* @param $errno
* @param $message
* @param $logger
* @throws \Adyen\AdyenException
* @throws \Adyen\ConnectionException
*/
protected function handleCurlError($url, $errno, $message, $logger)
{
Expand Down Expand Up @@ -221,12 +219,33 @@ protected function handleResultError($result, $logger)
{
$decodeResult = json_decode($result, true);

if(isset($decodeResult['message']) && isset($decodeResult['errorCode'])) {
if (isset($decodeResult['message']) && isset($decodeResult['errorCode'])) {
$logger->error($decodeResult['errorCode'] . ': ' . $decodeResult['message']);
throw new \Adyen\AdyenException($decodeResult['message'], $decodeResult['errorCode'], null, $decodeResult['status'], $decodeResult['errorType']);
throw new \Adyen\AdyenException($decodeResult['message'], $decodeResult['errorCode'], null,
$decodeResult['status'], $decodeResult['errorType']);
}
$logger->error($result);
throw new \Adyen\AdyenException($result);
}

}
/**
* Logs the API request, removing sensitive data
*
* @param \Psr\Log\LoggerInterface $logger
* @param $requestUrl
* @param $params
*/
private function logRequest(\Psr\Log\LoggerInterface $logger, $requestUrl, $params)
{
// log the requestUr, params and json request
$logger->info("Request url to Adyen: " . $requestUrl);
if (isset($params["additionalData"]) && isset($params["additionalData"]["card.encrypted.json"])) {
$params["additionalData"]["card.encrypted.json"] = "*";
}
if (isset($params["card"]) && isset($params["card"]["number"])) {
$params["card"]["number"] = "*";
$params["card"]["cvc"] = "*";
}
$logger->info('JSON Request to Adyen:' . json_encode($params));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ class StoreDetailsAndSubmitThirdParty extends \Adyen\Service\AbstractResource
'reference',
'recurring.contract',
'amount.currency',
'amount.value',
'bank.iban',
'bank.ownerName',
'bank.countryCode'
'amount.value'
);

protected $_endpoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class ListRecurringDetails extends \Adyen\Service\AbstractResource

protected $_requiredFields = array(
'merchantAccount',
'shopperReference',
'recurring',
'recurring.contract'
'shopperReference'
);

public function __construct($service)
Expand Down
8 changes: 6 additions & 2 deletions src/Adyen/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

class Util
{

public static function calculateSha256Signature($hmacKey, $params)
{
// validate if hmacKey is provided
if ($hmacKey == "") {
if (empty($hmacKey)) {
throw new \Adyen\AdyenException("You did not provide a HMAC key");
}

// validate if hmacKey contains only hexadecimal chars to be packed with H*
if (!ctype_xdigit($hmacKey)) {
throw new \Adyen\AdyenException("Invalid HMAC key: $hmacKey");
}

if (empty($params)) {
throw new \Adyen\AdyenException("You did not provide any parameters");
}
Expand Down
42 changes: 0 additions & 42 deletions tests/PayoutThirdPartyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,6 @@ public function testStoreDetailAndSubmitPayoutThirdPartyMissingReference()

}

public function testStoreDetailAndSubmitPayoutThirdPartyBadPaymentMethod()
{
// initialize client
$client = $this->createPayoutClient();

// initialize service
$service = new Service\Payout($client);

$json = '{
"card": {
"number": "4111111111111111",
"expiryMonth": "08",
"expiryYear": "2018",
"cvc": "737",
"holderName": "John Smith"
},
"amount": {
"value": 1500,
"currency": "EUR"
},
"reference": "payout-test",
"recurring": {
"contract": "PAYOUT"
},
"shopperEmail": "[email protected]",
"shopperReference": "johnsmithuniqueid",
"merchantAccount": "' . $this->_merchantAccount .'"
}';

$params = json_decode($json, true);
$e = null;
try {
$result = $service->storeDetailsAndSubmitThirdParty($params);
} catch (\Exception $e) {
$this->validateApiPermission($e);
}

// check if exception is correct
$this->assertEquals('Adyen\AdyenException', get_class($e));
$this->assertEquals('Missing the following fields: bank.iban,bank.ownerName,bank.countryCode', $e->getMessage());
}

public function testStoreDetailAndSubmitPayoutThirdPartyInvalidIban()
{
// initialize client
Expand Down
25 changes: 25 additions & 0 deletions tests/Util/UtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Adyen\Tests\Util;

use Adyen\Util\Util;

class UtilTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException Adyen\AdyenException
* @expectedExceptionMessage Invalid HMAC key: INVALID
*/
public function testSha256Invalid() {
Util::calculateSha256Signature("INVALID", array('key' => 'value'));
}

public function testSha256() {
$signature = Util::calculateSha256Signature("123ABC", array(
'akey' => 'val\\ue',
'ckey' => 'val:ue',
'bkey' => '1'
));
$this->assertEquals("YtbpYcrdbvk0RSVwTwENMzomS0LYtiItMwXhI5tohXs=", $signature);
}
}