# laravel/cashier-stripe architecture

> Architecture diagram of the GitHub repository laravel/cashier-stripe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/laravel/cashier-stripe
- Repository: https://github.com/laravel/cashier-stripe

## Overview

##### Main Components
- Laravel Integration: Since it’s a Laravel package, a core element is how it integrates with a Laravel application. This includes its service provider (found in src/CashierServiceProvider.php) and the Billable interface (src/Billable.php).
- Billing & Subscription Logic: The key classes here include Cashier (src/Cashier.php), Subscription (src/Subscription.php), SubscriptionBuilder, and related models like Invoice (src/Invoice.php), Payment (src/Payment.php), and PaymentMethod (src/PaymentMethod.php). Also, note related concerns (inside src/Concerns/) that handle coupons, taxes, invoice management, and more.
- HTTP Layer & Controllers: The project defines controllers (e.g., src/Http/Controllers/PaymentController.php, WebhookController.php) and related middleware (src/Http/Middleware/VerifyWebhookSignature.php, VerifyRedirectUrl.php) that process incoming HTTP requests like payment initiations and Stripe webhooks.
- Events, Jobs, and Notifications: There’s an event-driven component with events (src/Events) and jobs (src/Jobs/SyncCustomerDetails.php) that handle asynchronous tasks or real-time updates.
- Database Integration: With migrations in the database/migrations directory, the package creates and manages tables for customers, subscriptions, invoices, etc. Factories under database/factories also help in seeding and testing.
- External Service (Stripe): Although not directly visible as a “Stripe API client,” the project is built around interfacing with Stripe’s subscription billing. The documentation and naming (Checkout, PaymentMethod handling, etc.) strongly indicate Stripe as the external dependency.
- Views & Invoice Rendering: The resources/views folder contains Blade templates for invoices and payments. Additionally, there is a contract for an InvoiceRenderer and an implementation (src/Invoices/DompdfInvoiceRenderer.php) to convert invoices into PDFs.

##### Relationships & Interactions
- A Laravel application will register the CashierServiceProvider, which sets up the package and binds necessary services.
- The HTTP Layer (controllers and middleware) handles user-initiated actions and webhook callbacks. These controllers call methods in the core billing classes (e.g., Cashier, Checkout) to manage subscription-related activities.
- When a request for a new subscription or an invoice is received, the package uses the Billing Logic classes to process the request, validate data, and update the database accordingly (using models and migrations).
- Events (like WebhookReceived and WebhookHandled) propagate changes and trigger Jobs (such as SyncCustomerDetails) to keep the local state in sync with Stripe.
- The InvoiceRenderer component converts invoice data into a user-friendly PDF using tools like Dompdf.

##### Architectural Patterns & Design Principles
- Service Provider Pattern: Encapsulated by the CashierServiceProvider to integrate with Laravel.
- MVC Organization: Segregation between controllers (in src/Http), models (in src/ and database), and views (in resources/views).
- Event-Driven Architecture: Use of events and asynchronous job processing ensures responsiveness, especially in handling Stripe webhooks.
- Separation of Concerns: Use of traits (in src/Concerns/) to modularize billing functionalities such as handling coupons, managing invoices, or processing taxes.

##### Technologies & Frameworks
- PHP and Laravel form the core tech stack.
- Composer for dependency management.
- Stripe as the external payment gateway.
- Dompdf for generating PDFs.
- Testing frameworks (PHPUnit) supported by tests in both Feature and Unit directories.
- GitHub workflows (YAML files inside .github/workflows) showcase integration for testing, static analysis, and CI/CD.

## Diagram (Mermaid)

