PHP code example of lacasera / collector-paystack

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

    

lacasera / collector-paystack example snippets


'collectables' => [
    'user' => [
        'model' => App\Models\User::class,
        'trial_days' => 14,
        'default_interval' => 'monthly',
        'plans' => [
            [
                'name' => 'Basic',
                'description' => 'Perfect for getting started',
                'monthly_id' => 'PLN_your_monthly_plan_id',
                'yearly_id' => 'PLN_your_yearly_plan_id',
                'yearly_incentive' => 'Save 20%',
                'features' => [
                    'Up to 10 projects',
                    '5GB storage',
                    'Email support',
                ],
            ],
            // More plans...
        ],
    ],
],



namespace App\Models;

use Collector\Collectable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Collectable;
    
    // Your existing model code...
}

'domain' => env('COLLECTOR_DOMAIN'),        // null = your app's current domain
'prefix' => env('COLLECTOR_PREFIX', 'collector'),
'path'   => 'billing',

route('collector.portal');   // or: $user->billingPortalUrl()

route('collector.manage');                              // overview
route('collector.manage', ['section' => 'history']);    // payment history
route('collector.manage', ['section' => 'methods']);    // payment methods
route('collector.manage', ['section' => 'subscriptions']);

// e.g. after a failed charge, drop them on their card:
return redirect()->route('collector.manage', ['section' => 'methods'])
    ->with('error', 'Your card was declined.');

$user = auth()->user();

// Is the user subscribed at all?
$user->subscribed();

// Subscribed to a specific PayStack plan?  (Cashier's subscribedToPrice)
$user->subscribedToPlan('PLN_basic_monthly');

// Subscribed to any plan under a product (the plan-name group in config)?
$user->subscribedToProduct('Basic');

// The user's active subscription (or null)
$subscription = $user->subscription();

// Returns the matching Subscription (or null)
$user->hasActivePlan('PLN_basic_monthly');

// The user's current active plan code, and all subscriptions (most recent first)
$user->currentActivePlan()?->paystack_plan;
$user->subscriptions;

// Inspecting a subscription
$subscription?->isActive();
$subscription?->onTrial();
$subscription?->onGracePeriod();
$subscription?->valid();      // active, on trial, or within grace period

// Create / update the PayStack customer
$user->createAsPayStackCustomer(['email' => $user->email]);
$user->createOrGetPayStackCustomer();
$user->updatePayStackCustomer(['first_name' => 'Ada']);
$user->hasPayStackId();

// Payment methods (from the PayStack customer's authorizations)
$user->paymentMethods();
$user->defaultPaymentMethod();   // the stored default card
$user->hasPaymentMethod();

// The in-app billing portal URL
$url = $user->billingPortalUrl();

return $request->user()
    ->newSubscription('default', 'PLN_basic_monthly')
    ->trialDays(5)
    ->checkout([
        'success_url' => route('billing.success'),
    ]);

$url = (string) $user->newSubscription('default', 'PLN_basic_monthly')->checkout();

$user->subscription()?->cancel('No longer needed');

use Collector\Events\SubscriptionCanceled;
use Illuminate\Support\Facades\Event;

Event::listen(SubscriptionCanceled::class, function (SubscriptionCanceled $event) {
    // $event->collectable, $event->subscription
});

use App\Models\User;
use Collector\Collector;
use Illuminate\Http\Request;

public function boot(): void
{
    // Resolve which model the portal manages
    Collector::collectable(User::class)->resolve(fn (Request $request) => $request->user());

    // Authorize who may view the portal
    Collector::collectable(User::class)->authorize(
        fn (User $collectable, Request $request) => $request->user()?->is($collectable)
    );
}

use Collector\Collector;

Collector::useCustomerModel(\App\Models\User::class);
Collector::useSubscriptionModel(\App\Models\Subscription::class);
bash
php artisan collector:install
bash
php artisan vendor:publish --tag="collector-config"   # config/collector.php
php artisan vendor:publish --tag="collector-assets"   # public/vendor/collector
php artisan vendor:publish --tag="collector-views"    # optional: resources/views/vendor/collector
php artisan migrate
bash
php artisan vendor:publish --tag="collector-views"