Looking to hire Laravel developers? Try LaraJobs

bachs-laravel maintained by kodedjackson

Description
Laravel client for the Bachs payment API
Last update
2026/08/08 14:10 (dev-main)
License
Links
Downloads
1

Comments
comments powered by Disqus

Bachs Laravel

A fluent, native Laravel integration for the Bachs.io payment API.

This package provides a clean Facade for interacting with the Bachs API (like creating Checkout Sessions and Products) and automatically handles secure webhook signature verification out of the box.

Installation

You can install the package via composer:

composer require kodedjackson/bachs-laravel

Publish the configuration file:

php artisan vendor:publish --tag="bachs-config"

This will create a config/bachs.php file in your application where you can configure your default settings.

Configuration

Add your Bachs API keys to your .env file. You can find these in your Bachs developer dashboard.

BACHS_PUBLIC_KEY=pk_test_...
BACHS_SECRET_KEY=sk_sandbox_...
BACHS_WEBHOOK_SECRET=whsec_...
BACHS_BASE_URL=[https://sandbox-api.bachs.io/v1](https://sandbox-api.bachs.io/v1)
BACHS_CURRENCY=USD

(Note: When moving to production, change your Base URL to https://api.bachs.io/v1 and use your live keys).

Usage

1. Creating a Checkout Session

You can create a hosted checkout session by passing a pricing array for raw amounts, or a product_cart array for catalog items.

use Kodedjackson\Bachs\Facades\Bachs;

$session = Bachs::createCheckoutSession([
    'pricing' => [
        'currency' => 'USD',
        'amount' => '42.00',
    ],
    'customer' => [
        'email' => 'jane@example.com',
        'name' => 'Jane Doe',
    ],
    'success_url' => url('/thanks'), // Must be a public URL in Live mode
    'cancel_url' => url('/cart'),
]);

// Redirect the user to the Bachs hosted checkout page
return redirect($session['checkout_url']);

2. Creating a Product

You can create a product in your Bachs catalog. If you omit the currency, the package will automatically use your configured default (BACHS_CURRENCY).

use Kodedjackson\Bachs\Facades\Bachs;

$product = Bachs::createProduct([
    'name' => 'Pro Plan',
    'description' => 'Monthly access to all Pro features.',
    'price' => [
        'price_type' => 'fixed',
        'amount' => '29.00',
    ],
    'billing_cycle' => [
        'interval' => 'month',
        'frequency' => 1
    ]
]);

Handling Webhooks (Fulfilling Orders)

Bachs recommends never relying on the browser redirect to fulfill an order. Instead, you should listen for webhooks.

This package automatically exposes a secure webhook endpoint at POST /bachs/webhook. It verifies the cryptographic signature of the incoming request and dispatches native Laravel events.

Step 1: Configure your Dashboard

In your Bachs developer portal, set your webhook URL to: https://yourdomain.com/bachs/webhook

Subscribe to the collection.succeeded event, and paste the generated signing secret into your .env file as BACHS_WEBHOOK_SECRET.

Step 2: Listen for the Event

When a payment is successful, the package fires the Kodedjackson\Bachs\Events\CollectionSucceeded event.

You can listen for this event in your application (e.g., inside EventServiceProvider or a dedicated listener) to fulfill the order:

use Illuminate\Support\Facades\Event;
use Kodedjackson\Bachs\Events\CollectionSucceeded;

Event::listen(function (CollectionSucceeded $event) {
    // The payment payload from Bachs
    $data =$event->paymentData;
    
    $checkoutId =$data['checkout_id'];
    $amount =$data['amount'];
    $status =$data['status']; // "SUCCEEDED"

    // Find the order in your database and mark it as paid
    // Order::where('checkout_id', $checkoutId)->update(['status' => 'paid']);
});

Security Vulnerabilities

If you discover any security-related issues, please email directly instead of using the issue tracker.

License

The MIT License (MIT).