From d0685e99ad1f31c16ab8e790749dcf5ae458388f Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 3 Dec 2021 10:30:33 +0100 Subject: [PATCH] Return URL: Append error/success message to query --- doc/api/guides/custom_checkout.rst | 3 ++- src/pretix/plugins/returnurl/signals.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/doc/api/guides/custom_checkout.rst b/doc/api/guides/custom_checkout.rst index 5a087257c5..26fb904a6f 100644 --- a/doc/api/guides/custom_checkout.rst +++ b/doc/api/guides/custom_checkout.rst @@ -97,7 +97,8 @@ For example, if you want users to be redirected to ``https://example.org/order/r either enter ``https://example.org`` or ``https://example.org/order/``. The user will be redirected back to your page instead of pretix' order confirmation page after the payment, -**regardless of whether it was successful or not**. Make sure you use our API to check if the payment actually +**regardless of whether it was successful or not**. We will append an ``error=…`` query parameter with an error +message, but you should not rely on that and instead make sure you use our API to check if the payment actually worked! Your final URL could look like this:: https://test.pretix.eu/democon/3vjrh/order/NSLEZ/ujbrnsjzbq4dzhck/pay/123/?return_url=https%3A%2F%2Fexample.org%2Forder%2Freturn%3Ftx_id%3D1234 diff --git a/src/pretix/plugins/returnurl/signals.py b/src/pretix/plugins/returnurl/signals.py index 595d04481a..d2e1d318d2 100644 --- a/src/pretix/plugins/returnurl/signals.py +++ b/src/pretix/plugins/returnurl/signals.py @@ -19,6 +19,10 @@ # You should have received a copy of the GNU Affero General Public License along with this program. If not, see # . # +from urllib.parse import urlencode + +from django.contrib.messages import get_messages +from django.contrib.messages import constants as messages from django.core.exceptions import PermissionDenied from django.dispatch import receiver from django.shortcuts import redirect @@ -43,7 +47,25 @@ def returnurl_process_request(sender, request, **kwargs): key = 'order_{}_{}_{}_return_url'.format(urlkwargs.get('organizer', '-'), urlkwargs.get('event', '-'), urlkwargs['order']) if urlname == 'event.order' and key in request.session: - r = redirect(request.session.get(key)) + url = request.session.get(key) + + query = [] + storage = get_messages(request) + for message in storage: + if message.level == messages.ERROR: + query.append(('error', str(message))) + elif message.level == messages.WARNING: + query.append(('warning', str(message))) + if message.level == messages.INFO: + query.append(('info', str(message))) + if message.level == messages.SUCCESS: + query.append(('success', str(message))) + if query: + if '?' in url: + url += '&' + urlencode(query) + else: + url += '?' + urlencode(query) + r = redirect(url) del request.session[key] return r elif urlname != 'event.order' and 'return_url' in request.GET: