PHP code example of zeevx / laravel-bachs

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

    

zeevx / laravel-bachs example snippets


return [
    'api_key' => env('BACHS_API_KEY'),
    'base_url' => env('BACHS_BASE_URL', 'https://api.bachs.io'),
    'timeout' => (int) env('BACHS_TIMEOUT', 30),

    'webhook' => [
        'secret' => env('BACHS_WEBHOOK_SECRET'),
        'tolerance' => (int) env('BACHS_WEBHOOK_TOLERANCE', 300),
        'path' => env('BACHS_WEBHOOK_PATH', 'bachs/webhook'),
        'route_enabled' => env('BACHS_WEBHOOK_ROUTE_ENABLED', true),
    ],
];

use Zeevx\Bachs\Bachs;

final readonly class CreateCheckout
{
    public function __construct(private Bachs $bachs) {}

    public function handle(): string
    {
        $checkout = $this->bachs->checkoutSessions()->create([
            'product_cart' => [['product_id' => 'product_123', 'quantity' => 1]],
            'customer' => [
                'email' => '[email protected]',
                'first_name' => 'Ada',
                'last_name' => 'Lovelace',
            ],
            'billing_currency' => 'USD',
        ]);

        return (string) $checkout->url;
    }
}

use Zeevx\LaravelBachs\Facades\Bachs;

$customer = Bachs::customers()->create([
    'email' => '[email protected]',
    'name' => 'Ada Lovelace',
]);

$bachs->checkoutSessions()->create($payload);
$bachs->checkoutSessions()->retrieve($checkoutId);

$bachs->customers()->create($payload);
$bachs->customers()->retrieve($customerId);
$bachs->customers()->update($customerId, $payload);
$bachs->customers()->createPortalSession($customerId, $payload);

$bachs->subscriptions()->retrieve($subscriptionId);
$bachs->subscriptions()->update($subscriptionId, $payload);
$bachs->subscriptions()->cancel($subscriptionId);

$middleware->validateCsrfTokens(except: [
    'bachs/webhook',
]);

use Illuminate\Support\Facades\Event;
use App\Listeners\ProcessBachsWebhook;
use Zeevx\LaravelBachs\Events\WebhookReceived;

Event::listen(WebhookReceived::class, ProcessBachsWebhook::class);

use Illuminate\Contracts\Queue\ShouldQueue;
use Zeevx\LaravelBachs\Events\WebhookReceived;

final class ProcessBachsWebhook implements ShouldQueue
{
    public function handle(WebhookReceived $received): void
    {
        $event = $received->event;

        $event->id;
        $event->type;
        $event->data;
        $event->payload;
    }
}

use Illuminate\Support\Facades\Event;
use Zeevx\LaravelBachs\Events\WebhookReceived;

Event::fake([WebhookReceived::class]);
text
POST /bachs/webhook