Skip to content

Commit

Permalink
Merge pull request #24 from maartenpaauw/feat/generic-trial-support
Browse files Browse the repository at this point in the history
feat: add support for generic trial days
  • Loading branch information
maartenpaauw authored Oct 24, 2024
2 parents 776bd5d + 01decd8 commit 8e53987
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ Add plans to your `cashier.php` config file:
'plans' => [
'default' => [
'price_id' => ENV('CASHIER_STRIPE_SUBSCRIPTION_DEFAULT_PRICE_ID'),
'type' => 'default', // Optional, by default it uses the array key as type...
'type' => 'default', // Optional, by default it uses the array key as type.
'trial_days' => 14, // Optional
'has_generic_trial' => true, // Optional, only `trial_days` OR `has_generic_trial` can be used.
'allow_promotion_codes' => true, // Optional
'collect_tax_ids' => true, // Optional
'metered_price' => true, // Optional
Expand Down
5 changes: 5 additions & 0 deletions src/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function trialDays(): int|false
return $this->repository->get("cashier.plans.$this->plan.trial_days", false);
}

public function hasGenericTrial(): bool
{
return $this->repository->get("cashier.plans.$this->plan.has_generic_trial", false);
}

public function allowPromotionCodes(): bool
{
return $this->repository->get("cashier.plans.$this->plan.allow_promotion_codes", false);
Expand Down
6 changes: 5 additions & 1 deletion src/Stripe/RedirectIfUserNotSubscribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function handle(Request $request, Closure $next, string $plan = 'default'
$tenant = TenantRepository::make()->current();
$plan = new Plan($this->repository, $plan);

if ($plan->hasGenericTrial() && $tenant->onGenericTrial()) {
return $next($request);
}

if ($tenant->subscribed($plan->type())) {
return $next($request);
}
Expand All @@ -42,7 +46,7 @@ public function handle(Request $request, Closure $next, string $plan = 'default'
static fn (SubscriptionBuilder $subscription): SubscriptionBuilder => $subscription->meteredPrice($plan->priceId()),
)
->when(
$plan->trialDays() !== false,
! $plan->hasGenericTrial() && $plan->trialDays() !== false,
static fn (SubscriptionBuilder $subscription): SubscriptionBuilder => $subscription->trialDays($plan->trialDays()),
)
->when(
Expand Down
5 changes: 5 additions & 0 deletions tests/PlanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'default' => [
'price_id' => 'price_Fxp5y8x0qjrm2jjk2nzuMpSF',
'type' => 'primary',
'has_generic_trial' => true,
'trial_days' => 14,
'allow_promotion_codes' => true,
'collect_tax_ids' => true,
Expand All @@ -33,6 +34,8 @@
->toEqual('price_Fxp5y8x0qjrm2jjk2nzuMpSF')
->trialDays()
->toEqual(14)
->hasGenericTrial()
->toBeTrue()
->allowPromotionCodes()
->toBeTrue()
->collectTaxIds()
Expand All @@ -49,6 +52,8 @@
->toEqual('price_ruVAAh5dwyhwbtWIVQn0lUvc')
->trialDays()
->toBeFalse()
->hasGenericTrial()
->toBeFalse()
->allowPromotionCodes()
->toBeFalse()
->collectTaxIds()
Expand Down

0 comments on commit 8e53987

Please sign in to comment.