Skip to content

Commit

Permalink
- [X] Upgrade SDK to version 6
Browse files Browse the repository at this point in the history
- [] Added missing methods
  • Loading branch information
aemaddin committed Mar 10, 2024
1 parent 41cbdd2 commit 40d359f
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 68 deletions.
27 changes: 22 additions & 5 deletions README.md
Expand Up @@ -19,11 +19,10 @@ your app:
1. Visit this page [https://api-console.zoho.com/](https://api-console.zoho.com)
2. Click on `ADD CLIENT`.
3. Choose a `Self Client`.
4. Enter `Scope: aaaserver.profile.READ,ZohoCRM.modules.ALL,ZohoCRM.settings.ALL`.
5. Create grant token by providing the necessary scopes, time duration (the duration for which the generated token is
4. Create grant token by providing the necessary scopes, time duration (the duration for which the generated token is
valid) and Scope Description.
6. Your Client app would have been created and displayed by now.
7. Select the created OAuth client.
5. Your Client app would have been created and displayed by now.
6. Select the created OAuth client.

## Installation

Expand All @@ -32,14 +31,26 @@ You can install the package via `composer require`:
```bash
composer require asciisd/zoho-v3
```
After installing the package you can publish the config file with:

```bash
php artisan vendor:publish --tag="zoho-v3-config"
```

after that you need to create the OAuth client and get the credentials from Zoho by run the following command:

```bash
php artisan zoho:install
```

You'll need to add the following variables to your .env file. Use the credentials previously obtained registering your
application.

```dotenv
ZOHO_AUTH_FLOW_TYPE=grantToken
ZOHO_CLIENT_ID="Code from Client Secrit Section"
ZOHO_CLIENT_SECRET="Code from Client Secrit Section"
ZOHO_REDIRECT_URI=https://APP_URL/zoho/oauth2callback
ZOHO_REDIRECT_URI="${APP_URL}/zoho/oauth2callback"
ZOHO_CURRENT_USER_EMAIL=admin@example.com
ZOHO_TOKEN="Code Generated from last step"
Expand All @@ -48,6 +59,12 @@ ZOHO_DATACENTER=USDataCenter
ZOHO_SANDBOX=true
```

After that you need to run the following command to add token and refresh token to your storage

```bash
php artisan zoho:grant
```

You can publish the config file with:

```bash
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -15,7 +15,7 @@
"php": "^8.1|^8.2",
"illuminate/contracts": "^9.0|^10.0",
"spatie/laravel-package-tools": "^1.14.2",
"zohocrm/php-sdk-5.0": "^1.0"
"zohocrm/php-sdk-6.0": "^1.0"
},
"require-dev": {
"nunomaduro/collision": "^6.0",
Expand Down Expand Up @@ -53,6 +53,6 @@
}
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true
}
4 changes: 3 additions & 1 deletion config/zoho.php
Expand Up @@ -25,7 +25,7 @@

/*
|--------------------------------------------------------------------------
| Api's Url
| Api Url
|--------------------------------------------------------------------------
|
| Zoho's Apis url
Expand Down Expand Up @@ -61,6 +61,8 @@
|
| Which authentication flow to use
| Zoho SDK Supports grantToken, refreshToken and accessToken
| You can use on of the following values:
| grantToken, refreshToken, accessToken
|
*/
'auth_flow_type' => env('ZOHO_AUTH_FLOW_TYPE', 'grantToken'),
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/ZohoAuthentication.php
Expand Up @@ -31,12 +31,11 @@ public function __construct()
/**
* Execute the console command.
*/
public function handle()
public function handle(): void
{
$client_id = config('zoho.client_id');
$client_domain = config('zoho.redirect_uri');
$accounts_url = config('zoho.accounts_url');

$scope = config('zoho.oauth_scope');
$prompt = 'consent';
$response_type = 'code';
Expand Down
8 changes: 5 additions & 3 deletions src/Commands/ZohoSetupCommand.php
Expand Up @@ -3,8 +3,8 @@
namespace Asciisd\Zoho\Commands;

use Asciisd\Zoho\Zoho;
use Illuminate\Console\Command;
use com\zoho\crm\api\exception\SDKException;
use Illuminate\Console\Command;

class ZohoSetupCommand extends Command
{
Expand Down Expand Up @@ -33,15 +33,17 @@ public function __construct()
*/
public function handle()
{
$grantToken = $this->argument('token');
$grantToken = $this->argument('token') ?? config('zoho.token');

if ( ! $grantToken && ! config('zoho.token')) {
if (!$grantToken) {
$this->error('The Grant Token is required.');
$this->info('generate token by visit : https://accounts.zoho.com/developerconsole');

return 0;
}

$this->info('Token: '.$grantToken);

Zoho::initialize($grantToken);

$this->info('Zoho CRM has been set up successfully.');
Expand Down
17 changes: 7 additions & 10 deletions src/Concerns/ManagesActions.php
Expand Up @@ -9,16 +9,13 @@
use com\zoho\crm\api\record\ActionWrapper;
use com\zoho\crm\api\record\SuccessResponse;
use com\zoho\crm\api\record\RecordOperations;
use com\zoho\crm\api\record\SuccessfulConvert;
use com\zoho\crm\api\record\ConvertBodyWrapper;
use com\zoho\crm\api\record\DeleteRecordsParam;
use com\zoho\crm\api\record\ConvertActionWrapper;

trait ManagesActions
{
public function create(array $args = []): SuccessResponse|array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$bodyWrapper = new BodyWrapper();
$record = new Record();

Expand All @@ -29,18 +26,18 @@ public function create(array $args = []): SuccessResponse|array
$bodyWrapper->setData([$record]);

return $this->handleActionResponse(
$recordOperations->createRecords($this->module_api_name, $bodyWrapper)
$recordOperations->createRecords($bodyWrapper)
);
}

public function update(Record $record): SuccessResponse|array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$request = new BodyWrapper();
$request->setData([$record]);

return $this->handleActionResponse(
$recordOperations->updateRecords($this->module_api_name, $request)
$recordOperations->updateRecords($request)
);
}

Expand All @@ -51,22 +48,22 @@ public function deleteRecord(string $record_id): SuccessResponse|array

public function delete(array $recordIds): SuccessResponse|array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$paramInstance = new ParameterMap();

foreach ($recordIds as $id) {
$paramInstance->add(DeleteRecordsParam::ids(), $id);
}

return $this->handleActionResponse(
$recordOperations->deleteRecords($this->module_api_name, $paramInstance)
$recordOperations->deleteRecords($paramInstance)
);
}

public function convertLead(string $id, $data)
{

$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$body = new ConvertBodyWrapper();
$body->setData($data);

Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/ManagesBulkActions.php
Expand Up @@ -11,15 +11,15 @@ trait ManagesBulkActions
{
public function bulkCreate(array $records = []): ActionWrapper|array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$bodyWrapper = new BodyWrapper();

$bodyWrapper->setData($records);
$trigger = array("approval", "workflow", "blueprint");
$bodyWrapper->setTrigger($trigger);

return $this->handleBulkActionResponse(
$recordOperations->createRecords($this->module_api_name, $bodyWrapper)
$recordOperations->createRecords($bodyWrapper)
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Concerns/ManagesModules.php
Expand Up @@ -2,7 +2,6 @@

namespace Asciisd\Zoho\Concerns;

use com\zoho\crm\api\modules\Module;
use com\zoho\crm\api\modules\APIException;
use com\zoho\crm\api\modules\ModulesOperations;
use com\zoho\crm\api\modules\ResponseWrapper as ModulesResponseWrapper;
Expand All @@ -16,7 +15,7 @@ public function getAllModules(): array
);
}

public function getModule(): Module
public function getModule()
{
return $this->handleModuleResponse(
(new ModulesOperations())->getModule($this->module_api_name)
Expand Down
24 changes: 12 additions & 12 deletions src/Concerns/ManagesRecords.php
Expand Up @@ -15,7 +15,7 @@ trait ManagesRecords
public function getRecord(string $record_id): Record
{
return $this->handleRecordResponse(
(new RecordOperations())->getRecord($record_id, $this->module_api_name)
(new RecordOperations($this->module_api_name))->getRecord($record_id)
)[0];
}

Expand All @@ -24,7 +24,7 @@ public function getRecord(string $record_id): Record
*/
public function getRecords($page = 1, $perPage = 200, $sortBy = 'id', $sortOrder = 'desc'): array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$paramInstance = new ParameterMap();

$paramInstance->add(GetRecordsParam::page(), $page);
Expand All @@ -33,13 +33,13 @@ public function getRecords($page = 1, $perPage = 200, $sortBy = 'id', $sortOrder
$paramInstance->add(GetRecordsParam::sortOrder(), $sortOrder);

return $this->handleRecordResponse(
$recordOperations->getRecords($this->module_api_name, $paramInstance)
$recordOperations->getRecords($paramInstance)
);
}

public function searchRecordsByCriteria(string $criteria, $page = 1, $perPage = 200, $sortBy = 'id', $sortOrder = 'desc'): array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$paramInstance = new ParameterMap();

$paramInstance->add(SearchRecordsParam::criteria(), $criteria);
Expand All @@ -49,13 +49,13 @@ public function searchRecordsByCriteria(string $criteria, $page = 1, $perPage =
$paramInstance->add(GetRecordsParam::sortOrder(), $sortOrder);

return $this->handleRecordResponse(
$recordOperations->searchRecords($this->module_api_name, $paramInstance)
$recordOperations->searchRecords($paramInstance)
);
}

public function searchRecordsByWord(string $word, $page = 1, $perPage = 200, $sortBy = 'id', $sortOrder = 'desc'): array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$paramInstance = new ParameterMap();

$paramInstance->add(SearchRecordsParam::word(), $word);
Expand All @@ -65,13 +65,13 @@ public function searchRecordsByWord(string $word, $page = 1, $perPage = 200, $so
$paramInstance->add(GetRecordsParam::sortOrder(), $sortOrder);

return $this->handleRecordResponse(
$recordOperations->searchRecords($this->module_api_name, $paramInstance)
$recordOperations->searchRecords($paramInstance)
);
}

public function searchRecordsByPhone(string $phone, $page = 1, $perPage = 200, $sortBy = 'id', $sortOrder = 'desc'): array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$paramInstance = new ParameterMap();

$paramInstance->add(SearchRecordsParam::phone(), $phone);
Expand All @@ -81,13 +81,13 @@ public function searchRecordsByPhone(string $phone, $page = 1, $perPage = 200, $
$paramInstance->add(GetRecordsParam::sortOrder(), $sortOrder);

return $this->handleRecordResponse(
$recordOperations->searchRecords($this->module_api_name, $paramInstance)
$recordOperations->searchRecords($paramInstance)
);
}

public function searchRecordsByEmail(string $email, $page = 1, $perPage = 200, $sortBy = 'id', $sortOrder = 'desc'): array
{
$recordOperations = new RecordOperations();
$recordOperations = new RecordOperations($this->module_api_name);
$paramInstance = new ParameterMap();

$paramInstance->add(SearchRecordsParam::email(), $email);
Expand All @@ -97,7 +97,7 @@ public function searchRecordsByEmail(string $email, $page = 1, $perPage = 200, $
$paramInstance->add(GetRecordsParam::sortOrder(), $sortOrder);

return $this->handleRecordResponse(
$recordOperations->searchRecords($this->module_api_name, $paramInstance)
$recordOperations->searchRecords($paramInstance)
);
}

Expand All @@ -116,7 +116,7 @@ private function handleRecordResponse($response): array
if ($responseHandler instanceof RecordResponseWrapper) {
return $responseHandler->getData();
} elseif ($responseHandler instanceof APIException) {
logger()->error($responseHandler->getMessage()->getValue());
logger()->error($responseHandler->getMessage());
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/Concerns/ManagesTags.php
Expand Up @@ -2,6 +2,7 @@

namespace Asciisd\Zoho\Concerns;

use com\zoho\crm\api\tags\AddTagsParam;
use com\zoho\crm\api\tags\Tag;
use com\zoho\crm\api\ParameterMap;
use com\zoho\crm\api\tags\GetTagsParam;
Expand All @@ -14,9 +15,7 @@
use com\zoho\crm\api\tags\ConflictWrapper;
use com\zoho\crm\api\tags\CreateTagsParam;
use com\zoho\crm\api\exception\SDKException;
use com\zoho\crm\api\tags\AddTagsToRecordParam;
use com\zoho\crm\api\tags\GetRecordCountForTagParam;
use com\zoho\crm\api\tags\RemoveTagsFromRecordParam;
use com\zoho\crm\api\tags\AddTagsToMultipleRecordsParam;
use com\zoho\crm\api\tags\RemoveTagsFromMultipleRecordsParam;
use com\zoho\crm\api\tags\ResponseWrapper as TagResponseWrapper;
Expand Down Expand Up @@ -217,14 +216,14 @@ public function addTagsToRecord(string $recordId, array $tagNames): array
$paramInstance = new ParameterMap();

foreach ($tagNames as $tagName) {
$paramInstance->add(AddTagsToRecordParam::tagNames(), $tagName);
$paramInstance->add(AddTagsParam::overWrite(), $tagName);
}

$paramInstance->add(AddTagsToRecordParam::overWrite(), "false");
$paramInstance->add(AddTagsParam::overWrite(), "false");

//Call addTagsToRecord method that takes paramInstance, moduleAPIName and recordId as parameter
return $this->handleTagResponse(
$tagsOperations->addTagsToRecord(
$tagsOperations->addTagsToMultipleRecords(
$recordId,
$this->module_api_name,
$paramInstance
Expand All @@ -250,12 +249,12 @@ public function removeTagsFromRecord(string $recordId, array $tagNames): array

foreach($tagNames as $tagName)
{
$paramInstance->add(RemoveTagsFromRecordParam::tagNames(), $tagName);
$paramInstance->add(RemoveTagsFromMultipleRecordsParam::ids(), $tagName);
}

//Call removeTagsFromRecord method that takes paramInstance, moduleAPIName and recordId as parameter
return $this->handleTagResponse(
$tagsOperations->removeTagsFromRecord(
$tagsOperations->removeTagsFromMultipleRecords(
$recordId,
$this->module_api_name,
$paramInstance)
Expand Down Expand Up @@ -364,7 +363,7 @@ private function handleTagResponse($response): array
}

if ($responseHandler instanceof APIException) {
logger()->error($responseHandler->getMessage()->getValue());
logger()->error($responseHandler->getMessage());
}
}
}
Expand Down

0 comments on commit 40d359f

Please sign in to comment.