Skip to content

Commit

Permalink
Deal with credit card numbers that include - and spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Jul 20, 2010
1 parent c8a3e88 commit 08acb91
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions paypal/pro/creditcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# Well known card regular expressions.
CARDS = {
'Visa': re.compile(r"^4\d{12}(\d{3})?$"),
'Mastercard': re.compile(r"(5[1-5]\d{4}|677189)\d{10}$"),
'Dinersclub': re.compile(r"^3(0[0-5]|[68]\d)\d{11}"),
'Mastercard': re.compile(r"^(5[1-5]\d{4}|677189)\d{10}$"),
'Dinersclub': re.compile(r"^3(0[0-5]|[68]\d)\d{11}$"),
'Amex': re.compile("^3[47]\d{13}$"),
'Discover': re.compile("^(6011|65\d{2})\d{12}$"),
}
Expand Down
1 change: 1 addition & 0 deletions paypal/pro/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, *args, **kwargs):
def clean(self, value):
"""Raises a ValidationError if the card is not valid and stashes card type."""
if value:
value = value.replace('-', '').replace(' ', '')
self.card_type = verify_credit_card(value)
if self.card_type is None:
raise forms.ValidationError("Invalid credit card number.")
Expand Down
6 changes: 4 additions & 2 deletions paypal/pro/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ class DummyPayPalWPP(PayPalWPP):


class CreditCardFieldTest(TestCase):
def testCreditCardField(self):
def test_CreditCardField(self):
field = CreditCardField()
field.clean('4797503429879309')
self.assertEquals(field.card_type, "Visa")
self.assertRaises(ValidationError, CreditCardField().clean, '1234567890123455')

def test_invalidCreditCards(self):
self.assertEquals(CreditCardField().clean('4797-5034-2987-9309'), '4797503429879309')

class PayPalWPPTest(TestCase):
def setUp(self):
Expand Down Expand Up @@ -92,7 +94,7 @@ def test_doDirectPayment_valid(self):
'ipaddress': '10.0.1.199',}
data.update(self.item)
self.assertTrue(self.wpp.doDirectPayment(data))

def test_doDirectPayment_invalid(self):
data = {
'firstname': 'Epic',
Expand Down

0 comments on commit 08acb91

Please sign in to comment.