2010-05-11 03:27:13 +04:00
|
|
|
import logging
|
2010-05-07 10:47:26 +04:00
|
|
|
import random
|
2010-02-05 02:14:16 +03:00
|
|
|
import socket
|
2010-03-15 23:37:20 +03:00
|
|
|
import urllib2
|
2010-02-05 02:14:16 +03:00
|
|
|
|
2010-03-15 23:37:20 +03:00
|
|
|
from django import http
|
2010-02-05 02:14:16 +03:00
|
|
|
from django.conf import settings
|
2010-05-02 10:46:17 +04:00
|
|
|
from django.core.cache import cache, parse_backend_uri
|
2010-02-05 02:14:16 +03:00
|
|
|
from django.views.decorators.cache import never_cache
|
2010-03-15 23:37:20 +03:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2010-02-05 02:14:16 +03:00
|
|
|
|
2010-02-05 04:51:14 +03:00
|
|
|
import jingo
|
2010-03-15 23:37:20 +03:00
|
|
|
import phpserialize as php
|
|
|
|
|
2010-05-13 07:56:50 +04:00
|
|
|
from amo.urlresolvers import reverse
|
|
|
|
from api.views import render_xml
|
2010-04-23 03:17:23 +04:00
|
|
|
from stats.models import Contribution, ContributionError, SubscriptionEvent
|
2010-03-24 21:50:50 +03:00
|
|
|
from . import log
|
2010-02-05 04:51:14 +03:00
|
|
|
|
2010-05-11 03:27:13 +04:00
|
|
|
paypal_log = logging.getLogger('z.paypal')
|
2010-02-05 02:14:16 +03:00
|
|
|
|
2010-05-13 08:12:11 +04:00
|
|
|
|
2010-02-05 02:14:16 +03:00
|
|
|
@never_cache
|
|
|
|
def monitor(request):
|
|
|
|
|
2010-02-05 08:13:41 +03:00
|
|
|
# For each check, a boolean pass/fail status to show in the template
|
|
|
|
status_summary = {}
|
2010-02-05 02:14:16 +03:00
|
|
|
status = 200
|
|
|
|
|
2010-02-05 08:13:41 +03:00
|
|
|
# Check all memcached servers
|
|
|
|
scheme, servers, _ = parse_backend_uri(settings.CACHE_BACKEND)
|
|
|
|
memcache_results = []
|
|
|
|
status_summary['memcache'] = True
|
2010-02-05 02:14:16 +03:00
|
|
|
if 'memcached' in scheme:
|
|
|
|
hosts = servers.split(';')
|
|
|
|
for host in hosts:
|
|
|
|
ip, port = host.split(':')
|
|
|
|
try:
|
|
|
|
s = socket.socket()
|
|
|
|
s.connect((ip, int(port)))
|
|
|
|
except Exception, e:
|
2010-02-05 08:13:41 +03:00
|
|
|
result = False
|
|
|
|
status_summary['memcache'] = False
|
|
|
|
status = 500
|
2010-03-24 22:51:03 +03:00
|
|
|
log.critical('Failed to connect to memcached (%s): %s' %
|
|
|
|
(host, e))
|
2010-02-05 02:14:16 +03:00
|
|
|
else:
|
2010-02-05 08:13:41 +03:00
|
|
|
result = True
|
2010-02-05 02:14:16 +03:00
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
2010-02-05 08:13:41 +03:00
|
|
|
memcache_results.append((ip, port, result))
|
|
|
|
if len(memcache_results) < 2:
|
2010-02-05 11:14:43 +03:00
|
|
|
status = 500
|
2010-02-05 08:13:41 +03:00
|
|
|
status_summary['memcache'] = False
|
2010-03-24 22:51:03 +03:00
|
|
|
log.warning('You should have 2+ memcache servers. You have %s.' %
|
|
|
|
len(memcache_results))
|
|
|
|
if not memcache_results:
|
|
|
|
status = 500
|
|
|
|
status_summary['memcache'] = False
|
|
|
|
log.info('Memcache is not configured.')
|
2010-02-05 02:14:16 +03:00
|
|
|
|
|
|
|
return jingo.render(request, 'services/monitor.html',
|
2010-02-05 08:13:41 +03:00
|
|
|
{'memcache_results': memcache_results,
|
|
|
|
'status_summary': status_summary},
|
|
|
|
status=status)
|
2010-02-05 04:51:14 +03:00
|
|
|
|
|
|
|
|
2010-03-15 23:37:20 +03:00
|
|
|
@csrf_exempt
|
|
|
|
def paypal(request):
|
|
|
|
"""
|
|
|
|
Handle PayPal IPN post-back for contribution transactions.
|
|
|
|
|
|
|
|
IPN will retry periodically until it gets success (status=200). Any
|
|
|
|
db errors or replication lag will result in an exception and http
|
|
|
|
status of 500, which is good so PayPal will try again later.
|
2010-05-02 10:46:17 +04:00
|
|
|
|
|
|
|
PayPal IPN variables available at:
|
|
|
|
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content
|
|
|
|
&content_ID=developer/e_howto_html_IPNandPDTVariables
|
2010-03-15 23:37:20 +03:00
|
|
|
"""
|
2010-05-11 01:28:56 +04:00
|
|
|
try:
|
|
|
|
return _paypal(request)
|
|
|
|
except Exception, e:
|
2010-05-11 03:27:13 +04:00
|
|
|
paypal_log.error('%s\n%s' % (e, request))
|
2010-05-11 01:28:56 +04:00
|
|
|
return http.HttpResponseServerError('Unknown error.')
|
2010-05-07 10:47:26 +04:00
|
|
|
|
|
|
|
|
2010-05-11 01:28:56 +04:00
|
|
|
def _paypal(request):
|
2010-05-11 03:27:13 +04:00
|
|
|
|
2010-05-07 10:47:26 +04:00
|
|
|
def _log_error_with_data(msg, request):
|
|
|
|
"""Log a message along with some of the POST info from PayPal."""
|
|
|
|
|
2010-05-11 01:28:56 +04:00
|
|
|
id = random.randint(0, 99999999)
|
2010-05-07 10:47:26 +04:00
|
|
|
msg = "[%s] %s (dumping data)" % (id, msg)
|
|
|
|
|
2010-05-11 03:27:13 +04:00
|
|
|
paypal_log.error(msg)
|
2010-05-07 10:47:26 +04:00
|
|
|
|
|
|
|
logme = {'txn_id': request.POST.get('txn_id'),
|
2010-05-11 02:33:01 +04:00
|
|
|
'txn_type': request.POST.get('txn_type'),
|
2010-05-07 10:47:26 +04:00
|
|
|
'payer_email': request.POST.get('payer_email'),
|
|
|
|
'receiver_email': request.POST.get('receiver_email'),
|
|
|
|
'payment_status': request.POST.get('payment_status'),
|
|
|
|
'payment_type': request.POST.get('payment_type'),
|
|
|
|
'mc_gross': request.POST.get('mc_gross'),
|
|
|
|
'item_number': request.POST.get('item_number'),
|
|
|
|
}
|
|
|
|
|
2010-05-11 03:27:13 +04:00
|
|
|
paypal_log.error("[%s] PayPal Data: %s" % (id, logme))
|
2010-05-07 10:47:26 +04:00
|
|
|
|
2010-04-30 22:14:26 +04:00
|
|
|
if request.method != 'POST':
|
|
|
|
return http.HttpResponseNotAllowed(['POST'])
|
2010-03-15 23:37:20 +03:00
|
|
|
|
|
|
|
# Check that the request is valid and coming from PayPal.
|
2010-05-11 01:28:56 +04:00
|
|
|
data = '%s&%s' % ('cmd=_notify-validate', request.raw_post_data)
|
2010-05-07 10:47:26 +04:00
|
|
|
paypal_response = urllib2.urlopen(settings.PAYPAL_CGI_URL,
|
2010-05-11 01:28:56 +04:00
|
|
|
data, 20).readline()
|
2010-05-07 10:47:26 +04:00
|
|
|
if paypal_response != 'VERIFIED':
|
|
|
|
msg = ("Expecting 'VERIFIED' from PayPal, got '%s'. "
|
|
|
|
"Failing." % paypal_response)
|
|
|
|
_log_error_with_data(msg, request)
|
2010-03-15 23:37:20 +03:00
|
|
|
return http.HttpResponseForbidden('Invalid confirmation')
|
|
|
|
|
2010-04-23 03:17:23 +04:00
|
|
|
if request.POST.get('txn_type', '').startswith('subscr_'):
|
|
|
|
SubscriptionEvent.objects.create(post_data=php.serialize(request.POST))
|
2010-05-02 10:46:17 +04:00
|
|
|
return http.HttpResponse('Success!')
|
2010-04-23 03:17:23 +04:00
|
|
|
|
2010-03-15 23:37:20 +03:00
|
|
|
# We only care about completed transactions.
|
2010-04-23 03:17:23 +04:00
|
|
|
if request.POST.get('payment_status') != 'Completed':
|
2010-03-15 23:37:20 +03:00
|
|
|
return http.HttpResponse('Payment not completed')
|
|
|
|
|
|
|
|
# Make sure transaction has not yet been processed.
|
2010-05-02 10:46:17 +04:00
|
|
|
if (Contribution.objects
|
|
|
|
.filter(transaction_id=request.POST['txn_id']).count()) > 0:
|
2010-03-15 23:37:20 +03:00
|
|
|
return http.HttpResponse('Transaction already processed')
|
|
|
|
|
|
|
|
# Fetch and update the contribution - item_number is the uuid we created.
|
2010-03-25 00:55:09 +03:00
|
|
|
try:
|
2010-05-13 08:12:11 +04:00
|
|
|
c = Contribution.objects.get(uuid=request.POST['item_number'])
|
2010-03-25 00:55:09 +03:00
|
|
|
except Contribution.DoesNotExist:
|
2010-05-02 10:46:17 +04:00
|
|
|
key = "%s%s:%s" % (settings.CACHE_PREFIX, 'contrib',
|
|
|
|
request.POST['item_number'])
|
|
|
|
count = cache.get(key, 0) + 1
|
|
|
|
|
2010-05-11 03:27:13 +04:00
|
|
|
paypal_log.warning('Contribution (uuid=%s) not found for IPN request '
|
|
|
|
'#%s.' % (request.POST['item_number'], count))
|
2010-05-02 10:46:17 +04:00
|
|
|
if count > 10:
|
2010-05-07 10:47:26 +04:00
|
|
|
msg = ("Paypal sent a transaction that we don't know "
|
|
|
|
"about and we're giving up on it.")
|
|
|
|
_log_error_with_data(msg, request)
|
2010-05-02 10:46:17 +04:00
|
|
|
cache.delete(key)
|
|
|
|
return http.HttpResponse('Transaction not found; skipping.')
|
|
|
|
cache.set(key, count, 1209600) # This is 2 weeks.
|
2010-03-25 00:55:09 +03:00
|
|
|
return http.HttpResponseServerError('Contribution not found')
|
|
|
|
|
2010-03-15 23:37:20 +03:00
|
|
|
c.transaction_id = request.POST['txn_id']
|
|
|
|
c.amount = request.POST['mc_gross']
|
|
|
|
c.uuid = None
|
|
|
|
c.post_data = php.serialize(request.POST)
|
|
|
|
c.save()
|
|
|
|
|
|
|
|
# Send thankyou email.
|
|
|
|
try:
|
|
|
|
c.mail_thankyou(request)
|
|
|
|
except ContributionError as e:
|
|
|
|
# A failed thankyou email is not a show stopper, but is good to know.
|
2010-05-11 03:27:13 +04:00
|
|
|
paypal_log.error('Thankyou note email failed with error: %s' % e)
|
2010-03-15 23:37:20 +03:00
|
|
|
|
|
|
|
return http.HttpResponse('Success!')
|
|
|
|
|
|
|
|
|
2010-02-05 04:51:14 +03:00
|
|
|
def handler404(request):
|
|
|
|
return jingo.render(request, 'amo/404.lhtml', status=404)
|
|
|
|
|
|
|
|
|
|
|
|
def handler500(request):
|
2010-02-17 02:55:22 +03:00
|
|
|
return jingo.render(request, 'amo/500.lhtml', status=500)
|