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

Once you have decided the model that will be the billable, you should add the SpikeBillable trait to your model. There are two traits available, and you should use the correct one based on your selected payment provider.

  • Stripe - Opcodes\Spike\Stripe\SpikeBillable
  • Paddle - Opcodes\Spike\Paddle\SpikeBillable
use Opcodes\Spike\Stripe\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\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\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);
    });
}

Support

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