From e358bacfa36687ad75b7b3f34b063b7412b30004 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Wed, 15 Feb 2023 13:04:50 +0100 Subject: [PATCH] Expose some payment details in exports --- doc/development/api/payment.rst | 2 ++ src/pretix/base/exporters/orderlist.py | 20 +++++++++++++++++--- src/pretix/base/payment.py | 26 +++++++++++++++++++++++--- src/pretix/control/views/orders.py | 3 ++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/doc/development/api/payment.rst b/doc/development/api/payment.rst index 0dfe43540..a09844213 100644 --- a/doc/development/api/payment.rst +++ b/doc/development/api/payment.rst @@ -122,6 +122,8 @@ The provider class .. automethod:: refund_control_render + .. automethod:: refund_control_render_short + .. automethod:: new_refund_control_form_render .. automethod:: new_refund_control_form_process diff --git a/src/pretix/base/exporters/orderlist.py b/src/pretix/base/exporters/orderlist.py index 07fb310c9..1a027d539 100644 --- a/src/pretix/base/exporters/orderlist.py +++ b/src/pretix/base/exporters/orderlist.py @@ -796,17 +796,17 @@ class PaymentListExporter(ListExporter): payments = OrderPayment.objects.filter( order__event__in=self.events, state__in=form_data.get('payment_states', []) - ).order_by('created') + ).select_related('order').prefetch_related('order__event').order_by('created') refunds = OrderRefund.objects.filter( order__event__in=self.events, state__in=form_data.get('refund_states', []) - ).order_by('created') + ).select_related('order').prefetch_related('order__event').order_by('created') objs = sorted(list(payments) + list(refunds), key=lambda o: o.created) headers = [ _('Event slug'), _('Order'), _('Payment ID'), _('Creation date'), _('Completion date'), _('Status'), - _('Status code'), _('Amount'), _('Payment method'), _('Comment'), + _('Status code'), _('Amount'), _('Payment method'), _('Comment'), _('Matching ID'), _('Payment details'), ] yield headers @@ -819,6 +819,18 @@ class PaymentListExporter(ListExporter): d2 = obj.execution_date.astimezone(tz).date().strftime('%Y-%m-%d') else: d2 = '' + matching_id = '' + payment_details = '' + try: + if isinstance(obj, OrderPayment): + matching_id = obj.payment_provider.matching_id(obj) or '' + payment_details = obj.payment_provider.payment_control_render_short(obj) + elif isinstance(obj, OrderRefund): + matching_id = obj.payment_provider.refund_matching_id(obj) or '' + payment_details = obj.payment_provider.refund_control_render_short(obj) + except Exception: + pass + row = [ obj.order.event.slug, obj.order.code, @@ -830,6 +842,8 @@ class PaymentListExporter(ListExporter): obj.amount * (-1 if isinstance(obj, OrderRefund) else 1), provider_names.get(obj.provider, obj.provider), obj.comment if isinstance(obj, OrderRefund) else "", + matching_id, + payment_details, ] yield row diff --git a/src/pretix/base/payment.py b/src/pretix/base/payment.py index bb589d9f3..91ca237b4 100644 --- a/src/pretix/base/payment.py +++ b/src/pretix/base/payment.py @@ -821,11 +821,11 @@ class BasePaymentProvider: """ Will be called if the *event administrator* performs an action on the payment. Should return a very short version of the payment method. Usually, this should return e.g. - a transaction ID or account identifier, but no information on status, dates, etc. + an account identifier of the payee, but no information on status, dates, etc. The default implementation falls back to ``payment_presale_render``. - :param order: The order object + :param payment: The payment object """ return self.payment_presale_render(payment) @@ -842,6 +842,18 @@ class BasePaymentProvider: """ return '' + def refund_control_render_short(self, refund: OrderRefund) -> str: + """ + Will be called if the *event administrator* performs an action on the refund. Should + return a very short description of the refund method. Usually, this should return e.g. + an account identifier of the refund recipient, but no information on status, dates, etc. + + The default implementation returns an empty string. + + :param refund: The refund object + """ + return '' + def payment_presale_render(self, payment: OrderPayment) -> str: """ Will be called if the *ticket customer* views the details of a payment. This is @@ -1290,6 +1302,14 @@ class GiftCardPayment(BasePaymentProvider): } return template.render(ctx) + def payment_control_render_short(self, payment: OrderPayment) -> str: + d = payment.info_data + return d.get('gift_card_secret', self.public_name) + + def refund_control_render_short(self, refund: OrderRefund) -> str: + d = refund.info_data + return d.get('gift_card_secret', d.get('gift_card_code', self.public_name)) + def api_payment_details(self, payment: OrderPayment): from .models import GiftCard try: @@ -1464,7 +1484,7 @@ class GiftCardPayment(BasePaymentProvider): ) refund.info_data = { 'gift_card': gc.pk, - 'gift_card_code': gc.secret, + 'gift_card_secret': gc.secret, 'transaction_id': trans.pk, } refund.done() diff --git a/src/pretix/control/views/orders.py b/src/pretix/control/views/orders.py index c213928e9..0a5747237 100644 --- a/src/pretix/control/views/orders.py +++ b/src/pretix/control/views/orders.py @@ -62,6 +62,7 @@ from django.urls import reverse from django.utils import formats from django.utils.formats import date_format, get_format from django.utils.functional import cached_property +from django.utils.html import conditional_escape from django.utils.http import url_has_allowed_host_and_scheme from django.utils.timezone import make_aware, now from django.utils.translation import gettext, gettext_lazy as _, ngettext @@ -1126,7 +1127,7 @@ class OrderRefundView(OrderView): for p in payments: if p.payment_provider: - p.html_info = (p.payment_provider.payment_control_render_short(p) or "").strip() + p.html_info = conditional_escape(p.payment_provider.payment_control_render_short(p) or "").strip() return render(self.request, 'pretixcontrol/order/refund_choose.html', { 'payments': payments,