From 54f2411ab9c4508fe56fe120ee1f4ba4fe43a391 Mon Sep 17 00:00:00 2001 From: Raymond Yee Date: Tue, 10 Jan 2012 17:15:39 -0800 Subject: [PATCH] Put in a basic page for a pledge completion. --- frontend/templates/pledge_cancel.html | 17 ++-- frontend/templates/pledge_complete.html | 7 +- frontend/views.py | 108 ++++++++++++++++++------ payment/manager.py | 5 ++ 4 files changed, 98 insertions(+), 39 deletions(-) diff --git a/frontend/templates/pledge_cancel.html b/frontend/templates/pledge_cancel.html index 19659947..8d0d7748 100644 --- a/frontend/templates/pledge_cancel.html +++ b/frontend/templates/pledge_cancel.html @@ -8,21 +8,16 @@ {% block doccontent %} -
Would you consider pledging in the future?
- -
{{output}}
- {% if transaction %} -
You did not complete {{transaction.id}}
+
You were about to pledge ${{transaction.amount}} to {{work.title}} but hit the cancel link. + Naturally, we won't be charging your PayPal account for this campaign unless you give permission.
+
However, the campaign can definitely make use of your pledge -- so won't you reconsider?
+
You can finish the pledge transaction.
+ {% else %}
What transaction are you talking about?
{% endif %} - -
correct_user: {{correct_user}}
-
correct_transaction_type: {{correct_transaction_type}}
- -
pledge again
- + {% endblock %} diff --git a/frontend/templates/pledge_complete.html b/frontend/templates/pledge_complete.html index f79ad84e..49a59e4a 100644 --- a/frontend/templates/pledge_complete.html +++ b/frontend/templates/pledge_complete.html @@ -9,8 +9,11 @@ {% block doccontent %}
Thank you!
- -
{{output}}
+ +
You just pledged ${{transaction.amount}} to {{work.title}}.
+
If the campaign, which is slated to end at {{campaign.deadline}} reaches its target of ${{campaign.target}}, + your PayPal account will be charged soon after the deadline.
+
Tell your friends about this campaign!
{% endblock %} diff --git a/frontend/views.py b/frontend/views.py index 77f64f36..0dfbb61d 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -278,10 +278,7 @@ class PledgeView(FormView): if not self.embedded: - return_url = self.request.build_absolute_uri(reverse('work',kwargs={'work_id': str(work_id)})) - # I was hoping that we'd be able to pass in a transaction ID as part of the cancel_url, - # but we don't get a transaction ID until we already pass in the cancel_url. Hmmm. - # Possible approach: look in PaymentManager to see where we create our own ID + return_url = None cancel_url = None # the recipients of this authorization is not specified here but rather by the PaymentManager. @@ -293,9 +290,7 @@ class PledgeView(FormView): # embedded view triggerws instant payment: send to the partnering RH receiver_list = [{'email':settings.PAYPAL_NONPROFIT_PARTNER_EMAIL, 'amount':preapproval_amount}] - #redirect the page back to campaign page on success - return_url = self.request.build_absolute_uri(reverse('campaign_by_id',kwargs={'pk': str(pk)})) - # + return_url = None cancel_url = None t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user, @@ -311,7 +306,21 @@ class PledgeView(FormView): return HttpResponse(response) class PledgeCompleteView(TemplateView): - """A callback for PayPal to tell unglue.it that a payment transaction has completed successfully""" + """A callback for PayPal to tell unglue.it that a payment transaction has completed successfully. + + Possible things to implement: + + after pledging, supporter receives email including thanks, work pledged, amount, expiry date, any next steps they should expect; others? +study other confirmation emails for their contents +after pledging, supporters are returned to a thank-you screen +should have prominent "thank you" or "congratulations" message +should have prominent share options +should suggest other works for supporters to explore (on what basis?) +link to work page? or to page on which supporter entered the process? (if the latter, how does that work with widgets?) +should note that a confirmation email has been sent to $email from $sender +should briefly note next steps (e.g. if this campaign succeeds you will be emailed on date X) + + """ template_name="pledge_complete.html" @@ -323,21 +332,6 @@ class PledgeCompleteView(TemplateView): output += self.request.method + "\n" + str(self.request.REQUEST.items()) context["output"] = output - return context - - -class PledgeCancelView(TemplateView): - """A callback for PayPal to tell unglue.it that a payment transaction has been canceled by the user""" - template_name="pledge_cancel.html" - - def get_context_data(self): - # pick up all get and post parameters and display - context = super(PledgeCancelView, self).get_context_data() - - output = "pledge cancel" - output += self.request.method + "\n" + str(self.request.REQUEST.items()) - context["output"] = output - if self.request.user.is_authenticated(): user = self.request.user else: @@ -347,6 +341,14 @@ class PledgeCancelView(TemplateView): transaction_id = self.request.REQUEST.get("tid") transaction = Transaction.objects.get(id=transaction_id) + # work and campaign in question + try: + campaign = transaction.campaign + work = campaign.work + except Exception, e: + campaign = None + work = None + # we need to check whether the user tied to the transaction is indeed the authenticated user. correct_user = False @@ -362,12 +364,64 @@ class PledgeCancelView(TemplateView): # is it of type=PAYMENT_TYPE_AUTHORIZATION and status is NONE or ACTIVE (but approved is false) if transaction.type == PAYMENT_TYPE_AUTHORIZATION: - correct_transaction_type = 'True' + correct_transaction_type = True else: - correct_transaction_type = 'False' + correct_transaction_type = False + + context["transaction"] = transaction + context["correct_user"] = correct_user + context["correct_transaction_type"] = correct_transaction_type + context["work"] = work + context["campaign"] = campaign + + return context + + +class PledgeCancelView(TemplateView): + """A callback for PayPal to tell unglue.it that a payment transaction has been canceled by the user""" + template_name="pledge_cancel.html" + + def get_context_data(self): + context = super(PledgeCancelView, self).get_context_data() + + if self.request.user.is_authenticated(): + user = self.request.user + else: + user = None + + # pull out the transaction id and try to get the corresponding Transaction + transaction_id = self.request.REQUEST.get("tid") + transaction = Transaction.objects.get(id=transaction_id) + + # work and campaign in question + try: + campaign = transaction.campaign + work = campaign.work + except Exception, e: + campaign = None + work = None + + # we need to check whether the user tied to the transaction is indeed the authenticated user. + + correct_user = False + try: + if user.id == transaction.user.id: + correct_user = True + except Exception, e: + pass + + # check that the user had not already approved the transaction + # do we need to first run PreapprovalDetails to check on the status + + # is it of type=PAYMENT_TYPE_AUTHORIZATION and status is NONE or ACTIVE (but approved is false) + + if transaction.type == PAYMENT_TYPE_AUTHORIZATION: + correct_transaction_type = True + else: + correct_transaction_type = False # status? - + # give the user an opportunity to approved the transaction again # provide a URL to click on. # https://www.sandbox.paypal.com/?cmd=_ap-preapproval&preapprovalkey=PA-6JV656290V840615H @@ -377,6 +431,8 @@ class PledgeCancelView(TemplateView): context["correct_user"] = correct_user context["correct_transaction_type"] = correct_transaction_type context["try_again_url"] = try_again_url + context["work"] = work + context["campaign"] = campaign return context diff --git a/payment/manager.py b/payment/manager.py index c35f3adb..9b984435 100644 --- a/payment/manager.py +++ b/payment/manager.py @@ -631,6 +631,11 @@ class PaymentManager( object ): cancel_path = "{0}?{1}".format(reverse('pledge_cancel'), urllib.urlencode({'tid':t.id})) cancel_url = urlparse.urljoin(settings.BASE_URL, cancel_path) + + if return_url is None: + return_path = "{0}?{1}".format(reverse('pledge_complete'), + urllib.urlencode({'tid':t.id})) + return_url = urlparse.urljoin(settings.BASE_URL, return_path) p = Preapproval(t, amount, expiry, return_url=return_url, cancel_url=cancel_url)