You are browsing documentation for an outdated version of Spike.

Use the version selector on the left (navigation), or visit latest docs here.

Configuring billables in Spike

A billable is any model in your app that should be billed/invoiced for the subscriptions and product purchases. Usually this is the User model or the Team model for multi-tenancy applications.

Set up the billable

If you're using a billable model other than App\Models\User, you will need to customize the migrations. See the Customize Migrations section below.

Once you have decided the model that will be the billable, you should add the SpikeBillable trait to your model:

use Opcodes\Spike\SpikeBillable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SpikeBillable;
}

Next, you should add it to the billable_models list in config/spike.php configuration file:

'billable_models' => [
    'App\Models\User',
],

This will give the model abilities to interact with credits and subscriptions.

Resolving billable

When a user visits the Spike billing portal, their billable model is automatically resolved using Spike::resolve(). By default, it returns the authenticated user via Auth::user().

If you would like to change how the billable user is resolved, you can do so by calling Spike::billable(...)->resolve(...) method inside the boot method of your AppServiceProvider. The callback receives the Request instance for convenience.

use Opcodes\Spike\SubscriptionPlan;
use Opcodes\Spike\Facades\Spike;
use App\Models\Team;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Spike::billable(Team::class)->resolve(function ($request) {
        return $request->user()->currentTeam;
    });
}

Authenticating billable

By default, anyone is allowed to visit the Spike billing portal. If you would like to change it, you can do so by calling Spike::billable(...)->authorize(...) method inside the boot method of your AppServiceProvider class. The callback receives the resolved billable and the Request instance for convenience and expects a boolean value to be returned.

use Opcodes\Spike\SubscriptionPlan;
use Opcodes\Spike\Facades\Spike;
use App\Models\Team;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Spike::billable(Team::class)->authorize(function (Team $billable, $request) {
        return $request->user()->ownsTeam($billable);
    });
}

Customize migrations

Spike runs a few database migrations to add new columns for managing subscriptions for the billables. When your billable model is different from the default App\Models\User, you will need to customize these migrations.

  1. If you have previously run the migrations after installing Spike, roll them back with php artisan migrate:rollback
  2. Publish migrations with php artisan vendor:publish --tag=spike-migrations
  3. Open the migrations/2022_05_01_000001_create_customer_columns.php file and update the "users" table to be the table of the billable (e.g. "teams" if your billable is App\Models\Team)
  4. Open the migrations/2022_05_01_000002_create_subscriptions_table.php file and update the "user_id" column name to an appropriate foreign key name for your billable (e.g. "team_id" if your billable is App\Models\Team)
  5. Run the migrations with php artisan migrate.

Support

If you have any questions, feedback, or need any help setting up Spike within your project, feel free to reach out to me.