PHP code example of elkadrey / fiskaly-laravel

1. Go to this page and download the library: Download elkadrey/fiskaly-laravel 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/ */

    

elkadrey / fiskaly-laravel example snippets


 namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;

//use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Cache;

class Controller extends BaseController
{
    private ?FiskalyLaravelClient $client = null;

    public function __construct()
    {
        //Make the connection
        $this->client = FiskalyLaravelClient::create([
            "api_key" => env("TSE_API_KEY"), 
            "api_secret" => env("TSE_API_SECRET"), 
            "baseUrl" => env("TSE_API_URL", null),
            "token" => Cache::get("tse_token"),
            "token_expire_at" => Cache::get("tse_token_expire_at"),
        ]);
        //Set debug level (2 is default)
        /** Debug levels list
        * 0 = errors only
        * 1 = errors and warning
        * 2 = all with info for each API response
        */
        $this->client->setLog(2);

        //make Auth with token in case empty token or expired
        $token = $this->client->MakeAuth();
        
        //Also you can use getToken
        //$token = $this->client->getToken();
        
        Cache::put("tse_token", $token->access_token);
        Cache::put("tse_token_expire_at", $token->access_token_expires_at);
    }

    public function createTSS(Request $request)
    {
        $this->validate($request, [
            "metadata" => "nullable|array"
        ]);

        try
        {
            //create a ready INITIALIZED TSS (5 steps in one)
            set_time_limit(0);
            $results = $this->client->createTSS($request->metadata);
            return JsonResource::make($results);
        }
        catch(Exception $e)
        {
            //Your exception code here
        }
    }
}

$this->client->put_tss_95a03ad9-61ad-4757-8a1d-57daf47db25c_client($params, true);

    public function createManuallyTSS(Request $request)
    {
        $this->validate($request, [
            "metadata" => "nullable|array"
        ]);

        try
        {
            //create a ready INITIALIZED TSS (5 steps in one)
            set_time_limit(0);

            //API => method:put /tss/{$guid}
            $tss = $this->client->put_tss($request->metaData, true); //return laravel collection 
            if($tss->state == "CREATED")
            {
                //1- Change state to UNINITIALIZED
                //API => method:patch /tss/{tssid}
                $this->client->{"patch_tss_$tss->_id"}(["state" => "UNINITIALIZED"]);
                $tss->put("state", "UNINITIALIZED");

                //2 - Set admin pin
                $adminPin = uniqid();
                //change admin pin api
                $this->client->changeAdminPin($tss->_id, $tss->admin_puk, $adminPin);                
                $tss->put("adminpin", $adminPin);

                //3- Make admin auth
                //make admin auth
                $this->client->adminAuth($tss->_id, $adminPin);

                //4- Change state to INITIALIZED
                //API => method:patch /tss/{tssid}
                $this->client->{"patch_tss_$tss->_id"}(["state" => "INITIALIZED"]);                
                $tss->put("state", "INITIALIZED");
                return JsonResource::make($tss);
            }
        }
        catch(Exception $e)
        {
            //Your exception code here
        }
    }

public function addClient(string $tssid, Request $request)
{
    $this->validate($request, [
        "metadata" => "nullable|array"
    ]);

    try
    {
        $guid = $this->client->getUUID();
        
        $serial_number = "ERS $guid";
        $info = compact('serial_number', 'metadata');

        //API => method:PUT /tss/{tssid}/client/{guid}
        $results = $this->client->{"put_tss_$tssid"."_client_$guid"}($info);
        return JsonResource::make($results);
    }
    catch(Exception $e)
    {
        //Your exception code here
    }
}

public function startTransaction(string $tssid, string $clientid)
{
    try
    {
        //API => method:PUT /tss/{{tssId}}/tx/{{$guid}}?tx_revision=1
        $results = $this->client->{"put_tss_$tssid"."_tx?tx_revision=1"}(["state" => "ACTIVE", "client_id" => $clientid], true);
        return JsonResource::make($results);
    }
    catch(Exception $e)
    {
        //Your exception code here
    }
}