Skip to content

Commit

Permalink
feat: dynamic allow promotion codes based on configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenpaauw committed Apr 2, 2024
1 parent 43810b5 commit 62205fb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Add plans to your `cashier.php` config file:
'default' => [
'price_id' => ENV('CASHIER_STRIPE_SUBSCRIPTION_DEFAULT_PRICE_ID'),
'trial_days' => 14, // Optional
'allow_promotion_codes' => true, // Optional
'collect_tax_ids' => true, // Optional
],
],
Expand Down
45 changes: 45 additions & 0 deletions src/Plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Maartenpaauw\Filament\Cashier;

use Illuminate\Config\Repository;
use InvalidArgumentException;

final class Plan
{
public function __construct(
private readonly Repository $repository,
private readonly string $type = 'default',
) {
}

public function type(): string
{
return $this->type;
}

public function priceId(): string
{
return $this->repository->get(
"cashier.plans.$this->type.price_id",
static fn (): never => throw new InvalidArgumentException('Invalid plan configuration'),
);
}

public function trialDays(): int | false
{
return $this->repository->get("cashier.plans.$this->type.trial_days", false);
}

public function allowPromotionCodes(): bool
{
return $this->repository->get("cashier.plans.$this->type.allow_promotion_codes", false);
}

public function collectTaxIds(): bool
{
return $this->repository->get("cashier.plans.$this->type.collect_tax_ids", false);
}
}
27 changes: 18 additions & 9 deletions src/Stripe/RedirectIfUserNotSubscribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laravel\Cashier\Cashier;
use Laravel\Cashier\SubscriptionBuilder;
use LogicException;
use Maartenpaauw\Filament\Cashier\Plan;
use Symfony\Component\HttpFoundation\Response;

final class RedirectIfUserNotSubscribed
Expand Down Expand Up @@ -41,18 +42,26 @@ public function handle(Request $request, Closure $next, string $plan = 'default'
throw new LogicException('Customer model does not use Cashier Billable trait');
}

if ($tenant->subscribed($plan)) {
$plan = new Plan($this->repository, $plan);

if ($tenant->subscribed($plan->type())) {
return $next($request);
}

$priceId = $this->repository->get("cashier.plans.$plan.price_id");
$trialDays = $this->repository->get("cashier.plans.$plan.trial_days", false);
$collectTaxIds = $this->repository->get("cashier.plans.$plan.collect_tax_ids", false);

return $tenant->newSubscription($plan, $priceId)
->allowPromotionCodes()
->when($trialDays, static fn (SubscriptionBuilder $subscription) => $subscription->trialDays($trialDays))
->when($collectTaxIds, static fn (SubscriptionBuilder $subscription) => $subscription->collectTaxIds())
return $tenant
->newSubscription($plan->type(), $plan->priceId())
->when(
$plan->trialDays() !== false,
static fn (SubscriptionBuilder $subscription): SubscriptionBuilder => $subscription->trialDays($plan->trialDays()),
)
->when(
$plan->allowPromotionCodes(),
static fn (SubscriptionBuilder $subscription): SubscriptionBuilder => $subscription->allowPromotionCodes(),
)
->when(
$plan->collectTaxIds(),
static fn (SubscriptionBuilder $subscription): SubscriptionBuilder => $subscription->collectTaxIds(),
)
->checkout([
'success_url' => Dashboard::getUrl(),
'cancel_url' => Dashboard::getUrl(),
Expand Down

0 comments on commit 62205fb

Please sign in to comment.