You are browsing documentation for an outdated version of Spike.

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

Custom resolvers in Spike

Spike allows changing the way the subscriptions and products are resolved. You can instead load them from a database, cache, or any other way, giving you lots of flexibility.

Resolving subscriptions

Instead of configuring all your subscriptions in config/spike.php configuration file, you can instead provide these from other sources by setting a custom resolver. This allows you to load the subscriptions from your database, file, or any other source.

When a custom subscription plan resolver is set, the subscription plans configured in config/spike.php will be ignored.

You can set a custom resolvers for subscription plans by calling Spike::resolveSubscriptionPlansUsing($callback) method in the boot method of your AppServiceProvider.

The callback receives an optional billable instance and should return an array (or a Laravel Collection) of SubscriptionPlan objects or associative arrays.

use Opcodes\Spike\SubscriptionPlan;
use Opcodes\Spike\Facades\Spike;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Spike::resolveSubscriptionPlansUsing(function ($billable) {
        // You can load the subscription plans from database/file/etc
        // $billable can be null

        return [
            new SubscriptionPlan(
                id: 'free',
                name: 'Free',
                monthly_credits: 200,
            ),
            new SubscriptionPlan(
                id: 'standard',
                name: 'Standard',
                monthly_credits: 5000,
                stripe_price_id: 'stripe_price_id',
                price_in_cents: 1000,
                period: SubscriptionPlan::PERIOD_MONTHLY,
            ),
            new SubscriptionPlan(
                id: 'standard',
                name: 'Standard',
                monthly_credits: 5000,
                stripe_price_id: 'stripe_price_id',
                price_in_cents: 12000,
                period: SubscriptionPlan::PERIOD_YEARLY,
            ),
        ];
    });
}

The provided $billable parameter is the resolved billable who will see these subscription plans. This allows you to control what subscription plans are returned to the user.

Resolving products

Instead of configuring all your products in config/spike.php configuration file, you can instead provide these from other sources by setting a custom resolver. This allows you to load the products from your database, file, or any other source.

When a custom product resolver is set, the products configured in config/spike.php will be ignored.

You can set a custom resolvers for products by calling Spike::resolveProductsUsing($callback) method in the boot method of your AppServiceProvider.

The callback receives an optional billable instance and should return an array (or a Laravel Collection) of Product objects or associative arrays.

use Opcodes\Spike\Product;
use Opcodes\Spike\Facades\Spike;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Spike::resolveProductsUsing(function ($billable) {
        // You can load the products from database/file/etc
        // $billable can be null

        return [
            new Product(
                id: 'free',
                name: 'Free',
                credits: 200,
            ),
            new Product(
                id: 'standard',
                name: 'Standard',
                credits: 5000,
                stripe_price_id: 'stripe_price_id',
                price_in_cents: 1000,
                expires_after: CarbonInterval::months(6),
            )
        ];
    });
}

Support

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