From a0071003b4d86f55c4dc9881c66aae81d7a16e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Baconnier?= Date: Wed, 20 Jul 2022 14:14:56 +0200 Subject: [PATCH] Upgrade URLs to Zoho V3 and add sandbox urls to web --- config/zoho.php | 3 +- src/Clients/ZohoUrlFactory.php | 41 +++++++++++++++++++++++----- tests/Clients/ZohoUrlFactoryTest.php | 24 ++++++++++++---- tests/Zoho/ZohoHttpTest.php | 2 +- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/config/zoho.php b/config/zoho.php index 9828842..02ac40f 100644 --- a/config/zoho.php +++ b/config/zoho.php @@ -6,9 +6,10 @@ */ 'url' => '/oauth2/zoho', 'redirect_url' => '/', + 'sandbox' => env('ZOHO_SANDBOX', false), // When requesting an accessToken, the API may return an error, - // The controller will redirect the user to `on_error_url` + // The controller will redirect the user to `on_error_redirect_to` // with the error flashed in the session with the key `zoho.access_token_error` // known error code: // - 403: invalid_client_secret diff --git a/src/Clients/ZohoUrlFactory.php b/src/Clients/ZohoUrlFactory.php index cceb95d..3724ecd 100644 --- a/src/Clients/ZohoUrlFactory.php +++ b/src/Clients/ZohoUrlFactory.php @@ -41,7 +41,12 @@ public function web(ZohoModules $module, string $url, array $parameters = []) $url = Str::replaceFirst('/', '', $url); } - $url = Str::finish($this->baseWebUrl($module), '/') . $url; + if (config('zoho.sandbox', false)) { + $url = Str::finish($this->baseWebUrlSandbox($module), '/') . $url; + } else { + $url = Str::finish($this->baseWebUrl($module), '/') . $url; + } + foreach ($parameters as $parameter => $value) { $url = $this->addParameterToUrlQuery($url, $parameter, $value); @@ -51,9 +56,9 @@ public function web(ZohoModules $module, string $url, array $parameters = []) } /** - * @internal * @param string $type * @return string + * @internal */ public function oauthApiUrl(string $type): string { @@ -119,11 +124,11 @@ protected function baseApiUrl(ZohoModules $module): string 'CN' => 'https://books.zoho.com.cn/api/v3', ], ZohoModules::CRM->value => [ - 'EU' => 'https://www.zohoapis.eu/crm/v2', - 'US' => 'https://www.zohoapis.com/crm/v2', - 'IN' => 'https://www.zohoapis.in/crm/v2', - 'AU' => 'https://www.zohoapis.com.au/crm/v2', - 'CN' => 'https://www.zohoapis.com.cn/crm/v2', + 'EU' => 'https://www.zohoapis.eu/crm/v3', + 'US' => 'https://www.zohoapis.com/crm/v3', + 'IN' => 'https://www.zohoapis.in/crm/v3', + 'AU' => 'https://www.zohoapis.com.au/crm/v3', + 'CN' => 'https://www.zohoapis.com.cn/crm/v3', ], ZohoModules::RECRUIT->value => [ 'EU' => 'https://recruit.zoho.eu/recruit/v2', @@ -135,6 +140,28 @@ protected function baseApiUrl(ZohoModules $module): string ][$module->value][$region]; } + protected function baseWebUrlSandbox(ZohoModules $module): string + { + $region = $this->config->region() ?? 'US'; + $organization = $this->config->currentOrganizationId(); + + return [ + ZohoModules::BOOKS->value => [ + // Not implemented yet + ], + ZohoModules::CRM->value => [ + 'EU' => "https://crmsandbox.zoho.eu/crm/{$organization}", + 'US' => "https://crmsandbox.zoho.com/crm/{$organization}", + 'IN' => "https://crmsandbox.zoho.in/crm/{$organization}", + 'AU' => "https://crmsandbox.zoho.com.eu/crm/{$organization}", + 'CN' => "https://crmsandbox.zoho.com.cn/crm/{$organization}", + ], + ZohoModules::RECRUIT->value => [ + // Not implemented yet + ], + ][$module->value][$region]; + } + protected function baseWebUrl(ZohoModules $module): string { $region = $this->config->region() ?? 'US'; diff --git a/tests/Clients/ZohoUrlFactoryTest.php b/tests/Clients/ZohoUrlFactoryTest.php index 9a820ae..f3ae62b 100644 --- a/tests/Clients/ZohoUrlFactoryTest.php +++ b/tests/Clients/ZohoUrlFactoryTest.php @@ -28,7 +28,7 @@ public function it_build_url_for_a_module(): void $urlFactory = app(ZohoUrlFactory::class); $this->assertEquals( - 'https://www.zohoapis.eu/crm/v2/users/4', + 'https://www.zohoapis.eu/crm/v3/users/4', $urlFactory->api(ZohoModules::CRM, '/users/4') ); @@ -49,7 +49,7 @@ public function it_returns_the_base_urls_for_the_api(): void $urlFactory = app(ZohoUrlFactory::class); $this->assertEquals( - 'https://www.zohoapis.eu/crm/v2', + 'https://www.zohoapis.eu/crm/v3', invade($urlFactory)->baseApiUrl(ZohoModules::CRM) ); @@ -130,7 +130,7 @@ public function it_build_using_api_method(): void $urlFactory = app(ZohoUrlFactory::class); $this->assertEquals( - 'https://www.zohoapis.eu/crm/v2/users/4', + 'https://www.zohoapis.eu/crm/v3/users/4', $urlFactory->api(ZohoModules::CRM, '/users/4') ); @@ -173,7 +173,7 @@ public function it_adds_parameter_to_api_url() $urlFactory = app(ZohoUrlFactory::class); $this->assertEquals( - 'https://www.zohoapis.eu/crm/v2/users/4?foo=1&bar=2&baz=3', + 'https://www.zohoapis.eu/crm/v3/users/4?foo=1&bar=2&baz=3', $urlFactory->api(ZohoModules::CRM, '/users/4', [ 'foo' => 1, 'bar' => 2, @@ -182,7 +182,7 @@ public function it_adds_parameter_to_api_url() ); $this->assertEquals( - 'https://www.zohoapis.eu/crm/v2/users/4?foo=1&bar=2&baz=3', + 'https://www.zohoapis.eu/crm/v3/users/4?foo=1&bar=2&baz=3', $urlFactory->api(ZohoModules::CRM, '/users/4?foo=1', [ 'bar' => 2, 'baz' => 3, @@ -212,4 +212,18 @@ public function it_adds_parameter_to_web_url() ]) ); } + + /** @test */ + public function it_uses_the_sandbox_for_web() + { + config(['zoho.sandbox' => true]); + + $urlFactory = app(ZohoUrlFactory::class); + + $this->assertEquals( + 'https://crmsandbox.zoho.eu/crm/1234/tab/Potentials/292528000000000000', + $urlFactory->web(ZohoModules::CRM, '/tab/Potentials/292528000000000000') + ); + + } } \ No newline at end of file diff --git a/tests/Zoho/ZohoHttpTest.php b/tests/Zoho/ZohoHttpTest.php index 7510f91..a4bd232 100644 --- a/tests/Zoho/ZohoHttpTest.php +++ b/tests/Zoho/ZohoHttpTest.php @@ -69,7 +69,7 @@ public function it_can_make_a_request_using_header(): void public function it_can_make_a_request_zoho_url_facade(): void { Http::fake([ - 'https://www.zohoapis.com/crm/v2/users' => Http::response($this->fakeResponse), + 'https://www.zohoapis.com/crm/v3/users' => Http::response($this->fakeResponse), ]); $this->assertEquals(