1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-28 06:52:42 +02:00

Create Hello Asso checkout intents

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-02-19 00:17:14 +01:00
parent 98d04b9093
commit b3555a7807
6 changed files with 157 additions and 6 deletions

View File

@ -1,13 +1,13 @@
# Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later
from datetime import date
from datetime import date, datetime
from django.contrib.sites.models import Site
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.template import loader
from django.urls import reverse_lazy
from django.urls import reverse_lazy, reverse
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.encoding import force_bytes
@ -16,6 +16,8 @@ from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from polymorphic.models import PolymorphicModel
from tfjm import helloasso
from tfjm.tokens import email_validation_token
@ -564,6 +566,46 @@ class Payment(models.Model):
default=False,
)
def get_checkout_intent(self):
if self.checkout_intent_id is None:
return None
return helloasso.get_checkout_intent(self.checkout_intent_id)
def create_checkout_intent(self):
checkout_intent = self.get_checkout_intent()
if checkout_intent is not None:
return checkout_intent
from participation.models import Tournament
tournament = self.registrations.first().team.participation.tournament \
if not self.final else Tournament.final_tournament()
year = datetime.now().year
base_site = "https://" + Site.objects.first().domain
checkout_intent = helloasso.create_checkout_intent(
amount=100 * self.amount,
name=f"Participation au TFJM² {year} - {tournament.name}",
back_url=base_site + reverse('registration:update_payment', args=(self.id,)),
error_url=base_site + reverse('registration:update_payment', args=(self.id,)),
return_url=base_site + reverse('registration:update_payment', args=(self.id,)),
contains_donation=False,
metadata=dict(
users=[
dict(user_id=registration.user.id,
first_name=registration.user.first_name,
last_name=registration.user.last_name,
email=registration.user.email,)
for registration in self.registrations.all()
],
payment_id=self.id,
final=self.final,
tournament_id=tournament.id,
)
)
self.checkout_intent_id = checkout_intent["id"]
self.save()
return checkout_intent
def get_absolute_url(self):
return reverse_lazy("registration:update_payment", args=(self.pk,))