<?php

namespace App\Rules;

use Illuminate\Validation\Rule;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Validation\Validator;



class Recaptcha implements Rule
{
    /**
     * 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)
    {
        $data = array('secret' => env('GOOGLE_RECAPTCHA_SECRET'),
        'response' => $value);

    try {
        $verify = curl_init();
        curl_setopt($verify, CURLOPT_URL, 
"https://www.google.com/recaptcha/api/siteverify");
        curl_setopt($verify, CURLOPT_POST, true);
        curl_setopt($verify, CURLOPT_POSTFIELDS, 
                    http_build_query($data));
        curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($verify);

        return json_decode($response) -> success;
    }
    catch (\Exception $e) {
        return false;
    }
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    
    
}
