Mercado Pago Python SDK

repository·master·Indexed 42 days ago

https://github.com/mercadopago/sdk-python

Pythonic bindings for the Mercado Pago API (version 3.6.0) to integrate online payments, manage orders, and handle transactions. Features include the Orders API for payment acceptance, legacy Payments API support, and typed request dataclasses for improved type safety.

Tokens
3.6K
Snippets
12
Records
17
Agent score
53%

What's inside Mercado Pago

  1. Security guidelines for reporting issues

    master

    To protect your account and comply with security policies, NEVER share the following information in public GitHub issues:

    • Access tokens or production credentials.
    • Client IDs / Client Secrets.
    • Customer or card data.
    • Personally Identifiable Information (PII).

    Note: Issues containing real sensitive data will be closed and reported immediately.

  2. How to report issues and get support

    master

    The Mercado Pago SDK uses GitHub Issues strictly for technical SDK support. For other issues, use the official Mercado Pago support channels.

    When to use GitHub Issues

    • Questions: Doubts about SDK usage, configuration, or best practices. (Include: SDK version, country, environment, and a code example).
    • Bugs: Errors, unexpected behavior, or crashes. (Include: reproducible steps, logs, environment, and SDK version).
    • Feature Requests: Proposing improvements or new features. (Include: the problem it solves, use cases, and benefits).
    • Contributions: To help improve the SDK, refer to CONTRIBUTING.md.

    When to use Official Mercado Pago Support

    Do NOT use GitHub for account, transaction, or commercial issues. Use the Official MP Support for:

    • Account problems or access issues.
    • Specific transactions, payments, or billing topics.
    • Homologation/certification.
    • Webhook configuration within your account.
    • Reports and statistics.
  3. Initialize the SDK with an access token

    master

    To use the Mercado Pago SDK, instantiate the SDK class with your OAuth access token. This central client provides factory methods to access all Mercado Pago API resources (such as payments, orders, and customers).

    By default, the SDK uses a built-in HttpClient based on requests and applies default RequestOptions (timeout, retries, etc.) to all requests.

    import mercadopago
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
  4. Initialize the MercadoPago SDK

    master

    To interact with the MercadoPago REST API, import the SDK class from mercadopago and initialize it with your access token. Once initialized, you can access various API resources (like payment()) to perform operations such as creating payments.

    import mercadopago
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
    # Example: Creating a payment
    payment = sdk.payment().create({
        # ... payment data
    })
  5. Create an order using typed request classes

    master

    For better type safety, use the provided dataclasses from mercadopago.resources. Fields set to None are automatically omitted from the resulting JSON body.

    import mercadopago
    from mercadopago.resources.order_create import OrderCreateRequest, OrderPayerRequest
    from mercadopago.resources.order_item import OrderItemRequest
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
    
    order = OrderCreateRequest(
        type="online",
        total_amount="100.00",
        external_reference="ext_ref_1234",
        payer=OrderPayerRequest(email="test_user_123456@testuser.com"),
        items=[OrderItemRequest(title="A book", unit_price="100.00", quantity=1)],
    )
    
    result = sdk.order().create(order)
    print(result["response"])
  6. Create an order using the Orders API

    master

    The Orders API (/v1/orders) is the recommended way to accept payments. You can use sdk.order().create() by passing either a plain dict or typed request dataclasses (like OrderCreateRequest). Both methods result in the same JSON body sent to the API.

    import mercadopago
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
    
    order_data = {
        "type": "online",
        "total_amount": "100.00",
        "external_reference": "ext_ref_1234",
        "transactions": {
            "payments": [
                {
                    "amount": "100.00",
                    "payment_method": {
                        "id": "master",
                        "type": "credit_card",
                        "token": "CARD_TOKEN",
                        "installments": 1,
                    },
                }
            ]
        },
        "payer": {
            "email": "test_user_123456@testuser.com"
        },
    }
    result = sdk.order().create(order_data)
    order = result["response"]
    print(order)
  7. Create a payment using the legacy Payments API

    master

    You can use the legacy Payments API via sdk.payment().create() by passing a dictionary containing payment details.

    import mercadopago
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
    
    payment_data = {
        "transaction_amount": 100,
        "token": "CARD_TOKEN",
        "description": "Payment description",
        "payment_method_id": 'visa',
        "installments": 1,
        "payer": {
            "email": 'test_user_123456@testuser.com'
        }
    }
    result = sdk.payment().create(payment_data)
    payment = result["response"]
    print(payment)
  8. Configure per-request options

    master

    All API methods accept an optional RequestOptions object. This allows you to override credentials (like access_token) or provide custom_headers (such as an x-idempotency-key) for a specific request.

    import mercadopago
    from mercadopago.config import RequestOptions
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
    
    # Example 1: Using custom headers for idempotency
    request_options = RequestOptions()
    request_options.custom_headers = {
        'x-idempotency-key': '<SOME_UNIQUE_VALUE>'
    }
    
    # Example 2: Overriding the access token for a specific request
    request_options_alt = RequestOptions(access_token='YOUR_ACCESS_TOKEN')
    
    # Applying options to a call
    payment_data = {"transaction_amount": 100, "token": "CARD_TOKEN", "payment_method_id": "visa", "installments": 1}
    result = sdk.payment().create(payment_data, request_options)
  9. Use factory methods to access API resources

    master

    The SDK class acts as a factory. Each factory method returns a fresh resource instance bound to the SDK's credentials and HTTP client. You can call these methods to interact with specific parts of the Mercado Pago API.

    Example of creating a payment using the payment() factory method:

    import mercadopago
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
    result = sdk.payment().create({
        "transaction_amount": 100,
        "payment_method_id": "pix",
        "payer": {"email": "buyer@example.com"},
    })
    import mercadopago
    
    sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
        result = sdk.payment().create({
            "transaction_amount": 100,
            "payment_method_id": "pix",
            "payer": {"email": "buyer@example.com"},
        })