Skip to main content

Overview

The PreferenceService class handles creation of payment preferences for MercadoPago’s checkout experience. Preferences define the items being purchased, payment details, and redirect URLs. Source: src/Services/PreferenceService.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 payment preference in MercadoPago.
public function create(array $payload): mixed
Source: src/Services/PreferenceService.php:15

Parameters

payload
array
required
The preference configuration data.

Returns

response
mixed
MercadoPago preference object containing:

Usage

use Fitodac\LaravelMercadoPago\Services\PreferenceService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

final class CheckoutController
{
    public function store(
        Request $request,
        PreferenceService $preferenceService
    ): JsonResponse {
        $preference = $preferenceService->create([
            'items' => [
                [
                    'title' => 'Premium Subscription',
                    'quantity' => 1,
                    'unit_price' => 99.99,
                ],
            ],
            'payer' => [
                'email' => '[email protected]',
            ],
            'back_urls' => [
                'success' => route('payment.success'),
                'pending' => route('payment.pending'),
                'failure' => route('payment.failure'),
            ],
            'notification_url' => route('mercadopago.webhooks'),
            'external_reference' => 'ORDER-12345',
        ]);
        
        return response()->json([
            'preference_id' => data_get($preference, 'id'),
            'checkout_url' => data_get($preference, 'init_point'),
        ]);
    }
}

Error Handling

The service may throw the following exceptions:
MercadoPagoConfigurationException
exception
Thrown when:
  • MercadoPago SDK is not installed
  • Required SDK classes are not found
  • SDK methods are unavailable
MercadoPagoException
exception
Thrown by the MercadoPago SDK for API errors:
  • Invalid credentials
  • Invalid payload data
  • Network errors
  • API rate limits

Error handling example

use Fitodac\LaravelMercadoPago\Exceptions\MercadoPagoConfigurationException;

try {
    $preference = $preferenceService->create($payload);
} catch (MercadoPagoConfigurationException $e) {
    \Log::error('MercadoPago configuration error', [
        'message' => $e->getMessage(),
    ]);
    return response()->json(['error' => 'Service configuration error'], 500);
} catch (\Exception $e) {
    \Log::error('Preference creation failed', [
        'message' => $e->getMessage(),
        'payload' => $payload,
    ]);
    return response()->json(['error' => 'Preference creation failed'], 500);
}

PaymentService

Process direct card payments

WebhookService

Handle payment notifications

Additional Resources

Creating Preferences Guide

Comprehensive guide to preference creation

MercadoPago API Documentation

Official API reference

Build docs developers (and LLMs) love