PHP code example of okeke-dev / laravel-bachs

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

    

okeke-dev / laravel-bachs example snippets


'connections' => [
    'default' => [/* ... */],
    'partner' => [/* ... */],
],

use OkekeDev\Bachs\Facades\Bachs;

$client = Bachs::connection();          // default connection
$client = Bachs::connection('partner'); // named connection

// Or use the global helper:
$client = bachs();

use OkekeDev\Bachs\Resources\Products;

$products = Products::list(['limit' => 10]);
$product = Products::create([
    'name' => 'Pro Plan',
    'description' => 'Access to all features',
    'price' => ['amount' => '29.99', 'currency' => 'USD'],
]);

Products::update($product->id(), ['name' => 'Pro Plan (Annual)']);
Products::archive($product->id());
Products::unarchive($product->id());

use OkekeDev\Bachs\Resources\Customers;

$customer = Customers::create([
    'email' => ' [email protected]',
    'name' => 'Jane Doe',
]);

Customers::update($customer->id(), ['name' => 'Jane Smith']);
$portalSession = Customers::createPortalSession($customer->id());
$url = $portalSession->url();

use OkekeDev\Bachs\Concerns\Billsable;

class User extends Authenticatable
{
    use Billsable;
}

// Create a Bachs customer from the model
$user->createAsBachsCustomer();

// Checkout
$session = $user->checkout([
    'product_cart' => [['product_id' => 'prod_xxx', 'quantity' => 1]],
]);
return redirect($session->url());

// Subscriptions
$session = $user->subscribeTo('prod_xxx');
$user->subscribed();  // bool
$user->subscription(); // ?Subscription
$user->cancel();
$user->resume();

// Billing portal
return redirect($user->billingPortalUrl());

use OkekeDev\Bachs\Resources\CheckoutSessions;

$session = CheckoutSessions::create([
    'product_cart' => [['product_id' => 'prod_xxx', 'quantity' => 1]],
    'customer' => ['customer_id' => 'cus_xxx'],
    'success_url' => 'https://example.com/success',
    'cancel_url' => 'https://example.com/cancel',
]);

$session->url();     // redirect URL
$session->status();  // checkout status

use OkekeDev\Bachs\Resources\Subscriptions;

$subscription = Subscriptions::get('sub_xxx');
$subscription->isActive();
$subscription->isTrialing();
$subscription->isPastDue();
$subscription->isCanceled();

Subscriptions::cancel('sub_xxx');
Subscriptions::update('sub_xxx', ['cancel_at_period_end' => true]);

use OkekeDev\Bachs\Resources\Payments;
use OkekeDev\Bachs\Resources\Refunds;

$payments = Payments::list(['limit' => 10]);
$payment = Payments::get('pay_xxx');
$payment->isSucceeded();
$payment->isRefundable();

// Full refund
$refund = $payment->refund();

// Partial refund via the Refunds resource
$refund = Refunds::create([
    'payment_id' => 'pay_xxx',
    'amount' => '10.00',
]);

Route::post('bachs/webhook', [\OkekeDev\Bachs\Http\Controllers\WebhookController::class, '__invoke']);

use OkekeDev\Bachs\Events\PaymentSucceeded;
use OkekeDev\Bachs\Events\SubscriptionCanceled;

protected $listen = [
    PaymentSucceeded::class => [/* ... */],
    SubscriptionCanceled::class => [/* ... */],
];

'database' => [
    'sync' => true,
],

$response = $client->get('products', ['limit' => 20]);
$response->status();      // 200
$response->json();        // decoded payload
$response->json('price.amount', '0.00'); // dot-notated access
$response->toArray();     // decoded payload as an array
$response->requestId();   // x-request-id, for support

$client->post('customers', ['email' => '[email protected]'], 'idem_123');
bash
php artisan bachs:install
bash
php artisan vendor:publish --tag=bachs-config
php artisan vendor:publish --tag=bachs-migrations
php artisan vendor:publish --tag=bachs-views
bash
# Publish config, migrations, and views
php artisan bachs:install

# Verify API connectivity
php artisan bachs:health

# Webhook management
php artisan bachs:webhook:test https://example.com/webhook
php artisan bachs:webhook:list
php artisan bachs:webhook:inspect evt_xxx
php artisan bachs:webhook:replay evt_xxx