Skip to content

Commit

Permalink
merged changes from john boxalls master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mthornhill committed May 29, 2009
2 parents 2b3991b + 4d657dd commit ebb3317
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 58 deletions.
2 changes: 1 addition & 1 deletion standard/pdt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from django.http import QueryDict
from django.utils.http import urlencode
from paypal.standard.models import PayPalStandardBase
from signals import pdt_successful, pdt_failed
from paypal.standard.conf import POSTBACK_ENDPOINT, SANDBOX_POSTBACK_ENDPOINT
from paypal.standard.pdt.signals import pdt_successful, pdt_failed

# ### Todo: Move this logic to conf.py:
# if paypal.standard.pdt is in installed apps
Expand Down
34 changes: 14 additions & 20 deletions standard/pdt/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@
from django.dispatch import Signal

# Sent when a payment is successfully processed.
payment_was_successful = Signal()

# Sent when a payment is flagged.
payment_was_flagged = Signal()

# Sent when a subscription was cancelled.
subscription_cancel = Signal()

# Sent when a subscription expires.
subscription_eot = Signal()

# Sent when a subscription was modified.
subscription_modify = Signal()

# Sent when a subscription ends.
subscription_signup = Signal()

# Sent when a successful pdt occurs
pdt_successful = Signal()

# Sent when a failed pdt occurs
pdt_failed = Signal()
# Sent when a payment is flagged.
pdt_failed = Signal()

# # Sent when a subscription was cancelled.
# subscription_cancel = Signal()
#
# # Sent when a subscription expires.
# subscription_eot = Signal()
#
# # Sent when a subscription was modified.
# subscription_modify = Signal()
#
# # Sent when a subscription ends.
# subscription_signup = Signal()
2 changes: 1 addition & 1 deletion standard/pdt/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from pdt import *
from test_pdt import *
19 changes: 19 additions & 0 deletions standard/pdt/tests/templates/pdt/pdt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% ifequal pdt_obj.st 'SUCCESS' %}
<h1>Transaction complete</h1>
<p>Thank you for your payment</p>
<p>Please print this page for your records</p>

<div>
<table>
<tr><td>Payer:</td><td>{{ pdt_obj.first_name }} {{ pdt_obj.last_name }} </td></tr>
<tr><td>Payer Email:</td><td>{{ pdt_obj.payer_email }}</td></tr>
<tr><td>Amount:</td><td>{{ pdt_obj.mc_currency }} {{ pdt_obj.mc_gross }}</td></tr>
<tr><td>Reference:</td><td>{{ pdt_obj.txn_id }}</td></tr>

</table>
</div>

