PHP code example of revolution / laravel-freee-sdk

1. Go to this page and download the library: Download revolution/laravel-freee-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

revolution / laravel-freee-sdk example snippets


use Freee\Accounting\Configuration;
use Revolution\Freee\Facades\Freee;

class Controller
{
    public function meFacade(Request $request)
    {
        $user = $request->user();

        // Freee::driver()
        // Freee::driver('accounting')
        // デフォルトはaccountingなのでどちらでも同じ。
        // driver()を省略しても同じ。
        // 現在はaccounting SDKのみ対応なのでFreee=AccountingClientのインスタンスの認識でいいです。
        // 今後SDKが増えたら変わる。

        // 以降の使い方はaccountingが前提。

        // 普通の使い方と同じように$configを作ってセット。
        $config = Configuration::getDefaultConfiguration()->setAccessToken($user->token);
        // Freee::config($config)->companies()と繋げることも可能
        Freee::config($config);

        // companies()はCompaniesApiクラスのインスタンスを返すマジックメソッド。
        // 先頭のみ小文字、最後のApiを除いたメソッド名がルール。TrialBalanceApiならtrialBalance()
        // getCompanies()はCompaniesApiクラスのメソッド。後の使い方は同じ。
        // Responseは(string)でjsonに変換されるのでdecodeしてarrayで取得。Laravelならこの形のほうが扱いやすい。
        $companiesResponse = json_decode((string) Freee::companies()->getCompanies(), true);
        $targetCompanyId = $companiesResponse['companies'][0]['id'];

        // deals()はDealsApi。変換せずにそのままviewへ。null多すぎの解決方法はmacroで。
        $limit = 5;
        $deals = Freee::deals()->getDeals(
            $targetCompanyId,
            null, null, null, null, null, null, null, null, null, null, null, null,
            $limit)->getDeals();

        return view('account.me', compact('user', 'deals'));
    }
}

'Freee' => Revolution\Freee\Facades\Freee::class,

    public function testMeFacade()
    {
        Freee::shouldReceive('config');

        // shouldReceiveですべてモックするのでAPIは実行されない。
        Freee::shouldReceive('companies->getCompanies')->andReturn(json_encode([
            'companies' => [
                [
                    'id' => 1,
                    'role' => 'admin',
                ]
            ]
        ]));

        Freee::shouldReceive('deals->getDeals->getDeals')->andReturn([]);

        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
            ->get(route('me_facade'));

        $response->assertStatus(200)
            ->assertViewHas(['user', 'deals'])
            ->assertSeeText($user->email);
    }

use Revolution\Freee\Traits\FreeeSDK;

class User
{
    use FreeeSDK;

    /**
     * @param  string  $driver
     * 
     * @return string
     */
    protected function tokenForFreee(string $driver): string
    {
        return $this->token ?? '';
    }
}

    public function meTrait(Request $request)
    {
        $user = $request->user();

        // $user->freee()はconfigをセット済のAccountingClientなので後は同じ。freee()もaccountingがデフォルト。
        // 他のSDKにも対応したら$user->freee('custom')->で使用。
        $companiesResponse = json_decode($user->freee()->companies()->getCompanies(), true);
        $targetCompanyId = $companiesResponse['companies'][0]['id'];
        
        $limit = 5;
        $deals = $user->freee()->deals()->getDeals(
            $targetCompanyId,
            null, null, null, null, null, null, null, null, null, null, null, null,
            $limit)->getDeals();

        return view('account.me', compact('user', 'deals'));
    }

    public function testMeTrait()
    {
        // freee()内では毎回driver()->config()が実行されているのでモック時にも必要
        Freee::shouldReceive('driver->config->companies->getCompanies')->andReturn(json_encode([
            'companies' => [
                [
                    'id' => 1,
                    'role' => 'admin',
                ]
            ]
        ]));

        Freee::shouldReceive('driver->config->deals->getDeals->getDeals')->andReturn([]);

        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
            ->get(route('me_trait'));

        $response->assertStatus(200)
            ->assertViewHas(['user', 'deals'])
            ->assertSeeText($user->email);
    }

use Revolution\Freee\Facades\Freee;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // AccountingClientへのmacroの追加なことに注意。
        // 他のSDKにも対応した場合はFreee::driver('')->macro()
        Freee::macro('getCompanyId', function ($index = 0) {
            $companiesApi = $this->companies();
            $companiesResponse = $companiesApi->getCompanies();
            return $companiesResponse->getCompanies()[$index]->getId();
        });

        Freee::driver()->macro('getDeals', function ($company_id, $limit = 5) {
            $dealsApi = $this->deals();
            $dealsResponse = $dealsApi->getDeals(
                $company_id,
                null, null, null, null, null, null, null, null, null, null, null, null,
                $limit);
            return $dealsResponse->getDeals();
        });
    }
}

    public function meMacro(Request $request)
    {
        $user = $request->user();

        // 長いコードをmacroに追い出したのでこれだけで済む
        $company_id = $user->freee()->getCompanyId();

        $deals = $user->freee()->getDeals($company_id);

        return view('account.me', compact('user', 'deals'));
    }

    public function testMeMacro()
    {
        // freee()なのでdriver->configは必要だけどその後はmacroのメソッドのみ
        Freee::shouldReceive('driver->config->getCompanyId')->andReturn(0);
        Freee::shouldReceive('driver->config->getDeals')->andReturn([]);

        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
            ->get(route('me_macro'));

        $response->assertStatus(200)
            ->assertSeeText($user->email);
    }

    public function refresh(Request $request)
    {
        $response = Freee::refreshToken($request->user()->refresh_token);

        $request->user()->fill([
            'token' => $response['access_token'] ?? '',
            'refresh_token' => $response['refresh_token'] ?? '',
            'expired_at' => now()->addSeconds($response['expires_in']),
        ])->save();

        return back();
    }

use Freee\Accounting\Configuration;
use Revolution\Freee\Drivers\AccountingClient;

$freee = new AccountingClient(['client_id' => '', 'client_secret' => '']);
$config = Configuration::getDefaultConfiguration()->setAccessToken('token');
$freee->config($config);
$companiesResponse = json_decode($freee->companies()->getCompanies(), true);