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
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 38 additions and 18 deletions

View File

@ -12,49 +12,63 @@ _refresh_token = None
_expires_at = 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(): def get_hello_asso_access_token():
global _access_token, _refresh_token, _expires_at global _access_token, _refresh_token, _expires_at
base_url = _get_hello_asso_api_base_url()
now = datetime.now() now = datetime.now()
if _access_token is None: if _access_token is None:
response = requests.post( response = requests.post(
"https://api.helloasso.com/oauth2/token", f"{base_url}/oauth2/token",
data={ data={
"grant_type": "client_credentials", "grant_type": "client_credentials",
"client_id": settings.HELLOASSO_CLIENT_ID, "client_id": settings.HELLOASSO_CLIENT_ID,
"client_secret": settings.HELLOASSO_CLIENT_SECRET, "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: elif now >= _expires_at:
response = requests.post( response = requests.post(
"https://api.helloasso.com/oauth2/token", f"{base_url}/oauth2/token",
data={ data={
"grant_type": "refresh_token", "grant_type": "refresh_token",
"refresh_token": _refresh_token, "refresh_token": _refresh_token,
}, },
) )
response.raise_for_status() else:
data = response.json() return _access_token
_access_token = data["access_token"]
_refresh_token = data["refresh_token"] if response.status_code == 400:
_expires_at = now + timedelta(seconds=data["expires_in"]) 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 return _access_token
def get_checkout_intent(checkout_id): def get_checkout_intent(checkout_id):
base_url = _get_hello_asso_api_base_url()
token = get_hello_asso_access_token() token = get_hello_asso_access_token()
response = requests.get( 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}"}, headers={"Authorization": f"Bearer {token}"},
) )
if response.status_code == 404: if response.status_code == 404:
return None return None
elif response.status_code == 400:
raise ValueError(str(response.json()['errors']))
response.raise_for_status() response.raise_for_status()
checkout_intent = response.json() 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): 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() token = get_hello_asso_access_token()
metadata = metadata or {} metadata = metadata or {}
response = requests.post( 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}"}, headers={"Authorization": f"Bearer {token}"},
json={ json={
"totalAmount": amount, "totalAmount": amount,
@ -81,6 +97,7 @@ def create_checkout_intent(amount, name, back_url, error_url, return_url, contai
"metadata": metadata, "metadata": metadata,
}, },
) )
print(response.text) if response.status_code == 400:
raise ValueError(str(response.json()['errors']))
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()

View File

@ -30,8 +30,11 @@ ADMINS = [("Emmy D'Anello", "emmy.danello@animath.fr")]
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'CHANGE_ME_IN_ENV_SETTINGS') SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'CHANGE_ME_IN_ENV_SETTINGS')
# dev in development mode, prod in production mode
TFJM_STAGE = os.getenv('TFJM_STAGE', 'dev')
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = TFJM_STAGE != "prod"
SITE_ID = 1 SITE_ID = 1
@ -76,7 +79,7 @@ if "test" not in sys.argv: # pragma: no cover
'mailer', 'mailer',
] ]
if os.getenv("TFJM_STAGE", "dev") == "prod": # pragma: no cover if TFJM_STAGE == "prod": # pragma: no cover
INSTALLED_APPS += [ INSTALLED_APPS += [
'channels_redis', 'channels_redis',
] ]
@ -277,7 +280,7 @@ CHANNEL_LAYERS = {
} }
} }
if os.getenv("TFJM_STAGE", "dev") == "prod": # pragma: no cover if TFJM_STAGE == "prod": # pragma: no cover
from .settings_prod import * # noqa: F401,F403 from .settings_prod import * # noqa: F401,F403
else: else:
from .settings_dev import * # noqa: F401,F403 from .settings_dev import * # noqa: F401,F403