PHP code example of vimatech / laravel-integrations
1. Go to this page and download the library: Download vimatech/laravel-integrations 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/ */
use Vimatech\Integrations\Contracts\Driver;
interface EInvoiceNetwork extends Driver
{
public function send(Invoice $invoice): string;
}
final class ChorusProAdapter implements EInvoiceNetwork
{
public function __construct(private array $config) {}
public function send(Invoice $invoice): string
{
// talk to Chorus Pro using $this->config['api_key']
}
}
> app(IntegrationManager::class)->extend('einvoice', 'chorus_pro', function (array $config) {
> return new ChorusProAdapter(ChorusClient::make($config['api_key']));
> });
>
use Vimatech\Integrations\Facades\Integrations;
// Explicit key
$driver = Integrations::driver('einvoice', 'sdi');
// Capability default
$driver = Integrations::driver('einvoice');
// Route by context — uses the capability's `routing.by` dimension
$driver = Integrations::for('einvoice')->resolve(['country' => $invoice->country]);
use Vimatech\Integrations\Contracts\ResolvesTenantDriver;
final class TenantDriverResolver implements ResolvesTenantDriver
{
public function resolveDriverKey(string $capability, array $context): ?string
{
return Tenant::find($context['tenant'] ?? null)
?->integrationKey($capability);
}
}
// In a service provider:
$this->app->bind(ResolvesTenantDriver::class, TenantDriverResolver::class);
use Illuminate\Http\Request;
use Vimatech\Integrations\Contracts\Driver;
use Vimatech\Integrations\Contracts\WebhookTranslator;
use Vimatech\Integrations\Webhooks\CanonicalEvent;
final class ChorusProAdapter implements EInvoiceNetwork, WebhookTranslator
{
public function __construct(private array $config) {}
public function verify(Request $request): bool
{
return hash_equals(
$this->config['webhook_secret'],
(string) $request->header('X-Signature'),
);
}
public function translate(Request $request): iterable
{
foreach ($request->input('events', []) as $raw) {
yield new InvoiceDelivered($raw['id']);
}
}
}
use Vimatech\Integrations\Webhooks\CanonicalEvent;
final class InvoiceDelivered extends CanonicalEvent
{
public function __construct(public readonly string $invoiceId) {}
public function idempotencyKey(): string
{
return "einvoice:delivered:{$this->invoiceId}";
}
}
use Vimatech\Integrations\Contracts\CredentialStore;
$this->app->singleton(CredentialStore::class, SecureFieldsCredentialStore::class);
use Vimatech\Integrations\Facades\Integrations;
it('uses the SDI driver for Italian invoices', function () {
$fake = Integrations::fake();
app(InvoiceSender::class)->send($italianInvoice);
$fake->assertDriverUsed('einvoice', 'sdi');
});
$fake = Integrations::fake([
'einvoice:sdi' => new FakeEInvoiceNetwork(), // your double implementing the capability contract
]);
use Laravel\Octane\Events\RequestReceived;
use Vimatech\Integrations\IntegrationManager;
Event::listen(RequestReceived::class, fn () => app(IntegrationManager::class)->forgetDrivers());
bash
php artisan vendor:publish --tag=integrations-config
php artisan vendor:publish --tag=integrations-migrations # only for the "database" webhook store
POST {prefix}/{capability}/{driver?}
bash
php artisan integrations:list
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.