Skip to content

Instantly share code, notes, and snippets.

@dmitry-udod
Last active April 24, 2019 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry-udod/b5a07d1418aa1e018c4bced2c93bc128 to your computer and use it in GitHub Desktop.
Save dmitry-udod/b5a07d1418aa1e018c4bced2c93bc128 to your computer and use it in GitHub Desktop.
Валидация банковских данных. соответствие БИК и расчетного счета (корреспондентского) банка (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