Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 15263ea

Browse files
authored
Merge pull request #3 from biscolab/v2
v 2.0.0 Invisible ReCAPTCHA added
2 parents 8b6406f + 7958d36 commit 15263ea

File tree

7 files changed

+218
-26
lines changed

7 files changed

+218
-26
lines changed

README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# laravel-recaptcha
1+
# Laravel ReCAPTCHA - v2.*
22
Simple Google ReCaptcha package for Laravel 5
33

44
## Installation
55

66
You can install the package via composer:
77
```sh
8-
composer require biscolab/laravel-recaptcha
8+
9+
composer require biscolab/laravel-recaptcha:^2.0
910
```
1011
The service **provider** must be registered in `config/app.php`:
1112
```php
@@ -29,14 +30,19 @@ php artisan vendor:publish --provider="Biscolab\ReCaptcha\ReCaptchaServiceProvid
2930
## Configuration
3031

3132
### Add your API Keys
32-
Open `config/recaptcha.php` configuration file and set `api_site_key` and `api_secret_key`:
33+
34+
Open `config/recaptcha.php` configuration file and set `api_site_key`, `api_secret_key` and `version`:
35+
3336
```php
3437
return [
3538
'api_site_key' => 'YOUR_API_SITE_KEY',
3639
'api_secret_key' => 'YOUR_API_SECRET_KEY',
40+
'version' => 'v2' // supported: v2|invisible
3741
];
3842
```
3943
For more invermation about Site Key and Secret Key please visit [Google RaCaptcha developer documentation](https://developers.google.com/recaptcha/docs/start)
44+
Get more info about ReCAPTCHA version at https://developers.google.com/recaptcha/docs/versions
45+
4046

4147
### Customize error message
4248
Before starting please add validation recaptcha message to `resources/lang/[LANG]/validation.php` file
@@ -51,21 +57,41 @@ return [
5157

5258
### Embed in Blade
5359

54-
Insert `{!!ReCaptcha::htmlScriptTagJsApi()!!}` before closing `</head>` tag
60+
Insert `htmlScriptTagJsApi($formId)` helper before closing `</head>` tag
61+
You can also use `ReCaptcha::htmlScriptTagJsApi($formId)`.
62+
`$formId` is required only if you are using **ReCAPTCHA INVISIBLE**
63+
5564
```blade
5665
<!DOCTYPE html>
5766
<html>
5867
<head>
5968
...
60-
{!!ReCaptcha::htmlScriptTagJsApi()!!}
69+
{!! htmlScriptTagJsApi(/* $formId - INVISIBLE version only */) !!}
70+
or
71+
{!! ReCaptcha::htmlScriptTagJsApi(/* $formId - INVISIBLE version only */) !!}
6172
</head>
6273
```
6374

64-
After you have to insert `{!!ReCaptcha::htmlFormSnippet()!!}` inside the form where you want to use the field `g-recaptcha-response`
75+
#### If you are using ReCAPTCHA v2
76+
After you have to insert `htmlFormSnippet()` helper inside the form where you want to use the field `g-recaptcha-response`
77+
You can also use `ReCaptcha::htmlFormSnippet()` .
78+
```blade
79+
<form>
80+
...
81+
{!! htmlFormSnippet() !!}
82+
<input type="submit">
83+
</form>
84+
```
85+
86+
#### If you are using ReCAPTCHA INVISIBLE
87+
After you have to insert `htmlFormButton($buttonInnerHTML)` helper inside the form where you want to use ReCAPTCHA. This function creates submit button therefore **you don't have to insert <input type="submit"> or similar**.
88+
You can also use `ReCaptcha::htmlFormButton($buttonInnerHTML)` .
89+
`$buttonInnerHTML` is what you want to write on the submit button
6590
```blade
6691
<form>
6792
...
68-
{!!ReCaptcha::htmlFormSnippet()!!}
93+
{!! htmlFormButton(/* $buttonInnerHTML - Optional */) !!}
94+
6995
</form>
7096
```
7197

@@ -74,6 +100,7 @@ After you have to insert `{!!ReCaptcha::htmlFormSnippet()!!}` inside the form wh
74100
Add **recaptcha** to your rules
75101
```php
76102
$v = Validator::make(request()->all(), [
103+
...
77104
'g-recaptcha-response' => 'recaptcha',
78105
]);
79106
```
@@ -82,4 +109,3 @@ Print form errors
82109
```php
83110
dd($v->errors());
84111
```
85-

config/recaptcha.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
<?php
22

3-
// please visit https://developers.google.com/recaptcha/docs/start
3+
/**
4+
*
5+
* Biscolab Laravel ReCaptcha configuration file
6+
* MIT License @ https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
7+
* author: Roberto Belotti - [email protected]
8+
* web : robertobelotti.com, github.com/biscolab
9+
*
10+
* To configure correctly please visit https://developers.google.com/recaptcha/docs/start
11+
*
12+
*/
413

514
return [
615

16+
/**
17+
*
18+
* The site key
19+
* get site key @ www.google.com/recaptcha/admin
20+
*
21+
*/
722
'api_site_key' => '',
23+
24+
/**
25+
*
26+
* The secret key
27+
* get secret key @ www.google.com/recaptcha/admin
28+
*
29+
*/
830
'api_secret_key' => '',
31+
32+
/**
33+
*
34+
* ReCATCHA version
35+
* Supported: "v2", "invisible",
36+
*
37+
* get more info @ https://developers.google.com/recaptcha/docs/versions
38+
*
39+
*/
40+
'version' => 'v2'
941
];

src/ReCaptchaBuilder.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<?php
22

3+
/**
4+
*
5+
* Biscolab Laravel ReCaptcha - ReCaptchaBuilder Class
6+
* MIT License @ https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
7+
* author: Roberto Belotti - [email protected]
8+
* web : robertobelotti.com, github.com/biscolab
9+
*
10+
*/
11+
312
namespace Biscolab\ReCaptcha;
413

14+
use Exception;
15+
516
class ReCaptchaBuilder {
617

718
/**
@@ -16,33 +27,42 @@ class ReCaptchaBuilder {
1627
*/
1728
protected $api_secret_key;
1829

30+
/**
31+
* The chosen ReCAPTCHA version
32+
* please visit https://developers.google.com/recaptcha/docs/start
33+
*/
34+
protected $version;
35+
1936
/**
2037
* The API request URI
2138
*/
2239
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';
2340

24-
public function __construct($api_site_key, $api_secret_key)
41+
public function __construct($api_site_key, $api_secret_key, $version = 'v2')
2542
{
2643
$this->api_site_key = $api_site_key;
2744
$this->api_secret_key = $api_secret_key;
45+
$this->version = $version;
2846
}
2947

3048
/**
3149
* Write script HTML tag in you HTML code
3250
* Insert before </head> tag
51+
*
52+
* @param $formId required if you are using invisible ReCaptcha
3353
*/
34-
public function htmlScriptTagJsApi()
35-
{
36-
return "<script src='https://www.google.com/recaptcha/api.js'></script>";
37-
}
38-
39-
/**
40-
* Write HTML tag in you HTML code
41-
* Insert before </head> tag
42-
*/
43-
public function htmlFormSnippet()
54+
public function htmlScriptTagJsApi($formId = '')
4455
{
45-
return "<div class='g-recaptcha' data-sitekey='".$this->api_site_key."'></div>";
56+
$html = "<script src='https://www.google.com/recaptcha/api.js' async defer></script>";
57+
if($this->version != 'v2'){
58+
if(!$formId) throw new Exception("formId required", 1);
59+
$html.= '<script>
60+
function biscolabLaravelReCaptcha(token) {
61+
document.getElementById("'.$formId.'").submit();
62+
}
63+
</script>';
64+
}
65+
return $html;
4666
}
4767

4868
/**

src/ReCaptchaBuilderInvisible.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Biscolab Laravel ReCaptcha - ReCaptchaBuilderInvisible Class
6+
* MIT License @ https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
7+
* author: Roberto Belotti - [email protected]
8+
* web : robertobelotti.com, github.com/biscolab
9+
*
10+
*/
11+
12+
namespace Biscolab\ReCaptcha;
13+
14+
class ReCaptchaBuilderInvisible extends ReCaptchaBuilder {
15+
16+
/**
17+
* Write HTML <button> tag in your HTML code
18+
* Insert before </form> tag
19+
*/
20+
public function htmlFormButton($buttonInnerHTML = 'Submit')
21+
{
22+
return ($this->version == 'invisible')? '<button class="g-recaptcha" data-sitekey="'.$this->api_site_key.'" data-callback="biscolabLaravelReCaptcha">'.$buttonInnerHTML.'</button>' : '';
23+
}
24+
25+
}

src/ReCaptchaBuilderV2.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
*
5+
* Biscolab Laravel ReCaptcha - ReCaptchaBuilderV2 Class
6+
* MIT License @ https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
7+
* author: Roberto Belotti - [email protected]
8+
* web : robertobelotti.com, github.com/biscolab
9+
*
10+
*/
11+
12+
namespace Biscolab\ReCaptcha;
13+
14+
class ReCaptchaBuilderV2 extends ReCaptchaBuilder {
15+
16+
/**
17+
* Write ReCAPTCHA HTML tag in your FORM
18+
* Insert before </form> tag
19+
*/
20+
public function htmlFormSnippet()
21+
{
22+
return ($this->version == 'v2')? '<div class="g-recaptcha" data-sitekey="'.$this->api_site_key.'"></div>' : '';
23+
}
24+
25+
}

src/ReCaptchaServiceProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public function register()
3737
);
3838

3939
$this->registerReCaptchaBuilder();
40-
41-
$this->app->alias('recaptcha', 'Biscolab\ReCaptcha\ReCaptchaBuilder');
4240
}
4341

4442
/**
@@ -49,7 +47,8 @@ public function register()
4947
protected function registerReCaptchaBuilder()
5048
{
5149
$this->app->singleton('recaptcha', function ($app) {
52-
return new ReCaptchaBuilder(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'));
50+
if(config('recaptcha.version') == 'v2') return new ReCaptchaBuilderV2(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'), config('recaptcha.version'));
51+
else return new ReCaptchaBuilderInvisible(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'), config('recaptcha.version'));
5352
});
5453
}
5554

@@ -70,7 +69,7 @@ public function addValidationRule()
7069
*/
7170
public function provides()
7271
{
73-
return ['recaptcha', 'Biscolab\ReCaptcha\ReCaptchaBuilder'];
72+
return ['recaptcha'];
7473
}
7574

7675
}

src/helpers.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/**
4+
*
5+
* Biscolab Laravel ReCaptcha - helpers
6+
* MIT License @ https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
7+
* author: Roberto Belotti - [email protected]
8+
* web : robertobelotti.com, github.com/biscolab
9+
*
10+
*/
11+
312
if (!function_exists('recaptcha')) {
413
/**
514
* @return Biscolab\ReCaptcha\ReCaptchaBuilder
@@ -8,4 +17,60 @@ function recaptcha()
817
{
918
return app('recaptcha');
1019
}
11-
}
20+
}
21+
22+
/**
23+
* call ReCaptcha::htmlScriptTagJsApi()
24+
* Write script HTML tag in you HTML code
25+
* Insert before </head> tag
26+
*
27+
* @param $formId required if you are using invisible ReCaptcha
28+
*/
29+
if (!function_exists('htmlScriptTagJsApi')) {
30+
31+
/**
32+
* @return Biscolab\ReCaptcha\ReCaptchaBuilder::htmlScriptTagJsApi()
33+
*/
34+
function htmlScriptTagJsApi($formId = '')
35+
{
36+
return ReCaptcha::htmlScriptTagJsApi($formId);
37+
}
38+
}
39+
40+
/**
41+
* call ReCaptcha::htmlFormButton()
42+
* Write HTML <button> tag in your HTML code
43+
* Insert before </form> tag
44+
*
45+
* Warning! Using only with ReCAPTCHA INVISIBLE
46+
* @param $buttonInnerHTML What you want to write on the submit button
47+
*/
48+
if (!function_exists('htmlFormButton')) {
49+
50+
/**
51+
* @return Biscolab\ReCaptcha\ReCaptchaBuilder::htmlFormButton()
52+
*/
53+
function htmlFormButton($buttonInnerHTML = 'Submit')
54+
{
55+
return ReCaptcha::htmlFormButton($buttonInnerHTML);
56+
}
57+
}
58+
59+
/**
60+
* call ReCaptcha::htmlFormSnippet()
61+
* Write ReCAPTCHA HTML tag in your FORM
62+
* Insert before </form> tag
63+
*
64+
* Warning! Using only with ReCAPTCHA v2
65+
*/
66+
if (!function_exists('htmlFormSnippet')) {
67+
68+
/**
69+
* @return Biscolab\ReCaptcha\ReCaptchaBuilder::htmlFormSnippet()
70+
*/
71+
function htmlFormSnippet()
72+
{
73+
return ReCaptcha::htmlFormSnippet();
74+
}
75+
}
76+

0 commit comments

Comments
 (0)