46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from django.conf import settings
|
|
import requests
|
|
|
|
|
|
SUMUP_CLIENT_ID = settings.SUMUP_CLIENT_ID
|
|
SUMUP_CLIENT_SECRET = settings.SUMUP_CLIENT_SECRET
|
|
SUMUP_EMAIL = settings.SUMUP_MERCHANT_EMAIL
|
|
|
|
def obtener_token():
|
|
"""Obtiene un access token de SumUp"""
|
|
url = "https://api.sumup.com/token"
|
|
data = {
|
|
"grant_type": "client_credentials",
|
|
"client_id": SUMUP_CLIENT_ID,
|
|
"client_secret": SUMUP_CLIENT_SECRET
|
|
}
|
|
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
|
|
|
response = requests.post(url, headers=headers, data=data)
|
|
if response.status_code == 200:
|
|
return response.json().get("access_token")
|
|
else:
|
|
raise Exception(f"Error al obtener token: {response.json()}")
|
|
|
|
def crear_checkout(monto, moneda="EUR"):
|
|
"""Crea un pago en SumUp y devuelve el checkout_id"""
|
|
access_token = obtener_token()
|
|
url = "https://api.sumup.com/v0.1/checkouts"
|
|
headers = {
|
|
"Authorization": f"Bearer {access_token}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
data = {
|
|
"checkout_reference": "pedido-12345",
|
|
"amount": monto,
|
|
"currency": moneda,
|
|
"pay_to_email": SUMUP_EMAIL,
|
|
"description": "Pago de prueba"
|
|
}
|
|
|
|
response = requests.post(url, headers=headers, json=data)
|
|
if response.status_code == 201:
|
|
return response.json().get("id") # checkout_id
|
|
else:
|
|
raise Exception(f"Error al crear checkout: {response.json()}")
|