Skip to content

Commit

Permalink
[PW-2250] Outsource the builders from PrestaShop to the php api libra…
Browse files Browse the repository at this point in the history
…ry (Adyen#206)

* Added:
The remaining request builders
The functionality for Open Invoice
The unit test for the utils and builders

* Convert id to string in the openinvoiceTest

* Sonar cloud code smell fixes

* Sonar cloud code smell fixes 2

* Suppress sonar cloud wanring for many paramater on builder

* try to suppress sonar warning for paramaters on builder

* try to suppress sonar anotate class

* try to suppress sonar warnings with Suppress anotation

* Try to suppress warnings again

* Id and ItemId to string

* Fix suggested changes

* Refactor isAuthenticated

* Add copyright
  • Loading branch information
AlexandrosMor authored Jun 26, 2020
1 parent f6e8f38 commit 51f54e0
Show file tree
Hide file tree
Showing 12 changed files with 694 additions and 91 deletions.
86 changes: 86 additions & 0 deletions src/Adyen/Service/Builder/Browser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen API Library for PHP
*
* Copyright (c) 2020 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
*/

namespace Adyen\Service\Builder;

class Browser
{
const BROWSER_INFO = 'browserInfo';

/**
* @param string $userAgent
* @param string $acceptHeader
* @param int $screenWidth
* @param int $screenHeight
* @param int $colorDepth
* @param int $timeZoneOffsetß
* @param string $language
* @param bool $javaEnabled
* @param array $request
* @return array
* @SuppressWarnings:php:S107
*/
public function buildBrowserData(
$userAgent = '',
$acceptHeader = '',
$screenWidth = 0,
$screenHeight = 0,
$colorDepth = 0,
$timeZoneOffset = 0,
$language = '',
$javaEnabled = false,
$request = array()
) {
if (!empty($userAgent)) {
$request[self::BROWSER_INFO]['userAgent'] = $userAgent;
}

if (!empty($acceptHeader)) {
$request[self::BROWSER_INFO]['acceptHeader'] = $acceptHeader;
}

if (!empty($screenWidth)) {
$request[self::BROWSER_INFO]['screenWidth'] = $screenWidth;
}

if (!empty($screenHeight)) {
$request[self::BROWSER_INFO]['screenHeight'] = $screenHeight;
}

if (!empty($colorDepth)) {
$request[self::BROWSER_INFO]['colorDepth'] = $colorDepth;
}

if (!empty($timeZoneOffset)) {
$request[self::BROWSER_INFO]['timeZoneOffset'] = $timeZoneOffset;
}

if (!empty($language)) {
$request[self::BROWSER_INFO]['language'] = $language;
}

$request[self::BROWSER_INFO]['javaEnabled'] = $javaEnabled;

return $request;
}
}
24 changes: 3 additions & 21 deletions src/Adyen/Service/Builder/OpenInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OpenInvoice
* @param float $itemVatPercentage
* @param int $numberOfItems
* @param string $vatCategory
* @param int $itemId
* @param string $itemId
* @return mixed
*/
public function buildOpenInvoiceLineItem(
Expand All @@ -44,11 +44,11 @@ public function buildOpenInvoiceLineItem(
$itemVatPercentage,
$numberOfItems,
$vatCategory,
$itemId = 0
$itemId
) {
$lineItem = array();
// item id is optional
if (0 !== $itemId) {
if (!empty($itemId)) {
$lineItem['id'] = $itemId;
$lineItem['itemId'] = $itemId;
}
Expand All @@ -62,22 +62,4 @@ public function buildOpenInvoiceLineItem(

return $lineItem;
}

/**
* For Klarna And AfterPay use Vat category High others use none
*
* @param $paymentMethod
* @return string 'High'/'None'
*/
public function getVatCategory($paymentMethod)
{
if ($paymentMethod == "klarna" ||
strlen($paymentMethod) >= 9 &&
mb_substr($paymentMethod, 0, 9) == 'afterpay_'
) {
return 'High';
}

return 'None';
}
}
55 changes: 55 additions & 0 deletions src/Adyen/Service/Builder/Refund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen API Library for PHP
*
* Copyright (c) 2020 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
*/

namespace Adyen\Service\Builder;

use Adyen\Util\Currency;

class Refund
{
/**
* @param $orderAmount
* @param stirng $orderId
* @param stirng $pspReference
* @param string $currency
* @param string $merchantAccount
*
* @return array
*/
public function buildRefundRequest($orderAmount, $orderId, $pspReference, $currency, $merchantAccount)
{
$currencyConverter = new Currency();
$amount = $currencyConverter->sanitize($orderAmount, $currency);

return
array(
'originalReference' => $pspReference,
'modificationAmount' => array(
'value' => $amount,
'currency' => $currency
),
'reference' => $orderId,
'merchantAccount' => $merchantAccount
);
}
}
108 changes: 38 additions & 70 deletions src/Adyen/Service/NotificationReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,119 +28,87 @@
use Adyen\Exception\HMACKeyValidationException;
use Adyen\Exception\MerchantAccountCodeException;
use Adyen\Util\HmacSignature;
use Psr\Log\LoggerInterface;

class NotificationReceiver
{
/**
* @var string
*/
private $notificationHMAC;

/**
* @var HmacSignature
*/
private $hmacSignature;

/**
* @var string
*/
private $merchantAccount;

/**
* @var string
*/
private $notificationUsername;

/**
* @var string
*/
private $notificationPassword;

/**
* @var LoggerInterface
*/
private $logger;

/**
* NotificationReceiver constructor.
*
* @param HmacSignature $hmacSignature
* @param $notificationHMAC
* @param $merchantAccount
* @param $notificationUsername
* @param $notificationPassword
* @param LoggerInterface $logger
*/
public function __construct(
HmacSignature $hmacSignature,
$notificationHMAC,
$merchantAccount,
$notificationUsername,
$notificationPassword,
LoggerInterface $logger
HmacSignature $hmacSignature
) {
$this->hmacSignature = $hmacSignature;
$this->notificationHMAC = $notificationHMAC;
$this->merchantAccount = $merchantAccount;
$this->notificationUsername = $notificationUsername;
$this->notificationPassword = $notificationPassword;
$this->logger = $logger;
}

/**
* HTTP Authentication of the notification
*
* Checks if the hmac key is valid
* @param $response
* @param $hmacKey
* @return bool
* @throws MerchantAccountCodeException
* @throws AuthenticationException
* @throws HMACKeyValidationException
* @throws AdyenException
* @throws HMACKeyValidationException
*/
public function validateHmac($response, $hmacKey)
{
$isTestNotification = $this->isTestNotification($response['pspReference']);
if (!$this->hmacSignature->isValidNotificationHMAC($hmacKey, $response)) {
if ($isTestNotification) {
$message = 'HMAC key validation failed';
throw new HMACKeyValidationException($message);
}
return false;
}
return true;
}

/**
* @param $response
* @param $merchantAccount
* @param $notificationUsername
* @param $notificationPassword
* @return bool
* @throws AuthenticationException
* @throws MerchantAccountCodeException
*/
protected function isAuthorised($response)
public function isAuthenticated($response, $merchantAccount, $notificationUsername, $notificationPassword)
{
$internalMerchantAccount = $this->merchantAccount;
$submittedMerchantAccount = $response['merchantAccountCode'];

$isTestNotification = $this->isTestNotification($response['pspReference']);
if (empty($submittedMerchantAccount) && empty($internalMerchantAccount)) {
if (empty($submittedMerchantAccount) || empty($merchantAccount)) {
if ($isTestNotification) {
throw new MerchantAccountCodeException('merchantAccountCode is empty in settings');
throw new MerchantAccountCodeException(
'merchantAccountCode is empty in settings or in the notification'
);
}
return false;
}

// validate username and password
if (!isset($_SERVER['PHP_AUTH_USER']) && !isset($_SERVER['PHP_AUTH_PW'])) {
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
if ($isTestNotification) {
$message = 'Authentication failed: PHP_AUTH_USER and PHP_AUTH_PW are empty.';
$this->logger->addAdyenNotification($message);
$message = 'Authentication failed: PHP_AUTH_USER or PHP_AUTH_PW are empty.';
throw new AuthenticationException($message);
}
return false;
}

// validate hmac
if (!$this->hmacSignature->isValidNotificationHMAC($this->notificationHMAC, $response)) {
$message = 'HMAC key validation failed';
$this->logger->addAdyenNotification($message);
throw new HMACKeyValidationException($message);
}

$usernameIsValid = hash_equals($this->notificationUsername, $_SERVER['PHP_AUTH_USER']);
$passwordIsValid = hash_equals($this->notificationPassword, $_SERVER['PHP_AUTH_PW']);
$usernameIsValid = hash_equals($notificationUsername, $_SERVER['PHP_AUTH_USER']);
$passwordIsValid = hash_equals($notificationPassword, $_SERVER['PHP_AUTH_PW']);
if ($usernameIsValid && $passwordIsValid) {
return true;
}

// If notification is test check if fields are correct if not return error
if ($isTestNotification) {
$message = 'username and\or password are not the same as in settings';
$this->logger->addAdyenNotification($message);
throw new AuthenticationException($message);
}

return false;
}

Expand All @@ -151,7 +119,7 @@ protected function isAuthorised($response)
* @param $testMode bool
* @return bool
*/
protected function validateNotificationMode($notificationMode, $testMode)
public function validateNotificationMode($notificationMode, $testMode)
{
// Notification mode can be a string or a boolean
if (($testMode && ($notificationMode == 'false' || !$notificationMode)) ||
Expand All @@ -168,7 +136,7 @@ protected function validateNotificationMode($notificationMode, $testMode)
* @param $pspReference
* @return bool
*/
protected function isTestNotification($pspReference)
public function isTestNotification($pspReference)
{
if (strpos(strtolower($pspReference), 'test_') !== false
|| strpos(strtolower($pspReference), 'testnotification_') !== false
Expand All @@ -185,7 +153,7 @@ protected function isTestNotification($pspReference)
* @param $eventCode
* @return bool
*/
protected function isReportNotification($eventCode)
public function isReportNotification($eventCode)
{
if (strpos($eventCode, 'REPORT_') !== false) {
return true;
Expand Down
Loading

0 comments on commit 51f54e0

Please sign in to comment.