optimacloud/zoho-laravel

Laravel Zoho API V3 包

6.0.7 2024-04-24 13:54 UTC

This package is auto-updated.

Last update: 2024-09-24 15:02:40 UTC


README

Packagist Version Packagist Downloads

此包曾用于与 Zoho V6 API CRM 集成

要求

  • PHP >= 8.0
  • Laravel >= 8.0

注册 Zoho 客户端

由于 Zoho CRM API 使用 OAuth2 标准进行认证,您应在 Zoho 上注册您的客户端应用程序。要注册您的应用程序

  1. 访问此页面 https://api-console.zoho.com/
  2. 点击 添加客户端
  3. 选择 自用客户端
  4. 通过提供必要的权限范围、时间长度(生成的令牌的有效期)和权限描述来创建授权令牌。
  5. 您的客户端应用程序现已创建并显示。
  6. 选择创建的 OAuth 客户端。
  7. 在创建授权令牌时使用此权限 aaaserver.profile.READ,ZohoCRM.modules.ALL,ZohoCRM.settings.ALL

安装

您可以通过 composer require 安装此包

composer require optimacloud/zoho-laravel

安装包后,您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="zoho-laravel-config"

然后您需要通过运行以下命令创建 OAuth 客户端并从 Zoho 获取凭据

php artisan zoho:install

您需要将以下变量添加到您的 .env 文件中。使用之前注册应用程序时获得的凭据。

ZOHO_AUTH_FLOW_TYPE=grantToken
ZOHO_CLIENT_ID="Code from Client Secrit Section"
ZOHO_CLIENT_SECRET="Code from Client Secrit Section"
ZOHO_REDIRECT_URI="${APP_URL}/zoho/oauth2callback"
ZOHO_CURRENT_USER_EMAIL=admin@example.com
ZOHO_TOKEN="Code Generated from last step"

# available datacenters (USDataCenter, EUDataCenter, INDataCenter, CNDataCenter, AUDataCenter)
ZOHO_DATACENTER=USDataCenter
ZOHO_SANDBOX=true

之后,您需要运行以下命令将令牌和刷新令牌添加到您的存储中

php artisan zoho:grant

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="zoho-laravel-config"

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="zoho-laravel-migrations"
php artisan migrate

环境

在某些情况下,您可能希望强制 Zoho 使用 Zoho 的某个环境,因此您可以转到 AppServiceProvider 并使用 Zoho::useEnvironment() 方法

Zoho::useEnvironment(EUDataCenter::DEVELOPER());

这将覆盖配置设置。

用法

从 Zoho 获取所有模块。

use Optimacloud\Zoho\ZohoManager;

$response = ZohoManager::make(self::TESTING_MODULE);
$modules  = $response->getAllModules();

模型可以使用如下方式使用:-

v1.1.0 版本开始可用

Zohoable 作为扩展类添加如下:-

use Optimacloud\Zoho\Zohoable;
use Optimacloud\Zoho\ZohoManager;

class Invoice extends Zohoable {
    
    // this is your Zoho module API Name
    protected $zoho_module_name = 'Payments';

    public function searchCriteria(){
        // you should return string of criteria that you want to find current record in crm with.
        //EX:
        return ZohoManager::make('Payments')
                            ->where('PaymentID', $this->payment_id)
                            ->andWhere('Order_ID', $this->order_id)
                            ->getCriteria();
    }

    public function zohoMandatoryFields() {
        // you should return array of mandatory fields to create module from this model
        return ['Base_Currency' => $this->currency];
    }
}

现在您可以使用发票如下

$invoice = \App\Invoice::find(1);

// to check if has zoho id stored on local database or not
$invoice->hasZohoId();

// to return the stored zoho id
$invoice->zohoId();

// that will search on zoho with provided criteria to find the record and associated your model with returned id if exist
// if you provided an `id` that will be used instead of searching on Zoho
$invoice->createOrUpdateZohoId($id = null);

// you can also send current model to zoho
// that wil use `zohoMandatoryFields` method to Serialize model to zohoObject
// Also you can pass additional fields as array to this method
$invoice->createAsZohoable($options = []);

注意:要像这样使用发票,您必须在数据库中有一个名为 invoices 的表,就像使用任何 Laravel 模型一样。这允许您将数据保存到数据库中,并能够将其链接到 zohos 表,并使用 Zohoable 中的所有功能。如果您不打算以这种方式使用 Zohoable 模型,请使用以下 CRUD 函数。

CRUD 可以这样使用:-

读取

use Optimacloud\Zoho\ZohoManager;

// we can now deal with leads module
$leads = ZohoManager::useModule('Leads');

// OR

$leads = ZohoManager::make('Leads');

// find record by its ID
$lead = $leads->getRecord('3582074000002383003');

更新

$record = new Record();
$record->setId('3582074000002383003');

// Set value as field
$record->addFieldValue(Leads::FirstName(), 'Updated');
$record->addFieldValue(Leads::LastName(), 'Record');

// Set value as key value
$lead->setKeyValue('Phone', '5555555555552');

// Then call update() method
$response = $leads->update($record);

创建

// create the record into zoho crm then get the created instance data
$response = $leads->create([
    'First_Name' => 'Amr',
    'Last_Name' => 'Emad',
    'Email' => 'test@optimacloud.com',
    'Phone' => '012345678910',
]);

删除

// delete record by its id
$lead = $leads->delete('3582074000002383003');

搜索

单词
use Optimacloud\Zoho\ZohoManager;

$records = ZohoManager::useModule('Leads')->searchRecordsByWord('word to be searched');
$first_record = $records[0];
电话
use Optimacloud\Zoho\ZohoManager;

$records = ZohoManager::useModule('Leads')->searchRecordsByPhone('12345678910');
$first_record = $records[0];
电子邮件
use Optimacloud\Zoho\ZohoManager;

$records = ZohoManager::make('Leads')->searchRecordsByEmail('nobody@optimacloud.com');
$first_record = $records[0];
标准
use Optimacloud\Zoho\ZohoManager;

$records = ZohoManager::make('Leads')->searchRecordsByCriteria('(City:equals:NY) and (State:equals:Alden)')->get();
$first_record = $records[0];
自定义
use Optimacloud\Zoho\ZohoManager;

$records = ZohoManager::make('Leads')
                    ->where('City', 'NY')
                    ->andWhere('State','Alden')
                    ->get();

$first_record = $records[0];

国际版本

如果您使用的是 zoho.com,则无需更改任何内容。

如果您使用的是 zoho.eu,请添加到 .env

ZOHO_ACCOUNTS_URL=https://accounts.zoho.eu
ZOHO_API_BASE_URL=www.zohoapis.eu

如果您使用的是 zoho.com.cn,请添加到 .env

ZOHO_ACCOUNTS_URL=https://accounts.zoho.com.cn
ZOHO_API_BASE_URL=www.zohoapis.com.cn

支持

联系
optimacloud.com
aemad@asciisd.com
+2-010-1144-1444

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

致谢

许可

麻省理工学院许可证(MIT)。请参阅许可文件获取更多信息。