From f22d5915ea8718b34d386fdb9043b2729d9bda6c Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Wed, 18 Sep 2019 19:49:39 +0200 Subject: [PATCH] Handle refunds --- src/pretix/base/payment.py | 49 ++++++++++++++++++- .../pretixcontrol/giftcards/payment.html | 12 +++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/pretix/control/templates/pretixcontrol/giftcards/payment.html diff --git a/src/pretix/base/payment.py b/src/pretix/base/payment.py index bb51b7f2f..e3cfe4dac 100644 --- a/src/pretix/base/payment.py +++ b/src/pretix/base/payment.py @@ -8,6 +8,7 @@ import pytz from django import forms from django.conf import settings from django.core.exceptions import ImproperlyConfigured +from django.db import transaction from django.dispatch import receiver from django.forms import Form from django.http import HttpRequest @@ -899,7 +900,53 @@ class GiftCardPayment(BasePaymentProvider): def order_change_allowed(self, order: Order) -> bool: return False - # TODO: execute, refund, api, control render + def execute_payment(self, request: HttpRequest, payment: OrderPayment) -> str: + raise PaymentException("Invalid state, should never occur.") + + def payment_control_render(self, request, payment) -> str: + from .models import GiftCard + + gc = GiftCard.objects.get(pk=payment.info_data.get('gift_card')) + template = get_template('pretixcontrol/giftcards/payment.html') + + ctx = { + 'request': request, + 'event': self.event, + 'gc': gc, + } + return template.render(ctx) + + def api_payment_details(self, payment: OrderPayment): + from .models import GiftCard + gc = GiftCard.objects.get(pk=payment.info_data.get('gift_card')) + return { + 'gift_card': { + 'id': gc.pk, + 'secret': gc.secret, + 'organizer': gc.issuer.slug + } + } + + def payment_partial_refund_supported(self, payment: OrderPayment) -> bool: + return True + + def payment_refund_supported(self, payment: OrderPayment) -> bool: + return True + + @transaction.atomic() + def execute_refund(self, refund: OrderRefund): + from .models import GiftCard + gc = GiftCard.objects.get(pk=refund.payment.info_data.get('gift_card')) + trans = gc.transactions.create( + value=refund.amount, + order=refund.order, + refund=refund + ) + refund.info_data = { + 'gift_card': gc.pk, + 'transaction_id': trans.pk, + } + refund.done() @receiver(register_payment_providers, dispatch_uid="payment_free") diff --git a/src/pretix/control/templates/pretixcontrol/giftcards/payment.html b/src/pretix/control/templates/pretixcontrol/giftcards/payment.html new file mode 100644 index 000000000..39ba7aab9 --- /dev/null +++ b/src/pretix/control/templates/pretixcontrol/giftcards/payment.html @@ -0,0 +1,12 @@ +{% load i18n %} + +
+
{% trans "Gift card code" %}
+
+ + {{ gc.secret }} + +
+
{% trans "Issuer" %}
+
{{ gc.issuer }}
+