1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-08-07 23:20:19 +02:00

Use Hello Asso sandbox instance in dev mode

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-02-20 18:51:38 +01:00
parent b3555a7807
commit 8c7e9648dd
2 changed files with 38 additions and 18 deletions

View File

@@ -12,49 +12,63 @@ _refresh_token = None
_expires_at = None
def _get_hello_asso_api_base_url():
if not settings.DEBUG:
return "https://api.helloasso.com"
else:
return "https://api.helloasso-sandbox.com"
def get_hello_asso_access_token():
global _access_token, _refresh_token, _expires_at
base_url = _get_hello_asso_api_base_url()
now = datetime.now()
if _access_token is None:
response = requests.post(
"https://api.helloasso.com/oauth2/token",
f"{base_url}/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",
f"{base_url}/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"])
else:
return _access_token
if response.status_code == 400:
raise ValueError(str(response.json()['errors']))
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):
base_url = _get_hello_asso_api_base_url()
token = get_hello_asso_access_token()
response = requests.get(
f"https://api.helloasso.com/v5/organizations/animath/checkout-intents/{checkout_id}",
f"{base_url}/v5/organizations/animath/checkout-intents/{checkout_id}",
headers={"Authorization": f"Bearer {token}"},
)
if response.status_code == 404:
return None
elif response.status_code == 400:
raise ValueError(str(response.json()['errors']))
response.raise_for_status()
checkout_intent = response.json()
@@ -65,10 +79,12 @@ def get_checkout_intent(checkout_id):
def create_checkout_intent(amount, name, back_url, error_url, return_url, contains_donation=False, metadata=None):
base_url = _get_hello_asso_api_base_url()
token = get_hello_asso_access_token()
metadata = metadata or {}
response = requests.post(
"https://api.helloasso.com/v5/organizations/animath/checkout-intents/",
f"{base_url}/v5/organizations/animath/checkout-intents/",
headers={"Authorization": f"Bearer {token}"},
json={
"totalAmount": amount,
@@ -81,6 +97,7 @@ def create_checkout_intent(amount, name, back_url, error_url, return_url, contai
"metadata": metadata,
},
)
print(response.text)
if response.status_code == 400:
raise ValueError(str(response.json()['errors']))
response.raise_for_status()
return response.json()