2011-12-24 01:28:32 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-18 03:06:18 +04:00
|
|
|
from datetime import date
|
2012-01-04 22:02:54 +04:00
|
|
|
from decimal import Decimal, InvalidOperation
|
2010-10-11 22:24:41 +04:00
|
|
|
import urllib
|
|
|
|
import urlparse
|
2011-09-29 06:17:24 +04:00
|
|
|
import re
|
2010-10-11 22:24:41 +04:00
|
|
|
|
|
|
|
from django.conf import settings
|
2012-05-25 20:41:26 +04:00
|
|
|
from django.utils.http import urlquote
|
2010-10-11 22:24:41 +04:00
|
|
|
|
2011-01-26 20:06:45 +03:00
|
|
|
import commonware.log
|
2012-01-05 03:12:53 +04:00
|
|
|
from django_statsd.clients import statsd
|
2012-02-23 01:25:06 +04:00
|
|
|
from paypalx.getPermissionsAuthHeader import getAuthHeader as get_auth_header
|
2012-05-17 03:23:52 +04:00
|
|
|
import requests
|
2011-01-26 20:06:45 +03:00
|
|
|
|
2012-02-03 04:57:58 +04:00
|
|
|
import amo
|
2012-05-17 03:23:52 +04:00
|
|
|
from amo.helpers import absolutify, urlparams
|
2011-01-26 20:06:45 +03:00
|
|
|
from amo.urlresolvers import reverse
|
2011-12-20 23:30:38 +04:00
|
|
|
from amo.utils import log_cef
|
2011-12-24 01:28:32 +04:00
|
|
|
from tower import ugettext as _
|
2011-01-26 20:06:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PaypalError(Exception):
|
2011-12-24 01:28:32 +04:00
|
|
|
# The generic Paypal error and message.
|
2012-04-25 21:37:18 +04:00
|
|
|
def __init__(self, message='', id=None, paypal_data=None):
|
2012-02-07 01:23:22 +04:00
|
|
|
super(PaypalError, self).__init__(message)
|
2011-12-24 01:28:32 +04:00
|
|
|
self.id = id
|
2012-04-25 21:37:18 +04:00
|
|
|
self.paypal_data = paypal_data
|
2012-02-06 21:50:41 +04:00
|
|
|
self.default = _('There was an error communicating with PayPal. '
|
|
|
|
'Please try again later.')
|
2011-12-24 01:28:32 +04:00
|
|
|
|
|
|
|
def __str__(self):
|
2012-02-07 01:23:22 +04:00
|
|
|
msg = self.message
|
2012-02-06 21:50:41 +04:00
|
|
|
if not msg:
|
|
|
|
msg = messages.get(self.id, self.default)
|
|
|
|
return msg.encode('utf8') if isinstance(msg, unicode) else msg
|
2011-01-26 20:06:45 +03:00
|
|
|
|
|
|
|
|
2012-01-04 22:02:54 +04:00
|
|
|
class PaypalDataError(PaypalError):
|
|
|
|
# Some of the data passed to Paypal was incorrect. We'll catch them and
|
|
|
|
# re-raise as a PaypalError so they can be easily caught.
|
2012-02-07 01:23:22 +04:00
|
|
|
pass
|
2012-01-04 22:02:54 +04:00
|
|
|
|
2012-01-10 01:40:41 +04:00
|
|
|
|
2011-09-10 03:49:06 +04:00
|
|
|
class AuthError(PaypalError):
|
2011-12-24 01:28:32 +04:00
|
|
|
# We've got the settings wrong on our end.
|
2011-01-26 20:06:45 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-12-16 03:26:30 +04:00
|
|
|
class PreApprovalError(PaypalError):
|
2011-12-24 01:28:32 +04:00
|
|
|
# Something went wrong in pre approval, there's usually not much
|
|
|
|
# we can do about this.
|
2011-12-16 03:26:30 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-01-20 04:30:06 +04:00
|
|
|
class CurrencyError(PaypalError):
|
|
|
|
# This currency was bad.
|
2012-04-25 21:37:18 +04:00
|
|
|
|
|
|
|
def __str__(self):
|
2012-04-25 22:16:34 +04:00
|
|
|
default = _('There was an error with this currency.')
|
2012-04-26 05:29:34 +04:00
|
|
|
if self.paypal_data and 'currencyCode' in self.paypal_data:
|
2012-04-27 00:19:36 +04:00
|
|
|
try:
|
|
|
|
return (messages.get(self.id) %
|
2012-04-25 22:16:34 +04:00
|
|
|
amo.PAYPAL_CURRENCIES[self.paypal_data['currencyCode']])
|
2012-04-27 00:19:36 +04:00
|
|
|
# TODO: figure this out.
|
|
|
|
except:
|
|
|
|
pass
|
2012-04-25 22:16:34 +04:00
|
|
|
return default
|
|
|
|
|
2012-01-20 04:30:06 +04:00
|
|
|
|
2011-01-26 20:06:45 +03:00
|
|
|
errors = {'520003': AuthError}
|
2011-12-16 03:26:30 +04:00
|
|
|
# See http://bit.ly/vWV525 for information on these values.
|
|
|
|
# Note that if you have and invalid preapproval key you get 580022, but this
|
|
|
|
# also occurs in other cases so don't assume its preapproval only.
|
2012-04-17 00:44:57 +04:00
|
|
|
for number in ['569017', '569018', '569019', '569016', '579014', '579024',
|
|
|
|
'579025', '579026', '579027', '579028', '579030', '579031']:
|
2011-12-16 03:26:30 +04:00
|
|
|
errors[number] = PreApprovalError
|
2012-04-25 21:37:18 +04:00
|
|
|
for number in ['559044', '580027', '580022']:
|
2012-01-20 04:30:06 +04:00
|
|
|
errors[number] = CurrencyError
|
2011-12-16 03:26:30 +04:00
|
|
|
|
2011-12-24 01:28:32 +04:00
|
|
|
# Here you can map PayPal error messages into hopefully more useful
|
|
|
|
# error messages.
|
2012-04-12 23:45:50 +04:00
|
|
|
messages = {'589023': _("The amount is too small for conversion "
|
|
|
|
"into the receiver's currency."),
|
|
|
|
'579033': _('The buyer and seller must have different '
|
2012-04-25 21:37:18 +04:00
|
|
|
'PayPal accounts.'),
|
|
|
|
#L10n: {0} is the currency.
|
|
|
|
'559044': _(u'The seller does not accept payments in %s.')}
|
2011-12-24 01:28:32 +04:00
|
|
|
|
2011-01-26 20:06:45 +03:00
|
|
|
paypal_log = commonware.log.getLogger('z.paypal')
|
|
|
|
|
2011-09-29 03:49:05 +04:00
|
|
|
|
2011-09-10 03:49:06 +04:00
|
|
|
def should_ignore_paypal():
|
|
|
|
"""
|
|
|
|
Returns whether to skip PayPal communications for development
|
|
|
|
purposes or not.
|
|
|
|
"""
|
|
|
|
return settings.DEBUG and 'sandbox' not in settings.PAYPAL_PERMISSIONS_URL
|
|
|
|
|
2011-09-07 02:22:24 +04:00
|
|
|
|
2011-12-16 03:26:30 +04:00
|
|
|
def add_receivers(chains, email, amount, uuid, preapproval=False):
|
2011-10-19 22:29:55 +04:00
|
|
|
"""
|
|
|
|
Split a payment down into multiple receivers using the chains passed in.
|
|
|
|
"""
|
2012-01-04 22:02:54 +04:00
|
|
|
try:
|
|
|
|
remainder = Decimal(str(amount))
|
2012-02-06 22:08:16 +04:00
|
|
|
except (UnicodeEncodeError, InvalidOperation), msg:
|
2012-01-04 22:02:54 +04:00
|
|
|
raise PaypalDataError(msg)
|
|
|
|
|
2011-10-19 22:29:55 +04:00
|
|
|
result = {}
|
|
|
|
for number, chain in enumerate(chains, 1):
|
|
|
|
percent, destination = chain
|
2011-10-27 21:36:52 +04:00
|
|
|
this = (Decimal(str(float(amount) * (percent / 100.0)))
|
2011-10-26 00:20:42 +04:00
|
|
|
.quantize(Decimal('.01')))
|
2011-10-19 22:29:55 +04:00
|
|
|
remainder = remainder - this
|
2011-12-16 03:26:30 +04:00
|
|
|
key = 'receiverList.receiver(%s)' % number
|
2011-10-19 22:29:55 +04:00
|
|
|
result.update({
|
2011-12-16 03:26:30 +04:00
|
|
|
'%s.email' % key: destination,
|
|
|
|
'%s.amount' % key: str(this),
|
|
|
|
'%s.primary' % key: 'false',
|
2011-10-27 22:05:39 +04:00
|
|
|
# This is only done if there is a chained payment. Otherwise
|
|
|
|
# it does not need to be set.
|
|
|
|
'receiverList.receiver(0).primary': 'true',
|
2011-12-09 06:19:26 +04:00
|
|
|
# Mozilla pays the fees, because we've got a special rate.
|
|
|
|
'feesPayer': 'SECONDARYONLY'
|
2011-10-19 22:29:55 +04:00
|
|
|
})
|
2011-12-16 03:26:30 +04:00
|
|
|
if not preapproval:
|
|
|
|
result['%s.paymentType' % key] = 'DIGITALGOODS'
|
|
|
|
|
2011-10-19 22:29:55 +04:00
|
|
|
result.update({
|
|
|
|
'receiverList.receiver(0).email': email,
|
2011-10-27 21:36:52 +04:00
|
|
|
'receiverList.receiver(0).amount': str(amount),
|
2011-12-16 03:26:30 +04:00
|
|
|
'receiverList.receiver(0).invoiceID': 'mozilla-%s' % uuid
|
2011-10-19 22:29:55 +04:00
|
|
|
})
|
2011-12-16 03:26:30 +04:00
|
|
|
|
|
|
|
# Adding DIGITALGOODS to a pre-approval triggers an error in PayPal.
|
|
|
|
if not preapproval:
|
|
|
|
result['receiverList.receiver(0).paymentType'] = 'DIGITALGOODS'
|
|
|
|
|
2011-10-19 22:29:55 +04:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2011-01-26 20:06:45 +03:00
|
|
|
def get_paykey(data):
|
|
|
|
"""
|
|
|
|
Gets a paykey from Paypal. Need to pass in the following in data:
|
2011-09-10 01:28:05 +04:00
|
|
|
pattern: the reverse pattern to resolve
|
2011-01-26 20:06:45 +03:00
|
|
|
email: who the money is going to (required)
|
|
|
|
amount: the amount of money (required)
|
|
|
|
ip: ip address of end user (required)
|
|
|
|
uuid: contribution_uuid (required)
|
2012-01-20 04:30:06 +04:00
|
|
|
memo: any nice message (optional)
|
|
|
|
qs: anything you want to append to the complete or cancel (optional)
|
|
|
|
currency: valid paypal currency, defaults to USD (optional)
|
2011-01-26 20:06:45 +03:00
|
|
|
"""
|
2012-02-23 11:02:46 +04:00
|
|
|
if data['pattern']:
|
|
|
|
complete = reverse(data['pattern'], args=[data['slug'], 'complete'])
|
|
|
|
cancel = reverse(data['pattern'], args=[data['slug'], 'cancel'])
|
|
|
|
else:
|
|
|
|
# If there's no pattern given, just fake some urls.
|
2012-02-23 21:42:53 +04:00
|
|
|
complete = cancel = settings.SITE_URL + '/paypal/dummy/'
|
2011-09-30 04:55:50 +04:00
|
|
|
|
|
|
|
qs = {'uuid': data['uuid']}
|
|
|
|
if 'qs' in data:
|
|
|
|
qs.update(data['qs'])
|
|
|
|
uuid_qs = urllib.urlencode(qs)
|
2011-01-26 20:06:45 +03:00
|
|
|
|
|
|
|
paypal_data = {
|
|
|
|
'actionType': 'PAY',
|
2012-01-20 04:30:06 +04:00
|
|
|
'currencyCode': data.get('currency', 'USD'),
|
2011-09-09 21:21:30 +04:00
|
|
|
'cancelUrl': absolutify('%s?%s' % (cancel, uuid_qs)),
|
|
|
|
'returnUrl': absolutify('%s?%s' % (complete, uuid_qs)),
|
2011-10-18 20:38:14 +04:00
|
|
|
'trackingId': data['uuid'],
|
2012-02-23 21:42:53 +04:00
|
|
|
'ipnNotificationUrl': absolutify(reverse('amo.paypal'))}
|
2011-01-26 20:06:45 +03:00
|
|
|
|
2011-12-16 03:26:30 +04:00
|
|
|
receivers = (data.get('chains', ()), data['email'], data['amount'],
|
|
|
|
data['uuid'])
|
|
|
|
|
2011-12-21 00:43:42 +04:00
|
|
|
if data.get('preapproval'):
|
2011-12-16 03:26:30 +04:00
|
|
|
# The paypal_key might be empty if they have removed it.
|
|
|
|
key = data['preapproval'].paypal_key
|
|
|
|
if key:
|
|
|
|
paypal_log.info('Using preapproval: %s' % data['preapproval'].pk)
|
|
|
|
paypal_data['preapprovalKey'] = key
|
2012-01-04 05:53:56 +04:00
|
|
|
|
2012-01-04 22:49:52 +04:00
|
|
|
paypal_data.update(add_receivers(*receivers,
|
|
|
|
preapproval='preapprovalKey' in paypal_data))
|
2011-10-19 22:29:55 +04:00
|
|
|
|
2011-01-26 20:06:45 +03:00
|
|
|
if data.get('memo'):
|
|
|
|
paypal_data['memo'] = data['memo']
|
|
|
|
|
2011-12-16 03:26:30 +04:00
|
|
|
try:
|
|
|
|
with statsd.timer('paypal.paykey.retrieval'):
|
|
|
|
response = _call(settings.PAYPAL_PAY_URL + 'Pay', paypal_data,
|
|
|
|
ip=data['ip'])
|
2012-04-25 22:16:34 +04:00
|
|
|
except (CurrencyError, PreApprovalError), e:
|
|
|
|
# If this is NOT a preapproval, but a currency issue, re-raise.
|
|
|
|
if 'preapprovalKey' not in paypal_data:
|
|
|
|
raise
|
|
|
|
|
2011-12-16 03:26:30 +04:00
|
|
|
# Let's retry just once without preapproval.
|
|
|
|
paypal_log.error('Failed using preapproval, reason: %s' % e)
|
|
|
|
# Now it's not a pre-approval, make sure we get the
|
|
|
|
# DIGITALGOODS setting back in there.
|
2012-04-25 22:16:34 +04:00
|
|
|
|
2011-12-16 03:26:30 +04:00
|
|
|
del paypal_data['preapprovalKey']
|
2012-04-25 22:16:34 +04:00
|
|
|
# Flip the currency to USD.
|
|
|
|
paypal_data['currencyCode'] = 'USD'
|
2011-12-16 03:26:30 +04:00
|
|
|
paypal_data.update(add_receivers(*receivers))
|
|
|
|
# If this fails, we won't try again, just fail.
|
|
|
|
with statsd.timer('paypal.paykey.retrieval'):
|
|
|
|
response = _call(settings.PAYPAL_PAY_URL + 'Pay', paypal_data,
|
|
|
|
ip=data['ip'])
|
|
|
|
|
|
|
|
return response['payKey'], response['paymentExecStatus']
|
2011-09-07 02:22:24 +04:00
|
|
|
|
|
|
|
|
2011-09-10 01:28:05 +04:00
|
|
|
def check_purchase(paykey):
|
|
|
|
"""
|
|
|
|
When a purchase is complete checks paypal that the purchase has gone
|
|
|
|
through.
|
|
|
|
"""
|
|
|
|
with statsd.timer('paypal.payment.details'):
|
|
|
|
try:
|
|
|
|
response = _call(settings.PAYPAL_PAY_URL + 'PaymentDetails',
|
|
|
|
{'payKey': paykey})
|
|
|
|
except PaypalError:
|
|
|
|
paypal_log.error('Payment details error', exc_info=True)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return response['status']
|
|
|
|
|
2011-11-23 02:11:13 +04:00
|
|
|
|
2011-12-01 01:17:58 +04:00
|
|
|
def refund(paykey):
|
2011-09-29 06:17:24 +04:00
|
|
|
"""
|
|
|
|
Refund a payment.
|
|
|
|
|
2012-02-03 04:13:25 +04:00
|
|
|
Arguments: paykey of payment to refund
|
2011-09-29 06:17:24 +04:00
|
|
|
|
|
|
|
Returns: A list of dicts containing the refund info for each
|
|
|
|
receiver of the original payment.
|
|
|
|
"""
|
2012-03-30 03:30:01 +04:00
|
|
|
OK_STATUSES = ['REFUNDED', 'REFUNDED_PENDING',
|
|
|
|
'ALREADY_REVERSED_OR_REFUNDED']
|
2011-09-29 06:17:24 +04:00
|
|
|
with statsd.timer('paypal.payment.refund'):
|
|
|
|
try:
|
|
|
|
response = _call(settings.PAYPAL_PAY_URL + 'Refund',
|
2011-11-23 02:11:13 +04:00
|
|
|
{'payKey': paykey})
|
2011-09-29 06:17:24 +04:00
|
|
|
except PaypalError:
|
|
|
|
paypal_log.error('Refund error', exc_info=True)
|
|
|
|
raise
|
2012-03-30 03:30:01 +04:00
|
|
|
paypal_log.info('Refund response: %s' % response)
|
2011-09-29 06:17:24 +04:00
|
|
|
responses = []
|
|
|
|
for k in response:
|
|
|
|
g = re.match('refundInfoList.refundInfo\((\d+)\).(.*)', k)
|
|
|
|
if g:
|
|
|
|
i = int(g.group(1))
|
|
|
|
subkey = g.group(2)
|
|
|
|
while i >= len(responses):
|
|
|
|
responses.append({})
|
|
|
|
responses[i][subkey] = response[k]
|
2012-03-31 02:12:53 +04:00
|
|
|
clean_responses = []
|
2011-09-29 06:17:24 +04:00
|
|
|
for d in responses:
|
2012-03-31 02:12:53 +04:00
|
|
|
if d['refundStatus'] == 'NOT_PROCESSED':
|
|
|
|
# Probably, some other response failed, so PayPal
|
|
|
|
# ignored this one. We'll leave it out of the list we
|
|
|
|
# return.
|
|
|
|
continue
|
2012-03-28 02:32:38 +04:00
|
|
|
if d['refundStatus'] == 'NO_API_ACCESS_TO_RECEIVER':
|
|
|
|
# The refund didn't succeed, but let's not raise it as
|
|
|
|
# an error, because the caller needs to report this to
|
|
|
|
# the user.
|
2012-03-31 02:12:53 +04:00
|
|
|
clean_responses.append(d)
|
2012-03-28 02:32:38 +04:00
|
|
|
continue
|
2011-09-29 06:17:24 +04:00
|
|
|
if d['refundStatus'] not in OK_STATUSES:
|
2011-10-18 20:38:14 +04:00
|
|
|
raise PaypalError('Bad refund status for %s: %s'
|
|
|
|
% (d['receiver.email'],
|
|
|
|
d['refundStatus']))
|
2011-12-01 01:17:58 +04:00
|
|
|
paypal_log.debug('Refund successful for: %s, %s, %s' %
|
|
|
|
(paykey, d['receiver.email'], d['refundStatus']))
|
2012-03-31 02:12:53 +04:00
|
|
|
clean_responses.append(d)
|
2011-09-29 06:17:24 +04:00
|
|
|
|
2012-03-31 02:12:53 +04:00
|
|
|
return clean_responses
|
2011-09-29 06:17:24 +04:00
|
|
|
|
2011-09-10 01:28:05 +04:00
|
|
|
|
2012-02-03 04:57:58 +04:00
|
|
|
def get_personal_data(token):
|
|
|
|
"""
|
|
|
|
Ask PayPal for personal data based on the token. This makes two API
|
|
|
|
calls to PayPal. It's assumed you've already done the check_permission
|
|
|
|
call below.
|
|
|
|
Documentation: http://bit.ly/xy5BTs and http://bit.ly/yRYbRx
|
|
|
|
"""
|
|
|
|
def call(api, data):
|
|
|
|
try:
|
|
|
|
with statsd.timer('paypal.get.personal'):
|
2012-02-23 01:25:06 +04:00
|
|
|
r = _call(settings.PAYPAL_PERMISSIONS_URL + api, data,
|
|
|
|
token=token)
|
2012-02-03 04:57:58 +04:00
|
|
|
except PaypalError, error:
|
|
|
|
paypal_log.debug('Paypal returned an error when getting personal'
|
|
|
|
'data for token: %s... error: %s'
|
|
|
|
% (token[:10], error))
|
|
|
|
raise
|
|
|
|
return r
|
|
|
|
|
|
|
|
# A mapping fo the api and the values passed to the API.
|
|
|
|
calls = {
|
|
|
|
'GetBasicPersonalData':
|
|
|
|
{'attributeList.attribute':
|
|
|
|
[amo.PAYPAL_PERSONAL[k] for k in
|
2012-02-18 03:06:18 +04:00
|
|
|
['first_name', 'last_name', 'email', 'full_name',
|
2012-02-03 04:57:58 +04:00
|
|
|
'company', 'country', 'payerID']]},
|
|
|
|
'GetAdvancedPersonalData':
|
|
|
|
{'attributeList.attribute':
|
|
|
|
[amo.PAYPAL_PERSONAL[k] for k in
|
2012-05-30 03:45:26 +04:00
|
|
|
['post_code', 'address_one',
|
2012-02-18 03:06:18 +04:00
|
|
|
'address_two', 'city', 'state', 'phone']]}
|
2012-02-03 04:57:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
for url, data in calls.items():
|
|
|
|
data = call(url, data)
|
|
|
|
for k, v in data.items():
|
|
|
|
if k.endswith('personalDataKey'):
|
|
|
|
k_ = k.rsplit('.', 1)[0]
|
|
|
|
v_ = amo.PAYPAL_PERSONAL_LOOKUP[v]
|
|
|
|
# If the value isn't present the value won't be there.
|
|
|
|
result[v_] = data.get(k_ + '.personalDataValue', '')
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def check_permission(token, permissions):
|
2011-09-07 02:22:24 +04:00
|
|
|
"""
|
|
|
|
Asks PayPal whether the PayPal ID for this account has granted
|
2012-02-03 04:57:58 +04:00
|
|
|
the permissions requested to us. Permissions are strings from the
|
|
|
|
PayPal documentation.
|
|
|
|
Documentation: http://bit.ly/zlhXlT
|
|
|
|
|
2011-09-07 02:22:24 +04:00
|
|
|
"""
|
|
|
|
# This is set in settings_test so we don't start calling PayPal
|
|
|
|
# by accident. Explicitly set this in your tests.
|
|
|
|
if not settings.PAYPAL_PERMISSIONS_URL:
|
|
|
|
return False
|
2011-09-29 03:49:05 +04:00
|
|
|
paypal_log.debug('Checking refund permission for token: %s..'
|
|
|
|
% token[:10])
|
2011-09-07 02:22:24 +04:00
|
|
|
try:
|
|
|
|
with statsd.timer('paypal.permissions.refund'):
|
|
|
|
r = _call(settings.PAYPAL_PERMISSIONS_URL + 'GetPermissions',
|
2012-02-23 01:25:06 +04:00
|
|
|
{'token': urlparse.parse_qs(token)['token']})
|
2011-09-29 03:49:05 +04:00
|
|
|
except PaypalError, error:
|
|
|
|
paypal_log.debug('Paypal returned error for token: %s.. error: %s'
|
|
|
|
% (token[:10], error))
|
2011-09-07 02:22:24 +04:00
|
|
|
return False
|
|
|
|
# in the future we may ask for other permissions so let's just
|
|
|
|
# make sure REFUND is one of them.
|
2011-09-29 03:49:05 +04:00
|
|
|
paypal_log.debug('Paypal returned permissions for token: %s.. perms: %s'
|
|
|
|
% (token[:10], r))
|
2012-02-03 04:57:58 +04:00
|
|
|
result = [v for (k, v) in r.iteritems() if k.startswith('scope')]
|
|
|
|
return set(permissions).issubset(set(result))
|
2011-09-07 02:22:24 +04:00
|
|
|
|
|
|
|
|
2012-02-03 04:57:58 +04:00
|
|
|
def get_permission_url(addon, dest, scope):
|
2011-09-07 02:22:24 +04:00
|
|
|
"""
|
2012-02-03 04:57:58 +04:00
|
|
|
Send permissions request to PayPal for privileges on
|
|
|
|
this PayPal account. Returns URL on PayPal site to visit.
|
|
|
|
Documentation: http://bit.ly/zlhXlT
|
2011-09-07 02:22:24 +04:00
|
|
|
"""
|
|
|
|
# This is set in settings_test so we don't start calling PayPal
|
|
|
|
# by accident. Explicitly set this in your tests.
|
|
|
|
if not settings.PAYPAL_PERMISSIONS_URL:
|
|
|
|
return ''
|
2012-02-03 04:57:58 +04:00
|
|
|
|
2011-09-29 21:03:33 +04:00
|
|
|
paypal_log.debug('Getting refund permission URL for addon: %s' % addon.pk)
|
|
|
|
|
2011-09-07 02:22:24 +04:00
|
|
|
with statsd.timer('paypal.permissions.url'):
|
2012-02-18 03:06:18 +04:00
|
|
|
url = urlparams(addon.get_dev_url('acquire_refund_permission'),
|
2011-11-18 03:21:47 +04:00
|
|
|
dest=dest)
|
2011-09-29 21:03:33 +04:00
|
|
|
try:
|
|
|
|
r = _call(settings.PAYPAL_PERMISSIONS_URL + 'RequestPermissions',
|
2012-02-03 04:57:58 +04:00
|
|
|
{'scope': scope, 'callback': absolutify(url)})
|
2011-09-29 21:03:33 +04:00
|
|
|
except PaypalError, e:
|
|
|
|
paypal_log.debug('Error on refund permission URL addon: %s, %s' %
|
|
|
|
(addon.pk, e))
|
2012-01-07 00:20:00 +04:00
|
|
|
if e.id == '580028':
|
2011-09-29 21:03:33 +04:00
|
|
|
# PayPal is very picky about where they redirect users to.
|
|
|
|
# If you try and create a PayPal permissions URL on a
|
|
|
|
# zamboni that has a non-standard port number or a
|
|
|
|
# non-standard TLD, it will blow up with an error. We need
|
|
|
|
# to be able to at least visit these pages and alter them
|
|
|
|
# in dev, so this will give you a broken token that doesn't
|
|
|
|
# work, but at least the page will function.
|
|
|
|
r = {'token': 'wont-work-paypal-doesnt-like-your-domain'}
|
|
|
|
else:
|
|
|
|
raise
|
2011-09-07 02:22:24 +04:00
|
|
|
return (settings.PAYPAL_CGI_URL +
|
|
|
|
'?cmd=_grant-permission&request_token=%s' % r['token'])
|
|
|
|
|
|
|
|
|
|
|
|
def get_permissions_token(request_token, verification_code):
|
|
|
|
"""
|
|
|
|
Send request for permissions token, after user has granted the
|
|
|
|
requested permissions via the PayPal page we redirected them to.
|
|
|
|
"""
|
|
|
|
with statsd.timer('paypal.permissions.token'):
|
|
|
|
r = _call(settings.PAYPAL_PERMISSIONS_URL + 'GetAccessToken',
|
|
|
|
{'token': request_token, 'verifier': verification_code})
|
2012-02-23 01:25:06 +04:00
|
|
|
return urllib.urlencode({'token': r['token'], 'secret': r['tokenSecret']})
|
2011-09-07 02:22:24 +04:00
|
|
|
|
|
|
|
|
2011-12-13 02:27:46 +04:00
|
|
|
def get_preapproval_key(data):
|
|
|
|
"""
|
|
|
|
Get a preapproval key from PayPal. If this passes, you get a key that
|
|
|
|
you can use in a redirect to PayPal.
|
|
|
|
"""
|
|
|
|
paypal_data = {
|
|
|
|
'currencyCode': 'USD',
|
|
|
|
'startingDate': data['startDate'].strftime('%Y-%m-%d'),
|
|
|
|
'endingDate': data['endDate'].strftime('%Y-%m-%d'),
|
|
|
|
'maxTotalAmountOfAllPayments': str(data.get('maxAmount', '2000')),
|
|
|
|
'returnUrl': absolutify(reverse(data['pattern'], args=['complete'])),
|
|
|
|
'cancelUrl': absolutify(reverse(data['pattern'], args=['cancel'])),
|
|
|
|
}
|
2012-06-28 03:09:54 +04:00
|
|
|
if settings.PAYPAL_LIMIT_PREAPPROVAL:
|
|
|
|
paypal_data.update({
|
|
|
|
'maxAmountPerPayment': 15,
|
|
|
|
'maxNumberOfPaymentsPerPeriod': 15,
|
|
|
|
'paymentPeriod': 'DAILY',
|
|
|
|
})
|
|
|
|
|
2011-12-13 02:27:46 +04:00
|
|
|
with statsd.timer('paypal.preapproval.token'):
|
|
|
|
response = _call(settings.PAYPAL_PAY_URL + 'Preapproval', paypal_data,
|
|
|
|
ip=data.get('ip'))
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def get_preapproval_url(key):
|
|
|
|
"""
|
|
|
|
Returns the URL that you need to bounce user to in order to set up
|
|
|
|
pre-approval.
|
|
|
|
"""
|
|
|
|
return urlparams(settings.PAYPAL_CGI_URL, cmd='_ap-preapproval',
|
|
|
|
preapprovalkey=key)
|
|
|
|
|
|
|
|
|
2012-02-03 04:57:58 +04:00
|
|
|
def _nvp_dump(data):
|
|
|
|
"""
|
|
|
|
Dumps a dict out into NVP pairs suitable for PayPal to consume.
|
|
|
|
"""
|
|
|
|
out = []
|
|
|
|
escape = lambda k, v: '%s=%s' % (k, urlquote(v))
|
|
|
|
# This must be sorted for chained payments to work correctly.
|
|
|
|
for k, v in sorted(data.items()):
|
|
|
|
if isinstance(v, (list, tuple)):
|
|
|
|
out.extend([escape('%s(%s)' % (k, x), v_)
|
|
|
|
for x, v_ in enumerate(v)])
|
|
|
|
else:
|
|
|
|
out.append(escape(k, v))
|
|
|
|
|
|
|
|
return '&'.join(out)
|
|
|
|
|
|
|
|
|
2012-02-23 01:25:06 +04:00
|
|
|
def _call(url, paypal_data, ip=None, token=None):
|
2012-05-17 03:23:52 +04:00
|
|
|
headers = {}
|
2012-02-23 01:25:06 +04:00
|
|
|
auth = settings.PAYPAL_EMBEDDED_AUTH
|
2011-09-07 02:22:24 +04:00
|
|
|
if 'requestEnvelope.errorLanguage' not in paypal_data:
|
|
|
|
paypal_data['requestEnvelope.errorLanguage'] = 'en_US'
|
|
|
|
|
2012-02-23 01:25:06 +04:00
|
|
|
# We always need these headers.
|
2011-09-07 02:22:24 +04:00
|
|
|
for key, value in [
|
|
|
|
('application-id', settings.PAYPAL_APP_ID),
|
|
|
|
('request-data-format', 'NV'),
|
|
|
|
('response-data-format', 'NV')]:
|
2012-05-17 03:23:52 +04:00
|
|
|
headers['X-PAYPAL-%s' % key.upper()] = value
|
2011-09-07 02:22:24 +04:00
|
|
|
|
2012-02-23 01:25:06 +04:00
|
|
|
# If we've got a token, we need to auth using the token which uses the
|
|
|
|
# paypalx lib. This is primarily for the GetDetails API.
|
|
|
|
if token:
|
|
|
|
token = dict(urlparse.parse_qsl(token))
|
|
|
|
ts, sig = get_auth_header(auth['USER'], auth['PASSWORD'],
|
|
|
|
token['token'], token['secret'],
|
|
|
|
'POST', url)
|
2012-05-17 03:23:52 +04:00
|
|
|
headers['X-PAYPAL-AUTHORIZATION'] = ('timestamp=%s,token=%s,'
|
|
|
|
'signature=%s' %
|
|
|
|
(ts, token['token'], sig))
|
2012-02-23 01:25:06 +04:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Otherwise, we just authenticate the normal way.
|
|
|
|
for key, value in [
|
|
|
|
('application-id', settings.PAYPAL_APP_ID),
|
|
|
|
('security-userid', auth['USER']),
|
|
|
|
('security-password', auth['PASSWORD']),
|
|
|
|
('security-signature', auth['SIGNATURE'])]:
|
2012-05-17 03:23:52 +04:00
|
|
|
headers['X-PAYPAL-%s' % key.upper()] = value
|
2012-02-23 01:25:06 +04:00
|
|
|
|
2011-09-07 02:22:24 +04:00
|
|
|
if ip:
|
2012-05-17 03:23:52 +04:00
|
|
|
headers['X-PAYPAL-DEVICE-IPADDRESS'] = ip
|
2011-09-07 02:22:24 +04:00
|
|
|
|
2011-10-19 22:29:55 +04:00
|
|
|
# Warning, a urlencode will not work with chained payments, it must
|
|
|
|
# be sorted and the key should not be escaped.
|
2011-01-26 20:06:45 +03:00
|
|
|
try:
|
2012-05-17 03:23:52 +04:00
|
|
|
data = _nvp_dump(paypal_data)
|
|
|
|
feeddata = requests.post(url, headers=headers, timeout=10,
|
|
|
|
data=data,
|
|
|
|
verify=True,
|
|
|
|
cert=settings.PAYPAL_CERT)
|
2011-12-08 04:25:47 +04:00
|
|
|
except AuthError, error:
|
|
|
|
paypal_log.error('Authentication error: %s' % error)
|
|
|
|
raise
|
2011-01-26 20:06:45 +03:00
|
|
|
except Exception, error:
|
|
|
|
paypal_log.error('HTTP Error: %s' % error)
|
2011-12-24 01:28:32 +04:00
|
|
|
# We'll log the actual error and then raise a Paypal error.
|
|
|
|
# That way all the calling methods only have catch a Paypal error,
|
|
|
|
# the fact that there may be say, a http error, is internal to this
|
|
|
|
# method.
|
|
|
|
raise PaypalError
|
2011-01-26 20:06:45 +03:00
|
|
|
|
2012-05-17 03:23:52 +04:00
|
|
|
response = dict(urlparse.parse_qsl(feeddata.text))
|
2011-01-26 20:06:45 +03:00
|
|
|
|
|
|
|
if 'error(0).errorId' in response:
|
2011-12-24 01:28:32 +04:00
|
|
|
id_, msg = response['error(0).errorId'], response['error(0).message']
|
|
|
|
paypal_log.error('Paypal Error (%s): %s' % (id_, msg))
|
2012-04-25 21:37:18 +04:00
|
|
|
raise errors.get(id_, PaypalError)(id=id_, paypal_data=paypal_data)
|
2011-01-26 20:06:45 +03:00
|
|
|
|
2011-09-07 02:22:24 +04:00
|
|
|
return response
|
2011-01-26 20:06:45 +03:00
|
|
|
|
2010-10-11 22:24:41 +04:00
|
|
|
|
|
|
|
def check_paypal_id(name):
|
|
|
|
"""
|
|
|
|
Use the button API to check if name is a valid Paypal id.
|
|
|
|
|
|
|
|
Returns bool(valid), str(msg). msg will be None if there wasn't an error.
|
|
|
|
"""
|
2011-03-21 22:58:16 +03:00
|
|
|
d = dict(version=settings.PAYPAL_API_VERSION,
|
2010-10-11 22:24:41 +04:00
|
|
|
buttoncode='cleartext',
|
|
|
|
buttontype='donate',
|
|
|
|
method='BMCreateButton',
|
|
|
|
l_buttonvar0='business=%s' % name)
|
2011-12-23 22:06:06 +04:00
|
|
|
d['user'] = settings.PAYPAL_EMBEDDED_AUTH['USER']
|
|
|
|
d['pwd'] = settings.PAYPAL_EMBEDDED_AUTH['PASSWORD']
|
|
|
|
d['signature'] = settings.PAYPAL_EMBEDDED_AUTH['SIGNATURE']
|
2012-05-25 20:41:26 +04:00
|
|
|
r = requests.get(settings.PAYPAL_API_URL, params=d, timeout=10)
|
|
|
|
response = dict(urlparse.parse_qsl(r.text))
|
2010-10-11 22:24:41 +04:00
|
|
|
valid = response['ACK'] == 'Success'
|
|
|
|
msg = None if valid else response['L_LONGMESSAGE0']
|
|
|
|
return valid, msg
|
|
|
|
|
|
|
|
|
2011-12-20 23:30:38 +04:00
|
|
|
def paypal_log_cef(request, addon, uuid, msg, caps, longer):
|
|
|
|
log_cef('Paypal %s' % msg, 5, request,
|
|
|
|
username=request.amo_user,
|
|
|
|
signature='PAYPAL%s' % caps,
|
|
|
|
msg=longer, cs2=addon.name, cs2Label='PaypalTransaction',
|
|
|
|
cs4=uuid, cs4Label='TxID')
|