{% else %}
<h1>Transaction Failed</h1>
<p>Sorry transaction failed, please try a different form of payment</p>
{% endifequal %}
46 changes: 17 additions & 29 deletions standard/pdt/tests/pdt.py → standard/pdt/tests/test_pdt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
run this with ./manage.py test website
see http://www.djangoproject.com/documentation/testing/ for details
"""
import os
from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.template import Context
from django.template.loader import get_template
from django.test import TestCase
from django.test.client import Client
from paypal.standard.pdt.forms import PayPalPDTForm
from paypal.standard.pdt.models import PayPalPDT
from paypal.standard.pdt.signals import pdt_successful, pdt_failed


class DummyPayPalPDT():
class DummyPayPalPDT(object):

def __init__(self, update_context_dict={}):
self.context_dict = {'st': 'SUCCESS', 'custom':'cb736658-3aad-4694-956f-d0aeade80194',
Expand All @@ -32,17 +29,15 @@ def update_with_get_params(self, get_params):
if get_params.has_key('cm'):
self.context_dict['custom'] = get_params.get('cm')

def _postback(self):
"""
Perform a Fake PayPal PDT Postback request.
"""
t = get_template('pdt/fake_pdt_response.html')
c = Context(self.context_dict)
html = t.render(c)
self.response = html
return html
def _postback(self, test=True):
"""Perform a Fake PayPal PDT Postback request."""
# @@@ would be cool if this could live in the test templates dir...
return render_to_response("pdt/test_pdt_response.html", self.context_dict).content

class PDTTest(TestCase):
urls = "paypal.standard.pdt.tests.test_urls"
template_dirs = [os.path.join(os.path.dirname(__file__), 'templates'),]

class PDTTest(TestCase):
def setUp(self):
# set up some dummy PDT get parameters
self.get_params = {"tx":"4WJ86550014687441", "st":"Completed", "amt":"225.00", "cc":"EUR",
Expand All @@ -53,9 +48,6 @@ def setUp(self):
self.dpppdt = DummyPayPalPDT()
self.dpppdt.update_with_get_params(self.get_params)
PayPalPDT._postback = self.dpppdt._postback

# Every test needs a client.
self.client = Client()

def test_verify_postback(self):
dpppdt = DummyPayPalPDT()
Expand All @@ -72,7 +64,7 @@ def test_verify_postback(self):
def test_pdt(self):
self.assertEqual(len(PayPalPDT.objects.all()), 0)
self.dpppdt.update_with_get_params(self.get_params)
paypal_response = self.client.get(reverse('paypal-pdt'), self.get_params)
paypal_response = self.client.get("/pdt/", self.get_params)
self.assertContains(paypal_response, 'Transaction complete', status_code=200)
self.assertEqual(len(PayPalPDT.objects.all()), 1)

Expand All @@ -89,7 +81,7 @@ def failed_pdt(sender, **kwargs):
pdt_failed.connect(failed_pdt)

self.assertEqual(len(PayPalPDT.objects.all()), 0)
paypal_response = self.client.get(reverse('paypal-pdt'), self.get_params)
paypal_response = self.client.get("/pdt/", self.get_params)
self.assertContains(paypal_response, 'Transaction complete', status_code=200)
self.assertEqual(len(PayPalPDT.objects.all()), 1)
self.assertTrue(self.successful_pdt_fired)
Expand All @@ -99,12 +91,12 @@ def failed_pdt(sender, **kwargs):

def test_double_pdt_get(self):
self.assertEqual(len(PayPalPDT.objects.all()), 0)
paypal_response = self.client.get(reverse('paypal-pdt'), self.get_params)
paypal_response = self.client.get("/pdt/", self.get_params)
self.assertContains(paypal_response, 'Transaction complete', status_code=200)
self.assertEqual(len(PayPalPDT.objects.all()), 1)
pdt_obj = PayPalPDT.objects.all()[0]
self.assertEqual(pdt_obj.flag, False)
paypal_response = self.client.get(reverse('paypal-pdt'), self.get_params)
paypal_response = self.client.get("/pdt/", self.get_params)
self.assertContains(paypal_response, 'Transaction complete', status_code=200)
self.assertEqual(len(PayPalPDT.objects.all()), 1) # we don't create a new pdt
pdt_obj = PayPalPDT.objects.all()[0]
Expand All @@ -113,19 +105,15 @@ def test_double_pdt_get(self):
def test_no_txn_id_in_pdt(self):
self.dpppdt.context_dict.pop('txn_id')
self.get_params={}
paypal_response = self.client.get(reverse('paypal-pdt'), self.get_params)
paypal_response = self.client.get("/pdt/", self.get_params)
self.assertContains(paypal_response, 'Transaction Failed', status_code=200)
self.assertEqual(len(PayPalPDT.objects.all()), 0)


def test_custom_passthrough(self):
self.assertEqual(len(PayPalPDT.objects.all()), 0)
self.dpppdt.update_with_get_params(self.get_params)
paypal_response = self.client.get(reverse('paypal-pdt'), self.get_params)
paypal_response = self.client.get("/pdt/", self.get_params)
self.assertContains(paypal_response, 'Transaction complete', status_code=200)
self.assertEqual(len(PayPalPDT.objects.all()), 1)
pdt_obj = PayPalPDT.objects.all()[0]
self.assertEqual(pdt_obj.custom, self.get_params['cm'] )



self.assertEqual(pdt_obj.custom, self.get_params['cm'] )
5 changes: 5 additions & 0 deletions standard/pdt/tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.conf.urls.defaults import *

urlpatterns = patterns('paypal.standard.pdt.views',
(r'^pdt/$', 'pdt'),
)
14 changes: 7 additions & 7 deletions standard/pdt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
from django.views.decorators.http import require_GET
from paypal.standard.pdt.models import PayPalPDT
from paypal.standard.pdt.forms import PayPalPDTForm


@require_GET
def pdt(request, 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 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)
pdt_obj = PayPalPDT.objects.get(txn_id=txn_id)
except PayPalPDT.DoesNotExist:
# This is a new transaction so we continue processing PDT request
pass
Expand All @@ -44,7 +44,7 @@ def pdt(request, item_check_callable=None, template="pdt/pdt.html", context=None
# 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
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))
context.update({"failed":failed, "pdt_obj":pdt_obj})
return render_to_response(template, context, RequestContext(request))

0 comments on commit ebb3317

Please sign in to comment.