Skip to content

Commit

Permalink
- WPP allow error messages to be overridden by subclasses.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Boxall committed May 16, 2009
1 parent baae634 commit e9beace
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions pro/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,23 @@ class PayPalPro(object):
success_url / fail_url: URLs to be redirected to when the payment is comlete or fails.
"""
processing_error = "There was an error processing your payment. Check your information and try again."
form_error = "Please correct the errors below and try again."
paypal_error = "There was a problem contacting PayPal. Please try again later."

def __init__(self, item=None, payment_form_cls=PaymentForm,
payment_template="pro/payment.html", confirm_form_cls=ConfirmForm,
confirm_template="pro/confirm.html", success_url="?success",
fail_url=None, context=None):
fail_url=None, context=None, form_context_name="form"):
self.item = item
self.is_recurring = False
if 'billingperiod' in item:
self.is_recurring = True
self.payment_form_cls = payment_form_cls
self.payment_template = payment_template
self.confirm_form_cls = confirm_form_cls
self.confirm_template = confirm_template
self.success_url = success_url
self.fail_url = fail_url
self.context = context or {}
self.form_context_name = form_context_name

def __call__(self, request):
"""Return the appropriate response for the state of the transaction."""
Expand All @@ -106,6 +108,9 @@ def __call__(self, request):
# Default to the rendering the payment form.
return self.render_payment_form()

def is_recurring(self):
return self.item is not None and 'billingperiod' in self.item

def should_redirect_to_express(self):
return 'express' in self.request.GET

Expand All @@ -123,7 +128,7 @@ def should_validate_payment_form(self):

def render_payment_form(self):
"""Display the DirectPayment for entering payment information."""
self.context['form'] = self.payment_form_cls()
self.context[self.form_context_name] = self.payment_form_cls()
return render_to_response(self.payment_template, self.context, RequestContext(self.request))

def validate_payment_form(self):
Expand All @@ -135,10 +140,10 @@ def validate_payment_form(self):
payment_was_successful.send(sender=self.item)
return HttpResponseRedirect(self.success_url)
else:
self.context['errors'] = "There was an error processing your payment. Check your information and try again."
self.context['errors'] = self.processing_error

self.context['form'] = form
self.context.setdefault("errors", "Please correct the errors below and try again.")
self.context[self.form_context_name] = form
self.context.setdefault("errors", self.form_error)
return render_to_response(self.payment_template, self.context, RequestContext(self.request))

def get_endpoint(self):
Expand All @@ -161,7 +166,7 @@ def redirect_to_express(self):
pp_url = self.get_endpoint() % urlencode(pp_params)
return HttpResponseRedirect(pp_url)
else:
self.context['errors'] = 'There was a problem contacting PayPal. Please try again later.'
self.context['errors'] = self.paypal_error
return self.render_payment_form()

def render_confirm_form(self):
Expand All @@ -170,7 +175,7 @@ def render_confirm_form(self):
contains hidden fields with the token / PayerID from PayPal.
"""
initial = dict(token=self.request.GET['token'], PayerID=self.request.GET['PayerID'])
self.context['form'] = self.confirm_form_cls(initial=initial)
self.context[self.form_context_name] = self.confirm_form_cls(initial=initial)
return render_to_response(self.confirm_template, self.context, RequestContext(self.request))

def validate_confirm_form(self):
Expand All @@ -191,5 +196,5 @@ def validate_confirm_form(self):
payment_was_successful.send(sender=self.item)
return HttpResponseRedirect(self.success_url)
else:
self.context['errors'] = "There was a problem processing the payment. Check your information and try again."
self.context['errors'] = self.processing_error
return self.render_payment_form()

0 comments on commit e9beace

Please sign in to comment.