Реализация продажи SaaS-доступа через сайт
Продажа SaaS-подписок — технически более сложная задача, чем продажа разовых товаров. Нужны: тарифные планы с разными лимитами, управление подписками (апгрейд, даунгрейд, отмена), биллинг по циклам, пробные периоды.
Архитектура биллинга
Не реализовывайте биллинг самостоятельно — используйте Stripe Billing, Paddle или Chargebee. Они берут на себя: повторяющиеся платежи, неудавшиеся транзакции, dunning-механики, налоги по регионам.
Интеграция со Stripe Billing
class SubscriptionService
{
public function createSubscription(User $user, string $priceId, array $options = []): Subscription
{
// Создаём Stripe Customer если нет
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 {
// Отмена в конце периода
$this->stripe->subscriptions->update($subscription->stripe_subscription_id, [
'cancel_at_period_end' => true,
]);
}
}
}
Тарифные планы с ограничениями
// Таблица планов
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; // безлимит
return $currentUsage < $limit;
}
}
// В контроллере
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' => 'Превышен лимит проектов для вашего тарифа',
'upgrade_url' => route('billing.upgrade'),
], 402);
}
// Создаём проект
}
Webhook обработчик событий биллинга
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');
});
Сроки
SaaS-биллинг со Stripe, тарифными планами и управлением подписками: 14–20 рабочих дней.







