Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Валидация банковских данных. соответствие БИК и расчетного счета (корреспондентского) банка (Laravel). Bank data validation bik and accounts
<?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
You can’t perform that action at this time.