11<?php
2+
23/**
34 * Copyright (c) 2017 - present
45 * LaravelGoogleRecaptcha - ReCaptchaBuilder.php
@@ -44,6 +45,11 @@ class ReCaptchaBuilder
4445 */
4546 const DEFAULT_RECAPTCHA_FIELD_NAME = 'g-recaptcha-response ' ;
4647
48+ /**
49+ * @var string
50+ */
51+ const DEFAULT_RECAPTCHA_API_DOMAIN = 'www.google.com ' ;
52+
4753 /**
4854 * The Site key
4955 * please visit https://developers.google.com/recaptcha/docs/start
@@ -71,10 +77,23 @@ class ReCaptchaBuilder
7177 */
7278 protected $ skip_by_ip = false ;
7379
80+ /**
81+ * The API domain (default: retrieved from config file)
82+ * @var string
83+ */
84+ protected $ api_domain = '' ;
85+
7486 /**
7587 * The API request URI
88+ * @var string
7689 */
77- protected $ api_url = 'https://www.google.com/recaptcha/api/siteverify ' ;
90+ protected $ api_url = '' ;
91+
92+ /**
93+ * The URI of the API Javascript file to embed in you pages
94+ * @var string
95+ */
96+ protected $ api_js_url = '' ;
7897
7998 /**
8099 * ReCaptchaBuilder constructor.
@@ -93,6 +112,8 @@ public function __construct(
93112 $ this ->setApiSecretKey ($ api_secret_key );
94113 $ this ->setVersion ($ version );
95114 $ this ->setSkipByIp ($ this ->skipByIp ());
115+ $ this ->setApiDomain ();
116+ $ this ->setApiUrls ();
96117 }
97118
98119 /**
@@ -165,6 +186,40 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder
165186 return $ this ;
166187 }
167188
189+ /**
190+ * @param null|string $api_domain
191+ *
192+ * @return ReCaptchaBuilder
193+ */
194+ public function setApiDomain (?string $ api_domain = null ): ReCaptchaBuilder
195+ {
196+
197+ $ this ->api_domain = $ api_domain ?? config ('recaptcha.api_domain ' , self ::DEFAULT_RECAPTCHA_API_DOMAIN );
198+
199+ return $ this ;
200+ }
201+
202+ /**
203+ * @return string
204+ */
205+ public function getApiDomain (): string
206+ {
207+
208+ return $ this ->api_domain ;
209+ }
210+
211+ /**
212+ * @return ReCaptchaBuilder
213+ */
214+ public function setApiUrls (): ReCaptchaBuilder
215+ {
216+
217+ $ this ->api_url = 'https:// ' . $ this ->api_domain . '/recaptcha/api/siteverify ' ;
218+ $ this ->api_js_url = 'https:// ' . $ this ->api_domain . '/recaptcha/api.js ' ;
219+
220+ return $ this ;
221+ }
222+
168223 /**
169224 * @return array|mixed
170225 */
@@ -233,7 +288,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
233288
234289 // Create query string
235290 $ query = ($ query ) ? '? ' . http_build_query ($ query ) : "" ;
236- $ html .= "<script src= \"https://www.google.com/recaptcha/api.js " . $ query . "\" async defer></script> " ;
291+ $ html .= "<script src= \"" . $ this -> api_js_url . $ query . "\" async defer></script> " ;
237292
238293 return $ html ;
239294 }
@@ -300,7 +355,6 @@ public function validate($response)
300355 }
301356
302357 return $ response ['success ' ];
303-
304358 }
305359
306360 /**
@@ -329,4 +383,4 @@ protected function returnArray(): bool
329383
330384 return ($ this ->version == 'v3 ' );
331385 }
332- }
386+ }
0 commit comments