2021-01-18 22:00:39 +00:00
|
|
|
# Copyright (C) 2020 by Animath
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.management import BaseCommand
|
2021-01-18 23:13:22 +00:00
|
|
|
import requests
|
2021-01-18 22:00:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
def handle(self, *args, **options): # noqa: C901
|
2021-03-15 08:57:05 +00:00
|
|
|
# Get access token
|
|
|
|
response = requests.post('https://api.helloasso.com/oauth2/token', headers={
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
}, data={
|
|
|
|
'client_id': os.getenv('HELLOASSO_CLIENT_ID', ''),
|
|
|
|
'client_secret': os.getenv('HELLOASSO_CLIENT_SECRET', ''),
|
|
|
|
'grant_type': 'client_credentials',
|
|
|
|
}).json()
|
|
|
|
|
|
|
|
token = response['access_token']
|
|
|
|
|
2021-01-18 22:00:39 +00:00
|
|
|
organization = "animath"
|
2022-03-21 18:58:36 +00:00
|
|
|
form_slug = "tfjm-2022-tournois-regionaux"
|
2021-01-18 22:00:39 +00:00
|
|
|
from_date = "2000-01-01"
|
|
|
|
url = f"https://api.helloasso.com/v5/organizations/{organization}/forms/Event/{form_slug}/payments" \
|
2022-03-21 18:58:36 +00:00
|
|
|
f"?from={from_date}&pageIndex=1&pageSize=100&retrieveOfflineDonations=false"
|
2021-01-18 22:00:39 +00:00
|
|
|
headers = {
|
|
|
|
"Accept": "application/json",
|
2021-03-15 08:57:05 +00:00
|
|
|
"Authorization": f"Bearer {token}",
|
2021-01-18 22:00:39 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2021-03-15 09:07:59 +00:00
|
|
|
for payment in response["data"]:
|
2021-01-18 22:00:39 +00:00
|
|
|
if payment["state"] != "Authorized":
|
|
|
|
continue
|
|
|
|
|
|
|
|
payer = payment["payer"]
|
|
|
|
email = payer["email"]
|
2022-03-28 19:32:16 +00:00
|
|
|
last_name = payer["lastName"]
|
|
|
|
first_name = payer["firstName"]
|
2021-01-18 22:00:39 +00:00
|
|
|
qs = User.objects.filter(email=email)
|
|
|
|
if not qs.exists():
|
2022-03-28 19:32:16 +00:00
|
|
|
qs = User.objects.filter(last_name__icontains=last_name)
|
|
|
|
if qs.count() >= 2:
|
|
|
|
qs = qs.filter(first_name__icontains=first_name)
|
|
|
|
if not qs.exists():
|
|
|
|
self.stderr.write(f"Warning: a payment was found by {first_name} {last_name} ({email}), "
|
2021-01-18 22:00:39 +00:00
|
|
|
"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")
|