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 a collection of subscription plans resolved from config, and should return an array (or a Laravel Collection) of SubscriptionPlan
objects or associative arrays (same structure as defined in Spike config).
use Opcodes\Spike\SubscriptionPlan;
use Opcodes\Spike\Facades\Spike;
use Opcodes\Spike\CreditAmount;
use Illuminate\Support\Collection;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Spike::resolveSubscriptionPlansUsing(function ($billable, Collection $currentPlans) {
// $currentPlans contains the subscription plans configured in config/spike.php.
// You can load the subscription plans from database/file/etc
// or return a subset/updated $currentPlans, etc.
// $billable can be null
return [
new SubscriptionPlan(
id: 'free',
name: 'Free',
provides_monthly: [
CreditAmount::make(200),
],
),
new SubscriptionPlan(
id: 'standard',
name: 'Standard',
provides_monthly: [
CreditAmount::make(5000),
],
payment_provider_price_id: 'stripe_or_paddle_price_id',
price_in_cents: 10_00,
period: SubscriptionPlan::PERIOD_MONTHLY,
),
new SubscriptionPlan(
id: 'standard',
name: 'Standard',
provides_monthly: [
CreditAmount::make(5000),
],
payment_provider_price_id: 'stripe_or_paddle_price_id',
price_in_cents: 120_00,
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 a collection of products resolved from config, and should return an array (or a Laravel Collection) of Product
objects or associative arrays (same structure as defined in Spike config).
use Opcodes\Spike\Product;
use Opcodes\Spike\Facades\Spike;
use Opcodes\Spike\CreditAmount;
use Illuminate\Support\Collection;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Spike::resolveProductsUsing(function ($billable, Collection $currentProducts) {
// $currentProducts contains products configured in config/spike.php
// You can load the products from database/file/etc
// Or return a subset/updated $currentProducts collection.
// $billable can be null
return [
new Product(
id: 'free',
name: 'Free',
provides: [
CreditAmount::make(200),
],
),
new Product(
id: 'standard',
name: 'Standard',
provides: [
CreditAmount::make(5000)->expiresAfterMonths(6),
],
payment_provider_price_id: 'stripe_or_paddle_price_id',
price_in_cents: 1000,
)
];
});
}