```mermaid
flowchart TD

    subgraph "Laravel Integration"
        A["Cashier Service Provider"]
        B["Billable Interface"]
    end

    subgraph "Billing & Subscription Logic"
        C["Cashier"]
        D["Subscription"]
        E["SubscriptionBuilder"]
        F["Invoice"]
        G["Payment"]
        H["PaymentMethod"]
        I["Concerns"]
    end

    subgraph "HTTP Layer"
        J["PaymentController"]
        K["WebhookController"]
        L["VerifyWebhookSignature"]
        M["VerifyRedirectUrl"]
    end

    subgraph "Events / Jobs / Notifications"
        N["WebhookHandled"]
        O["WebhookReceived"]
        P["SyncCustomerDetails"]
        Q["ConfirmPayment"]
    end

    subgraph "Database Integration"
        R["Migrations"]
        S["Factories"]
    end

    subgraph "Views & Invoice Rendering"
        T["Invoice Blade"]
        U["Payment Blade"]
        V["DompdfInvoiceRenderer"]
    end

    subgraph "External Service"
        W["Stripe API"]
    end

    %% Relationships
    A -->|"registers"| C
    J -->|"initiates"| C
    K -->|"handles"| O
    C -->|"processes"| D
    C -->|"builds"| E
    C -->|"generates"| F
    C -->|"processes"| G
    C -->|"manages"| H
    C -->|"uses"| I
    F -->|"rendersPDF"| V
    C -->|"updatesDB"| R
    C -->|"triggersJob"| P
    O -->|"leadsToJob"| P
    C -->|"integratesWith"| W

    %% Styling
    classDef integration fill:#f9c,stroke:#333,stroke-width:2px;
    classDef logic fill:#ccf,stroke:#333,stroke-width:2px;
    classDef http fill:#cfc,stroke:#333,stroke-width:2px;
    classDef events fill:#fc9,stroke:#333,stroke-width:2px;
    classDef database fill:#cff,stroke:#333,stroke-width:2px;
    classDef views fill:#fcc,stroke:#333,stroke-width:2px;
    classDef external fill:#efe,stroke:#333,stroke-width:2px;

    class A,B integration;
    class C,D,E,F,G,H,I logic;
    class J,K,L,M http;
    class N,O,P,Q events;
    class R,S database;
    class T,U,V views;
    class W external;

    %% Click Events
    click A "https://github.com/laravel/cashier-stripe/blob/15.x/src/CashierServiceProvider.php"
    click B "https://github.com/laravel/cashier-stripe/blob/15.x/src/Billable.php"
    click C "https://github.com/laravel/cashier-stripe/blob/15.x/src/Cashier.php"
    click D "https://github.com/laravel/cashier-stripe/blob/15.x/src/Subscription.php"
    click E "https://github.com/laravel/cashier-stripe/blob/15.x/src/SubscriptionBuilder.php"
    click F "https://github.com/laravel/cashier-stripe/blob/15.x/src/Invoice.php"
    click G "https://github.com/laravel/cashier-stripe/blob/15.x/src/Payment.php"
    click H "https://github.com/laravel/cashier-stripe/blob/15.x/src/PaymentMethod.php"
    click I "https://github.com/laravel/cashier-stripe/tree/15.x/src/Concerns"
    click J "https://github.com/laravel/cashier-stripe/blob/15.x/src/Http/Controllers/PaymentController.php"
    click K "https://github.com/laravel/cashier-stripe/blob/15.x/src/Http/Controllers/WebhookController.php"
    click L "https://github.com/laravel/cashier-stripe/blob/15.x/src/Http/Middleware/VerifyWebhookSignature.php"
    click M "https://github.com/laravel/cashier-stripe/blob/15.x/src/Http/Middleware/VerifyRedirectUrl.php"
    click N "https://github.com/laravel/cashier-stripe/blob/15.x/src/Events/WebhookHandled.php"
    click O "https://github.com/laravel/cashier-stripe/blob/15.x/src/Events/WebhookReceived.php"
    click P "https://github.com/laravel/cashier-stripe/blob/15.x/src/Jobs/SyncCustomerDetails.php"
    click Q "https://github.com/laravel/cashier-stripe/blob/15.x/src/Notifications/ConfirmPayment.php"
    click R "https://github.com/laravel/cashier-stripe/tree/15.x/database/migrations"
    click S "https://github.com/laravel/cashier-stripe/tree/15.x/database/factories"
    click T "https://github.com/laravel/cashier-stripe/blob/15.x/resources/views/invoice.blade.php"
    click U "https://github.com/laravel/cashier-stripe/blob/15.x/resources/views/payment.blade.php"
    click V "https://github.com/laravel/cashier-stripe/blob/15.x/src/Invoices/DompdfInvoiceRenderer.php"
```
