PHP code example of itechsection / salesprosdk

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

    

itechsection / salesprosdk example snippets


use ITechSection\SalesPro\SalesProClient;

$client = SalesProClient::create([
    'base_url'      => 'https://salespro.itechsection.com/',
    'client_id'     => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET',
    'username'      => '[email protected]',
    'password'      => 'your-password',
]);

// List products
$products = $client->products->list(['per_page' => 20]);
foreach ($products->data as $product) {
    echo $product['name'] . "\n";
}

// Create a contact
$contact = $client->contacts->create([
    'type'       => 'customer',
    'first_name' => 'John',
    'last_name'  => 'Doe',
    'email'      => '[email protected]',
]);

// Create a sale
$sell = $client->sales->create([
    'location_id'      => 1,
    'contact_id'       => $contact->data['id'],
    'transaction_date' => date('Y-m-d H:i:s'),
    'sell_lines' => [
        ['product_id' => 1, 'quantity' => 2, 'unit_price' => 50.00],
    ],
    'payments' => [
        ['method' => 'cash', 'amount' => 100.00],
    ],
]);

echo "Invoice: " . $sell->data['invoice_no'];

$config['salespro_base_url']      = 'https://salespro.itechsection.com/';
$config['salespro_client_id']     = 'YOUR_CLIENT_ID';
$config['salespro_client_secret'] = 'YOUR_CLIENT_SECRET';
$config['salespro_username']      = '[email protected]';
$config['salespro_password']      = 'your-password';

class Products extends CI_Controller
{
    public function index()
    {
        $this->load->library('salespro');

        $products = $this->salespro->products->list(['per_page' => 20]);
        $this->load->view('products/index', ['products' => $products->data]);
    }

    public function create()
    {
        $this->load->library('salespro');

        try {
            $result = $this->salespro->contacts->create([
                'type'       => 'customer',
                'first_name' => $this->input->post('first_name'),
                'email'      => $this->input->post('email'),
            ]);

            redirect('products');

        } catch (\ITechSection\SalesPro\Exceptions\ValidationException $e) {
            // $e->getErrors() returns ['field' => ['error message', ...]]
            $this->session->set_flashdata('errors', $e->getErrors());
            redirect('products/create');
        }
    }
}

public string $baseUrl      = 'https://salespro.itechsection.com/';
public string $clientId     = 'YOUR_CLIENT_ID';
public string $clientSecret = 'YOUR_CLIENT_SECRET';
public string $username     = '[email protected]';
public string $password     = 'your-password';

use CodeIgniter\Controller;

class ProductController extends Controller
{
    public function index()
    {
        $client   = \Config\Services::salespro();
        $products = $client->products->list(['per_page' => 20]);

        return view('products/index', ['products' => $products->data]);
    }
}

$client = SalesProClient::create([
    'base_url'      => '...',
    'client_id'     => '...',
    'client_secret' => '...',
    'username'      => '[email protected]',
    'password'      => 'secret',
]);

$client = SalesProClient::withToken(
    'https://salespro.itechsection.com/',
    'your-personal-access-token'
);

// Create
$tokenData = $client->auth->createPersonalAccessToken('my-app', ['*']);
echo $tokenData['accessToken']; // save this — shown only once

// List
$tokens = $client->auth->listPersonalAccessTokens();

// Delete
$client->auth->deletePersonalAccessToken($tokenData['id']);

$result = $client->contacts->list(['page' => 1, 'per_page' => 15]);

echo $result->total();          // total records
echo $result->meta->lastPage;   // last page number
echo $result->meta->currentPage;

if ($result->hasMorePages()) {
    $page2 = $client->contacts->list(['page' => 2, 'per_page' => 15]);
}

// Or fetch ALL pages automatically:
$allContacts = $client->httpClient->getAllPages('connector/api/contactapi');

use ITechSection\SalesPro\Exceptions\AuthenticationException;
use ITechSection\SalesPro\Exceptions\AuthorizationException;
use ITechSection\SalesPro\Exceptions\NotFoundException;
use ITechSection\SalesPro\Exceptions\ValidationException;
use ITechSection\SalesPro\Exceptions\RateLimitException;
use ITechSection\SalesPro\Exceptions\ServerException;
use ITechSection\SalesPro\Exceptions\NetworkException;
use ITechSection\SalesPro\Exceptions\SalesProException;

try {
    $result = $client->contacts->create($data);

} catch (ValidationException $e) {
    // Field-level validation failures (HTTP 422)
    foreach ($e->getErrors() as $field => $messages) {
        echo "{$field}: " . implode(', ', $messages) . "\n";
    }

} catch (AuthenticationException $e) {
    // 401 — credentials invalid or token expired
    echo "Auth error: {$e->getMessage()}\n";

} catch (NotFoundException $e) {
    // 404
    echo "Not found: {$e->getMessage()}\n";

} catch (RateLimitException $e) {
    // 429
    echo "Rate limited. Retry after {$e->getRetryAfter()} seconds.\n";

} catch (ServerException $e) {
    // 5xx
    echo "Server error ({$e->getCode()}): {$e->getMessage()}\n";

} catch (NetworkException $e) {
    // DNS / timeout / connection refused
    echo "Network error: {$e->getMessage()}\n";

} catch (SalesProException $e) {
    // Any other SDK error
    echo "SDK error: {$e->getMessage()}\n";
}
bash
# Copy the library
cp vendor/itechsection/salesprosdk/src/CodeIgniter3/Salespro.php \
   application/libraries/Salespro.php

# Copy the config stub
cp vendor/itechsection/salesprosdk/src/CodeIgniter3/config/salesproconf.php \
   application/config/salespro.php
bash
# Copy CI4 config class
cp vendor/itechsection/salesprosdk/src/CodeIgniter4/Config/SalesPro.php \
   app/Config/SalesPro.php

# Copy services registration
cp vendor/itechsection/salesprosdk/src/CodeIgniter4/Config/SalesProServices.php \
   app/Config/SalesProServices.php