Last active
July 8, 2024 12:01
-
-
Save dmitry-udod/b5a07d1418aa1e018c4bced2c93bc128 to your computer and use it in GitHub Desktop.
Валидация банковских данных. соответствие БИК и расчетного счета (корреспондентского) банка (Laravel). Bank data validation bik and accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class CheckBankAccount implements Rule | |
{ | |
public $bikForTest = ''; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$bik = request('bik'); | |
if (app()->runningUnitTests()) { | |
$bik = $this->bikForTest; | |
} | |
$accString = substr($bik, strlen($bik) - 3, 3) . $value; | |
return self::checkAccString($accString); | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'Несоответствие БИК и расчетного счета'; | |
} | |
/** | |
* @param $accString | |
* @return bool | |
*/ | |
public static function checkAccString($accString) | |
{ | |
$mask = [7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1]; | |
$checkSum = 0; | |
for ($i = 0; $i <= 22; $i++) { | |
//var_Dump($mask[$i]); | |
$checkSum += ((int)(substr($accString, $i, 1)) * $mask[$i]) % 10; | |
} | |
return $checkSum % 10 == 0; | |
} | |
} | |
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class CheckCorrBankAccount implements Rule | |
{ | |
public $bikForTest = ''; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$bik = request('bik'); | |
if (app()->runningUnitTests()) { | |
$bik = $this->bikForTest; | |
} | |
$accString = '0' . substr($bik, 4, 2) . $value; | |
return CheckBankAccount::checkAccString($accString); | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return 'Несоответствие БИК и расчетного счета'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment