En route to implementing pledge cancellation

pull/1/head
Raymond Yee 2012-05-21 07:56:18 -07:00
parent 272d795ded
commit b8336c4ed6
5 changed files with 31 additions and 53 deletions

View File

@ -314,8 +314,12 @@ class GoodreadsShelfLoadingForm(forms.Form):
class LibraryThingForm(forms.Form):
lt_username = forms.CharField(max_length=30, required=True)
class PledgeCancelForm(forms.Form):
# campaign_id is the id for the campaign
campaign_id = forms.IntegerField(required=True, widget=forms.HiddenInput())
class CampaignAdminForm(forms.Form):
pass
campaign_id = forms.IntegerField()
class EmailShareForm(forms.Form):
recipient = forms.EmailField(error_messages={'required': 'Please specify a recipient.'})

View File

@ -97,7 +97,7 @@
</div>
</div>
{% if faqmenu == 'modify' %}<div class="cancel_notice">(We hope you won't, but of course you're also free to <a href="{% url pledge_cancel %}?tid={{ tid }}">cancel your pledge</a>.)</div>{% endif %}
{% if faqmenu == 'modify' %}<div class="cancel_notice">(We hope you won't, but of course you're also free to <a href="{% url pledge_cancel campaign_id=work.last_campaign.id %}">cancel your pledge</a>.)</div>{% endif %}
{% endblock %}

View File

@ -11,7 +11,11 @@
{% if transaction %}
<div>You've asked to cancel your pledge of ${{ transaction.amount|intcomma }} to <a href="{% url work work.id %}">{{ work.title }}</a> Did you mean to do this?</div>
<div class="btn_support">If so, please click on the feedback form to the right to let us know, and we'll cancel the transaction for you.</div>
<form method="post" action="{% url pledge_cancel campaign_id=campaign.id %}">
{% csrf_token %}
<input type="submit" value="Cancel Pledge" id="pledgesubmit" />
</form>
{% comment %}
"Yes" should trigger whatever functionality we need to complete cancellation -- may differ depending on whether we've hit the back button from Amazon or the cancel-my-pledge link in pledge_modify.

View File

@ -49,7 +49,7 @@ urlpatterns = patterns(
url(r"^new_edition/(?P<work_id>\d*)/(?P<edition_id>\d*)$", "new_edition", name="new_edition"),
url(r"^googlebooks/(?P<googlebooks_id>.+)/$", "googlebooks", name="googlebooks"),
url(r"^pledge/(?P<work_id>\d+)/$", login_required(PledgeView.as_view()), name="pledge"),
url(r"^pledge/cancel/$", login_required(PledgeCancelView.as_view()), name="pledge_cancel"),
url(r"^pledge/cancel/(?P<campaign_id>\d+)$", login_required(PledgeCancelView.as_view()), name="pledge_cancel"),
url(r"^pledge/complete/$", login_required(PledgeCompleteView.as_view()), name="pledge_complete"),
url(r"^pledge/nevermind/$", login_required(PledgeNeverMindView.as_view()), name="pledge_nevermind"),
url(r"^pledge/modify/(?P<work_id>\d+)$", login_required(PledgeModifyView.as_view()), name="pledge_modify"),

View File

@ -46,7 +46,7 @@ from regluit.core.goodreads import GoodreadsClient
from regluit.frontend.forms import UserData, UserEmail, ProfileForm, CampaignPledgeForm, GoodreadsShelfLoadingForm
from regluit.frontend.forms import RightsHolderForm, UserClaimForm, LibraryThingForm, OpenCampaignForm
from regluit.frontend.forms import getManageCampaignForm, DonateForm, CampaignAdminForm, EmailShareForm, FeedbackForm
from regluit.frontend.forms import EbookForm, CustomPremiumForm, EditManagersForm, EditionForm
from regluit.frontend.forms import EbookForm, CustomPremiumForm, EditManagersForm, EditionForm, PledgeCancelForm
from regluit.payment.manager import PaymentManager
from regluit.payment.models import Transaction
from regluit.payment.parameters import TARGET_TYPE_CAMPAIGN, TARGET_TYPE_DONATION, PAYMENT_TYPE_AUTHORIZATION
@ -857,60 +857,30 @@ class PledgeCompleteView(TemplateView):
return context
class PledgeCancelView(TemplateView):
"""A callback for PayPal to tell unglue.it that a payment transaction has been canceled by the user"""
class PledgeCancelView(FormView):
"""A view for allowing a user to cancel the active transaction for specified campaign"""
template_name="pledge_cancel.html"
form_class = PledgeCancelForm
def get_context_data(self):
context = super(PledgeCancelView, self).get_context_data()
def get_context_data(self, **kwargs):
context = super(PledgeCancelView, self).get_context_data(**kwargs)
if self.request.user.is_authenticated():
error_msg = None
# the following should be true since PledgeCancelView.as_view is wrapped in login_required
assert 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)
campaign = get_object_or_404(models.Campaign, id=self.kwargs["campaign_id"])
assert campaign.status == 'ACTIVE'
# 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
try_again_url = '%s?cmd=_ap-preapproval&preapprovalkey=%s' % (settings.PAYPAL_PAYMENT_HOST, transaction.preapproval_key)
transactions = campaign.transactions().filter(user=user, status=TRANSACTION_STATUS_ACTIVE)
assert transactions.count() == 1
transaction = transactions[0]
assert transaction.type == PAYMENT_TYPE_AUTHORIZATION and transaction.status == TRANSACTION_STATUS_ACTIVE
context["transaction"] = transaction
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
context["faqmenu"] = "cancel"