Skip to content
This repository was archived by the owner on Jan 25, 2018. It is now read-only.

Commit d1abc92

Browse files
author
Andy McKay
committed
error on differing emails and allow messages to be built on the client (bug 776041)
1 parent 4237042 commit d1abc92

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

lib/paypal/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,12 @@ def _call(self, url, data, errs, auth_token=None):
172172
if 'error(0).errorId' in response:
173173
id_, msg = (response['error(0).errorId'],
174174
response['error(0).message'])
175-
log.error('Paypal Error (%s): %s' % (id_, msg))
176-
raise errs.get(id_, PaypalError)(id=id_, paypal_data=data,
177-
message=msg)
175+
# We want some data to produce a nice error. However
176+
# we do not want to pass everything back since this will go back in
177+
# the REST response and that might leak data.
178+
data = {'currency': data.get('currencyCode')}
179+
log.error('Paypal Error (%s): %s, %s' % (id_, msg, data))
180+
raise errs.get(id_, PaypalError)(id=id_, message=msg, data=data)
178181

179182
return response
180183

lib/paypal/errors.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
class PaypalError(Exception):
55
# The generic Paypal error and message.
6-
def __init__(self, message='', id=None, paypal_data=None):
6+
def __init__(self, message='', id=None, data=None):
77
super(PaypalError, self).__init__(message)
8-
self.id = id
9-
self.paypal_data = paypal_data
8+
self.id = str(id)
9+
self.data = data or {}
1010
self.default = ('There was an error communicating with PayPal. '
1111
'Please try again later.')
1212

@@ -36,12 +36,8 @@ class CurrencyError(PaypalError):
3636
# This currency was bad.
3737

3838
def __str__(self):
39-
if self.paypal_data and 'currencyCode' in self.paypal_data:
40-
try:
41-
return PAYPAL_CURRENCIES[self.paypal_data['currencyCode']]
42-
except:
43-
pass
44-
return 'Unknown currency'
39+
return PAYPAL_CURRENCIES.get(self.data.get('currency', ''),
40+
'Unknown currency')
4541

4642

4743
errors = {'default': {'520003': AuthError}}

lib/paypal/resources/personal.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from cached import Resource
22

33
from lib.paypal.client import Client
4+
from lib.paypal.errors import PaypalError
45
from lib.paypal.forms import GetPersonal
56

67

@@ -13,8 +14,14 @@ def obj_create(self, bundle, request, **kwargs):
1314

1415
paypal = Client()
1516
result = getattr(paypal, self._meta.method)(*form.args())
17+
if 'email' in result:
18+
if form.cleaned_data['seller'].paypal_id != result['email']:
19+
raise PaypalError('The user data did not match',
20+
data={'email': result['email']}, id=100001)
21+
1622
for k, v in result.items():
1723
setattr(form.cleaned_data['seller'], k, v)
24+
1825
form.cleaned_data['seller'].save()
1926
bundle.data = result
2027
bundle.obj = form.cleaned_data['seller']

lib/paypal/tests/test_personal.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from mock import patch
44
from nose.tools import eq_
55

6+
from lib.paypal.errors import PaypalError
67
from lib.paypal.header import escape
78
from lib.sellers.models import Seller, SellerPaypal
89
from solitude.base import APITest
@@ -27,6 +28,15 @@ def test_basic_data(self, result):
2728
obj = SellerPaypal.objects.get(pk=self.paypal.pk)
2829
eq_(obj.first_name, '..')
2930

31+
@patch('lib.paypal.resources.pay.Client.get_personal_basic')
32+
def test_email_differs(self, result):
33+
result.return_value = {'email': 'foo@bar.com'}
34+
res = self.client.post(self.get_list_url('personal-basic'),
35+
data={'seller': self.uid})
36+
err = json.loads(res.content)
37+
eq_(err['error_code'], '100001')
38+
eq_(err['error_data'], {'email': 'foo@bar.com'})
39+
3040
@patch('lib.paypal.resources.pay.Client.get_personal_advanced')
3141
def test_advanced_data(self, result):
3242
result.return_value = {'phone': '..'}

solitude/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def _handle_500(self, request, exception):
133133
extra={'status_code': 500, 'request': request})
134134
data = {
135135
'error_message': str(exception),
136-
'error_code': getattr(exception, 'id', '')
136+
'error_code': getattr(exception, 'id', ''),
137+
'error_data': getattr(exception, 'data', {})
137138
}
138139
serialized = self.serialize(request, data, 'application/json')
139140
return http.HttpApplicationError(content=serialized,

solitude/tests/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ def test_paypal_error(self):
3030
res = self.resource._handle_500(self.request, error)
3131

3232
data = json.loads(res.content)
33-
eq_(data['error_code'], 520003)
33+
eq_(data['error_code'], '520003')
3434
eq_(data['error_message'], 'wat?')

0 commit comments

Comments
 (0)