Package Architecture
Laravel MercadoPago is built with clean architecture principles, leveraging Laravel’s service container, dependency injection, and auto-discovery to provide a maintainable and testable integration with Mercado Pago.Architectural Overview
The package follows a layered architecture that separates concerns and promotes code reusability:Service Layer
Dedicated service classes for each Mercado Pago resource
Support Layer
Factory and helper classes for SDK client management
Contract Layer
Interfaces for extensibility and testing
DTO Layer
Data Transfer Objects for type-safe credential handling
Core Components
Service Provider
TheLaravelMercadoPagoServiceProvider is the entry point for package registration and bootstrapping.
Location: src/LaravelMercadoPagoServiceProvider.php:23
Auto-Discovery
The package leverages Laravel’s package auto-discovery feature. When you install the package via Composer, Laravel automatically:- Detects the service provider in
composer.json - Registers the service provider on application boot
- Binds all services to the container
- Loads package routes and middleware
No manual provider registration is needed. The package is ready to use immediately after installation.
Dependency Injection
All services are registered as singletons in Laravel’s service container, enabling clean dependency injection:- Services are instantiated once per request lifecycle
- Reduced memory footprint and initialization overhead
- Shared SDK configuration across all services
- Consistent credential resolution
Component Relationships
Here’s how the major components interact:Service Layer
Each service encapsulates operations for a specific Mercado Pago resource:- PreferenceService - Payment preferences (
src/Services/PreferenceService.php:9) - PaymentService - Payment processing (
src/Services/PaymentService.php:9) - CustomerService - Customer management (
src/Services/CustomerService.php:9) - CardService - Customer cards (
src/Services/CardService.php:9) - RefundService - Refund operations (
src/Services/RefundService.php:9) - PaymentMethodService - Payment methods (
src/Services/PaymentMethodService.php:9) - WebhookService - Webhook validation (
src/Services/WebhookService.php:13) - TestUserService - Test user creation (
src/Services/TestUserService.php:9)
Support Layer
MercadoPagoClientFactory
The factory handles SDK client instantiation and configuration. Location:src/Support/MercadoPagoClientFactory.php:11
Key responsibilities:
- SDK Configuration - Configures the Mercado Pago SDK with access tokens
- Client Resolution - Dynamically instantiates SDK client classes
- Method Compatibility - Handles SDK version differences
- Runtime Environment - Sets appropriate environment (local/server)
Why use makeFirstAvailable?
Why use makeFirstAvailable?
The Mercado Pago SDK occasionally changes class names between versions. The
makeFirstAvailable method allows the package to support multiple SDK versions by attempting to instantiate clients from an array of possible class names.This ensures backward and forward compatibility without requiring package updates for every SDK change.SdkHttpClient
Provides low-level HTTP access to Mercado Pago’s API for endpoints not covered by the SDK. Location:src/Support/SdkHttpClient.php:11
Used by: TestUserService for creating test users via direct API calls
Contract Layer
CredentialResolverInterface
Defines the contract for credential resolution, enabling custom implementations. Location:src/Contracts/CredentialResolverInterface.php:9
EnvCredentialResolver) reads from Laravel’s configuration, but you can bind your own resolver for:
- Database-backed credentials
- Multi-tenant configurations
- Vault or secrets manager integration
- Per-user credential resolution
DTO Layer
MercadoPagoCredentials
A readonly Data Transfer Object for type-safe credential handling. Location:src/DTO/MercadoPagoCredentials.php:7
Configuration Management
The package uses a single configuration file that merges with Laravel’s config system: Location:config/mercadopago.php:5
Exception Handling
The package defines custom exceptions for better error handling:MercadoPagoConfigurationException
Location:src/Exceptions/MercadoPagoConfigurationException.php:9
Thrown when:
- Access token is missing (
src/Exceptions/MercadoPagoConfigurationException.php:11) - SDK is not installed (
src/Exceptions/MercadoPagoConfigurationException.php:16) - Client class not found (
src/Exceptions/MercadoPagoConfigurationException.php:21) - Client method not found (
src/Exceptions/MercadoPagoConfigurationException.php:31)
InvalidWebhookSignatureException
Location:src/Exceptions/InvalidWebhookSignatureException.php:9
Thrown when:
- Webhook signature header is malformed (
src/Exceptions/InvalidWebhookSignatureException.php:11) - HMAC signature validation fails (
src/Exceptions/InvalidWebhookSignatureException.php:16)
Design Principles
1. Single Responsibility
Each service class handles exactly one Mercado Pago resource type:- PreferenceService only manages preferences
- PaymentService only manages payments
- WebhookService only validates webhooks
2. Dependency Inversion
Services depend on abstractions (CredentialResolverInterface) rather than concrete implementations, making the package extensible and testable.
3. Open/Closed Principle
The package is open for extension (implement custom credential resolvers) but closed for modification (core services don’t need changes).4. Interface Segregation
TheCredentialResolverInterface contains only one method, ensuring implementing classes aren’t forced to implement unused methods.
Extending the Package
You can extend the package behavior in several ways:Custom Credential Resolver
AppServiceProvider:
Service Decoration
Next Steps
Services
Explore all available services and their methods
Credentials
Learn about credential management and security
API Reference
View detailed API documentation for each service
Testing
Learn how to test your Mercado Pago integration