From 8daec91def748f342960b4a961d1b25165a0da54 Mon Sep 17 00:00:00 2001 From: Ariful Islam Date: Sat, 8 Apr 2023 00:04:36 +0600 Subject: [PATCH] Fix: Code Reformatted, remove unwanted namespace and added method return type --- src/Base.php | 12 ++++++------ src/Helper.php | 27 ++++++++++++-------------- src/RequestHandler.php | 19 +++++++++--------- src/Test/Functionality/FeatureTest.php | 1 - 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/Base.php b/src/Base.php index 6d952e1..841b7ab 100644 --- a/src/Base.php +++ b/src/Base.php @@ -120,7 +120,7 @@ public function payNowWithoutRedirection(Base $base) * @return string * @since v1.3.1 */ - public function getTimezone() + public function getTimezone(): string { return $this->timezone; } @@ -170,7 +170,7 @@ public function getMerchantID() * @return string * @since v1.3.1 */ - public function getBaseUrl() + public function getBaseUrl(): string { return $this->base_url; } @@ -183,7 +183,7 @@ public function getBaseUrl() public function verifyPayment($paymentRefId) { $url = $this->base_url . 'verify/payment/' . $paymentRefId; - return Helper::HttpGet($url); + return Helper::httpGet($url); } /** @@ -200,13 +200,13 @@ private function checkParams($config, $params) } if (!array_key_exists('amount', $params)) { - throw new NagadPaymentException("Array key amount missing. Check array format from readme file"); + throw new NagadPaymentException("Array key amount missing. Check configuration array format from github repository's readme.md file"); } if (!array_key_exists('invoice', $params)) { - throw new NagadPaymentException("Array key invoice missing. Check array format from readme file"); + throw new NagadPaymentException("Array key invoice missing. Check configuration array format from github repository's readme.md file"); } if (!array_key_exists('merchantCallback', $params)) { - throw new NagadPaymentException("Array key merchantCallback missing. Check array format from readme file"); + throw new NagadPaymentException("Array key merchantCallback missing. Check configuration array format from github repository's readme.md file"); } } diff --git a/src/Helper.php b/src/Helper.php index 57ac422..f7a7f54 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -14,7 +14,6 @@ use Exception; -use GuzzleHttp\Exception\GuzzleException; use RuntimeException; use Xenon\NagadApi\Exception\ExceptionHandler; use Xenon\NagadApi\Exception\NagadPaymentException; @@ -31,7 +30,6 @@ class Helper extends Key */ public function __construct($config) { - parent::__construct($config); } @@ -43,7 +41,7 @@ public function __construct($config) * @return string * @since v1.3.1 */ - public static function generateRandomString(int $length = 40, string $prefix = '', string $suffix = '') + public static function generateRandomString(int $length = 40, string $prefix = '', string $suffix = ''): string { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); @@ -68,9 +66,8 @@ public static function generateRandomString(int $length = 40, string $prefix = ' * @throws ExceptionHandler * @since v1.3.1 */ - function EncryptDataWithPublicKey($data) + public function encryptDataWithPublicKey($data): string { - $publicKey = "-----BEGIN PUBLIC KEY-----\n" . $this->getPgPublicKey() . "\n-----END PUBLIC KEY-----"; $keyResource = openssl_get_publickey($publicKey); $status = openssl_public_encrypt($data, $cryptoText, $keyResource); @@ -88,7 +85,7 @@ function EncryptDataWithPublicKey($data) * @throws ExceptionHandler * @since v1.3.1 */ - public function SignatureGenerate($data) + public function signatureGenerate($data): string { $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->getMerchantPrivateKey() . "\n-----END RSA PRIVATE KEY-----"; $status = openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256); @@ -106,7 +103,7 @@ public function SignatureGenerate($data) * @return array|mixed * @since v1.3.1 */ - public function HttpPostMethod(string $postUrl, array $postData) + public function httpPostMethod(string $postUrl, array $postData) { $url = curl_init($postUrl); $postToken = json_encode($postData); @@ -133,7 +130,7 @@ public function HttpPostMethod(string $postUrl, array $postData) ]; } - $response = json_decode($resultData, true, 512); + $response = json_decode($resultData, true); curl_close($url); return $response; @@ -146,7 +143,7 @@ public function HttpPostMethod(string $postUrl, array $postData) * @return bool|string * @throws Exception */ - public static function HttpGet($url) + public static function httpGet($url) { $ch = curl_init(); $timeout = 10; @@ -196,10 +193,10 @@ public function getClientIP() /** * @param $cryptoText - * @return mixed + * @return string * @since v1.3.1 */ - public function DecryptDataWithPrivateKey($cryptoText) + public function decryptDataWithPrivateKey($cryptoText) { $private_key = "-----BEGIN RSA PRIVATE KEY-----\n" . $this->getMerchantPrivateKey() . "\n-----END RSA PRIVATE KEY-----"; openssl_private_decrypt(base64_decode($cryptoText), $plain_text, $private_key); @@ -217,7 +214,7 @@ public function DecryptDataWithPrivateKey($cryptoText) * @return string * @since v1.3.1 */ - public static function generateFakeInvoice($length = 20, $capitalize = false, $prefix = '', $suffix = '') + public static function generateFakeInvoice(int $length = 20, bool $capitalize = false, string $prefix = '', string $suffix = ''): string { $invoice = $prefix . self::generateRandomString($length) . $suffix; if ($capitalize === true) { @@ -256,7 +253,7 @@ public static function errorLog($data) * @return array * @since v1.3.1 */ - public static function serverDetails() + public static function serverDetails(): array { return [ 'base' => $_SERVER['SERVER_ADDR'], @@ -270,7 +267,7 @@ public static function serverDetails() /** * This is for formatting and getting returning response data from url; * @param $response - * @return mixed + * @return array * @since v1.3.1 */ public static function successResponse($response) @@ -297,7 +294,7 @@ public function verifyPayment($paymentRefId) $url = $this->base_url . 'verify/payment/' . $paymentRefId; - return self::HttpGet($url); + return self::httpGet($url); } } \ No newline at end of file diff --git a/src/RequestHandler.php b/src/RequestHandler.php index 269a7a5..bb75bef 100644 --- a/src/RequestHandler.php +++ b/src/RequestHandler.php @@ -77,7 +77,7 @@ public function sendRequest(bool $redirection = true) try { - $publicSignature = $this->helper->EncryptDataWithPublicKey(json_encode($sensitiveData)); + $publicSignature = $this->helper->encryptDataWithPublicKey(json_encode($sensitiveData)); } catch (Exception $e) { throw new ExceptionHandler($e->getMessage()); @@ -85,7 +85,7 @@ public function sendRequest(bool $redirection = true) } try { - $signature = $this->helper->SignatureGenerate(json_encode($sensitiveData)); + $signature = $this->helper->signatureGenerate(json_encode($sensitiveData)); } catch (Exception $e) { throw new ExceptionHandler($e->getMessage()); @@ -98,10 +98,10 @@ public function sendRequest(bool $redirection = true) 'signature' => $signature ); - $resultData = $this->helper->HttpPostMethod($postUrl, $postData); + $resultData = $this->helper->httpPostMethod($postUrl, $postData); if (!is_array($resultData)) { - throw new ExceptionHandler("Failed to generate nagad payment url as it is returning null response from nagad api server. Please be confirm that you have whitelisted your server ip or fix other server ip related issue. Sometimes it happens if you take server from outside Bangladesh. Contact with Nagad authority for assistance with your support ticket id"); + throw new ExceptionHandler("Failed to generate nagad payment url as it is returning null response from nagad api server. Please be confirm that you have whitelisted your server ip or fix other server ip related issue. Sometimes it happens if your hosting server is located outside Bangladesh. Contact with Nagad authority for assistance with your support ticket id"); } if (array_key_exists('error', $resultData)) { @@ -110,8 +110,7 @@ public function sendRequest(bool $redirection = true) if (array_key_exists('reason', $resultData)) { - throw new ExceptionHandler($resultData['reason'] . ', ' . $resultData['message']); - + throw new ExceptionHandler( $resultData['message']); } @@ -119,7 +118,7 @@ public function sendRequest(bool $redirection = true) if (array_key_exists('sensitiveData', $resultData) && array_key_exists('signature', $resultData)) { if (!empty($resultData['sensitiveData']) && !empty($resultData['signature'])) { - $plainResponse = json_decode($this->helper->DecryptDataWithPrivateKey($resultData['sensitiveData']), true); + $plainResponse = json_decode($this->helper->decryptDataWithPrivateKey($resultData['sensitiveData']), true); if (isset($plainResponse['paymentReferenceId'], $plainResponse['challenge'])) { $paymentReferenceId = $plainResponse['paymentReferenceId']; @@ -134,15 +133,15 @@ public function sendRequest(bool $redirection = true) ); $postDataOrder = array( - 'sensitiveData' => $this->helper->EncryptDataWithPublicKey(json_encode($sensitiveDataOrder)), - 'signature' => $this->helper->SignatureGenerate(json_encode($sensitiveDataOrder)), + 'sensitiveData' => $this->helper->encryptDataWithPublicKey(json_encode($sensitiveDataOrder)), + 'signature' => $this->helper->signatureGenerate(json_encode($sensitiveDataOrder)), 'merchantCallbackURL' => $this->base->merchantCallback, ); $OrderSubmitUrl = $this->base->getBaseUrl() . "check-out/complete/" . $paymentReferenceId; - $resultDataOrder = $this->helper->HttpPostMethod($OrderSubmitUrl, $postDataOrder); + $resultDataOrder = $this->helper->httpPostMethod($OrderSubmitUrl, $postDataOrder); if (array_key_exists('status', $resultDataOrder)) { diff --git a/src/Test/Functionality/FeatureTest.php b/src/Test/Functionality/FeatureTest.php index 0a8f67c..58c361e 100644 --- a/src/Test/Functionality/FeatureTest.php +++ b/src/Test/Functionality/FeatureTest.php @@ -13,7 +13,6 @@ class FeatureTest extends TestCase */ public function testTrueAssetsToTrue() { - $condition = true; $this->assertTrue(true); }