Skip to content

Commit

Permalink
refactor: indents & initial values
Browse files Browse the repository at this point in the history
  • Loading branch information
omalizadeh committed Sep 27, 2021
1 parent 1f93dfc commit 4f3e640
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
40 changes: 24 additions & 16 deletions src/Drivers/Novin/Novin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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']));
}
Expand All @@ -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
);
Expand Down Expand Up @@ -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
Expand All @@ -241,7 +249,7 @@ protected function getPaymentUrl(): string

protected function getVerificationUrl(): string
{
return $this->getBaseRestServiceUrl() . 'verifyMerchantTrans/';
return $this->getBaseRestServiceUrl().'verifyMerchantTrans/';
}

private function getBaseRestServiceUrl(): string
Expand All @@ -264,23 +272,23 @@ 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;
}

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';
}
}
43 changes: 22 additions & 21 deletions src/Drivers/Pasargad/Helpers/RSA.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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++;
}
Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Drivers/Pasargad/Helpers/RSAProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down

0 comments on commit 4f3e640

Please sign in to comment.