Stripe Billing Integration for SaaS Subscriptions

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    823
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    848
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Stripe Billing Integration for SaaS Subscriptions

Stripe Billing is the most complete ready-made billing implementation for SaaS on the market. Covers subscriptions, trial periods, upgrades/downgrades, prorated billing, metered consumption, automatic retry on failure. Implementation from scratch would take months — Stripe Billing reduces it to 1–2 weeks.

Key Entities

ProductPriceSubscriptionInvoicePaymentIntent. Product is a plan (Basic, Pro, Enterprise). Multiple Prices are attached to product — monthly and yearly. Subscription links customer to Price. Invoice is issued automatically at start of each period.

Creating Products and Prices

// Create product (once during setup)
$product = \Stripe\Product::create([
    'name'     => 'Pro Plan',
    'metadata' => ['plan_id' => 'pro'],
]);

// Monthly price
$monthlyPrice = \Stripe\Price::create([
    'product'       => $product->id,
    'unit_amount'   => 2900,  // $29.00
    'currency'      => 'usd',
    'recurring'     => ['interval' => 'month'],
    'lookup_key'    => 'pro_monthly', // to find without storing ID
]);

// Annual (with discount)
$yearlyPrice = \Stripe\Price::create([
    'product'       => $product->id,
    'unit_amount'   => 27900, // $279.00
    'currency'      => 'usd',
    'recurring'     => ['interval' => 'year'],
    'lookup_key'    => 'pro_yearly',
]);

User Registration and Subscription Creation

// On registration — create Stripe Customer
$stripeCustomer = \Stripe\Customer::create([
    'email'    => $user->email,
    'name'     => $user->name,
    'metadata' => ['user_id' => $user->id],
]);
$user->update(['stripe_customer_id' => $stripeCustomer->id]);

// Create subscription with trial
$subscription = \Stripe\Subscription::create([
    'customer'          => $user->stripe_customer_id,
    'items'             => [['price' => 'pro_monthly']],
    'trial_period_days' => 14,
    'payment_behavior'  => 'default_incomplete',
    'payment_settings'  => ['save_default_payment_method' => 'on_subscription'],
    'expand'            => ['latest_invoice.payment_intent'],
]);

// Return client_secret for card confirmation on frontend
$clientSecret = $subscription->latest_invoice->payment_intent->client_secret;

payment_behavior: default_incomplete means subscription is created but activated only after successful first payment. Important for free trials: card is linked but not charged.

Upgrade/Downgrade

public function changePlan(User $user, string $newPriceLookupKey): void
{
    $prices = \Stripe\Price::all(['lookup_keys' => [$newPriceLookupKey]]);
    $newPrice = $prices->data[0];

    $subscription = \Stripe\Subscription::retrieve($user->stripe_subscription_id);

    \Stripe\Subscription::update($subscription->id, [
        'items'              => [[
            'id'    => $subscription->items->data[0]->id,
            'price' => $newPrice->id,
        ]],
        'proration_behavior' => 'create_prorations', // or 'none' for annual
        'billing_cycle_anchor'=> 'unchanged',
    ]);
}

On upgrade with proration, Stripe automatically credits unused time of current plan and issues invoice for the difference.

Webhook: Status Synchronization

All business logic should be built on webhooks, not synchronous API responses. Events to handle:

protected array $handlers = [
    'customer.subscription.created'       => 'onSubscriptionCreated',
    'customer.subscription.updated'       => 'onSubscriptionUpdated',
    'customer.subscription.deleted'       => 'onSubscriptionCancelled',
    'invoice.payment_succeeded'           => 'onInvoicePaid',
    'invoice.payment_failed'              => 'onInvoicePaymentFailed',
    'customer.subscription.trial_will_end'=> 'onTrialEndingSoon',
];

public function onInvoicePaymentFailed(array $event): void
{
    $subscription = $event['data']['object']['subscription'];
    $user = User::where('stripe_subscription_id', $subscription)->firstOrFail();

    // Don't block immediately — Stripe does retry
    // next_payment_attempt in invoice
    $nextRetry = $event['data']['object']['next_payment_attempt'];
    Notification::send($user, new PaymentFailedNotification($nextRetry));
}

Customer Portal

Stripe Customer Portal is ready UI to manage subscription (change card, cancel, invoice history). No need to write from scratch:

$session = \Stripe\BillingPortal\Session::create([
    'customer'   => $user->stripe_customer_id,
    'return_url' => route('dashboard'),
]);
return redirect($session->url);

Configured in Stripe Dashboard: allow/disallow plan change, cancellation, invoice download.

Metered Billing

For SaaS with consumption-based pricing (API calls, storage, users):

// Price with metered billing
$price = \Stripe\Price::create([
    'product'   => $product->id,
    'currency'  => 'usd',
    'recurring' => [
        'interval'       => 'month',
        'usage_type'     => 'metered',
        'aggregate_usage'=> 'sum',
    ],
    'billing_scheme' => 'per_unit',
    'unit_amount'    => 1, // $0.01 per unit
]);

// Report consumption (once per hour or end of period)
\Stripe\SubscriptionItem::createUsageRecord(
    $subscriptionItemId,
    ['quantity' => $apiCallsThisPeriod, 'action' => 'set']
);

action: set sets absolute value, increment adds to current. set is safer on retry.