From 8e1adf4ccc0ae83e76d889ed801a00e19c7305b7 Mon Sep 17 00:00:00 2001
From: Tom Teichler <tom.teichler@teckids.org>
Date: Sat, 12 Mar 2022 01:06:31 +0100
Subject: [PATCH] Handle payment via Tezor in wizard

---
 aleksis/apps/paweljong/forms.py               | 10 +++-
 .../migrations/0018_payment_with_tezor.py     | 49 +++++++++++++++++++
 .../paweljong/event_registration/full.html    |  2 +-
 aleksis/apps/paweljong/views.py               | 14 ++++--
 4 files changed, 68 insertions(+), 7 deletions(-)
 create mode 100644 aleksis/apps/paweljong/migrations/0018_payment_with_tezor.py

diff --git a/aleksis/apps/paweljong/forms.py b/aleksis/apps/paweljong/forms.py
index 1e8ad83..d364971 100644
--- a/aleksis/apps/paweljong/forms.py
+++ b/aleksis/apps/paweljong/forms.py
@@ -8,6 +8,7 @@ from django_select2.forms import ModelSelect2MultipleWidget, ModelSelect2Widget
 from material import Fieldset, Layout, Row
 from phonenumber_field.formfields import PhoneNumberField
 
+from aleksis.apps.tezor.models.invoice import Invoice
 from aleksis.core.mixins import ExtensibleForm
 from aleksis.core.models import Group, Person
 
@@ -326,7 +327,8 @@ class RegisterEventFinancial(ExtensibleForm):
     layout = Layout(
         Fieldset(
             _("Financial data"),
-            "voucher_code", "donation"
+            "payment_method",
+            Row("voucher_code", "donation"),
         ),
     )
 
@@ -336,6 +338,12 @@ class RegisterEventFinancial(ExtensibleForm):
         required=False,
     )
 
+    payment_method = forms.ChoiceField(
+        choices=Invoice.get_variant_choices(),
+        label=_("Paymeht method"),
+        help_text=_("Please choose a payment method. The actual payment will be made after the registration.")
+    )
+
     class Meta:
         model = EventRegistration
         fields = ["voucher_code", "donation"]
diff --git a/aleksis/apps/paweljong/migrations/0018_payment_with_tezor.py b/aleksis/apps/paweljong/migrations/0018_payment_with_tezor.py
new file mode 100644
index 0000000..bed3611
--- /dev/null
+++ b/aleksis/apps/paweljong/migrations/0018_payment_with_tezor.py
@@ -0,0 +1,49 @@
+# Generated by Django 3.2.12 on 2022-03-11 23:23
+
+from django.db import migrations, models
+
+from payments import PaymentStatus
+
+def create_invoices(apps, schema_editor):
+    db_alias = schema_editor.connection.alias
+
+    Person = apps.get_model("core", "Person")
+    EventRegistration = apps.get_model("paweljong", "EventRegistration")
+    SEPAMandate = apps.get_model("djp_sepa", "SEPAMandate")
+
+    from aleksis.apps.paweljong.models import EventRegistration as RealEventRegistration
+
+    EventRegistration.get_invoice = RealEventRegistration.get_invoice
+    EventRegistration._get_total_amount = RealEventRegistration._get_total_amount
+    EventRegistration.get_purchased_items = RealEventRegistration.get_purchased_items
+    Person.addressing_name = property(lambda p: p.first_name + " " + p.last_name)
+
+    for registration in EventRegistration.objects.using(db_alias).all():
+        invoice = registration.get_invoice()
+        invoice.status = PaymentStatus.PREAUTH
+        if registration.accept_sepa:
+            invoice.variant = "sdd"
+            SEPAMandate.objects.create(payment_id=invoice.pk, account_holder=f"{registration.person.addressing_name}", iban=registration.iban, bic="XXXXXXXXXXX", date=registration.date_registered)
+        else:
+            invoice.variant = "pledge"
+        invoice.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('paweljong', '0017_fix_voucher_max_length'),
+        ('tezor', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.RunPython(create_invoices),
+        migrations.RemoveField(
+            model_name='eventregistration',
+            name='accept_sepa',
+        ),
+        migrations.RemoveField(
+            model_name='eventregistration',
+            name='iban',
+        ),
+    ]
diff --git a/aleksis/apps/paweljong/templates/paweljong/event_registration/full.html b/aleksis/apps/paweljong/templates/paweljong/event_registration/full.html
index f1acef8..4c7931f 100644
--- a/aleksis/apps/paweljong/templates/paweljong/event_registration/full.html
+++ b/aleksis/apps/paweljong/templates/paweljong/event_registration/full.html
@@ -39,7 +39,7 @@
       {% endif %}
 
       {% if can_print_invoice %}
-        <a href="{% url 'get_invoice_by_pk' registration.get_invoice.pk %}" class="btn waves-effect waves-light">
+        <a href="{% url 'invoice_by_token' registration.get_invoice.token %}" class="btn waves-effect waves-light">
           <i class="material-icons left">attach_money</i>
           {% trans "Invoice" %}
         </a>
diff --git a/aleksis/apps/paweljong/views.py b/aleksis/apps/paweljong/views.py
index 4d9d533..6a7bd98 100644
--- a/aleksis/apps/paweljong/views.py
+++ b/aleksis/apps/paweljong/views.py
@@ -426,9 +426,9 @@ class RegisterEventWizardView(SessionWizardView):
         elif self.steps.current == "financial":
             context["info_title"] = _("Payment")
             context["info_text"] = _(
-                "By default, we will send you an invoice, which you or your parents "
-                "can pay by bank transfer. You can also choose to pay by direct debit – "
-                "please make sure to enter exactly what your parents tell you."
+                "Please decide with your parents how you want to pay. In this step, "
+                "you only select a payment method. The real payment will be done "
+                "in a separate step, after the registration is complete."
             )
         elif self.steps.current == "consent":
             context["info_title"] = _("Consent")
@@ -646,13 +646,17 @@ class RegisterEventWizardView(SessionWizardView):
             else:
                 messages.error(self.request, _("You entered an invalid voucher code!"))
 
+        invoice = registration.get_invoice()
+        invoice.variant = cleaned_data_financial["payment_method"]
+        invoice.save()
+
         context = {}
         context["registration"] = registration
 
         send_templated_mail(
             template_name="event_registered",
             from_email=get_site_preferences()["mail__address"],
-            recipient_list=get_site_prefenreces()["paweljong__event_notification_recipient"],
+            recipient_list=[get_site_preferences()["paweljong__event_notification_recipient"]],
             headers={
                 "reply_to": [
                     person.email,
@@ -679,7 +683,7 @@ class RegisterEventWizardView(SessionWizardView):
             user=person,
         )
 
-        return redirect("index")
+        return redirect("do_payment", registration.get_invoice().token)
 
 
 class EventFullView(DetailView):
-- 
GitLab