diff --git a/README.md b/README.md index 72c3322..cddd773 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Plan.php b/src/Plan.php index df9c13c..1608884 100644 --- a/src/Plan.php +++ b/src/Plan.php @@ -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); diff --git a/src/Stripe/RedirectIfUserNotSubscribed.php b/src/Stripe/RedirectIfUserNotSubscribed.php index ac2df39..2955473 100644 --- a/src/Stripe/RedirectIfUserNotSubscribed.php +++ b/src/Stripe/RedirectIfUserNotSubscribed.php @@ -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); } @@ -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( diff --git a/tests/PlanTest.php b/tests/PlanTest.php index bac93c8..190b024 100644 --- a/tests/PlanTest.php +++ b/tests/PlanTest.php @@ -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, @@ -33,6 +34,8 @@ ->toEqual('price_Fxp5y8x0qjrm2jjk2nzuMpSF') ->trialDays() ->toEqual(14) + ->hasGenericTrial() + ->toBeTrue() ->allowPromotionCodes() ->toBeTrue() ->collectTaxIds() @@ -49,6 +52,8 @@ ->toEqual('price_ruVAAh5dwyhwbtWIVQn0lUvc') ->trialDays() ->toBeFalse() + ->hasGenericTrial() + ->toBeFalse() ->allowPromotionCodes() ->toBeFalse() ->collectTaxIds()