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

Commit bb4d232

Browse files
committed
Add getValidationUrlWithToken to V3 Builder
1 parent ac51bf0 commit bb4d232

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/ReCaptchaBuilderV3.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@ public function __construct(string $api_site_key, string $api_secret_key)
3232
parent::__construct($api_site_key, $api_secret_key, 'v3');
3333
}
3434

35+
public function getTokenParameterName(): string
36+
{
37+
return config(
38+
'recaptcha.default_token_parameter_name',
39+
'token'
40+
);
41+
}
42+
43+
public function getValidationUrl(): string
44+
{
45+
return url(config(
46+
'recaptcha.default_validation_route',
47+
'biscolab-recaptcha/validate'
48+
));
49+
}
50+
51+
public function getValidationUrlWithToken(): string
52+
{
53+
return implode(
54+
"?",
55+
[
56+
$this->getValidationUrl(),
57+
$this->getTokenParameterName()
58+
]
59+
);
60+
}
61+
3562
/**
3663
* Write script HTML tag in you HTML code
3764
* Insert before </head> tag
@@ -66,13 +93,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
6693
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
6794

6895
$validate_function = "
69-
fetch('" . url(config(
70-
'recaptcha.default_validation_route',
71-
'biscolab-recaptcha/validate'
72-
)) . "?" . config(
73-
'recaptcha.default_token_parameter_name',
74-
'token'
75-
) . "=' + token, {
96+
fetch('" . $this->getValidationUrlWithToken() . "=' + token, {
7697
headers: {
7798
\"X-Requested-With\": \"XMLHttpRequest\",
7899
\"X-CSRF-TOKEN\": csrfToken.content

tests/ReCaptchaV3Test.php

+37-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public function testCustomValidationFunction()
7474
$this->assertMatchesRegularExpression('/functionCustomValidation\(token\)/', $r);
7575
}
7676

77+
/**
78+
* @test
79+
*/
80+
public function testCustomValidationUrl()
81+
{
82+
83+
$r = $this->recaptcha_v3->htmlScriptTagJsApi();
84+
$this->assertMatchesRegularExpression('/http:\/\/localhost\/my-custom-url\?my-custom-token-name/', $r);
85+
}
86+
7787
/**
7888
* @test
7989
*/
@@ -109,6 +119,30 @@ public function testHtmlScriptTagJsApiCalledByFacade()
109119
htmlScriptTagJsApi([]);
110120
}
111121

122+
/**
123+
* @test
124+
*/
125+
public function testValidationUrlShouldBeMyCustomUrl()
126+
{
127+
$this->assertEquals($this->recaptcha_v3->getValidationUrl(), "http://localhost/my-custom-url");
128+
}
129+
130+
/**
131+
* @test
132+
*/
133+
public function testTokenParamNameShouldBeMyCustomTokenParamName()
134+
{
135+
$this->assertEquals($this->recaptcha_v3->getTokenParameterName(), "my-custom-token-name");
136+
}
137+
138+
/**
139+
* @test
140+
*/
141+
public function testValidationUrlShouldBeMyCustomValidationUrl()
142+
{
143+
$this->assertEquals($this->recaptcha_v3->getValidationUrlWithToken(), "http://localhost/my-custom-url?my-custom-token-name");
144+
}
145+
112146
/**
113147
* Define environment setup.
114148
*
@@ -121,6 +155,9 @@ protected function getEnvironmentSetUp($app)
121155

122156
$app['config']->set('recaptcha.version', 'v3');
123157
$app['config']->set('recaptcha.curl_timeout', 3);
158+
159+
$app['config']->set('recaptcha.default_validation_route', "my-custom-url");
160+
$app['config']->set('recaptcha.default_token_parameter_name', "my-custom-token-name");
124161
}
125162

126163
/**
@@ -130,7 +167,6 @@ protected function setUp(): void
130167
{
131168

132169
parent::setUp(); // TODO: Change the autogenerated stub
133-
134170
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
135171
}
136172
}

0 commit comments

Comments
 (0)