30 lines
784 B
PHP
30 lines
784 B
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class Recaptcha implements ValidationRule
|
|
{
|
|
/**
|
|
* Run the validation rule.
|
|
*
|
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
$gResponseToken = (string) $value;
|
|
|
|
$response = Http::asForm()->post(
|
|
'https://www.google.com/recaptcha/api/siteverify',
|
|
['secret' => setting('recaptcha_secret_key'), 'response' => $gResponseToken]
|
|
);
|
|
|
|
if (!json_decode($response->body(), true)['success']) {
|
|
$fail('Invalid recaptcha');
|
|
}
|
|
}
|
|
}
|