Skip to content

Commit

Permalink
Fix: Code Reformatted, remove unwanted namespace and added method ret…
Browse files Browse the repository at this point in the history
…urn type
  • Loading branch information
arif98741 committed Apr 7, 2023
1 parent c0de8c4 commit 8daec91
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
12 changes: 6 additions & 6 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public function getMerchantID()
* @return string
* @since v1.3.1
*/
public function getBaseUrl()
public function getBaseUrl(): string
{
return $this->base_url;
}
Expand All @@ -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);
}

/**
Expand All @@ -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");
}
}

Expand Down
27 changes: 12 additions & 15 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


use Exception;
use GuzzleHttp\Exception\GuzzleException;
use RuntimeException;
use Xenon\NagadApi\Exception\ExceptionHandler;
use Xenon\NagadApi\Exception\NagadPaymentException;
Expand All @@ -31,7 +30,6 @@ class Helper extends Key
*/
public function __construct($config)
{

parent::__construct($config);
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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'],
Expand All @@ -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)
Expand All @@ -297,7 +294,7 @@ public function verifyPayment($paymentRefId)

$url = $this->base_url . 'verify/payment/' . $paymentRefId;

return self::HttpGet($url);
return self::httpGet($url);
}

}
19 changes: 9 additions & 10 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ 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());

}

try {
$signature = $this->helper->SignatureGenerate(json_encode($sensitiveData));
$signature = $this->helper->signatureGenerate(json_encode($sensitiveData));
} catch (Exception $e) {
throw new ExceptionHandler($e->getMessage());

Expand All @@ -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)) {
Expand All @@ -110,16 +110,15 @@ public function sendRequest(bool $redirection = true)

if (array_key_exists('reason', $resultData)) {

throw new ExceptionHandler($resultData['reason'] . ', ' . $resultData['message']);

throw new ExceptionHandler( $resultData['message']);
}


//check existence of sensitiveData and signature
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'];
Expand All @@ -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)) {

Expand Down
1 change: 0 additions & 1 deletion src/Test/Functionality/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class FeatureTest extends TestCase
*/
public function testTrueAssetsToTrue()
{
$condition = true;
$this->assertTrue(true);
}

Expand Down

0 comments on commit 8daec91

Please sign in to comment.