Overview
The CustomerService class handles customer creation and retrieval in MercadoPago. Customers are used to store payer information and manage saved payment methods (cards).
Source : src/Services/CustomerService.php:9
Constructor
public function __construct (
private MercadoPagoClientFactory $clientFactory ,
) {}
Dependencies
clientFactory
MercadoPagoClientFactory
required
Factory for creating and configuring MercadoPago SDK clients. Automatically injected by Laravel’s service container.
Methods
create()
Creates a new customer in MercadoPago.
public function create ( array $payload ) : mixed
Source : src/Services/CustomerService.php:15
Parameters
The customer configuration data. Customer’s email address (must be valid email format)
Phone information:
area_code (string): Area code
number (string): Phone number
Identification document:
type (string): Document type (e.g., “DNI”, “CPF”, “CUIT”)
number (string): Document number
Customer address:
zip_code (string): Postal code
street_name (string): Street name
street_number (integer): Street number
Customer description or notes
Custom key-value data for your application
Returns
MercadoPago customer object containing: Unique customer identifier (used for card operations)
Identification document details
ISO 8601 creation timestamp
ISO 8601 last update timestamp
Usage
Controller injection
Link to User model
Simple creation
use Fitodac\LaravelMercadoPago\Services\ CustomerService ;
use Illuminate\Http\ JsonResponse ;
use Illuminate\Http\ Request ;
final class CustomerController
{
public function store (
Request $request ,
CustomerService $customerService
) : JsonResponse {
$customer = $customerService -> create ([
'email' => $request -> email ,
'first_name' => $request -> first_name ,
'last_name' => $request -> last_name ,
'phone' => [
'area_code' => $request -> area_code ,
'number' => $request -> phone_number ,
],
'identification' => [
'type' => 'DNI' ,
'number' => $request -> dni ,
],
'description' => 'Premium customer' ,
'metadata' => [
'user_id' => auth () -> id (),
'plan' => 'premium' ,
],
]);
// Store customer ID for future use
auth () -> user () -> update ([
'mercadopago_customer_id' => data_get ( $customer , 'id' ),
]);
return response () -> json ([
'customer_id' => data_get ( $customer , 'id' ),
], 201 );
}
}
get()
Retrieves customer information by customer ID.
public function get ( string $customerId ) : mixed
Source : src/Services/CustomerService.php:24
Parameters
The unique customer identifier returned when the customer was created
Returns
MercadoPago customer object with the same structure as the create() method response. Contains all customer information and metadata.
Usage
Retrieve customer data
Controller method
Verify customer exists
use Fitodac\LaravelMercadoPago\Services\ CustomerService ;
$service = app ( CustomerService :: class );
$customerId = auth () -> user () -> mercadopago_customer_id ;
$customer = $service -> get ( $customerId );
$email = data_get ( $customer , 'email' );
$name = data_get ( $customer , 'first_name' ) . ' ' . data_get ( $customer , 'last_name' );
Common Patterns
One customer per user
Create a MercadoPago customer for each application user:
// Add to User model
public function getOrCreateMercadoPagoCustomer ()
{
if ( $this -> mercadopago_customer_id ) {
return app ( CustomerService :: class )
-> get ( $this -> mercadopago_customer_id );
}
$customer = app ( CustomerService :: class ) -> create ([
'email' => $this -> email ,
'first_name' => $this -> first_name ,
'last_name' => $this -> last_name ,
'metadata' => [
'user_id' => $this -> id ,
],
]);
$this -> update ([
'mercadopago_customer_id' => data_get ( $customer , 'id' ),
]);
return $customer ;
}
Customer with saved cards
Customers are required for saving payment methods:
use Fitodac\LaravelMercadoPago\Services\ CustomerService ;
use Fitodac\LaravelMercadoPago\Services\ CardService ;
// Create customer first
$customer = app ( CustomerService :: class ) -> create ([
'email' => '[email protected] ' ,
'first_name' => 'John' ,
'last_name' => 'Doe' ,
]);
$customerId = data_get ( $customer , 'id' );
// Then save card
$card = app ( CardService :: class ) -> create ( $customerId , [
'token' => $cardToken ,
]);
Error Handling
MercadoPagoConfigurationException
Thrown when:
MercadoPago SDK is not installed
Required SDK classes are not found
SDK methods are unavailable
Thrown by the MercadoPago SDK for API errors:
Invalid credentials
Invalid customer data
Customer not found (for get() method)
Duplicate customer
Network errors
Error handling example
use Fitodac\LaravelMercadoPago\Exceptions\ MercadoPagoConfigurationException ;
try {
$customer = $customerService -> create ( $payload );
} catch ( MercadoPagoConfigurationException $e ) {
\ Log :: error ( 'MercadoPago configuration error' , [
'message' => $e -> getMessage (),
]);
return response () -> json ([ 'error' => 'Service misconfigured' ], 500 );
} catch ( \ Exception $e ) {
\ Log :: error ( 'Customer creation failed' , [
'message' => $e -> getMessage (),
'payload' => $payload ,
]);
return response () -> json ([ 'error' => 'Customer creation failed' ], 500 );
}
CardService Save and manage customer payment methods
PaymentService Process payments for customers
Additional Resources
Managing Customers Guide Complete guide to customer management
MercadoPago API Documentation Official customers API reference