Example #1
0
 def test_create_payment(self):
     with self.subTest("Creates new payment with right payment type"):
         p = services.create_payment(
             MockPayable(self.member), self.member, Payment.CASH
         )
         self.assertEqual(p.processing_date, timezone.now())
         self.assertEqual(p.amount, 5)
         self.assertEqual(p.topic, "mock topic")
         self.assertEqual(p.notes, "mock notes")
         self.assertEqual(p.paid_by, self.member)
         self.assertEqual(p.processed_by, self.member)
         self.assertEqual(p.type, Payment.CASH)
     with self.subTest("Updates payment if one already exists"):
         existing_payment = Payment(amount=2)
         p = services.create_payment(
             MockPayable(payer=self.member, payment=existing_payment),
             self.member,
             Payment.CASH,
         )
         self.assertEqual(p, existing_payment)
         self.assertEqual(p.amount, 5)
     with self.subTest("Does not allow Thalia Pay when not enabled"):
         with self.assertRaises(PaymentError):
             services.create_payment(
                 MockPayable(payer=self.member), self.member, Payment.TPAY
             )
Example #2
0
    def create_payment(self):
        possible_events = list(
            filter(
                lambda e: e.registrations.count() > 0,
                Event.objects.filter(price__gt=0).order_by("?"),
            ))
        while len(possible_events) == 0:
            print(
                "No event where can be payed could be found, creating a new event"
            )
            self.create_event()
            possible_events = list(
                filter(
                    lambda e: e.registrations.count() > 0,
                    Event.objects.filter(price__gt=0).order_by("?"),
                ))

        event = possible_events[0]
        if len(event.registrations) == 0:
            print(
                "No registrations found. Create some more registrations first")
            return

        registration = event.registrations.order_by("?")[0]

        processed_by = Member.objects.order_by("?")[0]
        create_payment(
            registration,
            processed_by,
            random.choice([Payment.CASH, Payment.CARD, Payment.WIRE]),
        )
Example #3
0
    def test_tpay_balance(self):
        self.assertEqual(self.member.tpay_balance, 0)
        BankAccount.objects.create(
            owner=self.member,
            initials="J",
            last_name="Test2",
            iban="NL91ABNA0417164300",
            mandate_no="11-2",
            valid_from=timezone.now().date() - timezone.timedelta(days=5),
            last_used=timezone.now().date() - timezone.timedelta(days=5),
            signature="base64,png",
        )
        p1 = services.create_payment(MockPayable(self.member), self.member,
                                     Payment.TPAY)
        self.assertEqual(self.member.tpay_balance, Decimal(-5))

        p2 = services.create_payment(MockPayable(self.member), self.member,
                                     Payment.TPAY)
        self.assertEqual(self.member.tpay_balance, Decimal(-10))

        batch = Batch.objects.create()
        p1.batch = batch
        p1.save()

        p2.batch = batch
        p2.save()

        self.assertEqual(self.member.tpay_balance, Decimal(-10))

        batch.processed = True
        batch.save()

        self.assertEqual(self.member.tpay_balance, 0)
Example #4
0
 def _update_payment(order, payment_type=None, processed_by=None):
     if order.payment and payment_type == Payment.NONE:
         delete_payment(order)
     elif order.payment:
         if payment_type is None:
             payment_type = order.payment.type
         order.payment = create_payment(order, processed_by, payment_type)
         order.payment.save()
     else:
         order.payment = create_payment(order, processed_by, payment_type)
         order.save()
Example #5
0
 def form_valid(self, form):
     try:
         services.create_payment(
             self.payable,
             PaymentUser.objects.get(pk=self.request.member.pk),
             Payment.TPAY,
         )
         self.payable.save()
     except PaymentError as e:
         messages.error(self.request, str(e))
     return super().form_valid(form)
Example #6
0
def pay_order(request):
    """ View that marks the order as paid using Thalia Pay """
    if "order" in request.POST:
        try:
            order = get_object_or_404(Order, pk=int(request.POST["order"]))
            if order.member == request.member:
                create_payment(order, Payment.TPAY, order.member)
                messages.success(
                    request, _("Your order has been paid with "
                               "Thalia Pay."))
        except Http404:
            messages.error(request, _("Your order could not be found."))
    return redirect("pizzas:index")
Example #7
0
    def post(self, request, *args, app_label, model_name, payable, **kwargs):
        if "type" not in request.POST:
            raise SuspiciousOperation("Missing POST parameters")

        if "next" in request.POST and not url_has_allowed_host_and_scheme(
            request.POST.get("next"), allowed_hosts={request.get_host()}
        ):
            raise DisallowedRedirect

        payable_model = apps.get_model(app_label=app_label, model_name=model_name)
        payable_obj = payable_model.objects.get(pk=payable)

        result = services.create_payment(
            payable_obj, self.request.member, request.POST["type"],
        )
        payable_obj.save()

        if result:
            messages.success(
                request, _("Successfully paid %s.") % model_ngettext(payable_obj, 1),
            )
        else:
            messages.error(
                request, _("Could not pay %s.") % model_ngettext(payable_obj, 1),
            )
            return redirect(f"admin:{app_label}_{model_name}_change", payable_obj.pk)

        if "next" in request.POST:
            return redirect(request.POST["next"])

        return redirect("admin:payments_payment_change", result.pk)
Example #8
0
def update_registration_by_organiser(registration, member, data):
    if not is_organiser(member, registration.event):
        raise RegistrationError(_("You are not allowed to update this registration."))

    if "payment" in data:
        if data["payment"]["type"] == Payment.NONE and registration.payment is not None:
            delete_payment(registration)
        else:
            registration.payment = create_payment(
                payable=registration,
                processed_by=member,
                pay_type=data["payment"]["type"],
            )

    if "present" in data:
        registration.present = data["present"]

    registration.save()
Example #9
0
def pay_with_tpay(member, event):
    """
    Add a Thalia Pay payment to an event registration

    :param member: the user
    :param event: the event
    """
    try:
        registration = EventRegistration.objects.get(event=event, member=member)
    except EventRegistration.DoesNotExist:
        raise RegistrationError(_("You are not registered for this event."))

    if registration.payment is None:
        registration.payment = create_payment(
            payable=registration, processed_by=member, pay_type=Payment.TPAY
        )
        registration.save()
    else:
        raise RegistrationError(_("You have already paid for this event."))
Example #10
0
    def test_process_batch(self):
        with patch("payments.services.send_tpay_batch_processing_emails"
                   ) as mock_mails:
            ba = BankAccount.objects.create(
                owner=self.member,
                initials="J",
                last_name="Test1",
                iban="NL91ABNA0417164300",
                mandate_no="11-1",
                valid_from=timezone.now().date() -
                timezone.timedelta(days=2000),
                signature="base64,png",
            )
            p = services.create_payment(MockPayable(self.member), self.member,
                                        Payment.TPAY)
            b = Batch.objects.create()
            p.batch = b
            p.save()

            services.process_batch(b)

            mock_mails.assert_called_once()
            ba.refresh_from_db()
            self.assertEqual(b.withdrawal_date.date(), ba.last_used)
Example #11
0
 def _update_payment(order, payment_type=None, processed_by=None):
     if order.payment and payment_type == PaymentTypeField.NO_PAYMENT:
         delete_payment(order)
     else:
         order.payment = create_payment(order, processed_by, payment_type)
         order.save()