From 0ea2f24e508a0571ed52e299cfa0f17a39509c78 Mon Sep 17 00:00:00 2001 From: Johannes Spielmann Date: Fri, 25 Mar 2011 13:50:22 +0100 Subject: [PATCH] refactored pdt functionality into a decorator --- README.md | 12 ++++++- paypal/standard/pdt/decorators.py | 56 +++++++++++++++++++++++++++++++ paypal/standard/pdt/views.py | 48 ++++---------------------- 3 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 paypal/standard/pdt/decorators.py diff --git a/README.md b/README.md index 1a0b063..4ab5bd4 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Using PayPal Payments Standard IPN: 1. Download the code from GitHub: - git clone git://github.com/johnboxall/django-paypal.git paypal + git clone git://github.com/shezi/django-paypal.git paypal 1. Edit `settings.py` and add `paypal.standard.ipn` to your `INSTALLED_APPS` and `PAYPAL_RECEIVER_EMAIL`: @@ -142,6 +142,16 @@ Paypal Payment Data Transfer (PDT) allows you to display transaction details to (r'^paypal/pdt/', include('paypal.standard.pdt.urls')), ... ) + Alternatively, you can use the pdt decorator to work with PDT information in one of your own views. + To do this, add the decorator to one of your views. + # views.py + from paypal.standard.pdt.decorators import pdt + + @pdt + def view_func(request, *args, **kwargs): + ... + The decorator checks for any GET parameters corresponding to a PDT call and adds the keyword arguments `pdt_active`, `pdt_failed` and `pdt` to the view call. + Using PayPal Payments Standard with Subscriptions: -------------------------------------------------- diff --git a/paypal/standard/pdt/decorators.py b/paypal/standard/pdt/decorators.py new file mode 100644 index 0000000..3539fdf --- /dev/null +++ b/paypal/standard/pdt/decorators.py @@ -0,0 +1,56 @@ +# -*- encoding: utf-8 -*- + +from paypal.standard.pdt.models import PayPalPDT +from paypal.standard.pdt.forms import PayPalPDTForm + + +def pdt(f): + """Parses out GET parameters corresponding to a paypal PDT request and adds `pdt_active`, `pdt_failed` and `pdt` to the call **kwargs. + + Payment data transfer implementation: http://tinyurl.com/c9jjmw""" + + def aux(request, *args, **kwargs): + if request.method == 'POST': + return f(request, *args, **kwargs) + + pdt_obj = None + pdt_active = False + txn_id = request.GET.get('tx') + failed = False + if txn_id is not None: + pdt_active = True + # If an existing transaction with the id tx exists: use it + try: + pdt_obj = PayPalPDT.objects.get(txn_id=txn_id) + except PayPalPDT.DoesNotExist: + # This is a new transaction so we continue processing PDT request + pass + + if pdt_obj is None: + form = PayPalPDTForm(request.GET) + if form.is_valid(): + try: + pdt_obj = form.save(commit=False) + except Exception, e: + error = repr(e) + failed = True + else: + error = form.errors + failed = True + + if failed: + pdt_obj = PayPalPDT() + pdt_obj.set_flag("Invalid form. %s" % error) + + pdt_obj.initialize(request) + + if not failed: + # The PDT object gets saved during verify + pdt_obj.verify(item_check_callable) + else: + pass # we ignore any PDT requests that don't have a transaction id + + kwargs.update({'pdt_active': pdt_active, 'pdt_failed': failed, 'pdt_obj': pdt_obj}) + return f(request, *args, **kwargs) + + return aux diff --git a/paypal/standard/pdt/views.py b/paypal/standard/pdt/views.py index 0993411..e1e612b 100644 --- a/paypal/standard/pdt/views.py +++ b/paypal/standard/pdt/views.py @@ -3,48 +3,12 @@ from django.template import RequestContext from django.shortcuts import render_to_response from django.views.decorators.http import require_GET -from paypal.standard.pdt.models import PayPalPDT -from paypal.standard.pdt.forms import PayPalPDTForm - +from paypal.standard.pdt.decorators import pdt @require_GET -def pdt(request, item_check_callable=None, template="pdt/pdt.html", context=None): +@pdt +def pdt(request, pdt_active=True, pdt_failed=False, pdt_obj=None, item_check_callable=None, template="pdt/pdt.html", context=None): """Payment data transfer implementation: http://tinyurl.com/c9jjmw""" - context = context or {} - pdt_obj = None - txn_id = request.GET.get('tx') - failed = False - if txn_id is not None: - # If an existing transaction with the id tx exists: use it - try: - pdt_obj = PayPalPDT.objects.get(txn_id=txn_id) - except PayPalPDT.DoesNotExist: - # This is a new transaction so we continue processing PDT request - pass - - if pdt_obj is None: - form = PayPalPDTForm(request.GET) - if form.is_valid(): - try: - pdt_obj = form.save(commit=False) - except Exception, e: - error = repr(e) - failed = True - else: - error = form.errors - failed = True - - if failed: - pdt_obj = PayPalPDT() - pdt_obj.set_flag("Invalid form. %s" % error) - - pdt_obj.initialize(request) - - if not failed: - # The PDT object gets saved during verify - pdt_obj.verify(item_check_callable) - else: - pass # we ignore any PDT requests that don't have a transaction id - - context.update({"failed":failed, "pdt_obj":pdt_obj}) - return render_to_response(template, context, RequestContext(request)) \ No newline at end of file + context = context or {} + context.update({"failed":pdt_failed, "pdt_obj":pdt_obj}) + return render_to_response(template, context, RequestContext(request))