Skip to content

RyanDaDeng/laravel-google-recaptcha-v2

Repository files navigation

Laravel Package for Google reCAPTCHA v2

Latest Version on Packagist Total Downloads Coverage Status Build

Welcome all tickets (features/questions/bugs/improvements), I will respond to all tickets within 48 hours.

This is a package for Google reCAPTCHA v2.

If you want to use v3, please go to: https://github.com/RyanDaDeng/laravel-google-recaptcha-v3

DEMO

Checkbox

Invisible - hidden

Invisible - Inline

Corner

Description

A Laravel package for Google reCAPTCHA v2.

If you want to make your own font-end template, you have full access to modify template file, so you can customise your own template by reading through Google official guide for either invisible badge or inline checkbox. https://developers.google.com/recaptcha/docs/display

Features

  • Support invisible, corner and inline badge style
  • Support multiple reCAPTCHA on the same page for different forms
  • Support multiple actions to be placed on the same page
  • Support custom implementation on config interface
  • Support custom implementation on request method interface
  • Support custom implementation on Template file

Requirement

This package requires the following dependencies:

  • Laravel 5.x

  • If you want to use Validation Class your Laravel version needs to be >= 5.5

  • php > 5

  • Please ensure that you have read basic information from Google reCAPTCHA v2.

Installation

Via Composer

        $ composer require timehunter/laravel-google-recaptcha-v2 "~1.0.0" -vvv

If your Laravel framework version <= 5.4, please register the service provider in your config file: /config/app.php, otherwise please skip it.

'providers'=[
    ....,
    TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider::class
]

And also

'aliases'=[
     ....,
     'GoogleReCaptchaV2'=> TimeHunter\LaravelGoogleReCaptchaV2\Facades\GoogleReCaptchaV2::class
 ]

If your Laravel framework version is >= 5.5, just run the following command to publish config.

$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider" --tag=googlerecaptchav2.config

Optional: if you want to modify or customise your own template, you can publish a default view first, and change 'template' in config file:

$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV2\Providers\GoogleReCaptchaV2ServiceProvider" --tag=googlerecaptchav2.views

After installation, you should see a googlerecaptchav2/template.blade under views folder and googlerecaptchav2.php in your app/config folder.

Basic Usage

Setting up your Google reCAPTCHA details in config file

Please register all details on host_name, site_key, secret_key and site_verify_url.

For more details please check comments in config file.

Display reCAPTCHA v2

Note: for styling with reCAPTCHA v2 badge, the official site does not support it. You can still customise it on its div element if you want.

Blade

Include div with an ID inside your form, e.g.

 <div id="form_id_1"></div>
 <div id="form_id_2"></div>

Include Template script in your bottom/header of your page, e.g.

 {!!  GoogleReCaptchaV2::render('form_id_1','form_id_2') !!}
Example Usage
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}}
<form method="POST" action="/verify">
    @csrf
    <div id="form_1_id"></div>
    <input type="submit" value="submit">
</form>

<form method="POST" action="/verify">
    @csrf
    <div id="form_2_id"></div>
    <input type="submit" value="submit">
</form>

{!!  GoogleReCaptchaV2::render('form_1_id','form_2_id') !!}

The backend request will receive a value for 'g-recaptcha-response', please take a look at Sample Use Case and Facade usage sections.

Badge Display

Importance: you can always make your own template, just assign your template in config:

    [
        ...
        'template' => 'test.template' // if your template is located at resources/views/test/template
        ...
    ]

Checkbox

  1. Go to config file, and set
    [
        ...
        'badge' => 'inline'
        ...
    ]
  1. Badge will be displayed as checkbox format within the form.

Invisible - inline

  1. Set size as invisible
    [
        ...
        'size' => 'invisible'
        ...
    ]
  1. Set badge as inline or bottomright or bottomleft
    [
        ...
        'badge' => 'inline' // also support: bottomright,bottomleft
        ...
    ]

Invisible - hidden

  1. Set size as invisible
    [
        ...
        'size' => 'invisible'
        ...
    ]
  1. Modify your div with style display:none
  2. Refer to Google official site: https://developers.google.com/recaptcha/docs/faq , you need to include the following text:
   This site is protected by reCAPTCHA and the Google
       <a href="https://policies.google.com/privacy">Privacy Policy</a> and
       <a href="https://policies.google.com/terms">Terms of Service</a> apply.

Corner

  1. Set size as invisible
    [
        ...
        'size' => 'invisible'
        ...
    ]
  1. Set badge as bottomright/bottomleft
    [
        ...
        'badge' => 'bottomright'
        ...
    ]

Validation Class (Only support Laravel >= 5.5)

You can use provided Validation object to verify your reCAPTCHA.

   use TimeHunter\LaravelGoogleReCaptchaV2\Validations\GoogleReCaptchaV2ValidationRule
   $rule = [
            'g-recaptcha-response' => [new GoogleReCaptchaV2ValidationRule()]
        ];

Facade Usage

You can also directly use registered service by calling the following method.

  • verifyResponse() which accepts the token value from your form. This return Google reCAPTCHA Response object.
   GoogleReCaptchaV2::verifyResponse($value, $ip=null);

Example Usage

   GoogleReCaptchaV2::verifyResponse($value,$ip)->getMessage();
   GoogleReCaptchaV2::verifyResponse($value)->isSuccess();
   GoogleReCaptchaV2::verifyResponse($value)->toArray();
   GoogleReCaptchaV2::verifyResponse($request->input('g-recaptcha-response'))->getMessage()

Sample Use Case

  1. Register your action in config, also enable score and set up your own site key and secret key:

  2. Register two routes in web.php

Route::get('/index', 'ReCaptchaController@index');
Route::post('/verify', 'ReCaptchaController@verify');
  1. Create two functions in controller:
    public function verify(Request $request)
    {
        dd(GoogleReCaptchaV2::verifyResponse($request->input('g-recaptcha-response'))->getMessage());
    }
    public function index(Request $request)
    {
        return view('index');    
   }
  1. Create your form in index.blade.php:
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}}

<form method="POST" action="/verify">
    @csrf
    <div id="contact_us_id"></div>
    <input type="submit" value="submit">
</form>


<form method="POST" action="/verify">
    @csrf
    <div id="signup_id"></div>
    <input type="submit" value="submit">
</form>

{!!  GoogleReCaptchaV2::render('contact_us_id','signup_id') !!}

Advanced Usage

Custom implementation on Template

After publish views, a blade file created under googlerecaptchaV2, you can customise it and change template value in config file, e.g. if your template is saved in resources/views/test/template, you should put values as below:

    [
        ...
        'template' => 'test.template'
        ...
    ]

Custom implementation on Config

For some users, they might store the config details in their own storage e.g database. You can create your own class and implement:

TimeHunter\LaravelGoogleReCaptchaV2\Interfaces\ReCaptchaConfigv2Interface

Remember to register it in your own service provider

     $this->app->bind(
                ReCaptchaConfigV2Interface::class,
                YourOwnCustomImplementation::class
            );

Custom implementation on Request method

The package has two default options to verify: Guzzle and Curl, if you want to use your own request method, You can create your own class and implement

TimeHunter\LaravelGoogleReCaptchaV2\Interfaces\RequestClientInterface

Remember to register it in your own service provider

     $this->app->bind(
                RequestClientInterface::class,
                YourOwnCustomImplementation::class
            );

Contributers

@markheramis

Security

If you discover any security related issues, please email ryandadeng@gmail.com instead of using the issue tracker.

License

MIT. Please see the license file for more information.