pull/1/head
eric 2011-11-22 17:27:59 -05:00
commit 39eface590
5 changed files with 76 additions and 26 deletions

View File

@ -186,13 +186,13 @@ class Work(models.Model):
def last_campaign(self):
# stash away the last campaign to prevent repeated lookups
if hasattr(self, '_last_campaign'):
return self._last_campaign
if hasattr(self, '_last_campaign_'):
return self._last_campaign_
try:
self._last_campaign = self.campaigns.order_by('-created')[0]
self._last_campaign_ = self.campaigns.order_by('-created')[0]
except IndexError:
pass
return self._last_campaign
self._last_campaign_ = None
return self._last_campaign_
def last_campaign_status(self):
campaign = self.last_campaign()

View File

@ -1,7 +1,26 @@
{% extends "base.html" %}
{% url privacy as privacyurl %}
{% block extra_head %}
<script src ='https://www.paypalobjects.com/js/external/dg.js' type='text/javascript'></script>
{% if embedded %}
<script>
//
// This page is also used as the completion URL for purchases. Close the lightbox
// whenever we are opened
//
dgFlow = top.dgFlow || top.opener.top.dgFlow;
dgFlow.closeFlow();
top.close()
</script>
{% endif %}
{% endblock %}
{% block content %}
<h2>Campaign: {{campaign.name}}</h2>
<p>Work: {{campaign.work}}</p>
<p>Target: {{campaign.target}}</p>
@ -14,11 +33,12 @@
<p>PayPal receiver: {{campaign.paypal_receiver}}</p>
<p>Current total (pledged/authorized): {{campaign.current_total}}</p>
<p>Base URL: {{base_url}}</p>
<form method="POST" action="{% url campaign_by_id pk=campaign.id %}">
<form method="POST" action="{% url campaign_by_id pk=campaign.id %}" onsubmit="test()">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Pledge" />
<input type="submit" value="Pledge" id="pledgeBtn"/>
</form>
<!-- All associated transactions -->
{% if campaign.transactions %}
<p>Associated transactions:</p>
@ -30,4 +50,14 @@
{% else %}
<p>No associated transactions</p>
{% endif %}
{% if embedded %}
<script>
//
// This has to be included AFTER the definition of the button
//
var dgFlow = new PAYPAL.apps.DGFlow({ trigger: 'pledgeBtn' });
</script>
{% endif %}
{% endblock %}

View File

@ -421,6 +421,7 @@ def wishlist(request):
class CampaignFormView(FormView):
template_name="campaign_detail.html"
form_class = CampaignPledgeForm
embedded = True
def get_context_data(self, **kwargs):
pk = self.kwargs["pk"]
@ -428,6 +429,7 @@ class CampaignFormView(FormView):
context = super(CampaignFormView, self).get_context_data(**kwargs)
base_url = self.request.build_absolute_uri("/")[:-1]
context.update({
'embedded': self.embedded,
'campaign': campaign,
'base_url':base_url
})
@ -442,7 +444,7 @@ class CampaignFormView(FormView):
# right now, if there is a non-zero pledge amount, go with that. otherwise, do the pre_approval
campaign = models.Campaign.objects.get(id=int(pk))
p = PaymentManager()
p = PaymentManager(embedded=self.embedded)
# we should force login at this point -- or if no account, account creation, login, and return to this spot
if self.request.user.is_authenticated():
@ -452,20 +454,21 @@ class CampaignFormView(FormView):
# calculate the work corresponding to the campaign id
work_id = campaign.work.id
return_url = self.request.build_absolute_uri(reverse('work',kwargs={'work_id': str(work_id)}))
t, url = p.authorize('USD', TARGET_TYPE_CAMPAIGN, preapproval_amount, campaign=campaign, list=None, user=user,
if not self.embedded:
return_url = self.request.build_absolute_uri(reverse('work',kwargs={'work_id': str(work_id)}))
t, url = p.authorize('USD', TARGET_TYPE_CAMPAIGN, preapproval_amount, campaign=campaign, list=None, user=user,
return_url=return_url, anonymous=anonymous)
#else:
# # instant payment: send to the partnering RH
# # right now, all money going to Gluejar.
# receiver_list = [{'email':settings.PAYPAL_GLUEJAR_EMAIL, 'amount':pledge_amount}]
#
# # redirect the page back to campaign page on success
# #return_url = self.request.build_absolute_uri("/campaigns/%s" %(str(pk)))
# return_url = self.request.build_absolute_uri(reverse('campaign_by_id',kwargs={'pk': str(pk)}))
# t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user,
# return_url=return_url, anonymous=anonymous)
else:
# instant payment: send to the partnering RH
# right now, all money going to Gluejar.
receiver_list = [{'email':settings.PAYPAL_GLUEJAR_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)}))
t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user,
return_url=return_url, anonymous=anonymous)
if url:
logger.info("CampaignFormView paypal: " + url)

View File

@ -24,6 +24,9 @@ def append_element(doc, parent, name, text):
# at this point, there is no internal context and therefore, the methods of PaymentManager can be recast into static methods
class PaymentManager( object ):
def __init__( self, embedded=False):
self.embedded = embedded
def checkStatus(self):
@ -376,8 +379,12 @@ class PaymentManager( object ):
if p.status() == 'Success':
t.reference = p.paykey()
t.save()
logger.info("Authorize Success: " + p.next_url())
return t, p.next_url()
url = p.next_url()
logger.info("Authorize Success: " + url)
return t, url
else:
t.error = p.error()
@ -434,8 +441,15 @@ class PaymentManager( object ):
t.reference = p.paykey()
t.status = 'CREATED'
t.save()
logger.info("Pledge Success: " + p.next_url())
return t, p.next_url()
if self.embedded:
url = p.embedded_url()
print url
else:
url = p.next_url()
logger.info("Pledge Success: " + url)
return t, url
else:
t.error = p.error()

View File

@ -196,6 +196,9 @@ class Pay( object ):
def next_url( self ):
return '%s?cmd=_ap-payment&paykey=%s' % (settings.PAYPAL_PAYMENT_HOST, self.response['payKey'] )
def embedded_url(self):
return '%s/webapps/adaptivepayment/flow/pay?paykey=%s&expType=light' % ( settings.PAYPAL_PAYMENT_HOST, self.response['payKey'] )
class PaymentDetails(object):
def __init__(self, transaction=None):
@ -374,7 +377,7 @@ class Preapproval( object ):
return self.response['preapprovalKey']
else:
return None
def next_url( self ):
return '%s?cmd=_ap-preapproval&preapprovalkey=%s' % ( settings.PAYPAL_PAYMENT_HOST, self.response['preapprovalKey'] )