SaaS subscription sales implementation via website
SaaS subscription sales is technically more complex than selling one-off products. Required: pricing tiers with different limits, subscription management (upgrade, downgrade, cancellation), billing cycles, trial periods.
Billing architecture
Don't implement billing yourself — use Stripe Billing, Paddle, or Chargebee. They handle: recurring payments, failed transactions, dunning mechanics, taxes by region.
Stripe Billing integration
class SubscriptionService
{
public function createSubscription(User $user, string $priceId, array $options = []): Subscription
{
// Create Stripe Customer if not exists
if (!$user->stripe_customer_id) {
$customer = $this->stripe->customers->create([
'email' => $user->email,
'metadata' => ['user_id' => $user->id],
]);
$user->update(['stripe_customer_id' => $customer->id]);
}
$subscriptionData = [
'customer' => $user->stripe_customer_id,
'items' => [['price' => $priceId]],
'trial_period_days' => $options['trial_days'] ?? 0,
'payment_behavior' => 'default_incomplete',
'expand' => ['latest_invoice.payment_intent'],
];
$stripeSubscription = $this->stripe->subscriptions->create($subscriptionData);
return Subscription::create([
'user_id' => $user->id,
'stripe_subscription_id' => $stripeSubscription->id,
'plan' => $options['plan'],
'status' => $stripeSubscription->status,
'trial_ends_at' => $stripeSubscription->trial_end
? Carbon::createFromTimestamp($stripeSubscription->trial_end)
: null,
'current_period_end' => Carbon::createFromTimestamp($stripeSubscription->current_period_end),
]);
}
public function cancel(Subscription $subscription, bool $immediately = false): void
{
if ($immediately) {
$this->stripe->subscriptions->cancel($subscription->stripe_subscription_id);
} else {
// Cancel at end of period
$this->stripe->subscriptions->update($subscription->stripe_subscription_id, [
'cancel_at_period_end' => true,
]);
}
}
}
Pricing tiers with limits
// Plan limits table
class PlanLimits
{
private array $limits = [
'free' => ['projects' => 1, 'storage_gb' => 1, 'api_calls_month' => 1000],
'starter' => ['projects' => 5, 'storage_gb' => 10, 'api_calls_month' => 10000],
'pro' => ['projects' => 20, 'storage_gb' => 50, 'api_calls_month' => 100000],
'enterprise' => ['projects' => -1, 'storage_gb' => -1, 'api_calls_month' => -1], // -1 = unlimited
];
public function check(string $plan, string $feature, mixed $currentUsage): bool
{
$limit = $this->limits[$plan][$feature] ?? 0;
if ($limit === -1) return true; // unlimited
return $currentUsage < $limit;
}
}
// In controller
public function createProject(Request $request)
{
$user = auth()->user();
$plan = $user->subscription->plan;
$currentProjects = $user->projects()->count();
if (!app(PlanLimits::class)->check($plan, 'projects', $currentProjects)) {
return response()->json([
'error' => 'Project limit exceeded for your plan',
'upgrade_url' => route('billing.upgrade'),
], 402);
}
// Create project
}
Webhook event handler for billing
Route::post('/webhooks/stripe', function (Request $request) {
$event = \Stripe\Webhook::constructEvent(
$request->getContent(),
$request->header('Stripe-Signature'),
config('services.stripe.webhook_secret')
);
match($event->type) {
'customer.subscription.created' =>
HandleSubscriptionCreated::dispatch($event->data->object),
'customer.subscription.updated' =>
HandleSubscriptionUpdated::dispatch($event->data->object),
'customer.subscription.deleted' =>
HandleSubscriptionCancelled::dispatch($event->data->object),
'invoice.payment_failed' =>
HandlePaymentFailed::dispatch($event->data->object),
'invoice.payment_succeeded' =>
HandlePaymentSucceeded::dispatch($event->data->object),
default => null,
};
return response('ok');
});
Timeline
SaaS billing with Stripe, pricing tiers, and subscription management: 14–20 working days.







