1. Go to this page and download the library: Download pionect/visma-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/ */
pionect / visma-sdk example snippets
use Pionect\VismaSdk\Requests\Customer\CustomerGetAllRequest;
use Pionect\VismaSdk\Requests\Customer\CustomerGetBycustomerCdRequest;
use Pionect\VismaSdk\Requests\Customer\CustomerPostRequest;
use Pionect\VismaSdk\Dto\CustomerDto;
use Pionect\VismaSdk\VismaConnector;
class CustomerController extends Controller
{
public function __construct(
protected VismaConnector $visma
) {}
public function index()
{
// Fetch a single customer by customer number
$defaultCustomer = $this->visma->send(
new CustomerGetBycustomerCdRequest(customerCd: 'CUST001')
)->dtoOrFail();
// Fetch all customers using pagination
$customers = $this->visma->paginate(new CustomerGetAllRequest())
->dtoCollection();
return view('customers.index', compact('customers', 'defaultCustomer'));
}
public function store(Request $request)
{
$customer = new CustomerDto([
'number' => $request->input('number'),
'name' => $request->input('name'),
'status' => 'Active',
'creditLimit' => $request->input('credit_limit'),
'currencyId' => 'NOK',
]);
$created = $this->visma
->send(new CustomerPostRequest($customer))
->dtoOrFail();
return redirect()->route('customers.show', $created->number);
}
}
use Pionect\VismaSdk\Requests\Customer\CustomerGetAllRequest;
use Pionect\VismaSdk\VismaConnector;
class SyncCustomersCommand extends Command
{
public function handle(VismaConnector $visma): int
{
$customers = $visma->paginate(
new CustomerGetAllRequest()
)->dtoCollection();
foreach ($customers as $customer) {
// Process customers
$this->info("Syncing customer: {$customer->name}");
}
return Command::SUCCESS;
}
}
use Pionect\VismaSdk\VismaConnector;
use Pionect\VismaSdk\Dto\CustomerDto;
use Pionect\VismaSdk\Dto\CustomerInvoiceDto;
use Pionect\VismaSdk\Requests\Customer\CustomerGetAllRequest;
use Pionect\VismaSdk\Requests\Customer\CustomerGetBycustomerCdRequest;
use Pionect\VismaSdk\Requests\Customer\CustomerPostRequest;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
test('it displays customers', function () {
// Generate test data using factories
$customers = CustomerDto::factory()->count(2)->make();
// Create mock responses using factory-generated data
$mockClient = MockClient::global([
CustomerGetAllRequest::class => MockResponse::make($customers->toArray(), 200),
]);
// Make request
$response = $this->get(route('customers.index'));
// Assert
$response->assertOk();
$response->assertViewHas('customers');
$response->assertViewHas('defaultCustomer');
});
test('it creates a new customer', function () {
// Generate test data with specific attributes
$customer = CustomerDto::factory()->state([
'number' => 'CUST002',
'name' => 'New Customer Ltd',
'status' => 'Active',
'creditLimit' => 50000.00,
'currencyId' => 'NOK',
])->make();
$mockClient = MockClient::global([
CustomerPostRequest::class => MockResponse::make($customer->toArray(), 201),
]);
$response = $this->post(route('customers.store'), [
'number' => 'CUST002',
'name' => 'New Customer Ltd',
'credit_limit' => 50000.00,
]);
$response->assertRedirect(route('customers.show', 'CUST002'));
});
test('it sends a POST request to create a customer using the SDK', function () {
$customerToCreate = CustomerDto::factory()->state([
'number' => 'CUST003',
'name' => 'Test Customer AS',
'status' => 'Active',
'creditLimit' => 100000.00,
'currencyId' => 'NOK',
])->make();
$createdCustomer = CustomerDto::factory()->state([
'number' => 'CUST003',
'name' => 'Test Customer AS',
'status' => 'Active',
'creditLimit' => 100000.00,
'currencyId' => 'NOK',
])->make();
$mockClient = MockClient::global([
CustomerPostRequest::class => MockResponse::make($createdCustomer->toArray(), 201),
]);
artisan('sync:customers')->assertOk();
// Assert the request body was sent correctly
$mockClient->assertSent(function (CustomerPostRequest $request) {
$body = $request->body()->all();
return $body['number'] === 'CUST003'
&& $body['name'] === 'Test Customer AS'
&& $body['creditLimit'] === 100000.00;
});
});
// Create a single model with random data, without an ID
$customer = CustomerDto::factory()->make();
// Create multiple models with unique UUID IDs
$customers = CustomerDto::factory()->withId()->count(3)->make(); // Returns Collection
// Override specific attributes
$customer = CustomerDto::factory()->state([
'number' => 'CUST100',
'name' => 'Q1 Customer',
'status' => 'Active',
'creditLimit' => 10000.00,
])->make();
// Chain state calls for complex scenarios
$customer = CustomerDto::factory()
->state(['number' => $customerNumber])
->state(['currencyId' => 'NOK'])
->make();
use Pionect\VismaSdk\VismaConnector;
use Pionect\VismaSdk\Requests\Customer\CustomerGetAllRequest;
// Simple: Get all pages as a Laravel Collection
$customers = $visma->paginate(new CustomerGetAllRequest())
->dtoCollection();
foreach ($customers as $customer) {
// Process each customer
// All pages have been fetched automatically
}
$paginator = $visma->paginate(new CustomerGetAllRequest());
// Optional: configure page size (default is API default)
$paginator->setPerPageLimit(50);
// Stream through all pages lazily (fetches pages as needed)
foreach ($paginator->items() as $customer) {
// Process each customer
// Pages are fetched on-demand, reducing memory usage
}