Send preferred data to MercadoPago with Django

2

I'm doing a page in Python 3.4 and Django 1.7, using MercadoPago's basic checkout, but I want to send more info from the buyer than what the basic proposes, but I get the error that is not serializable. I need to send the following information:

    preference = {
    "items": [
        {
            "title": "Título del art.",
            "quantity": 1,
            "currency_id": "ARS",  # Available currencies at: https://api.mercadopago.com/currencies
            "unit_price": 1800
        }
    ],
    "payers": [
        {
            "name": nombre,
            "surname": apellido,
            "email": email,
            "phone.number": telefono,
            "identification":
                {
                    "type": tipo_documento,
                    "number": num_doc,
                }, # identification
            "address":
                {
                    "zip_code": cp,
                    "street_name": calle,
                    "street_number": num_calle,
                }  # address
        },
        ],
    "back_urls": [
        {
            "success": redirect('venta_exitosa_r'),
            "failure": redirect('venta_fallida_r'),
            "pending": redirect('venta_pendiente_r')
        },
    ]

}

mp = mercadopago.MP(settings.CLIENT_ID, settings.CLIENT_SECRET)
preferenceresult = mp.create_preference(preference)
url = preferenceresult["response"]["init_point"]
return url

Thank you very much!

    
asked by Fabio 20.01.2017 в 20:25
source

1 answer

2

As you said, and as the error tells you:

  

"django.http.response.HttpResponseRedirect object at 0x0487BF10 is not serializable JSON

It means that the module that is serializing, does not serialize objects, you should know that the function redirect() returns an object, the objects usually are not serialized, so it is better to pass solid data, that is if the content of the JSON (or dictionary) that you have, was destined to javascrpit, then javascript would not know what to do with a python object, which also comes to you as a string.

The Quick Solution that I see to your problem, is instead of using redirect, since this function serves to do a redirection at the moment it is called, and I suppose you want to pass it is a url. use reverse , like this:

from django.core.urlresolvers import reverse

...
"back_urls": [
    {
        "success": reverse('venta_exitosa_r'),
        "failure": reverse('venta_fallida_r'),
        "pending": reverse('venta_pendiente_r')
    },
...

What you will do is replace your redirects with the url you want to string ... and so you can serialize.

Any question, comments.

    
answered by 20.01.2017 / 21:30
source