Skip to content

Commit

Permalink
Upgrade URLs to Zoho V3 and add sandbox urls to web
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaconnier committed Jul 20, 2022
1 parent dd9b383 commit a007100
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config/zoho.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 34 additions & 7 deletions src/Clients/ZohoUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
{
Expand Down Expand Up @@ -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',
Expand All @@ -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';
Expand Down
24 changes: 19 additions & 5 deletions tests/Clients/ZohoUrlFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);

Expand All @@ -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)
);

Expand Down Expand Up @@ -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')
);

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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')
);

}
}
2 changes: 1 addition & 1 deletion tests/Zoho/ZohoHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a007100

Please sign in to comment.