mirror of
https://gitlab.com/animath/si/plateforme.git
synced 2025-06-21 07:58:26 +02:00
Create Hello Asso checkout intents
Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
86
tfjm/helloasso.py
Normal file
86
tfjm/helloasso.py
Normal file
@ -0,0 +1,86 @@
|
||||
# Copyright (C) 2024 by Animath
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from django.conf import settings
|
||||
import requests
|
||||
|
||||
|
||||
_access_token = None
|
||||
_refresh_token = None
|
||||
_expires_at = None
|
||||
|
||||
|
||||
def get_hello_asso_access_token():
|
||||
global _access_token, _refresh_token, _expires_at
|
||||
|
||||
now = datetime.now()
|
||||
if _access_token is None:
|
||||
response = requests.post(
|
||||
"https://api.helloasso.com/oauth2/token",
|
||||
data={
|
||||
"grant_type": "client_credentials",
|
||||
"client_id": settings.HELLOASSO_CLIENT_ID,
|
||||
"client_secret": settings.HELLOASSO_CLIENT_SECRET,
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
_access_token = data["access_token"]
|
||||
_refresh_token = data["refresh_token"]
|
||||
_expires_at = now + timedelta(seconds=data["expires_in"])
|
||||
elif now >= _expires_at:
|
||||
response = requests.post(
|
||||
"https://api.helloasso.com/oauth2/token",
|
||||
data={
|
||||
"grant_type": "refresh_token",
|
||||
"refresh_token": _refresh_token,
|
||||
},
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
_access_token = data["access_token"]
|
||||
_refresh_token = data["refresh_token"]
|
||||
_expires_at = now + timedelta(seconds=data["expires_in"])
|
||||
|
||||
return _access_token
|
||||
|
||||
|
||||
def get_checkout_intent(checkout_id):
|
||||
token = get_hello_asso_access_token()
|
||||
response = requests.get(
|
||||
f"https://api.helloasso.com/v5/organizations/animath/checkout-intents/{checkout_id}",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
if response.status_code == 404:
|
||||
return None
|
||||
response.raise_for_status()
|
||||
|
||||
checkout_intent = response.json()
|
||||
if requests.head(checkout_intent["redirectUrl"]).status_code == 404:
|
||||
return None
|
||||
|
||||
return checkout_intent
|
||||
|
||||
|
||||
def create_checkout_intent(amount, name, back_url, error_url, return_url, contains_donation=False, metadata=None):
|
||||
token = get_hello_asso_access_token()
|
||||
metadata = metadata or {}
|
||||
response = requests.post(
|
||||
"https://api.helloasso.com/v5/organizations/animath/checkout-intents/",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
json={
|
||||
"totalAmount": amount,
|
||||
"initialAmount": amount,
|
||||
"itemName": name,
|
||||
"backUrl": back_url,
|
||||
"errorUrl": error_url,
|
||||
"returnUrl": return_url,
|
||||
"containsDonation": contains_donation,
|
||||
"metadata": metadata,
|
||||
},
|
||||
)
|
||||
print(response.text)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
@ -238,7 +238,9 @@ else:
|
||||
PHONENUMBER_DB_FORMAT = 'NATIONAL'
|
||||
PHONENUMBER_DEFAULT_REGION = 'FR'
|
||||
|
||||
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
||||
# Hello Asso API creds
|
||||
HELLOASSO_CLIENT_ID = os.getenv('HELLOASSO_CLIENT_ID', 'CHANGE_ME_IN_ENV_SETTINGS')
|
||||
HELLOASSO_CLIENT_SECRET = os.getenv('HELLOASSO_CLIENT_SECRET', 'CHANGE_ME_IN_ENV_SETTINGS')
|
||||
|
||||
# Custom parameters
|
||||
PROBLEMS = [
|
||||
|
Reference in New Issue
Block a user