diff --git a/.github/workflows/Lint.yml b/.github/workflows/Lint.yml new file mode 100644 index 0000000..4567bfc --- /dev/null +++ b/.github/workflows/Lint.yml @@ -0,0 +1,58 @@ +name: Lint + +on: + push: + branches: + - master + pull_request: + types: [opened, synchronize] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + matrix: + php: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + + - name: Lint + run: | + error=0 + for file in $(find Trustly -type f -name "*.php"); do + php -l -n $file | grep -v "No syntax errors detected" && error=1 + done + if [ $error -eq 1 ]; then + echo "Syntax errors were found." + exit 1 + else + echo "No syntax errors were detected." + fi + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cache/composer/files + key: ${{ matrix.php }}-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ matrix.php }}- + + - name: Check dependencies + run: composer install + + - name: PHPStan + if: ${{ startsWith(matrix.php, '7.') || startsWith(matrix.php, '8.') }} + run: | + composer require --dev phpstan/phpstan + vendor/bin/phpstan analyze Trustly --no-progress --level 7 diff --git a/.gitignore b/.gitignore index e52ccc1..e005a0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ example/var/ +/vendor +/composer.lock diff --git a/Trustly/Api/api.php b/Trustly/Api/api.php index c9fee7d..5c5cfc4 100644 --- a/Trustly/Api/api.php +++ b/Trustly/Api/api.php @@ -44,13 +44,13 @@ abstract class Trustly_Api { * * @var string FQHN */ - protected $api_host = NULL; + protected $api_host; /** * API port used for communication. * * @var integer Normally either 443 (https) or 80 (http) */ - protected $api_port = NULL; + protected $api_port; /** * Inidicator wether the API host is communicating using https * @@ -63,10 +63,13 @@ abstract class Trustly_Api { * * @see Trustly_Api::getLastRequest() * - * @var array Last API call in data form. + * @var ?Trustly_Data_Request Last API call in data form. */ public $last_request = NULL; + /** @var mixed */ + public $trustly_publickey = NULL; + /** * API Constructor * @@ -89,9 +92,9 @@ abstract class Trustly_Api { public function __construct($host='trustly.com', $port=443, $is_https=TRUE) { $this->api_is_https = $is_https; - if($this->loadTrustlyPublicKey($host, $port, $is_https) === FALSE) { + if($this->loadTrustlyPublicKey($host, $port) === FALSE) { $error = openssl_error_string(); - throw new InvalidArgumentException("Cannot load Trustly public key file for host $host".(isset($error)?", error $error":'')); + throw new InvalidArgumentException("Cannot load Trustly public key file for host $host".($error?", error $error":'')); } /* Make sure the curl extension is loaded so we can open URL's */ @@ -139,9 +142,9 @@ public function loadTrustlyPublicKey($host, $port) { * * @link https://eu.developers.trustly.com/doc/reference/authentication * - * @param array $data Input data to serialize + * @param mixed $data Input data to serialize * - * @return array The input data in a serialized form + * @return string The input data in a serialized form */ public function serializeData($data) { if(is_array($data)) { @@ -167,13 +170,13 @@ public function serializeData($data) { * * @link https://eu.developers.trustly.com/doc/reference/authentication * - * @param string $method Method in the API call + * @param ?string $method Method in the API call * - * @param string $uuid UUID in the API call + * @param ?string $uuid UUID in the API call * - * @param string $signature in the API call + * @param ?string $signature in the API call * - * @param array $data in the API call + * @param mixed $data in the API call * * @return boolean Indicating wether or not the host key was used for * signing this data. @@ -204,7 +207,7 @@ protected function verifyTrustlySignedData($method, $uuid, $signature, $data) { * Trustly_Data_Response) has been signed with the correct key when * originating from the host * - * @param Trustly_Data_Response $response Response from the API call. + * @param Trustly_Data_JSONRPCSignedResponse $response Response from the API call. * * @return boolean Indicating if the data was indeed properly signed by the * API we think we are talking to @@ -223,7 +226,7 @@ public function verifyTrustlySignedResponse($response) { * Trustly_Data_JSONRPCNotificationRequest) has been signed with the * correct key originating from the host * - * @param Trustly_Data_JSONRPCNotificationRequest incoming notification + * @param Trustly_Data_JSONRPCNotificationRequest $notification incoming notification * * @return boolean Indicating if the data was indeed properly signed by the * API we think we are talking to @@ -243,15 +246,17 @@ public function verifyTrustlySignedNotification($notification) { * @throws InvalidArgumentException If the public key for the API host * cannot be loaded. * - * @param string $host API host used for communication. Fully qualified + * @param ?string $host API host used for communication. Fully qualified * hostname. When integrating with our public API this is typically * either 'test.trustly.com' or 'trustly.com'. NULL means do not change. * - * @param integer $port Port on API host used for communicaiton. Normally + * @param ?integer $port Port on API host used for communicaiton. Normally * 443 for https, or 80 for http. NULL means do not change. * - * @param bool $is_https Indicator wether the port on the API host expects + * @param ?bool $is_https Indicator wether the port on the API host expects * https. NULL means do not change. + * + * @return void */ public function setHost($host=NULL, $port=NULL, $is_https=NULL) { if(!isset($host)) { @@ -264,7 +269,7 @@ public function setHost($host=NULL, $port=NULL, $is_https=NULL) { if($this->loadTrustlyPublicKey($host, $port) === FALSE) { $error = openssl_error_string(); - throw new InvalidArgumentException("Cannot load Trustly public key file for host $host".(isset($error)?", error $error":'')); + throw new InvalidArgumentException("Cannot load Trustly public key file for host $host".($error?", error $error":'')); } if(isset($is_https)) { @@ -275,11 +280,11 @@ public function setHost($host=NULL, $port=NULL, $is_https=NULL) { /** * Setup and return a curl handle for submitting data to the API peer. * - * @param string $url The URL to communicate with + * @param ?string $url The URL to communicate with * - * @param string $postdata The (optional) data to post. + * @param ?string $postdata The (optional) data to post. * - * @return Array($body, $response_code) + * @return array{string, int} Array($body, $response_code) */ public function post($url=NULL, $postdata=NULL) { /* Do note that that if you are going to POST JSON you need to set the @@ -295,13 +300,13 @@ public function post($url=NULL, $postdata=NULL) { curl_setopt($curl, CURLOPT_PORT, $this->api_port); if($this->api_is_https) { - if(@CURLOPT_PROTOCOLS != 'CURLOPT_PROTOCOLS') { + if(defined('CURLOPT_PROTOCOLS')) { curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); } curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE); } else { - if(@CURLOPT_PROTOCOLS != 'CURLOPT_PROTOCOLS') { + if(defined('CURLOPT_PROTOCOLS')) { curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP); } } @@ -313,9 +318,9 @@ public function post($url=NULL, $postdata=NULL) { curl_setopt($curl, CURLOPT_URL, $url); $body = curl_exec($curl); - if($body === FALSE) { + if(!is_string($body)) { $error = curl_error($curl); - if($error === NULL) { + if(!$error) { $error = 'Failed to connect to the Trusly API'; } throw new Trustly_ConnectionException($error); @@ -378,7 +383,7 @@ public function post($url=NULL, $postdata=NULL) { /** * Return a properly formed url to communicate with this API. * - * @return URL pointing to the API peer. + * @return string URL pointing to the API peer. */ public function baseURL() { if($this->api_is_https) { @@ -392,9 +397,9 @@ public function baseURL() { /** * Return a URL to the API to the given request path. * - * @param Trustly_Data_Request $request Data to send in the request + * @param ?Trustly_Data_Request $request Data to send in the request * - * @return URL to the API peer with the given query path. + * @return string URL to the API peer with the given query path. */ public function url($request=NULL) { return $this->baseURL() . $this->urlPath($request); @@ -404,7 +409,7 @@ public function url($request=NULL) { /** * Return the last request that we attempted to make via this API * - * @return array Last request data structure. + * @return ?Trustly_Data_Request Last request data structure. */ public function getLastRequest() { return $this->last_request; @@ -465,6 +470,7 @@ public function notificationResponse($request, $success=TRUE) { * * @param Trustly_Data_Request $request Outgoing data request. * + * @return Trustly_Data_JSONRPCResponse */ public function call($request) { if($this->insertCredentials($request) !== TRUE) { @@ -473,6 +479,9 @@ public function call($request) { $this->last_request = $request; $jsonstr = $request->json(); + if($jsonstr === FALSE) { + throw new Trustly_DataException('Unable to json encode payload'); + } $url = $this->url($request); list($body, $response_code) = $this->post($url, $jsonstr); @@ -484,9 +493,9 @@ public function call($request) { /** * Return a boolean value formatted for communicating with the API. * - * @param boolean $value Boolean value to encode + * @param ?boolean $value Boolean value to encode * - * @return API encoded boolean value + * @return ?string API encoded boolean value */ protected function apiBool($value) { if(isset($value)) { @@ -506,7 +515,7 @@ protected function apiBool($value) { * * See specific class implementing the call for more information. * - * @param Trustly_Data_Request $request Data to send in the request + * @param ?Trustly_Data_Request $request Data to send in the request * * @return string The URL path */ @@ -524,6 +533,8 @@ abstract protected function urlPath($request=NULL); * @param string $body The body recieved in response to the request * * @param integer $response_code the HTTP response code for the call + * + * @return Trustly_Data_JSONRPCResponse */ abstract protected function handleResponse($request, $body, $response_code); @@ -535,6 +546,8 @@ abstract protected function handleResponse($request, $body, $response_code); * * @param Trustly_Data_Request $request Request to be used in the outgoing * call + * + * @return bool */ abstract protected function insertCredentials($request); diff --git a/Trustly/Api/signed.php b/Trustly/Api/signed.php index 69d0b5b..6e10810 100644 --- a/Trustly/Api/signed.php +++ b/Trustly/Api/signed.php @@ -37,10 +37,16 @@ class Trustly_Api_Signed extends Trustly_Api { /** * Loaded merchant private key resource - * @var resource from openssl with the loaded privatekey + * @var mixed from openssl with the loaded privatekey */ private $merchant_privatekey = NULL; + /** @var string */ + public $api_username; + + /** @var string */ + public $api_password; + /** * Constructor. * @@ -70,7 +76,7 @@ public function __construct($merchant_privatekey, $username, $password, $host='t parent::__construct($host, $port, $is_https); $this->api_username = $username; - $this->api_password = $password; + $this->api_password = $password; if($merchant_privatekey != NULL) { if(strpos($merchant_privatekey, "\n") !== FALSE) { if($this->useMerchantPrivateKey($merchant_privatekey) === FALSE) { @@ -107,7 +113,9 @@ public function loadMerchantPrivateKey($filename) { * * @see https://eu.developers.trustly.com/doc/reference/authentication * - * @param string $cert Loaded private RSA key as a string + * @param string|false $cert Loaded private RSA key as a string + * + * @return bool */ public function useMerchantPrivateKey($cert) { if($cert !== FALSE) { @@ -124,7 +132,9 @@ public function useMerchantPrivateKey($cert) { * @throws Trustly_SignatureException if private key has not been loaded * yet or if we for some other reason fail to sign the request. * - * @param Trustly_Data_JSONRPCRequest $request Request to sign. + * @param Trustly_Data_JSONRPCRequest|Trustly_Data_JSONRPCNotificationResponse $request Request to sign. + * + * @return string */ public function signMerchantRequest($request) { if(!isset($this->merchant_privatekey)) { @@ -166,9 +176,6 @@ protected function insertCredentials($request) { $request->setData('Password', $this->api_password); $signature = $this->signMerchantRequest($request); - if($signature === FALSE) { - return FALSE; - } $request->setParam('Signature', $signature); return TRUE; @@ -194,7 +201,7 @@ protected function insertCredentials($request) { * @return Trustly_Data_JSONRPCSignedResponse */ protected function handleResponse($request, $body, $response_code) { - $response = new Trustly_Data_JSONRPCSignedResponse($body, $response_code); + $response = new Trustly_Data_JSONRPCSignedResponse($body); if($this->verifyTrustlySignedResponse($response) !== TRUE) { throw new Trustly_SignatureException('Incomming message signature is not valid', $response); @@ -223,9 +230,6 @@ public function notificationResponse($request, $success=TRUE) { $response = new Trustly_Data_JSONRPCNotificationResponse($request, $success); $signature = $this->signMerchantRequest($response); - if($signature === FALSE) { - return FALSE; - } $response->setSignature($signature); return $response; @@ -239,7 +243,7 @@ public function notificationResponse($request, $success=TRUE) { * * See specific class implementing the call for more information. * - * @param Trustly_Data_JSONRPCRequest $request Data to send in the request + * @param ?Trustly_Data_JSONRPCRequest $request Data to send in the request * * @return string The URL path */ @@ -251,6 +255,8 @@ protected function urlPath($request=NULL) { /** * Quirks mode implementation of clearing all pending openssl error messages. + * + * @return void */ private function clearOpenSSLError() { /* Not really my favourite part of this library implementation. As @@ -303,7 +309,7 @@ protected function generateUUID() { * * @param Trustly_Data_JSONRPCRequest $request Outgoing request * - * @return Trustly_Data_JSONRPCSignedResponse Response from the API. + * @return Trustly_Data_JSONRPCResponse Response from the API. */ public function call($request) { $uuid = $request->getUUID(); @@ -437,7 +443,7 @@ public function call($request) { * in the previously used account being preselected * @param bool $requestKYC Flag to pass whether we request KYC check or not (Pay N Play feature) * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function deposit($notificationurl, $enduserid, $messageid, $locale=NULL, $amount=NULL, $currency=NULL, $country=NULL, @@ -515,10 +521,10 @@ public function deposit($notificationurl, $enduserid, $messageid, * @param float $amount The amount to refund the customer with exactly two * decimals. Only digits. Use dot (.) as decimal separator. * - * @param string currency The currency of the amount to refund the + * @param string $currency The currency of the amount to refund the * customer. * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function refund($orderid, $amount, $currency) { @@ -617,7 +623,7 @@ public function refund($orderid, $amount, $currency) { * * @param string $address The account holders address * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function withdraw($notificationurl, $enduserid, $messageid, $locale=NULL, $currency=NULL, $country=NULL, @@ -676,7 +682,7 @@ public function withdraw($notificationurl, $enduserid, $messageid, * * @param integer $orderid The OrderID of the withdrawal to approve. * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function approveWithdrawal($orderid) { @@ -699,7 +705,7 @@ public function approveWithdrawal($orderid) { * * @param integer $orderid The OrderID of the withdrawal to deny. * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function denyWithdrawal($orderid) { @@ -767,7 +773,7 @@ public function denyWithdrawal($orderid) { * @param boolean $requestdirectdebitmandate Initiate a direct debit * mandate request for the selected account. * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function selectAccount($notificationurl, $enduserid, $messageid, $locale=NULL, $country=NULL, $ip=NULL, $successurl=NULL, $urltarget=NULL, @@ -860,7 +866,7 @@ public function selectAccount($notificationurl, $enduserid, $messageid, * * @param string $address The account holders address * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function registerAccount($enduserid, $clearinghouse, $banknumber, $accountnumber, $firstname, $lastname, $mobilephone=NULL, @@ -935,7 +941,7 @@ public function registerAccount($enduserid, $clearinghouse, $banknumber, * system during development. Intead you can get you notifications on * https://test.trustly.com/notifications.html * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function accountPayout($notificationurl, $accountid, $enduserid, $messageid, $amount, $currency, $holdnotifications=NULL) { @@ -980,76 +986,80 @@ public function accountPayout($notificationurl, $accountid, $enduserid, * * @param string $messageid Your unique ID for the deposit. * - * @param string $locale The end-users localization preference in the + * @param ?string $locale The end-users localization preference in the * format [language[_territory]]. Language is the ISO 639-1 code and * territory the ISO 3166-1-alpha-2 code. * - * @param float $amount with exactly two decimals in the currency specified + * @param ?float $amount with exactly two decimals in the currency specified * by Currency. Do not use this attribute in combination with * SuggestedMinAmount or SuggestedMaxAmount. Only digits. Use dot (.) * as decimal separator. * - * @param string $currency The currency of the end-user's account in the + * @param ?string $currency The currency of the end-user's account in the * merchant's system. * - * @param string $country The ISO 3166-1-alpha-2 code of the end-user's + * @param ?string $country The ISO 3166-1-alpha-2 code of the end-user's * country. This will be used for preselecting the correct country for * the end-user in the iframe. * - * @param string $mobilephone The mobile phonenumber to the end-user in + * @param ?string $mobilephone The mobile phonenumber to the end-user in * international format. This is used for KYC and AML routines. * - * @param string $firstname The end-user's firstname. Useful for some banks + * @param ?string $firstname The end-user's firstname. Useful for some banks * for identifying transactions. * - * @param string $lastname The end-user's lastname. Useful for some banks + * @param ?string $lastname The end-user's lastname. Useful for some banks * for identifying transactions. * - * @param string $nationalidentificationnumber The end-user's social + * @param ?string $nationalidentificationnumber The end-user's social * security number / personal number / birth number / etc. Useful for * some banks for identifying transactions and KYC/AML. * - * @param string $shopperstatement The text to show on the end-user's bank + * @param ?string $shopperstatement The text to show on the end-user's bank * statement. * - * @param string $ip The IP-address of the end-user. + * @param ?string $ip The IP-address of the end-user. * - * @param string $successurl The URL to which the end-user should be + * @param ?string $successurl The URL to which the end-user should be * redirected after a successful deposit. Do not put any logic on that * page since it's not guaranteed that the end-user will in fact visit * it. * - * @param string $failurl The URL to which the end-user should be + * @param ?string $failurl The URL to which the end-user should be * redirected after a failed deposit. Do not put any logic on that * page since it's not guaranteed that the end-user will in fact visit * it. * - * @param string $templateurl The URL to your template page for the + * @param ?string $templateurl The URL to your template page for the * checkout process. * - * @param string $urltarget The html target/framename of the SuccessURL. + * @param ?string $urltarget The html target/framename of the SuccessURL. * Only _top, _self and _parent are suported. * - * @param float $suggestedminamount The minimum amount the end-user is + * @param ?float $suggestedminamount The minimum amount the end-user is * allowed to deposit in the currency specified by Currency. Only * digits. Use dot (.) as decimal separator. * - * @param float $suggestedmaxamount The maximum amount the end-user is + * @param ?float $suggestedmaxamount The maximum amount the end-user is * allowed to deposit in the currency specified by Currency. Only * digits. Use dot (.) as decimal separator. * - * @param string $integrationmodule Version information for your + * @param ?string $integrationmodule Version information for your * integration module. This is for informational purposes only and can * be useful when troubleshooting problems. Should contain enough * version information to be useful. * - * @param boolean $holdnotifications Do not deliver notifications for this + * @param ?boolean $holdnotifications Do not deliver notifications for this * order. This is a parameter available when using test.trustly.com * and can be used for manually delivering notifications to your local * system during development. Intead you can get you notifications on * https://test.trustly.com/notifications.html * - * @return Trustly_Data_JSONRPCSignedResponse + * @param mixed $authorizeonly + * + * @param mixed $templatedata + * + * @return Trustly_Data_JSONRPCResponse */ public function p2p($notificationurl,$enduserid, $messageid, $locale=NULL, $amount=NULL, $currency=NULL, $country=NULL, @@ -1115,7 +1125,7 @@ public function p2p($notificationurl,$enduserid, $messageid, * * @param integer $currency The currency of the amount * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function capture($orderid, $amount, $currency) { @@ -1142,7 +1152,7 @@ public function capture($orderid, $amount, $currency) { * * @param integer $orderid The OrderID of the deposit to void * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function void($orderid) { @@ -1186,10 +1196,10 @@ public function void($orderid) { * @param string $currency The currency of the end-user's account in the * merchant's system. * - * @param string $shopperstatement The text to show on the end-user's bank + * @param ?string $shopperstatement The text to show on the end-user's bank * statement. * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function charge($accountid, $notificationurl, $enduserid, $messageid, $amount, $currency, $shopperstatement=NULL) { @@ -1220,7 +1230,7 @@ public function charge($accountid, $notificationurl, $enduserid, $messageid, * * @param integer $orderid The OrderID of the order to query * - * @return Trustly_Data_JSONRPCSignedResponse + * @return Trustly_Data_JSONRPCResponse */ public function getWithdrawals($orderid) { diff --git a/Trustly/Api/unsigned.php b/Trustly/Api/unsigned.php index c62d6b9..def7776 100644 --- a/Trustly/Api/unsigned.php +++ b/Trustly/Api/unsigned.php @@ -41,16 +41,16 @@ class Trustly_Api_Unsigned extends Trustly_Api { * newSessionCookie after which the $session_uuid is used instead. * @var string */ - private $api_username = NULL; + private $api_username; /** * Login password when using the API. Used only in the first API call to * newSessionCookie after which the $session_uuid is used instead. * @var string */ - private $api_password = NULL; + private $api_password; /** * Session UUID used for authenticating calls. - * @var string + * @var ?string */ private $session_uuid = NULL; @@ -87,7 +87,7 @@ public function __construct($username, $password, $host='trustly.com', $port=443 * * See specific class implementing the call for more information. * - * @param Trustly_Data_JSONRPCRequest $request Data to send in the request + * @param ?Trustly_Data_JSONRPCRequest $request Data to send in the request * * @return string The URL path */ @@ -113,7 +113,7 @@ protected function urlPath($request=NULL) { */ protected function handleResponse($request, $body, $response_code) { /* No signature here, just build the response object */ - return new Trustly_Data_JSONRPCResponse($body, $response_code); + return new Trustly_Data_JSONRPCResponse($body); } @@ -141,7 +141,7 @@ public function insertCredentials($request) { * @return boolean indicating wether we have a sessionuuid */ protected function hasSessionUUID() { - return (bool)isset($this->session_uuid); + return isset($this->session_uuid); } @@ -163,10 +163,8 @@ public function newSessionCookie() { * missing session uuid here and call this function if it is not set */ $response = parent::call($request); - if(isset($response)) { - if($response->isSuccess()) { - $this->session_uuid = $response->getResult('sessionuuid'); - } + if($response->isSuccess()) { + $this->session_uuid = $response->getResult('sessionuuid'); } if(!isset($this->session_uuid)) { throw new Trustly_AuthentificationException(); @@ -181,11 +179,11 @@ public function newSessionCookie() { * * @param string $viewname Name of view * - * @param string $dateorder 'OLDER'|'NEVER' or NULL + * @param ?string $dateorder 'OLDER'|'NEVER' or NULL * - * @param string $datestamp Order used in relation with $dateorder + * @param ?string $datestamp Order used in relation with $dateorder * - * @param array $filterkeys Array of arrays of filters to apply to the data. + * @param ?array> $filterkeys Array of arrays of filters to apply to the data. * Arrays in the array consists of 1. Key name, 2. Key value, 3. * Operator, 4. Key value 2. Operator is one of 'NOT', 'BETWEEN' (in * which case Key value 2 must be set), 'LIKE', 'DECRYPTED', 'IN' @@ -195,11 +193,11 @@ public function newSessionCookie() { * * @param integer $offset Skip these many records in the start of the request * - * @param string $params Parameters for the view + * @param ?string $params Parameters for the view * - * @param string $sortby Column to sort by + * @param ?string $sortby Column to sort by * - * @param string $sortorder Sort order ASC or DESC + * @param ?string $sortorder Sort order ASC or DESC * * @return Trustly_Data_JSONRPCResponse Response from the API. * @@ -208,7 +206,8 @@ public function getViewStable($viewname, $dateorder=NULL, $datestamp=NULL, $filterkeys=NULL, $limit=100, $offset=0, $params=NULL, $sortby=NULL, $sortorder=NULL) { - return $this->call('GetViewStable', array( + $request = new Trustly_Data_JSONRPCRequest('GetViewStable'); + return $this->call($request, array( 'DateOrder' => $dateorder, 'Datestamp' => $datestamp, 'FilterKeys' => $filterkeys, @@ -229,15 +228,13 @@ public function getViewStable($viewname, $dateorder=NULL, $datestamp=NULL, * the outgoing call. Take care when supplying the arguments for the call * so they match the function prototype properly. * - * @param string $method API method to call + * @param Trustly_Data_JSONRPCRequest $request Outgoing request * - * @param Trustly_Data_JSONRPCRequest $params Outgoing call params + * @param ?array $params Outgoing call params * * @return Trustly_Data_JSONRPCResponse Response from the API. */ - public function call($method, $params=NULL) { - $request = new Trustly_Data_JSONRPCRequest($method); - + public function call($request, $params=NULL) { if(isset($params)) { foreach($params as $key => $value) { $request->setParam($key, $value); diff --git a/Trustly/Data/data.php b/Trustly/Data/data.php index a77274b..df0c510 100644 --- a/Trustly/Data/data.php +++ b/Trustly/Data/data.php @@ -37,26 +37,18 @@ class Trustly_Data { /** * Data payload - * @var array + * @var array */ - protected $payload = NULL; - - /** - * Constructur. - */ - public function __construct() { - $this->payload = array(); - } - + protected $payload = array(); /** * Utility function to vacuum the supplied data end remove unset * values. This is used to keep the requests cleaner rather then * supplying NULL values in the payload * - * @param array $data data to clean + * @param mixed $data data to clean * - * @return array cleaned data + * @return mixed cleaned data */ public function vacuum($data) { if(is_null($data)) { @@ -83,7 +75,7 @@ public function vacuum($data) { * Get the specific data value from the payload or the full payload if * no value is supplied * - * @param string $name The optional data parameter to get. If NULL then the + * @param ?string $name The optional data parameter to get. If NULL then the * entire payload will be returned. * * @return mixed value @@ -106,7 +98,7 @@ public function get($name=NULL) { * * @param string $str String to process * - * @return string UTF-8 variant of string + * @return ?string UTF-8 variant of string */ public static function ensureUTF8($str) { if($str == NULL) { @@ -128,6 +120,8 @@ public static function ensureUTF8($str) { * @param string $name * * @param mixed $value + * + * @return void */ public function set($name, $value) { $this->payload[$name] = Trustly_Data::ensureUTF8($value); @@ -158,7 +152,7 @@ public function pop($name) { * @param boolean $pretty Format the output in a prettified easy-to-read * formatting * - * @return string The current payload in JSON + * @return string|false The current payload in JSON */ public function json($pretty=FALSE) { if($pretty) { @@ -179,7 +173,9 @@ public function json($pretty=FALSE) { * pretty printer * * @param mixed $data Payload to sort. Will be sorted in place - * */ + * + * @return void + */ private function sortRecursive(&$data) { if(is_array($data)) { foreach($data as $k => $v) { diff --git a/Trustly/Data/jsonrpcnotificationrequest.php b/Trustly/Data/jsonrpcnotificationrequest.php index 6be3c5f..4134ab5 100644 --- a/Trustly/Data/jsonrpcnotificationrequest.php +++ b/Trustly/Data/jsonrpcnotificationrequest.php @@ -40,7 +40,7 @@ class Trustly_Data_JSONRPCNotificationRequest extends Trustly_Data { * The RAW incoming notification body * @var string */ - var $notification_body = NULL; + public $notification_body; /** @@ -52,11 +52,9 @@ class Trustly_Data_JSONRPCNotificationRequest extends Trustly_Data { * request seems to be valid but is for a JSON RPC version we do not * support. * - * @param string $notification RAW incoming notification body + * @param string $notification_body RAW incoming notification body */ public function __construct($notification_body) { - parent::__construct(); - $this->notification_body = $notification_body; if(empty($notification_body)) { @@ -84,7 +82,7 @@ public function __construct($notification_body) { /** * Get value from or the entire params payload. * - * @param string $name Name of the params parameter to obtain. Leave blank + * @param ?string $name Name of the params parameter to obtain. Leave blank * to get the entire payload * * @return mixed The value for the params parameter or the entire payload @@ -110,7 +108,7 @@ public function getParams($name=NULL) { * Get the value of a parameter in the params->data section of the * notification response. * - * @param string $name The name of the parameter. Leave as NULL to get the + * @param ?string $name The name of the parameter. Leave as NULL to get the * entire payload. * * @return mixed The value sought after or the entire payload depending on @@ -135,7 +133,7 @@ public function getData($name=NULL) { /** * Get the UUID from the request. * - * @return string The UUID value + * @return ?string The UUID value */ public function getUUID() { return $this->getParams('uuid'); @@ -145,7 +143,7 @@ public function getUUID() { /** * Get the Method from the request. * - * @return string The Method value. + * @return ?string The Method value. */ public function getMethod() { return $this->get('method'); @@ -155,7 +153,7 @@ public function getMethod() { /** * Get the Signature from the request. * - * @return string The Signature value. + * @return ?string The Signature value. */ public function getSignature() { return $this->getParams('signature'); @@ -165,7 +163,7 @@ public function getSignature() { /** * Get the JSON RPC version from the request. * - * @return string The Version. + * @return ?string The Version. */ public function getVersion() { return $this->get('version'); diff --git a/Trustly/Data/jsonrpcnotificationresponse.php b/Trustly/Data/jsonrpcnotificationresponse.php index 87894a8..57d4afe 100644 --- a/Trustly/Data/jsonrpcnotificationresponse.php +++ b/Trustly/Data/jsonrpcnotificationresponse.php @@ -42,13 +42,10 @@ class Trustly_Data_JSONRPCNotificationResponse extends Trustly_Data { * @param Trustly_Data_JSONRPCNotificationRequest $request Incoming * notification request to which we are responding * - * @param boolean $success Set to true to indicate that the notification + * @param ?boolean $success Set to true to indicate that the notification * was successfully processed. */ public function __construct($request, $success=NULL) { - - parent::__construct(); - $uuid = $request->getUUID(); $method = $request->getMethod(); @@ -70,10 +67,10 @@ public function __construct($request, $success=NULL) { /** * Set the success status in the response. * - * @param boolean $success Set to true to indicate that the notification + * @param ?boolean $success Set to true to indicate that the notification * was successfully processed. * - * @return $success + * @return ?boolean $success */ public function setSuccess($success=NULL) { $status = 'OK'; @@ -91,7 +88,7 @@ public function setSuccess($success=NULL) { * * @param string $signature Signature of the outgoing data. * - * @return string $signature + * @return void */ public function setSignature($signature) { $this->setResult('signature', $signature); @@ -120,7 +117,7 @@ public function setResult($name, $value) { * Get the value of a parameter in the result section of the notification * response. * - * @param string $name The name of the parameter. Leave as NULL to get the + * @param ?string $name The name of the parameter. Leave as NULL to get the * entire payload. * * @return mixed The value sought after or the entire payload depending on @@ -148,7 +145,7 @@ public function getResult($name=NULL) { * Get the value of a parameter in the result->data section of the * notification response. * - * @param string $name The name of the parameter. Leave as NULL to get the + * @param ?string $name The name of the parameter. Leave as NULL to get the * entire payload. * * @return mixed The value sought after or the entire payload depending on @@ -198,7 +195,7 @@ public function setData($name, $value) { /** * Get the Method value from the response. * - * @return string The Method value. + * @return ?string The Method value. */ public function getMethod() { return $this->getResult('method'); @@ -208,7 +205,7 @@ public function getMethod() { /** * Get the UUID value from the response. * - * @return string The UUID value + * @return ?string The UUID value */ public function getUUID() { return $this->getResult('uuid'); diff --git a/Trustly/Data/jsonrpcrequest.php b/Trustly/Data/jsonrpcrequest.php index 2b10f9a..57c481e 100644 --- a/Trustly/Data/jsonrpcrequest.php +++ b/Trustly/Data/jsonrpcrequest.php @@ -41,7 +41,7 @@ class Trustly_Data_JSONRPCRequest extends Trustly_Data_Request { * @throws Trustly_DataException If the combination of $data and * $attributes is invalid * - * @param string $method Outgoing call API method + * @param ?string $method Outgoing call API method * * @param mixed $data Outputgoing call Data (if any). This can be either an * array or a simple non-complex value. @@ -149,7 +149,7 @@ public function setUUID($uuid) { /** * Get the UUID value from the outgoing call. * - * @return string The UUID value + * @return ?string The UUID value */ public function getUUID() { if(isset($this->payload['params']['UUID'])) { @@ -163,17 +163,17 @@ public function getUUID() { * * @param string $method The name of the API method this call is for * - * @return string $method + * @return void */ public function setMethod($method) { - return $this->set('method', $method); + $this->set('method', $method); } /** * Get the Method value from the outgoing call. * - * @return string The Method value. + * @return ?string The Method value. */ public function getMethod() { return $this->get('method'); @@ -202,7 +202,7 @@ public function setData($name, $value) { * Get the value of one parameter in the params->Data section of the * request. Or the entire Data section if no name is given. * - * @param string $name Name of the Data param to obtain. Leave as NULL to + * @param ?string $name Name of the Data param to obtain. Leave as NULL to * get the entire structure. * * @return mixed The value or the entire Data depending on $name diff --git a/Trustly/Data/jsonrpcresponse.php b/Trustly/Data/jsonrpcresponse.php index 98a2c4c..a53a72e 100644 --- a/Trustly/Data/jsonrpcresponse.php +++ b/Trustly/Data/jsonrpcresponse.php @@ -78,7 +78,7 @@ public function __construct($response_body) { /** * Get error code (if any) from the API response * - * @return integer The error code (numerical) + * @return ?integer The error code (numerical) */ public function getErrorCode() { if($this->isError() && isset($this->response_result['code'])) { @@ -90,7 +90,7 @@ public function getErrorCode() { /** * Get error message (if any) from the API response * - * @return string The error message + * @return ?string The error message */ public function getErrorMessage() { if($this->isError() && isset($this->response_result['message'])) { diff --git a/Trustly/Data/jsonrpcsignedresponse.php b/Trustly/Data/jsonrpcsignedresponse.php index 4b16b02..5ff0d1d 100644 --- a/Trustly/Data/jsonrpcsignedresponse.php +++ b/Trustly/Data/jsonrpcsignedresponse.php @@ -88,7 +88,7 @@ public function __construct($response_body) { /** * Get data from the data section of the response * - * @param string $name Name of the data parameter to fetch. NULL value will + * @param ?string $name Name of the data parameter to fetch. NULL value will * return entire data section. * * @return mixed The value for parameter $name or the entire data block if @@ -116,7 +116,7 @@ public function getData($name=NULL) { /** * Get error code (if any) from the API call * - * @return integer The error code (numerical) + * @return ?integer The error code (numerical) */ public function getErrorCode() { if($this->isError() && isset($this->response_result['data']['code'])) { @@ -129,7 +129,7 @@ public function getErrorCode() { /** * Get error message (if any) from the API call * - * @return string The error message + * @return ?string The error message */ public function getErrorMessage() { if($this->isError() && isset($this->response_result['data']['message'])) { diff --git a/Trustly/Data/request.php b/Trustly/Data/request.php index 3daaca7..817b91f 100644 --- a/Trustly/Data/request.php +++ b/Trustly/Data/request.php @@ -36,20 +36,18 @@ class Trustly_Data_Request extends Trustly_Data { /** * Call method name - * @var string + * @var ?string */ - var $method = NULL; + public $method = NULL; /** * Constructor. * - * @param string $method Method name for the call + * @param ?string $method Method name for the call * - * @param array $payload Call payload + * @param ?array $payload Call payload */ public function __construct($method=NULL, $payload=NULL) { - parent::__construct(); - $vpayload = $this->vacuum($payload); if(isset($vpayload)) { $this->payload = $vpayload; @@ -62,7 +60,7 @@ public function __construct($method=NULL, $payload=NULL) { /** * Convenience function for getting the uuid from the call * - * @return string uuid + * @return ?string uuid */ public function getUUID() { if(isset($this->payload['uuid'])) { @@ -75,7 +73,9 @@ public function getUUID() { /** * Convenience function for setting the uuid in the call * - * @param string uuid + * @param string $uuid + * + * @return void */ public function setUUID($uuid) { $this->set('uuid', $uuid); @@ -85,7 +85,7 @@ public function setUUID($uuid) { /** * Get the method in the outgoing call * - * @return string method name + * @return ?string method name */ public function getMethod() { return $this->method; @@ -95,7 +95,9 @@ public function getMethod() { /** * Set the medhod in the call * - * @param string method name + * @param string $method method name + * + * @return void */ public function setMethod($method) { $this->method = $method; diff --git a/Trustly/Data/response.php b/Trustly/Data/response.php index 0f4b97b..a19ab85 100644 --- a/Trustly/Data/response.php +++ b/Trustly/Data/response.php @@ -37,21 +37,21 @@ class Trustly_Data_Response extends Trustly_Data { /** * Raw copy of the incoming response body - * @var integer + * @var ?string */ - var $response_body = NULL; + public $response_body = NULL; /** * The response HTTP code - * @var integer + * @var ?integer */ - var $response_code = NULL; + public $response_code = NULL; /** * Shortcut to the part of the result being actually interesting. The guts will contain all returned data. * @var mixed */ - var $response_result = NULL; + public $response_result = NULL; /** @@ -64,11 +64,9 @@ class Trustly_Data_Response extends Trustly_Data { * * @param string $response_body RAW response body from the API call * - * @param integer $response_code HTTP response code from the API call + * @param ?integer $response_code HTTP response code from the API call */ public function __construct($response_body, $response_code=NULL) { - parent::__construct(); - $this->response_code = $response_code; $this->response_body = $response_body; @@ -135,7 +133,7 @@ public function isSuccess() { /** * Get error message (if any) from the API response * - * @return string The error message + * @return ?string The error message */ public function getErrorMessage() { if($this->isError()) { @@ -150,7 +148,7 @@ public function getErrorMessage() { /** * Get error code (if any) from the API response * - * @return integer The error code (numerical) + * @return ?integer The error code (numerical) */ public function getErrorCode() { if($this->isError()) { @@ -165,7 +163,7 @@ public function getErrorCode() { /** * Get data from the result section of the response * - * @param string $name Name of the result parameter to fetch. NULL value + * @param ?string $name Name of the result parameter to fetch. NULL value * will return entire result section. * * @return mixed The value for parameter $name or the entire result block @@ -188,7 +186,7 @@ public function getResult($name=NULL) { /** * Convenience function for getting the uuid in the response * - * @param string uuid + * @return ?string uuid */ public function getUUID() { if(isset($this->response_result['uuid'])) { @@ -201,7 +199,7 @@ public function getUUID() { /** * Get the method from the response * - * @return string method name + * @return ?string method name */ public function getMethod() { if(isset($this->response_result['method'])) { @@ -214,7 +212,7 @@ public function getMethod() { /** * Get the signature from the response * - * @return string signature + * @return ?string signature */ public function getSignature() { if(isset($this->response_result['signature'])) { diff --git a/Trustly/exceptions.php b/Trustly/exceptions.php index e1a9724..f4fa489 100644 --- a/Trustly/exceptions.php +++ b/Trustly/exceptions.php @@ -47,13 +47,15 @@ class Trustly_JSONRPCVersionException extends Exception { } * indication that message contents are being tampered with. */ class Trustly_SignatureException extends Exception { + /** @var mixed */ + public $signature_data = NULL; /** * Constructor * * @param string $message Exception message * - * @param array $data Data that was signed with an invalid signature + * @param mixed $data Data that was signed with an invalid signature */ public function __construct($message, $data=NULL) { parent::__construct($message); @@ -65,6 +67,8 @@ public function __construct($message, $data=NULL) { * Get the data that had an invalid signature. This is the only way to get * data from anything with a bad signature. This should be used for * DEBUGGING ONLY. You should NEVER rely on the contents. + * + * @return mixed */ public function getBadData() { return $this->signature_data; diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..85237fc --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: 8 + paths: + - ./Trustly/