53 lines
1.1 KiB
PHP
53 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
class Recaptcha implements Rule
|
|
{
|
|
/**
|
|
* Create a new rule instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param mixed $attribute Attribute name.
|
|
* @param mixed $value Attribute value.
|
|
* @return boolean
|
|
*/
|
|
public function passes(mixed $attribute, mixed $value)
|
|
{
|
|
$endpoint = config('services.google_recaptcha');
|
|
|
|
$response = Http::asForm()->post($endpoint['url'], [
|
|
'secret' => $endpoint['secret_key'],
|
|
'response' => $value,
|
|
])->json();
|
|
|
|
if ($response['success'] === true && $response['score'] > 0.5) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
return 'Captcha failed. Refresh the page and try again';
|
|
}
|
|
}
|