Laravel 8 - Create Contact in ZOHO Crm Tutorial Example

  556 views   2 weeks ago Laravel

In this article, I will share with you how to integrate Zoho API in laravel 8 application and how to create contact in Zoho CRM using laravel. Zoho is the best application for creating lead, contact and you can manage your sale there.

So, recently i was working in one laravel sale-related application and they want to create a contact in Zoho platform from the laravel application. so, here I am sharing with you how to implement create a contact in Zoho CRM from the laravel 8.

Simply, follow the step. i make it this article very simple. 

Step - 1 : Create Routes

Here, we create two routes for add contact in Zoho CRM from the laravel 8 application.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\ZohoController;

// Listing contact route
Route::get('contacts', [ContactController::class, 'index'])->name('contacts');

// Add contact in ZOHO
Route::get('zohocrmauth', [ZohoController::class, 'auth'])->name('zohocrmauth');
Route::get('zohocrm', [ZohoController::class, 'store'])->name('zohocrm');

Step - 2 : Add Controller and Models

In, this step we need to created "ContactController", "ZohoController" and "Contact" Model using the following artisan command.

php artisan make:controller ContactController --resource --model=Contact
php artisan make:controller ZohoController

After the running above command your model and controller created in your application now just write the following code in it.

app/Http/Controllers/ContactController.php

namespace App\Http\Controllers;

use App\Models\Contact;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function index()
    {
    	$data = Contact::get();
        return view('articles.index', compact('data'));
    }
}

app/Http/Controllers/ZohoController.php

namespace App\Http\Controllers;

use App\Models\Contact;
use Illuminate\Http\Request;

class ZohoController extends Controller
{
    public function auth(Request $request)
    {
        $uri = route('zohocrm');
        $scope =  'ZohoInvoice.contacts.Create';
        $clientid = 'here your zoho clientid';
        $accestype = 'offline';

        $redirectTo = 'https://accounts.zoho.com/oauth/v2/auth' . '?' . http_build_query(
        [
        'client_id' => $clientid,
        'redirect_uri' => $uri,
        'scope' => 'ZohoInvoice.contacts.Create',
        'response_type' => 'code',
        ]);

        \Session()->put('zoho_contact_id', $request->id);

        return redirect($redirectTo);
    }

    public function store(Request $request)
    {
        $input = $request->all();
        $contact_id = \Session::get('zoho_contact_id');
        $client_id = 'Here your zoho crm client_id';
        $client_secret = 'Here your zoho crm client_secret';
        \Session::forget('zoho_contact_id');

        // Get ZohoCRM Token
        $tokenUrl = 'https://accounts.zoho.com/oauth/v2/token?code='.$input["code"].'&client_id='.$client_id.'&client_secret='.$client_secret.'&redirect_uri='.route('zohocrm').'&grant_type=authorization_code';

        $tokenData = [

        ];

        $curl = curl_init();     
        curl_setopt($curl, CURLOPT_VERBOSE, 0);     
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);     
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);     
        curl_setopt($curl, CURLOPT_TIMEOUT, 300);   
        curl_setopt($curl, CURLOPT_POST, TRUE);//Regular post  
        curl_setopt($curl, CURLOPT_URL, $tokenUrl);     
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);     
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($tokenData));

        $tResult = curl_exec($curl);
        curl_close($curl);
        $tokenResult = json_decode($tResult);
        // dd($tokenResult->);
        if(isset($tokenResult->access_token) && $tokenResult->access_token != '') {
            $getContact = Contact::where('id', $contact_id)->first();

            // Add Contact in ZohoCRM
            $jsonData = '{
                "contact_name": "'.$getContact->company_name.'",
                "company_name": "'.$getContact->company_name.'",
                "website": "'.$getContact->website_url.'",
                "billing_address": {
                    "attention": "Mr.'.$getContact->userName.'",
                    "address": "'.$getContact->getApplication.'",
                    "street2": "",
                    "state_code": "",
                    "city": "'.$getContact->city.'",
                    "state": "'.$getContact->state_country.'",
                    "zip": '.$getContact->zip_code.',
                    "country": "'.$getContact->country.'",
                    "fax": "",
                    "phone": "'.$getContact->phone_no.'"
                },
                "shipping_address": {
                    "attention": "Mr.'.$getContact->userName.'",
                    "address": "'.$getContact->getApplication.'",
                    "street2": "",
                    "state_code": "",
                    "city": "'.$getContact->city.'",
                    "state": "'.$getContact->state_country.'",
                    "zip": '.$getContact->zip_code.',
                    "country": "'.$getContact->country.'",
                    "fax": "",
                    "phone": "'.$getContact->phone_no.'"
                },
                "contact_persons": [
                    {
                        "salutation": "Mr",
                        "first_name": "'.$getContact->userName.'",
                        "last_name": "",
                        "email": "'.$getContact->userEmail.'",
                        "phone": "'.$getContact->phone_no.'",
                        "mobile": "'.$getContact->userMobile.'",
                        "is_primary_contact": true
                    }
                ]
            }';

            $curl = curl_init('https://invoice.zoho.com/api/v3/contacts');
            curl_setopt($curl, CURLOPT_VERBOSE, 0);     
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);     
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);     
            curl_setopt($curl, CURLOPT_TIMEOUT, 300);   
            curl_setopt($curl, CURLOPT_POST, TRUE);//Regular post  
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);     
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                "Authorization: Zoho-oauthtoken ".$tokenResult->access_token,
                "X-com-zoho-invoice-organizationid: 688931512"
            ) );
            curl_setopt($curl, CURLOPT_POSTFIELDS,'JSONString='.$jsonData);
            
            //Execute cUrl session
            $cResponse = curl_exec($curl);
            curl_close($curl);

            $contactResponse = json_decode($cResponse);
            // echo "<pre>";
            // print_r($jsonData);
            // dd($contactResponse);
            if(isset($contactResponse->code) && $contactResponse->code == 0) {
                \Session::put('success','Contact created in ZohoCRM successfully.!');
                return redirect()->route('contacts');
            } else {
                \Session::put('error','Contact not create, please try again.!!');
                return redirect()->route('contacts');
            }
        } else {
            \Session::put('error','ZohoCRM token not generated, please try again.!!');
            return redirect()->route('contacts');
        }        
    }
}

Step - 3 : Create Contact Listing View

Now we need to create a very simple listing view for our "contacts". just write the following simple bootstrap 4 views for listing data in table format.

resources/views/contacts/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 Contact add in Zoho CRM - laravelcode.com</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
  
<div class="container">
    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif
   
    <table class="table table-bordered">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Email</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($data as $key => $value)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $value->name }}</td>
            <td>{{ $value->email }}</td>
            <td>
                <a class="btn btn-info" href="{{ route('zohocrmauth', ['id' => $value->id]) }}">Add Contact in ZOHO</a>
            </td>
        </tr>
        @endforeach
    </table>
</div>
   
</body>
</html>

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles