diff --git a/apps/participation/management/commands/check_hello_asso.py b/apps/participation/management/commands/check_hello_asso.py new file mode 100644 index 0000000..a9d1bdc --- /dev/null +++ b/apps/participation/management/commands/check_hello_asso.py @@ -0,0 +1,54 @@ +# Copyright (C) 2020 by Animath +# SPDX-License-Identifier: GPL-3.0-or-later + +import os + +import requests +from django.contrib.auth.models import User +from django.core.management import BaseCommand + + +class Command(BaseCommand): + def handle(self, *args, **options): # noqa: C901 + organization = "animath" + form_slug = "tfjmm-2018" + from_date = "2000-01-01" + url = f"https://api.helloasso.com/v5/organizations/{organization}/forms/Event/{form_slug}/payments" \ + f"?from={from_date}&pageIndex=1&pageSize=10000&retrieveOfflineDonations=false" + headers = { + "Accept": "application/json", + "Authorization": f"Bearer {os.getenv('HELLO_ASSO_TOKEN', '')}", + } + http_response = requests.get(url, headers=headers) + response = http_response.json() + + if http_response.status_code != 200: + message = response["message"] + self.stderr.write(f"Error while querying Hello Asso: {message}") + return + + for payment in response: + if payment["state"] != "Authorized": + continue + + payer = payment["payer"] + email = payer["email"] + qs = User.objects.filter(email=email) + if not qs.exists(): + self.stderr.write(f"Warning: a payment was found by the email address {email}, " + "but this user is unknown.") + continue + user = qs.get() + if not user.registration.participates: + self.stderr.write(f"Warning: a payment was found by the email address {email}, " + "but this user is not a participant.") + continue + payment_obj = user.registration.payment + payment_obj.valid = True + payment_obj.type = "helloasso" + payment_obj.additional_information = f"Identifiant de transation : {payment['id']}\n" \ + f"Date : {payment['date']}\n" \ + f"Reçu : {payment['paymentReceiptUrl']}\n" \ + f"Montant : {payment['amount'] / 100:.2f} €" + payment_obj.save() + self.stdout.write(f"{payment_obj} is validated") diff --git a/requirements.txt b/requirements.txt index 314d2d7..003fd46 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +asgiref~=3.3.1 Django~=3.0 django-address~=0.2 django-bootstrap-datepicker-plus~=3.0 @@ -18,5 +19,6 @@ phonenumbers~=8.9.10 psycopg2-binary~=2.8 ipython~=7.19.0 python-magic==0.4.18 +requests~=2.25.0 sympasoap~=1.0 -whoosh~=2.7 +whoosh~=2.7 \ No newline at end of file diff --git a/tfjm.cron b/tfjm.cron index 9ff1c68..a9f34a5 100644 --- a/tfjm.cron +++ b/tfjm.cron @@ -12,3 +12,6 @@ # Update matrix channels */6 * * * * cd /code && python manage.py fix_matrix_channels &> /dev/null + +# Check payments from Hello Asso +*/6 * * * * cd /code && python manage.py check_hello_asso &> /dev/null