Skip to content

Server to Server Communication

Andrew Johnson edited this page Mar 3, 2020 · 1 revision

For the purposes of this page, I will be referencing Laravel specific configs. However, these steps can be applied to any php application and do not require the use of Laravel.

Also assuming that you have already installed the Infusionsoft PHP-SDK through composer.

1. Create a database table/model

Database migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateApiTokenTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
  public function up() {
      Schema::create('api_tokens', function (Blueprint $table) {
          $table->increments('id');
          $table->string('provider')->unique();
          $table->text('token');
          $table->text('url')->nullable();
      });
  }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
  public function down() {
      Schema::dropIfExists('api_tokens');
  }
}

Migrate your database php artisan migrate

APIToken Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ApiToken extends Model {
  protected $table = 'api_tokens';
  public $timestamps = false;
}

2. Create a callback route to fetch your Infusionsoft token

In your routes file add

Route::get('/infusionsoft/callback', 'InfusionsoftController@callback');

Note this route path should match the INFUSIONSOFT_REDIRECT_URL environment variable, otherwise the oauth process will fail.

3. Create an Infusionsoft Controller

php artisan make:controller InfusionsoftController

4. Create a callback function

You will want to replace app-name with the name of your infusionsoft application. This function is what handles storing your api token so that it can be used latter.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ApiToken;
use Infusionsoft\Infusionsoft;

class InfusionsoftController extends Controller {
  public function callback (Request $request) {
    $apiOBJ = ApiToken::where('provider', '=', 'app-name.infusionsoft.com')->first();
    if ($apiOBJ != null && $apiOBJ->token) {
      Infusionsoft::setToken(unserialize($apiOBJ->token));
    }

    if ($request->has('code') && !Infusionsoft::getToken()) {
      Infusionsoft::requestAccessToken($request->get('code'));
    }

    if (Infusionsoft::getToken()) {
      $apiOBJ = ApiToken::where('provider', '=', 'app-name.infusionsoft.com')->first();
      if($apiOBJ == null) {
        $apiOBJ = new ApiToken;
        $apiOBJ->provider = "app-name.infusionsoft.com";
        $apiOBJ->token = serialize(Infusionsoft::getToken());
        $apiOBJ->save();
      } else {
        $apiOBJ->token = serialize(Infusionsoft::getToken());
        $apiOBJ->save();
      }
    }
    return redirect()->to('/');
  }
}

To authorize your new application you will want to visit the url that is generated by Infusionsoft::getAuthorizationUrl() This will redirect you to Infusionsoft to complete the oauth process.

5. Making API Calls

Now that you have your token you are ready to start making api calls from your server without having to go through the oauth process every time.

Retrieving your API token from your database and make a call

$apiOBJ = ApiToken::where('provider', '=', 'app-name.infusionsoft.com')->first();
Infusionsoft::setToken(unserialize($apiOBJ->token));

if(Infusionsoft::isTokenExpired()) {
  Infusionsoft::refreshAccessToken();
  $apiOBJ->token = serialize(Infusionsoft::getToken());
  $apiOBJ->save();
}

Try and make your api call

try {
      $tags = Infusionsoft::data()->query('ContactGroupAssign', 1000, 0, ['ContactId' => 123],['DateCreated','GroupId','ContactGroup'], 'DateCreated', false);
    } catch (\Infusionsoft\TokenExpiredException $e) {
      Infusionsoft::refreshAccessToken();
      $apiOBJ->token = serialize(Infusionsoft::getToken());
      $apiOBJ->save();
      $tags = Infusionsoft::data()->query('ContactGroupAssign', 1000, 0, ['ContactId' => 123],['DateCreated','GroupId','ContactGroup'], 'DateCreated', false);
    }