0% found this document useful (0 votes)
15 views2 pages

mercadopago

Uploaded by

sujeto 003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

mercadopago

Uploaded by

sujeto 003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import os

import requests
import json
import uuid
from django.urls import reverse_lazy
import logging

class MercadoPago():

logger = logging.getLogger(__name__)
base_url = "https://api.mercadopago.com"

def get_token(self):
return os.getenv('MERCADOPAGO_TOKEN')

def create_preference(self, purchase):

url = "{0}/checkout/preferences".format(self.base_url)

payload = json.dumps({
"items": [
{
"title": "Sesión de coaching con {0}".format(purchase.coach),
"description": "Sesión de coaching con {0}".format(purchase.coach),
"picture_url": "http://www.myapp.com/myimage.jpg",
"quantity": int(1),
"currency_id": os.getenv('MERCADOPAGO_CURRENCY_ID'),
"category_id": os.getenv('MERCADOPAGO_CATEGORY_ID'),
"unit_price": float(purchase.total_amount)
}
],
"payer": {
"phone": {},
"identification": {},
"address": {}
},
"external_reference": str(uuid.uuid4()),
"back_urls": {
"success": "",
"pending": "",
"failure": ""
},
})

headers = {
'Authorization': 'Bearer {0}'.format(self.get_token()),
'Content-Type': 'application/json'
}

return requests.request("POST", url, headers=headers, data=payload).json()

def create_payment(self, purchase, request):

url = "{0}/v1/payments".format(self.base_url)

data = json.loads(request.body)

payload = json.dumps({
"additional_info": {
"items": [
{
"title": "Sesión de coaching con {0}".format(purchase.coach),
"description": "Sesión de coaching con
{0}".format(purchase.coach),
"picture_url": "http://www.myapp.com/myimage.jpg",
"quantity": int(1),
"category_id": "MLC1540",
"unit_price": float(purchase.total_amount)
}
],
},
"transaction_amount": float(purchase.total_amount),
"token": data["token"]['token'],
"description": "Sesión de coaching con {0}".format(purchase.coach),
"installments": int(data["installments"] or 1),
"payment_method_id": data["payment_method_id"],
"payer": {
"email": data["payer"]["email"],
"identification": {
"type": data["payer"]["identification"]["type"],
"number": data["payer"]["identification"]["number"]
},
"first_name": "{0}".format(purchase.profile)
}
})

headers = {
'Authorization': 'Bearer {0}'.format(self.get_token()),
'accept': 'application/json',
'content-type': 'application/json',
'X-meli-session-id': "{0}".format(data['device_id'])
}

response = requests.request("POST", url, headers=headers,


data=payload).json()
return response

def get_payment(self, payment_id):

url = "{0}/v1/payments/{1}".format(self.base_url, payment_id)


payload = {}
headers = {
'Authorization': 'Bearer {0}'.format(self.get_token()),
'Content-Type': 'application/json'
}

return requests.request("GET", url, headers=headers, data=payload).json()

mercadopago = MercadoPago()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy