diff --git a/src/Drivers/Novin/Novin.php b/src/Drivers/Novin/Novin.php index 4d3b622..d4626f9 100644 --- a/src/Drivers/Novin/Novin.php +++ b/src/Drivers/Novin/Novin.php @@ -14,20 +14,23 @@ class Novin extends Driver { - const BANK_BUY_TRANSACTION_TYPE = 'EN_GOODS'; + protected const BANK_BUY_TRANSACTION_TYPE = 'EN_GOODS'; private ?string $sessionId = null; public function purchase(): string { $response = $this->callApi($this->getLoginUrl(), $this->getLoginData()); + if ($response['Result'] == $this->getSuccessResponseStatusCode()) { $this->sessionId = $response['SessionId']; } else { throw new PurchaseFailedException($this->getStatusMessage($response['Result'])); } + $purchaseData = $this->getPurchaseData(); $response = $this->callApi($this->getPurchaseUrl(), $purchaseData); + if ($response['Result'] == $this->getSuccessResponseStatusCode()) { $dataToSign = $response['DataToSign']; $dataUniqueId = $response['UniqueId']; @@ -55,24 +58,29 @@ public function pay(): RedirectionForm 'Token' => $this->getInvoice()->getToken(), 'Language' => $this->getLanguage() ]; - $payUrl .= ('?token=' . $data['Token'] . '&language=' . $data['Language']); + $payUrl .= ('?token='.$data['Token'].'&language='.$data['Language']); return $this->redirect($payUrl, $data); } public function verify(): Receipt { - if (!empty(request('State')) and strtoupper(request('State')) !== 'OK') { + if (!empty(request('State')) && strtoupper(request('State')) !== 'OK') { throw new PaymentFailedException('کاربر از انجام تراکنش منصرف شده است.'); } $verificationData = $this->getVerificationData(); $response = $this->callApi($this->getVerificationUrl(), $verificationData); - if ($response['Result'] == $this->getSuccessResponseStatusCode() and $response['Amount'] == $this->getInvoice()->getAmount()) { + if ($response['Result'] == $this->getSuccessResponseStatusCode() && $response['Amount'] == $this->getInvoice()->getAmount()) { $this->getInvoice()->setTransactionId(request('RefNum')); $this->getInvoice()->setInvoiceId(request('ResNum')); - return new Receipt($this->getInvoice(), request('TraceNo'), request('CustomerRefNum'), request('CardMaskPan')); + return new Receipt( + $this->getInvoice(), + request('TraceNo'), + request('CustomerRefNum'), + request('CardMaskPan') + ); } throw new PaymentFailedException($this->getStatusMessage($response['Result'])); } @@ -90,8 +98,8 @@ private function getSignature(string $dataToSign): string openssl_pkcs7_sign( $this->getUnsignedDataFilePath(), $this->getSignedDataFilePath(), - 'file://' . $this->settings['certificate_path'], - array('file://' . $this->settings['certificate_path'], $this->settings['certificate_password']), + 'file://'.$this->settings['certificate_path'], + array('file://'.$this->settings['certificate_path'], $this->settings['certificate_password']), array(), PKCS7_NOSIGS ); @@ -221,17 +229,17 @@ protected function getSuccessResponseStatusCode(): string protected function getLoginUrl(): string { - return $this->getBaseRestServiceUrl() . 'merchantLogin/'; + return $this->getBaseRestServiceUrl().'merchantLogin/'; } protected function getPurchaseUrl(): string { - return $this->getBaseRestServiceUrl() . 'generateTransactionDataToSign/'; + return $this->getBaseRestServiceUrl().'generateTransactionDataToSign/'; } protected function getTokenGenerationUrl(): string { - return $this->getBaseRestServiceUrl() . 'generateSignedDataToken/'; + return $this->getBaseRestServiceUrl().'generateSignedDataToken/'; } protected function getPaymentUrl(): string @@ -241,7 +249,7 @@ protected function getPaymentUrl(): string protected function getVerificationUrl(): string { - return $this->getBaseRestServiceUrl() . 'verifyMerchantTrans/'; + return $this->getBaseRestServiceUrl().'verifyMerchantTrans/'; } private function getBaseRestServiceUrl(): string @@ -264,11 +272,11 @@ private function getLanguage(): string private function checkPhoneNumberFormat(string $phoneNumber): string { - if (strlen($phoneNumber) == 12 and Str::startsWith($phoneNumber, '98')) { + if (strlen($phoneNumber) === 12 && Str::startsWith($phoneNumber, '98')) { return Str::replaceFirst('98', '0', $phoneNumber); } - if (strlen($phoneNumber) == 10 and Str::startsWith($phoneNumber, '9')) { - return '0' . $phoneNumber; + if (strlen($phoneNumber) === 10 && Str::startsWith($phoneNumber, '9')) { + return '0'.$phoneNumber; } return $phoneNumber; @@ -276,11 +284,11 @@ private function checkPhoneNumberFormat(string $phoneNumber): string private function getUnsignedDataFilePath(): string { - return $this->settings['temp_files_dir'] . 'unsigned.txt'; + return $this->settings['temp_files_dir'].'unsigned.txt'; } private function getSignedDataFilePath(): string { - return $this->settings['temp_files_dir'] . 'signed.txt'; + return $this->settings['temp_files_dir'].'signed.txt'; } } diff --git a/src/Drivers/Pasargad/Helpers/RSA.php b/src/Drivers/Pasargad/Helpers/RSA.php index 8c5c2bd..1e42a2a 100644 --- a/src/Drivers/Pasargad/Helpers/RSA.php +++ b/src/Drivers/Pasargad/Helpers/RSA.php @@ -8,41 +8,41 @@ class RSA public static function rsaEncrypt($message, $publicKey, $modulus, $keylength) { - $padded = RSA::addPKCS1Padding($message, true, $keylength / 8); - $number = RSA::binaryToNumber($padded); - $encrypted = RSA::powMod($number, $publicKey, $modulus); - $result = RSA::numberToBinary($encrypted, $keylength / 8); - return $result; + $padded = self::addPKCS1Padding($message, true, $keylength / 8); + $number = self::binaryToNumber($padded); + $encrypted = self::powMod($number, $publicKey, $modulus); + + return self::numberToBinary($encrypted, $keylength / 8); } public static function rsaDecrypt($message, $privateKey, $modulus, $keylength) { - $number = RSA::binaryToNumber($message); - $decrypted = RSA::powMod($number, $privateKey, $modulus); - $result = RSA::numberToBinary($decrypted, $keylength / 8); - return RSA::removePKCS1Padding($result, $keylength / 8); + $number = self::binaryToNumber($message); + $decrypted = self::powMod($number, $privateKey, $modulus); + $result = self::numberToBinary($decrypted, $keylength / 8); + return self::removePKCS1Padding($result, $keylength / 8); } public static function rsaSign($message, $privateKey, $modulus, $keylength) { - $padded = RSA::addPKCS1Padding($message, false, $keylength / 8); - $number = RSA::binaryToNumber($padded); - $signed = RSA::powMod($number, $privateKey, $modulus); - $result = RSA::numberToBinary($signed, $keylength / 8); - return $result; + $padded = self::addPKCS1Padding($message, false, $keylength / 8); + $number = self::binaryToNumber($padded); + $signed = self::powMod($number, $privateKey, $modulus); + + return self::numberToBinary($signed, $keylength / 8); } public static function rsaVerify($message, $publicKey, $modulus, $keylength) { - return RSA::rsaDecrypt($message, $publicKey, $modulus, $keylength); + return self::rsaDecrypt($message, $publicKey, $modulus, $keylength); } public static function rsaKypVerify($message, $publicKey, $modulus, $keylength) { - $number = RSA::binaryToNumber($message); - $decrypted = RSA::powMod($number, $publicKey, $modulus); - $result = RSA::numberToBinary($decrypted, $keylength / 8); - return RSA::removeKYPPadding($result, $keylength / 8); + $number = self::binaryToNumber($message); + $decrypted = self::powMod($number, $publicKey, $modulus); + $result = self::numberToBinary($decrypted, $keylength / 8); + return self::removeKYPPadding($result, $keylength / 8); } public static function powMod($p, $q, $r) @@ -54,7 +54,7 @@ public static function powMod($p, $q, $r) $rem = bcmod($div, 2); $div = bcdiv($div, 2); if ($rem) { - array_push($factors, $powerOfTwo); + $factors[] = $powerOfTwo; } $powerOfTwo++; } @@ -67,13 +67,14 @@ public static function powMod($p, $q, $r) $partRes = bcmod($partRes, $r); $idx++; } - array_push($partialResults, $partRes); + $partialResults[] = $partRes; } $result = "1"; foreach ($partialResults as $partRes) { $result = bcmul($result, $partRes); $result = bcmod($result, $r); } + return $result; } diff --git a/src/Drivers/Pasargad/Helpers/RSAProcessor.php b/src/Drivers/Pasargad/Helpers/RSAProcessor.php index 0aa42d5..bbf3018 100644 --- a/src/Drivers/Pasargad/Helpers/RSAProcessor.php +++ b/src/Drivers/Pasargad/Helpers/RSAProcessor.php @@ -7,10 +7,10 @@ class RSAProcessor public const KEY_TYPE_XML_FILE = 'xml_file'; public const KEY_TYPE_XML_STRING = 'xml_string'; - private $publicKey = null; - private $privateKey = null; - private $modulus = null; - private $keyLength = "1024"; + private $publicKey; + private $privateKey; + private $modulus; + private $keyLength; public function __construct($key, $keyType = null) { @@ -20,7 +20,7 @@ public function __construct($key, $keyType = null) if ($keyType == null || $keyType == self::KEY_TYPE_XML_STRING) { $xmlObject = simplexml_load_string($key); } elseif ($keyType == self::KEY_TYPE_XML_FILE) { - $xmlObject = simplexml_load_file($key); + $xmlObject = simplexml_load_string(file_get_contents($key)); } $this->modulus = RSA::binaryToNumber(base64_decode($xmlObject->Modulus));