How laravel/cashier-stripe works
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.