From 2a9c105e51af8b4e9b3e0c0746b90df6496a0591 Mon Sep 17 00:00:00 2001 From: Martin Gross Date: Wed, 30 Oct 2019 10:48:15 +0100 Subject: [PATCH] PayPal: Show payment ID on paid invoices (#1471) * Always call render_invoice_text - even if order is already paid * Print PayPal payment ID on invoice if available and invoice is paid * Also display Sale ID on invoice * try/except for paymentId/SaleId --- src/pretix/base/services/invoices.py | 17 +++++------------ src/pretix/plugins/paypal/payment.py | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/pretix/base/services/invoices.py b/src/pretix/base/services/invoices.py index 04e7a5d73e..df47b5ff59 100644 --- a/src/pretix/base/services/invoices.py +++ b/src/pretix/base/services/invoices.py @@ -20,9 +20,7 @@ from django_scopes import scope, scopes_disabled from i18nfield.strings import LazyI18nString from pretix.base.i18n import language -from pretix.base.models import ( - Invoice, InvoiceAddress, InvoiceLine, Order, OrderPayment, -) +from pretix.base.models import Invoice, InvoiceAddress, InvoiceLine, Order from pretix.base.models.tax import EU_CURRENCIES from pretix.base.services.tasks import TransactionAwareTask from pretix.base.settings import GlobalSettingsObject @@ -37,9 +35,6 @@ logger = logging.getLogger(__name__) @transaction.atomic def build_invoice(invoice: Invoice) -> Invoice: lp = invoice.order.payments.last() - open_payment = None - if lp and lp.state not in (OrderPayment.PAYMENT_STATE_CONFIRMED, OrderPayment.PAYMENT_STATE_REFUNDED): - open_payment = lp with language(invoice.locale): invoice.invoice_from = invoice.event.settings.get('invoice_address_from') @@ -53,13 +48,11 @@ def build_invoice(invoice: Invoice) -> Invoice: introductory = invoice.event.settings.get('invoice_introductory_text', as_type=LazyI18nString) additional = invoice.event.settings.get('invoice_additional_text', as_type=LazyI18nString) footer = invoice.event.settings.get('invoice_footer_text', as_type=LazyI18nString) - if open_payment and open_payment.payment_provider: - if 'payment' in inspect.signature(open_payment.payment_provider.render_invoice_text).parameters: - payment = open_payment.payment_provider.render_invoice_text(invoice.order, open_payment) + if lp and lp.payment_provider: + if 'payment' in inspect.signature(lp.payment_provider.render_invoice_text).parameters: + payment = lp.payment_provider.render_invoice_text(invoice.order, lp) else: - payment = open_payment.payment_provider.render_invoice_text(invoice.order) - elif invoice.order.status == Order.STATUS_PAID: - payment = pgettext('invoice', 'The payment for this invoice has already been received.') + payment = lp.payment_provider.render_invoice_text(invoice.order) else: payment = "" diff --git a/src/pretix/plugins/paypal/payment.py b/src/pretix/plugins/paypal/payment.py index dd0f6f5dfa..c734ee944e 100644 --- a/src/pretix/plugins/paypal/payment.py +++ b/src/pretix/plugins/paypal/payment.py @@ -12,11 +12,12 @@ from django.template.loader import get_template from django.urls import reverse from django.utils.http import urlquote from django.utils.translation import ugettext as __, ugettext_lazy as _ +from i18nfield.strings import LazyI18nString from paypalrestsdk.exceptions import BadRequest from paypalrestsdk.openid_connect import Tokeninfo from pretix.base.decimal import round_decimal -from pretix.base.models import Event, OrderPayment, OrderRefund, Quota +from pretix.base.models import Event, Order, OrderPayment, OrderRefund, Quota from pretix.base.payment import BasePaymentProvider, PaymentException from pretix.base.services.mail import SendMailException from pretix.base.settings import SettingsSandbox @@ -531,3 +532,25 @@ class Paypal(BasePaymentProvider): le.data = json.dumps(d) le.shredded = True le.save(update_fields=['data', 'shredded']) + + def render_invoice_text(self, order: Order, payment: OrderPayment) -> str: + if order.status == Order.STATUS_PAID: + if payment.info_data.get('id', None): + try: + return '{}\r\n{}: {}\r\n{}: {}'.format( + _('The payment for this invoice has already been received.'), + _('PayPal payment ID'), + payment.info_data['id'], + _('PayPal sale ID'), + payment.info_data['transactions'][0]['related_resources'][0]['sale']['id'] + ) + except (KeyError, IndexError): + return '{}\r\n{}: {}'.format( + _('The payment for this invoice has already been received.'), + _('PayPal payment ID'), + payment.info_data['id'] + ) + else: + return super().render_invoice_text(order, payment) + + return self.settings.get('_invoice_text', as_type=LazyI18nString, default='')