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/ */
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.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();
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);