bgneal@35: """ bgneal@35: Views for the donations application. bgneal@35: """ bgneal@36: import urllib2 bgneal@36: import decimal bgneal@36: import datetime bgneal@36: bgneal@35: from django.shortcuts import render_to_response bgneal@35: from django.template import RequestContext bgneal@35: from django.conf import settings bgneal@35: from django.contrib.sites.models import Site bgneal@36: from django.http import HttpResponse bgneal@36: from django.http import HttpResponseServerError bgneal@36: from django.contrib.auth.models import User bgneal@35: bgneal@35: from donations.models import Donation bgneal@35: bgneal@36: PP_DATE_FMT = '%H:%M:%S %b %d, %Y PST' bgneal@35: bgneal@36: def paypal_params(): bgneal@36: """ bgneal@36: This function returns a tuple where the 1st element is the Paypal bgneal@36: URL and the 2nd element is the Paypal business email. This information bgneal@36: depends on the setting DONATIONS_DEBUG. bgneal@36: """ bgneal@36: if settings.DONATIONS_DEBUG: bgneal@35: form_action = 'https://www.sandbox.paypal.com/cgi-bin/webscr' bgneal@35: business = settings.DONATIONS_BUSINESS_DEBUG bgneal@35: else: bgneal@35: form_action = 'https://www.paypal.com/cgi-bin/webscr' bgneal@35: business = settings.DONATIONS_BUSINESS bgneal@35: bgneal@36: return form_action, business bgneal@36: bgneal@36: bgneal@36: def index(request): bgneal@36: gross, net, donations = Donation.objects.monthly_stats() bgneal@36: current_site = Site.objects.get_current() bgneal@36: form_action, business = paypal_params() bgneal@36: bgneal@35: return render_to_response('donations/index.html', { bgneal@35: 'goal': settings.DONATIONS_GOAL, bgneal@35: 'gross': gross, bgneal@35: 'net': net, bgneal@35: 'left': settings.DONATIONS_GOAL - net, bgneal@35: 'donations': donations, bgneal@35: 'form_action': form_action, bgneal@35: 'business': business, bgneal@35: 'anonymous': settings.DONATIONS_ANON_NAME, bgneal@35: 'item_name': settings.DONATIONS_ITEM_NAME, bgneal@35: 'item_number': settings.DONATIONS_ITEM_NUM, bgneal@35: 'item_anon_number': settings.DONATIONS_ITEM_ANON_NUM, bgneal@35: 'domain': current_site.domain, bgneal@35: }, bgneal@35: context_instance = RequestContext(request)) bgneal@35: bgneal@35: bgneal@35: def ipn(request): bgneal@36: """ bgneal@36: This function is the IPN listener and handles the IPN POST from Paypal. bgneal@35: bgneal@36: TODO: Add logging. bgneal@36: """ bgneal@36: parameters = None bgneal@36: try: bgneal@36: if request['payment_status'] == 'Completed': bgneal@36: if request.POST: bgneal@36: parameters = request.POST.copy() bgneal@36: else: bgneal@36: parameters = request.GET.copy() bgneal@35: bgneal@36: if parameters: bgneal@36: parameters['cmd']='_notify-validate' bgneal@36: req = urllib2.Request(paypal_params()[0], parameters.urlencode()) bgneal@36: req.add_header("Content-type", "application/x-www-form-urlencoded") bgneal@36: response = urllib2.urlopen(req) bgneal@36: status = response.read() bgneal@36: if status != "VERIFIED": bgneal@36: parameters = None bgneal@36: bgneal@36: if parameters: bgneal@36: # is this a donation to the site? bgneal@36: try: bgneal@36: item_number = int(parameters['item_number']) bgneal@36: except ValueError: bgneal@36: pass bgneal@36: else: bgneal@36: if item_number == settings.DONATIONS_ITEM_NUM or \ bgneal@36: item_number == settings.DONATIONS_ITEM_ANON_NUM: bgneal@36: process_donation(item_number, parameters) bgneal@36: bgneal@36: return HttpResponse("Ok") bgneal@36: bgneal@36: except Exception, e: bgneal@36: # print "An exeption was caught: " + str(e) bgneal@36: pass bgneal@36: bgneal@36: return HttpResponseServerError("Error") bgneal@36: bgneal@36: bgneal@36: def process_donation(item_number, params): bgneal@36: """ bgneal@36: Constructs a donation object from the parameters and stores bgneal@36: in the database. bgneal@36: """ bgneal@36: # Has this transaction been processed before? bgneal@36: if 'txn_id' not in params: bgneal@36: return bgneal@36: txn_id = params['txn_id'] bgneal@36: try: bgneal@36: donation = Donation.objects.get(txn_id__exact=txn_id) bgneal@36: except Donation.DoesNotExist: bgneal@36: pass bgneal@36: else: bgneal@36: return # no exception, this is a duplicate bgneal@36: bgneal@36: # Is the email address ours? bgneal@36: business = params.get('business') bgneal@36: if business != paypal_params()[1]: bgneal@36: return bgneal@36: bgneal@36: # is this a payment received? bgneal@36: txn_type = params.get('txn_type') bgneal@36: if txn_type != 'web_accept': bgneal@36: return bgneal@36: bgneal@36: # Looks like a donation, save it to the database. bgneal@36: # Determine which user this came from, if any. bgneal@36: # The username is stored in the custom field if the user was logged in when bgneal@36: # the donation was made. bgneal@36: user = None bgneal@36: if 'custom' in params: bgneal@36: try: bgneal@36: user = User.objects.get(username__exact=params['custom']) bgneal@36: except User.DoesNotExist: bgneal@36: pass bgneal@36: bgneal@36: is_anonymous = item_number == settings.DONATIONS_ITEM_ANON_NUM bgneal@36: test_ipn = params.get('test_ipn') == '1' bgneal@36: bgneal@36: first_name = params.get('first_name', '') bgneal@36: last_name = params.get('last_name', '') bgneal@36: payer_email = params.get('payer_email', '') bgneal@36: payer_id = params.get('payer_id', '') bgneal@36: memo = params.get('memo', '') bgneal@36: payer_status = params.get('payer_status', '') bgneal@36: bgneal@36: try: bgneal@36: mc_gross = decimal.Decimal(params['mc_gross']) bgneal@36: mc_fee = decimal.Decimal(params['mc_fee']) bgneal@36: except KeyError, decimal.InvalidOperation: bgneal@36: return bgneal@36: bgneal@36: try: bgneal@36: payment_date = datetime.datetime.strptime(params['payment_date'], bgneal@36: PP_DATE_FMT) bgneal@36: except KeyError, ValueError: bgneal@36: return bgneal@36: bgneal@36: donation = Donation( bgneal@36: user=user, bgneal@36: is_anonymous=is_anonymous, bgneal@36: test_ipn=test_ipn, bgneal@36: txn_id=txn_id, bgneal@36: first_name=first_name, bgneal@36: last_name=last_name, bgneal@36: payer_email=payer_email, bgneal@36: payer_id=payer_id, bgneal@36: memo=memo, bgneal@36: payer_status=payer_status, bgneal@36: mc_gross=mc_gross, bgneal@36: mc_fee=mc_fee, bgneal@36: payment_date=payment_date) bgneal@36: bgneal@36: donation.save() bgneal@36: