PHP code example of kodedjackson / bachs-laravel

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

    

kodedjackson / bachs-laravel example snippets


use Kodedjackson\Bachs\Facades\Bachs;

$session = Bachs::createCheckoutSession([
    'pricing' => [
        'currency' => 'USD',
        'amount' => '42.00',
    ],
    'customer' => [
        'email' => '[email protected]',
        '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']);

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,
    ],
]);

use Kodedjackson\Bachs\Facades\Bachs;

$session = Bachs::createCheckoutSession([
    'product_cart' => [
        ['product_id' => 'prod_abc123', 'quantity' => 1],
    ],
    'customer' => [
        'email' => '[email protected]',
    ],
    'success_url' => url('/thanks'),
    'cancel_url' => url('/pricing'),
]);

return redirect($session['checkout_url']);

// List (filter with 'customer_id', 'status', 'limit', 'offset')
$subscriptions = Bachs::listSubscriptions(['status' => 'active']);

// Get a single subscription
$subscription = Bachs::getSubscription('sub_1a2b3c4d5e6f');

// Change the plan. proration_behavior: 'invoice_now' (default), 'next_cycle', or 'none'.
Bachs::changeSubscriptionPlan('sub_1a2b3c4d5e6f', 'prod_premium', 'invoice_now');

// Swap the saved payment method
Bachs::updateSubscriptionPaymentMethod('sub_1a2b3c4d5e6f', 'pm_9f8e7d6c5b');

// Merge metadata (send a key with an empty string to remove it)
Bachs::updateSubscriptionMetadata('sub_1a2b3c4d5e6f', ['plan_tier' => 'pro']);

// Or drop down to the raw call for anything else
Bachs::updateSubscription('sub_1a2b3c4d5e6f', ['product_id' => 'prod_premium']);

// Let it run out the period the customer already paid for
Bachs::cancelSubscriptionAtPeriodEnd('sub_1a2b3c4d5e6f', 'Customer requested');

// Cancel right now
Bachs::cancelSubscriptionImmediately('sub_1a2b3c4d5e6f');

$product = Bachs::createProduct([
    'name' => 'Pro Plan',
    'price' => ['price_type' => 'fixed', 'amount' => '10.00'],
    'billing_cycle' => ['interval' => 'month', 'frequency' => 1],
    'trial_period' => ['interval' => 'day', 'frequency' => 14], // 14-day trial
]);

// Extend (or start) the trial — postpones billing
Bachs::extendSubscriptionTrial('sub_1a2b3c4d5e6f', '2026-08-10T12:00:00Z');

// End the trial right now — bills the first cycle immediately
Bachs::endSubscriptionTrial('sub_1a2b3c4d5e6f');

Bachs::changeSubscriptionPlan('sub_1a2b3c4d5e6f', 'prod_premium', 'next_cycle');

use Illuminate\Support\Facades\Event;
use Kodedjackson\Bachs\Events\CollectionSucceeded;
use Kodedjackson\Bachs\Events\SubscriptionCanceled;
use Kodedjackson\Bachs\Events\SubscriptionCreated;
use Kodedjackson\Bachs\Events\InvoicePaid;
use Kodedjackson\Bachs\Events\InvoicePaymentFailed;

// A one-off payment succeeded
Event::listen(function (CollectionSucceeded $event) {
    $data = $event->paymentData;

    // Order::where('checkout_id', $data['checkout_id'])->update(['status' => 'paid']);
});

// A subscription started (fires with status "trialing" or "active")
Event::listen(function (SubscriptionCreated $event) {
    $subscription = $event->subscription;

    // User::where('customer_id', $subscription['customer']['customer_id'])
    //     ->update(['subscription_id' => $subscription['id'], 'plan_status' => $subscription['status']]);
});

// A renewal (or the first post-trial charge, or a recovery retry) succeeded
Event::listen(function (InvoicePaid $event) {
    // Grant/extend access based on $event->invoice
});

// A renewal charge failed — subscription is now past_due
Event::listen(function (InvoicePaymentFailed $event) {
    // Optionally notify the customer; Bachs already emails them a card-update link
});

// The subscription reached the terminal "canceled" status
Event::listen(function (SubscriptionCanceled $event) {
    // Revoke access
});
bash
php artisan vendor:publish --tag="bachs-config"