2011-11-22 11:48:36 +04:00
|
|
|
from decimal import Decimal
|
2011-11-16 21:08:51 +04:00
|
|
|
import random
|
2011-11-22 11:48:36 +04:00
|
|
|
import re
|
2011-11-16 21:08:51 +04:00
|
|
|
|
|
|
|
from django import http
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
import commonware.log
|
2012-01-05 03:12:53 +04:00
|
|
|
from django_statsd.clients import statsd
|
2011-11-16 21:08:51 +04:00
|
|
|
import phpserialize as php
|
2012-05-17 03:23:52 +04:00
|
|
|
import requests
|
2012-07-05 01:16:37 +04:00
|
|
|
import waffle
|
2011-11-16 21:08:51 +04:00
|
|
|
|
|
|
|
import amo
|
2013-01-09 02:14:44 +04:00
|
|
|
from amo.decorators import post_required, write
|
2012-07-05 01:16:37 +04:00
|
|
|
from lib.pay_server import client
|
2011-12-20 23:30:38 +04:00
|
|
|
from paypal import paypal_log_cef
|
2011-11-23 04:10:11 +04:00
|
|
|
from stats.db import StatsDictField
|
2012-11-01 05:58:51 +04:00
|
|
|
from stats.models import Contribution, ContributionError
|
2011-11-16 21:08:51 +04:00
|
|
|
|
|
|
|
paypal_log = commonware.log.getLogger('z.paypal')
|
|
|
|
|
|
|
|
|
2011-11-23 03:16:21 +04:00
|
|
|
@write
|
2011-11-16 21:08:51 +04:00
|
|
|
@csrf_exempt
|
2011-12-03 00:13:08 +04:00
|
|
|
@post_required
|
2011-11-16 21:08:51 +04:00
|
|
|
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.
|
|
|
|
|
|
|
|
PayPal IPN variables available at:
|
|
|
|
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content
|
|
|
|
&content_ID=developer/e_howto_html_IPNandPDTVariables
|
|
|
|
"""
|
|
|
|
try:
|
2012-07-05 01:16:37 +04:00
|
|
|
if waffle.flag_is_active(request, 'solitude-payments'):
|
|
|
|
return ipn(request)
|
2011-11-16 21:08:51 +04:00
|
|
|
return _paypal(request)
|
|
|
|
except Exception, e:
|
2012-04-04 03:00:37 +04:00
|
|
|
paypal_log.error('%s\n%s' % (e, request), exc_info=True)
|
2012-05-02 21:30:28 +04:00
|
|
|
if settings.IN_TEST_SUITE:
|
|
|
|
raise
|
2011-11-16 21:08:51 +04:00
|
|
|
return http.HttpResponseServerError('Unknown error.')
|
|
|
|
|
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
def ipn(request):
|
2012-08-23 03:41:48 +04:00
|
|
|
result = client.post_ipn(data={'data': request.read()})
|
2012-07-05 02:49:36 +04:00
|
|
|
paypal_log.info('Solitude IPN returned: %s' % result['status'])
|
2012-07-05 01:16:37 +04:00
|
|
|
|
|
|
|
# PayPal could not verify this result.
|
|
|
|
if result['status'] in ['IGNORED', 'ERROR']:
|
|
|
|
paypal_log.info('Solitude IPN ignored: %s' % result['status'])
|
|
|
|
return http.HttpResponse('Ignored')
|
|
|
|
|
|
|
|
paypal_log.info('Solitude IPN processed: %s,' % result['action'])
|
|
|
|
|
|
|
|
# Process the payment.
|
|
|
|
if result['action'] == 'PAYMENT':
|
|
|
|
return paypal_completed(request, result['uuid'])
|
|
|
|
|
|
|
|
elif result['action'] == 'REFUND':
|
|
|
|
return paypal_refunded(request, result['uuid'],
|
|
|
|
amount=result['amount'])
|
|
|
|
|
|
|
|
elif result['action'] == 'REVERSAL':
|
|
|
|
return paypal_reversal(request, result['uuid'],
|
|
|
|
amount=result['amount'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
# TODO(solitude): alot of this can be removed or refactored.
|
2011-11-16 21:08:51 +04:00
|
|
|
def _log_error_with_data(msg, post):
|
|
|
|
"""Log a message along with some of the POST info from PayPal."""
|
|
|
|
|
|
|
|
id = random.randint(0, 99999999)
|
|
|
|
msg = "[%s] %s (dumping data)" % (id, msg)
|
|
|
|
|
|
|
|
paypal_log.error(msg)
|
|
|
|
|
|
|
|
logme = {'txn_id': post.get('txn_id'),
|
|
|
|
'txn_type': post.get('txn_type'),
|
|
|
|
'payer_email': post.get('payer_email'),
|
|
|
|
'receiver_email': post.get('receiver_email'),
|
|
|
|
'payment_status': post.get('payment_status'),
|
|
|
|
'payment_type': post.get('payment_type'),
|
|
|
|
'mc_gross': post.get('mc_gross'),
|
|
|
|
'item_number': post.get('item_number'),
|
|
|
|
}
|
|
|
|
|
|
|
|
paypal_log.error("[%s] PayPal Data: %s" % (id, logme))
|
|
|
|
|
|
|
|
|
2011-11-22 11:48:36 +04:00
|
|
|
def _log_unmatched(post):
|
|
|
|
key = "%s%s:%s" % (settings.CACHE_PREFIX, 'contrib',
|
|
|
|
post['item_number'])
|
|
|
|
count = cache.get(key, 0) + 1
|
|
|
|
|
|
|
|
paypal_log.warning('Contribution not found: %s, #%s, %s'
|
|
|
|
% (post['item_number'], count,
|
|
|
|
post.get('txn_id', '')))
|
|
|
|
|
|
|
|
if count > 10:
|
|
|
|
msg = ("PayPal sent a transaction that we don't know "
|
|
|
|
"about and we're giving up on it.")
|
|
|
|
_log_error_with_data(msg, post)
|
|
|
|
cache.delete(key)
|
|
|
|
return http.HttpResponse('Transaction not found; skipping.')
|
|
|
|
|
|
|
|
cache.set(key, count, 1209600) # This is 2 weeks.
|
|
|
|
return http.HttpResponseServerError('Contribution not found')
|
|
|
|
|
|
|
|
|
|
|
|
number = re.compile('transaction\[(?P<number>\d+)\]\.(?P<name>\w+)')
|
|
|
|
currency = re.compile('(?P<currency>\w+) (?P<amount>[\d.,]+)')
|
|
|
|
|
|
|
|
|
|
|
|
def _parse(post):
|
|
|
|
"""
|
|
|
|
List of (old, new) codes so we can transpose the data for
|
|
|
|
embedded payments.
|
|
|
|
"""
|
|
|
|
for old, new in [('payment_status', 'status'),
|
|
|
|
('item_number', 'tracking_id'),
|
|
|
|
('txn_id', 'tracking_id'),
|
|
|
|
('payer_email', 'sender_email')]:
|
|
|
|
if old not in post and new in post:
|
|
|
|
post[old] = post[new]
|
|
|
|
|
|
|
|
transactions = {}
|
|
|
|
for k, v in post.items():
|
|
|
|
match = number.match(k)
|
|
|
|
if match:
|
|
|
|
data = match.groupdict()
|
|
|
|
transactions.setdefault(data['number'], {})
|
|
|
|
transactions[data['number']][data['name']] = v
|
|
|
|
|
|
|
|
return post, transactions
|
|
|
|
|
|
|
|
|
2012-07-07 01:58:30 +04:00
|
|
|
def _parse_currency(value):
|
2011-11-22 11:48:36 +04:00
|
|
|
"""Parse USD 10.00 into a dictionary of currency and amount as Decimal."""
|
2012-07-07 01:58:30 +04:00
|
|
|
# If you are using solitude, it's a dictionary. However it returns
|
|
|
|
# amount as a string, not a decimal.
|
|
|
|
if not isinstance(value, dict):
|
|
|
|
value = currency.match(value).groupdict()
|
|
|
|
value['amount'] = Decimal(value['amount'])
|
|
|
|
return value
|
2011-11-22 11:48:36 +04:00
|
|
|
|
|
|
|
|
2011-11-16 21:08:51 +04:00
|
|
|
def _paypal(request):
|
2012-08-23 03:41:48 +04:00
|
|
|
# Must be this way around.
|
|
|
|
post, raw = request.POST.copy(), request.read()
|
2011-11-16 21:08:51 +04:00
|
|
|
paypal_log.info('IPN received: %s' % raw)
|
|
|
|
|
|
|
|
# Check that the request is valid and coming from PayPal.
|
|
|
|
# The order of the params has to match the original request.
|
|
|
|
data = u'cmd=_notify-validate&' + raw
|
|
|
|
with statsd.timer('paypal.validate-ipn'):
|
2012-05-17 03:23:52 +04:00
|
|
|
paypal_response = requests.post(settings.PAYPAL_CGI_URL, data,
|
2012-05-22 02:11:30 +04:00
|
|
|
verify=True,
|
|
|
|
cert=settings.PAYPAL_CERT)
|
2011-11-16 21:08:51 +04:00
|
|
|
|
2011-11-22 11:48:36 +04:00
|
|
|
post, transactions = _parse(post)
|
|
|
|
|
|
|
|
# If paypal doesn't like us, fail.
|
2012-05-17 03:23:52 +04:00
|
|
|
if paypal_response.text != 'VERIFIED':
|
2011-11-16 21:08:51 +04:00
|
|
|
msg = ("Expecting 'VERIFIED' from PayPal, got '%s'. "
|
|
|
|
"Failing." % paypal_response)
|
|
|
|
_log_error_with_data(msg, post)
|
|
|
|
return http.HttpResponseForbidden('Invalid confirmation')
|
|
|
|
|
|
|
|
payment_status = post.get('payment_status', '').lower()
|
2011-11-22 11:48:36 +04:00
|
|
|
if payment_status != 'completed':
|
2012-04-04 03:00:37 +04:00
|
|
|
paypal_log.info('Payment status not completed: %s, %s'
|
|
|
|
% (post.get('txn_id', ''), payment_status))
|
|
|
|
return http.HttpResponse('Ignoring %s' % post.get('txn_id', ''))
|
2011-11-16 21:08:51 +04:00
|
|
|
|
2011-12-02 00:15:23 +04:00
|
|
|
# There could be multiple transactions on the IPN. This will deal
|
|
|
|
# with them appropriately or cope if we don't know how to deal with
|
|
|
|
# any of them.
|
2011-11-22 11:48:36 +04:00
|
|
|
methods = {'refunded': paypal_refunded,
|
|
|
|
'completed': paypal_completed,
|
|
|
|
'reversal': paypal_reversal}
|
|
|
|
result = None
|
2011-12-02 00:15:23 +04:00
|
|
|
called = False
|
2012-04-04 03:00:37 +04:00
|
|
|
# Ensure that we process 0, then 1 etc.
|
|
|
|
for (k, v) in sorted(transactions.items()):
|
|
|
|
status = v.get('status', '').lower()
|
2011-12-02 00:15:23 +04:00
|
|
|
if status not in methods:
|
|
|
|
paypal_log.info('Unknown status: %s' % status)
|
|
|
|
continue
|
2012-07-05 01:16:37 +04:00
|
|
|
result = methods[status](request, post.get('txn_id'),
|
|
|
|
post, v.get('amount'))
|
2011-12-02 00:15:23 +04:00
|
|
|
called = True
|
2012-04-04 03:00:37 +04:00
|
|
|
# Because of chained payments a refund is more than one transaction.
|
|
|
|
# But from our point of view, it's actually only one transaction and
|
|
|
|
# we can safely ignore the rest.
|
|
|
|
if result.content == 'Success!' and status == 'refunded':
|
|
|
|
break
|
2011-12-02 00:15:23 +04:00
|
|
|
|
|
|
|
if not called:
|
|
|
|
# Whilst the payment status was completed, it contained
|
|
|
|
# no transactions with status, which means we don't know
|
|
|
|
# how to process it. Hence it's being ignored.
|
2012-04-04 03:00:37 +04:00
|
|
|
paypal_log.info('No methods to call on: %s' % post.get('txn_id', ''))
|
|
|
|
return http.HttpResponse('Ignoring %s' % post.get('txn_id', ''))
|
2011-11-16 21:08:51 +04:00
|
|
|
|
2011-11-22 11:48:36 +04:00
|
|
|
return result
|
2011-11-16 21:08:51 +04:00
|
|
|
|
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
def paypal_refunded(request, transaction_id, serialize=None, amount=None):
|
2011-11-22 11:48:36 +04:00
|
|
|
try:
|
2012-07-05 01:16:37 +04:00
|
|
|
original = Contribution.objects.get(transaction_id=transaction_id)
|
2011-11-22 11:48:36 +04:00
|
|
|
except Contribution.DoesNotExist:
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log.info('Ignoring transaction: %s' % transaction_id)
|
|
|
|
return http.HttpResponse('Transaction not found; skipping.')
|
2011-11-16 21:08:51 +04:00
|
|
|
|
2011-11-22 11:48:36 +04:00
|
|
|
# If the contribution has a related contribution we've processed it.
|
|
|
|
try:
|
|
|
|
original = Contribution.objects.get(related=original)
|
|
|
|
paypal_log.info('Related contribution, state: %s, pk: %s' %
|
|
|
|
(original.related.type, original.related.pk))
|
2011-11-16 21:08:51 +04:00
|
|
|
return http.HttpResponse('Transaction already processed')
|
2011-11-22 11:48:36 +04:00
|
|
|
except Contribution.DoesNotExist:
|
|
|
|
pass
|
2011-11-17 02:30:02 +04:00
|
|
|
|
2012-04-19 01:16:01 +04:00
|
|
|
original.handle_chargeback('refund')
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log.info('Refund IPN received: %s' % transaction_id)
|
2012-07-25 13:56:39 +04:00
|
|
|
price_currency = _parse_currency(amount)
|
|
|
|
amount = price_currency['amount']
|
|
|
|
currency = price_currency['currency']
|
2011-11-22 21:19:18 +04:00
|
|
|
|
2012-07-25 13:56:39 +04:00
|
|
|
# Contribution with negative amount for refunds.
|
2011-11-22 21:19:18 +04:00
|
|
|
Contribution.objects.create(
|
2011-11-16 21:08:51 +04:00
|
|
|
addon=original.addon, related=original,
|
|
|
|
user=original.user, type=amo.CONTRIB_REFUND,
|
2012-07-25 13:56:39 +04:00
|
|
|
amount=-amount, currency=currency,
|
|
|
|
price_tier=original.price_tier,
|
2012-07-05 01:16:37 +04:00
|
|
|
post_data=php.serialize(serialize)
|
2011-11-22 21:19:18 +04:00
|
|
|
)
|
2011-11-22 11:48:36 +04:00
|
|
|
paypal_log.info('Refund successfully processed')
|
2011-12-20 23:30:38 +04:00
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log_cef(request, original.addon, transaction_id,
|
2011-12-20 23:30:38 +04:00
|
|
|
'Refund', 'REFUND',
|
|
|
|
'A paypal refund was processed')
|
|
|
|
|
2011-11-17 02:30:02 +04:00
|
|
|
return http.HttpResponse('Success!')
|
|
|
|
|
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
def paypal_reversal(request, transaction_id, serialize=None, amount=None):
|
2011-11-22 21:19:18 +04:00
|
|
|
try:
|
2012-07-05 01:16:37 +04:00
|
|
|
original = Contribution.objects.get(transaction_id=transaction_id)
|
2011-11-22 21:19:18 +04:00
|
|
|
except Contribution.DoesNotExist:
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log.info('Ignoring transaction: %s' % transaction_id)
|
|
|
|
return http.HttpResponse('Transaction not found; skipping.')
|
2011-11-22 21:19:18 +04:00
|
|
|
|
2011-12-15 01:57:20 +04:00
|
|
|
# If the contribution has a related contribution we've processed it.
|
|
|
|
try:
|
|
|
|
original = Contribution.objects.get(related=original)
|
|
|
|
paypal_log.info('Related contribution, state: %s, pk: %s' %
|
|
|
|
(original.related.type, original.related.pk))
|
2011-11-17 02:30:02 +04:00
|
|
|
return http.HttpResponse('Transaction already processed')
|
2011-12-15 01:57:20 +04:00
|
|
|
except Contribution.DoesNotExist:
|
|
|
|
pass
|
2011-11-17 02:30:02 +04:00
|
|
|
|
2012-04-19 01:16:01 +04:00
|
|
|
original.handle_chargeback('reversal')
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log.info('Reversal IPN received: %s' % transaction_id)
|
|
|
|
amount = _parse_currency(amount)
|
2011-11-17 02:30:02 +04:00
|
|
|
refund = Contribution.objects.create(
|
|
|
|
addon=original.addon, related=original,
|
|
|
|
user=original.user, type=amo.CONTRIB_CHARGEBACK,
|
2011-11-22 21:19:18 +04:00
|
|
|
amount=-amount['amount'], currency=amount['currency'],
|
2012-07-05 01:16:37 +04:00
|
|
|
post_data=php.serialize(serialize)
|
2011-11-22 21:19:18 +04:00
|
|
|
)
|
2011-11-17 02:30:02 +04:00
|
|
|
refund.mail_chargeback()
|
2011-12-20 23:30:38 +04:00
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log_cef(request, original.addon, transaction_id,
|
2011-12-20 23:30:38 +04:00
|
|
|
'Chargeback', 'CHARGEBACK',
|
|
|
|
'A paypal chargeback was processed')
|
|
|
|
|
2011-11-16 21:08:51 +04:00
|
|
|
return http.HttpResponse('Success!')
|
|
|
|
|
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
def paypal_completed(request, transaction_id, serialize=None, amount=None):
|
2011-12-15 01:57:20 +04:00
|
|
|
# Make sure transaction has not yet been processed.
|
2012-07-05 01:16:37 +04:00
|
|
|
if Contribution.objects.filter(transaction_id=transaction_id).exists():
|
2011-12-15 01:57:20 +04:00
|
|
|
paypal_log.info('Completed IPN already processed')
|
|
|
|
return http.HttpResponse('Transaction already processed')
|
|
|
|
|
2011-11-22 11:48:36 +04:00
|
|
|
# Note that when this completes the uuid is moved over to transaction_id.
|
|
|
|
try:
|
2012-07-05 01:16:37 +04:00
|
|
|
original = Contribution.objects.get(uuid=transaction_id)
|
2011-11-22 11:48:36 +04:00
|
|
|
except Contribution.DoesNotExist:
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log.info('Ignoring transaction: %s' % transaction_id)
|
|
|
|
return http.HttpResponse('Transaction not found; skipping.')
|
2011-11-22 11:48:36 +04:00
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log.info('Completed IPN received: %s' % transaction_id)
|
|
|
|
data = StatsDictField().to_python(php.serialize(serialize))
|
|
|
|
update = {'transaction_id': transaction_id,
|
2011-11-24 22:46:42 +04:00
|
|
|
'uuid': None, 'post_data': data}
|
|
|
|
|
|
|
|
if original.type == amo.CONTRIB_PENDING:
|
|
|
|
# This is a purchase that has failed to hit the completed page.
|
|
|
|
# But this ok, this IPN means that it all went through.
|
|
|
|
update['type'] = amo.CONTRIB_PURCHASE
|
2012-05-22 15:21:44 +04:00
|
|
|
# If they failed to hit the completed page, they also failed
|
|
|
|
# to get it logged properly. This will add the log in.
|
|
|
|
amo.log(amo.LOG.PURCHASE_ADDON, original.addon)
|
2011-11-24 22:46:42 +04:00
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
if amount:
|
|
|
|
update['amount'] = _parse_currency(amount)['amount']
|
2011-11-16 21:08:51 +04:00
|
|
|
|
2011-11-24 22:46:42 +04:00
|
|
|
original.update(**update)
|
2011-11-16 21:08:51 +04:00
|
|
|
# Send thankyou email.
|
|
|
|
try:
|
2011-11-22 11:48:36 +04:00
|
|
|
original.mail_thankyou(request)
|
2011-11-16 21:08:51 +04:00
|
|
|
except ContributionError as e:
|
|
|
|
# A failed thankyou email is not a show stopper, but is good to know.
|
|
|
|
paypal_log.error('Thankyou note email failed with error: %s' % e)
|
2011-11-24 22:46:42 +04:00
|
|
|
|
2012-07-05 01:16:37 +04:00
|
|
|
paypal_log_cef(request, original.addon, transaction_id,
|
2011-12-20 23:30:38 +04:00
|
|
|
'Purchase', 'PURCHASE',
|
|
|
|
'A user purchased or contributed to an addon')
|
2011-11-22 11:48:36 +04:00
|
|
|
paypal_log.info('Completed successfully processed')
|
2011-11-16 21:08:51 +04:00
|
|
|
return http.HttpResponse('Success!')
|