39 lines
833 B
PHP
39 lines
833 B
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class Recaptcha implements Rule
|
|
{
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @return bool
|
|
*/
|
|
public function passes($attribute, $value)
|
|
{
|
|
$gResponseToken = (string) $value;
|
|
|
|
$response = Http::asForm()->post(
|
|
'https://www.google.com/recaptcha/api/siteverify',
|
|
['secret' => env('RECAPTCHA_SECRET_KEY'), 'response' => $gResponseToken]
|
|
);
|
|
|
|
return json_decode($response->body(), true)['success'];
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
return 'Invalid reCAPTCHA';
|
|
}
|
|
}
|