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

Commit e81a373

Browse files
committed
htmlScriptTagJsApi function: Google reCAPTCHA v3 full support/compatibility
1 parent 76c8cab commit e81a373

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/ReCaptchaBuilder.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,34 +152,44 @@ function biscolabLaravelReCaptcha(token) {
152152
</script>';
153153
}
154154
elseif ($this->version == 'v3') {
155+
155156
$action = array_get($configuration, 'action', 'homepage');
156-
$js_then_callback = array_get($configuration, 'callback_then', '');
157-
$js_callback_catch = array_get($configuration, 'callback_catch', '');
158157

159-
$js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : '';
160-
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
158+
$js_custom_validation = array_get($configuration, 'custom_validation', '');
159+
160+
// Check if set custom_validation. That function will override default fetch validation function
161+
if($js_custom_validation) {
162+
163+
$validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : '';
164+
}
165+
else {
166+
167+
$js_then_callback = array_get($configuration, 'callback_then', '');
168+
$js_callback_catch = array_get($configuration, 'callback_catch', '');
169+
170+
$js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : '';
171+
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
172+
173+
$validate_function = "
174+
fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, {
175+
headers: {
176+
\"X-Requested-With\": \"XMLHttpRequest\",
177+
\"X-CSRF-TOKEN\": csrfToken.content
178+
}
179+
})
180+
.then(function(response) {
181+
{$js_then_callback}
182+
})
183+
.catch(function(err) {
184+
{$js_callback_catch}
185+
});";
186+
}
161187

162188
$html .= "<script>
163189
var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]');
164190
grecaptcha.ready(function() {
165191
grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) {
166-
fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, {
167-
headers: {
168-
\"X-Requested-With\": \"XMLHttpRequest\",
169-
\"X-CSRF-TOKEN\": csrfToken.content
170-
}
171-
})
172-
173-
.then(function(response) {
174-
{$js_then_callback}
175-
// console.log(response.status);
176-
// response.json().then(function(data){
177-
// console.log(data);
178-
// });
179-
})
180-
.catch(function(err) {
181-
{$js_callback_catch}
182-
});
192+
{$validate_function}
183193
});
184194
});
185195
</script>";

0 commit comments

Comments
 (0)