PHPackages                             timsinakiran/hcaptcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. timsinakiran/hcaptcha

ActiveLibrary

timsinakiran/hcaptcha
=====================

hCaptcha for Laravel 12

4.4.8(1y ago)05MITPHPPHP &gt;=5.5.5CI failing

Since Apr 13Pushed 1y agoCompare

[ Source](https://github.com/timsinakiran/hcaptcha)[ Packagist](https://packagist.org/packages/timsinakiran/hcaptcha)[ RSS](/packages/timsinakiran-hcaptcha/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (20)Used By (0)

Introduction
============

[](#introduction)

The hCaptcha is a human-computer verification solution that replaces Google reCaptcha. It has high performance, high availability and high recognition, especially suitable for regions with poor network quality, such as East Asia, Middle East, etc. Many well-known companies are using the hCaptcha solution.

The package is one of the recommended package on [the official developer guide of HCaptcha](https://github.com/hCaptcha/hcaptcha-integrations-list#laravel). No malicious code, ensuring the security of your development supply chain.

- Purchase a [VPS](https://bwh88.net/aff.php?aff=20075) or [Akamai VPS](https://www.linode.com/lp/refer/?r=2d4a0088743a2a06e3405514d486b8966c51a439) for developing and deploying applications.

Installation
------------

[](#installation)

```
composer require scyllaly/hcaptcha

```

Laravel 5 and above
-------------------

[](#laravel-5-and-above)

### Setup

[](#setup)

In `app/config/app.php` add the following :

Step 1: The ServiceProvider to the providers array :

```
Scyllaly\HCaptcha\HCaptchaServiceProvider::class,
```

Step 2: The class alias to the aliases array :

```
'HCaptcha' => Scyllaly\HCaptcha\Facades\HCaptcha::class,
```

Step 3: Publish the config file

```
php artisan vendor:publish --provider="Scyllaly\HCaptcha\HCaptchaServiceProvider"
```

### Configuration

[](#configuration)

Add `HCAPTCHA_SECRET`, `HCAPTCHA_SITEKEY` and `HCAPTCHA_ENABLED` in **.env** file :

```
HCAPTCHA_SECRET=secret-key
HCAPTCHA_SITEKEY=site-key
HCAPTCHA_ENABLED=true

```

(You can obtain them from [Official Developer Guide](https://docs.hcaptcha.com/api#getapikey))

- Tips: If you do not have an account, please [sign up](https://hCaptcha.com/?r=d315c350eeee) it first.

### Usage

[](#usage)

#### Init js source

[](#init-js-source)

With default options :

```
 {!! HCaptcha::renderJs() !!}
```

With [language support](https://docs.hcaptcha.com/configuration) or [onloadCallback](https://docs.hcaptcha.com/configuration) option :

```
 {!! HCaptcha::renderJs('fr', true, 'hcaptchaCallback') !!}
```

#### Display hCaptcha

[](#display-hcaptcha)

Default widget :

```
{!! HCaptcha::display() !!}
```

With [custom attributes](https://docs.hcaptcha.com/configuration#themes) (theme, size, callback ...) :

```
{!! HCaptcha::display(['data-theme' => 'dark']) !!}
```

Invisible hCaptcha using a [submit button](https://docs.hcaptcha.com/configuration#themes):

```
{!! HCaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}
```

Notice that the id of the form is required in this method to let the autogenerated callback submit the form on a successful captcha verification.

#### Validation

[](#validation)

There are two ways to apply HCaptcha validation to your form:

#### 1. Basic Approach

[](#1-basic-approach)

This method always applies the HCaptcha validation rule.

```
$validate = Validator::make(Input::all(), [
    'h-captcha-response' => 'required|HCaptcha'
]);
```

In this approach, the `h-captcha-response` field is required and validated using the `HCaptcha` rule without any conditions.

#### 2. Conditional Approach

[](#2-conditional-approach)

This method applies the HCaptcha validation rule only if the `HCAPTCHA_ENABLED` environment variable is set to `true`.

```
$isHcaptchaEnabled = env('HCAPTCHA_ENABLED');
$rules = [
    // Other validation rules...
];

if ($isHcaptchaEnabled) {
    $rules['h-captcha-response'] = 'required|HCaptcha';
}

$request->validate($rules);
```

In this approach, the `h-captcha-response` field will be required and validated using the `HCaptcha` rule only when `HCAPTCHA_ENABLED` is set to `true`. This adds flexibility to your validation logic, allowing you to enable or disable HCaptcha validation as needed.

##### Custom Validation Message

[](#custom-validation-message)

Add the following values to the `custom` array in the `validation` language file :

```
'custom' => [
    'h-captcha-response' => [
        'required' => 'Please verify that you are not a robot.',
        'h_captcha' => 'Captcha error! try again later or contact site admin.',
    ],
],
```

Then check for captcha errors in the `Form` :

```
@if ($errors->has('h-captcha-response'))

        {{ $errors->first('h-captcha-response') }}

@endif
```

### Testing

[](#testing)

When using the [Laravel Testing functionality](http://laravel.com/docs/5.5/testing), you will need to mock out the response for the captcha form element.

So for any form tests involving the captcha, you can do this by mocking the facade behavior:

```
// prevent validation error on captcha
HCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// provide hidden input for your 'required' validation
HCaptcha::shouldReceive('display')
    ->zeroOrMoreTimes()
    ->andReturn('');
```

You can then test the remainder of your form as normal.

When using HTTP tests you can add the `h-captcha-response` to the request body for the 'required' validation:

```
// prevent validation error on captcha
HCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// POST request, with request body including `h-captcha-response`
$response = $this->json('POST', '/register', [
    'h-captcha-response' => '1',
    'name' => 'Scyllaly',
    'email' => 'Scyllaly@example.com',
    'password' => '123456',
    'password_confirmation' => '123456',
]);
```

Without Laravel
---------------

[](#without-laravel)

Checkout example below:

```
