PHP code example of bluerock / sellsy-client

1. Go to this page and download the library: Download bluerock/sellsy-client 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/ */

    

bluerock / sellsy-client example snippets


use Bluerock\Sellsy\Entities;
use Bluerock\Sellsy\Api;

# create a new Contact instance using the related DTO
$contact = new Entities\Contact([
    'civility'      => 'mr',
    'first_name'    => 'Jean',
    'last_name'     => 'MOULIN',
    'email'         => '[email protected]',
    'website'       => 'example.com',
    'mobile_number' => '0612121212',
    'position'      => 'Directeur',
    'social'        => new Entities\ContactSocials([
        'twitter' => 'https://twitter.com/u/example',
    ]),
]);

# store the freshly created Contact into Sellsy
$api = new Api\ContactsApi();
$response = $api->store($contact);

var_dump(
    $response->json()
);

$api = new Bluerock\Sellsy\Api\CompaniesApi();

# ... GET single entity
$company = $api->find("123")->entity();

# ... GET collection of entities (index, search..)
$companies = $api->index()->entities();

$config = Bluerock\Sellsy\Core\Config::getInstance();

$config->set('client_id', 'f48f0fgm-2703-5689-2005-27403b5adb8d')
      ->set('client_secret', 'av8v94jx0ildsjje50sm9x1hnmjsg27bnqyryc0zgbmtxxmzpjzlw2vnj9aockwe')
      ->set('url', 'https://api.sellsy.com/v2/'): // not 

// Configure the first Sellsy account
Bluerock\Sellsy\Core\Config::switchInstance('first-sellsy')
    ->set('client_id', 'id-1')
    ->set('client_secret', 'secret1');

// Configure another one
Bluerock\Sellsy\Core\Config::switchInstance('other-sellsy')
    ->set('client_id', 'id-2')
    ->set('client_secret', 'secret2');

// From now, each request is on the 'other-sellsy' account
// ...

// To switch back to the first account
Bluerock\Sellsy\Core\Config::switchInstance('first-sellsy');

use Bluerock\Sellsy\Api\ContactsApi;

$contacts = new ContactsApi();
$contacts->index();
$contacts->search($filters);
$contacts->show($contact_id);
$contacts->store($contact);

use Bluerock\Sellsy\Core\Client;

# via an instance...
$client = new Client();
$client->contacts()->show($contact_id);

# ... or statically.
Client::taxes()->index();

$response->entity();      # Get single resource entity, when available
$response->entities();    # Get resource entities collection, when available (for listing/search)
$response->pagination();  # Get the pagination object, when available
$response->type();        # Returns the type of the data, between "listing" and "single" 
$response->json();        # Get raw json data from response, as an associative array
$response->base();        # Get the underlying \Illuminate\Http\Client\Response object

$response->body(): string;
$response->json(): array|mixed;
$response->status(): int;
$response->ok(): bool;
$response->successful(): bool;
$response->failed(): bool;
$response->serverError(): bool;
$response->clientError(): bool;
$response->header($header): string;
$response->headers(): array;

/**
 * @return \Bluerock\Sellsy\Entities\Contact|false
 * @throws \Illuminate\Http\Client\RequestException
 */
public function maybeFindMyContact($contact_id)
{
    try {
        return Bluerock\Sellsy\Core\Client::contacts()
                    ->show($contact_id)
                    ->entity();
    } catch (\Illuminate\Http\Client\RequestException $e) {
        # return false if the contact is not found (404 error).
        if ($e->response->clientError() && $e->response->status() === 404) {
          return false;
        }

        # throw the exception for any other status code.
        throw $e;
    }
}
  
var_dump(
  Bluerock\Sellsy\Core\Client::taxes()->index()->entities()
);

// Output :
Bluerock\Sellsy\Collections\TaxCollection^ {
  #iterator: ArrayIterator {#5776
    -storage: array:6 [
      0 => Bluerock\Sellsy\Entities\Tax^ {
        +id: 4099258
        +rate: 20
        +label: ""
      }
      1 => Bluerock\Sellsy\Entities\Tax^ {
        +id: 4099259
        +rate: 10
        +label: ""
      }
    ]
  }
}

var_dump(
  Bluerock\Sellsy\Core\Client::taxes()->index()->json()
);

// Output :
array:2 [
  "data" => array:6 [
    0 => array:4 [
      "id" => 4099258
      "is_active" => true
      "rate" => 20
      "label" => ""
    ]
    1 => array:4 [
      "id" => 4099259
      "is_active" => true
      "rate" => 10
      "label" => ""
    ]
  ],
  "pagination" => array:4 [
    "limit" => 100
    "count" => 6
    "total" => 6
    "offset" => "WyI0MDk5MjYzIl0="
  ]
]

$contactsApi = new ContactsApi();

$response = $contactsApi->index([
  'limit'  => 10,
  'offset' => 0,
  'embed'  => [
      'invoicing_address',
      'main_contact',
      'dunning_contact',
      'invoicing_contact',
      'smart_tags',
  ],
]);

$response->entities();    // The API entities
$response->pagination();  // The pagination DTO

$response = $contactsApi->show('123', [
    'embed' => [
        'smart_tags'
    ],
]);

$response->entity();    // The API entity

Bluerock\Sellsy\Entities\Contact^ {
  +id: 12345
  +civility: "ms"
  +first_name: "Amélie"
  +last_name: "PETIT"
  +email: "[email protected]"
  +website: null
  +phone_number: null
  +mobile_number: null
  +fax_number: null
  +position: "Gérante"
  +birth_date: null
  +avatar: null
  +note: ""
  +social: Bluerock\Sellsy\Entities\ContactSocials^ {
    +twitter: null
    +facebook: null
    +linkedin: null
    +viadeo: null
  }
  +sync: Bluerock\Sellsy\Entities\ContactSync^ {
    +mailchimp: true
    +mailjet: true
    +simplemail: true
  }
  +is_archived: false
  +invoicing_address_id: null
  +delivery_address_id: null
  +invoicing_address: null
  +delivery_address: null
  +created: "2022-02-16T15:56:17+01:00"
  +owner: array:2 [
    "id" => 567
    "type" => "staff"
  ]
}

use Bluerock\Sellsy\Entities;

$contactsApi->store(new Entities\Contact([
    'civility'      => 'mr',
    'first_name'    => 'Jean',
    'last_name'     => 'MOULIN',
    'email'         => '[email protected]',
    'website'       => 'example.com',
    'mobile_number' => '0612121212',
    'position'      => 'Directeur',
    'sync'          => new Entities\ContactSync(),
    'social'        => new Entities\ContactSocials([
        'twitter' => 'https://twitter.com/u/example',
    ]),
]));

$response->entity();  // The created entity
$response->json();    // The Sellsy response

use Bluerock\Sellsy\Entities;

$contactsApi->update(new Entities\Contact([
    'id'         => 35536947,
    'first_name' => 'Jean',
    'last_name'  => 'CASTEX',
    'note'       => '',
]));

$response->entity();  // The updated entity
$response->json();    // The Sellsy response

$contactsApi->delete(123)->json();