[Checkins] SVN: zc.authorizedotnet/trunk/src/zc/authorizedotnet/ the method to identify the credit card type is now a module level method as it

Satchidanand Haridas satchit at zope.com
Wed May 23 14:38:01 EDT 2007


Log message for revision 75912:
  the method to identify the credit card type is now a module level method as it
  does not really use the Authorize.net service. Also refactored the code to use
  constants instead of plain strings. Added i18n support as well.
  
  

Changed:
  U   zc.authorizedotnet/trunk/src/zc/authorizedotnet/README.txt
  A   zc.authorizedotnet/trunk/src/zc/authorizedotnet/i18n.py
  U   zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py

-=-
Modified: zc.authorizedotnet/trunk/src/zc/authorizedotnet/README.txt
===================================================================
--- zc.authorizedotnet/trunk/src/zc/authorizedotnet/README.txt	2007-05-23 17:30:16 UTC (rev 75911)
+++ zc.authorizedotnet/trunk/src/zc/authorizedotnet/README.txt	2007-05-23 18:38:00 UTC (rev 75912)
@@ -34,6 +34,8 @@
 key:
 
     >>> from zc.authorizedotnet.processing import CcProcessor
+    >>> from zc.authorizedotnet.processing import (AMEX, DISCOVER, MASTERCARD,
+    ...                                            VISA, UNKNOWN_CARD_TYPE)
     >>> cc = CcProcessor(server=SERVER_NAME, login=LOGIN, key=KEY)
 
 
@@ -60,13 +62,13 @@
 
 When the card_num is sent in, the result also contains the type of credit card:
 
-    >>> result.card_type
-    'Visa'
+    >>> result.card_type == VISA
+    True
 
-If no credit card number is provided, it won't add the card_type field:
+If no credit card number is provided, card_type is None:
 
     >>> result2 = cc.authorize(amount='2.00', exp_date='0530')
-    >>> getattr(result2, 'card_type', None) == None
+    >>> result2.card_type == None
     True
    
 
@@ -225,11 +227,10 @@
     >>> result.response
     'approved'
 
-If the card_num is sent in the input, then the result will have a card_type 
-attribute as well: 
+the result will have a card_type attribute.
 
-    >>> result.card_type
-    'Visa'
+    >>> result.card_type == VISA
+    True
 
 
 The MD5 Hash Security Feature

Added: zc.authorizedotnet/trunk/src/zc/authorizedotnet/i18n.py
===================================================================
--- zc.authorizedotnet/trunk/src/zc/authorizedotnet/i18n.py	                        (rev 0)
+++ zc.authorizedotnet/trunk/src/zc/authorizedotnet/i18n.py	2007-05-23 18:38:00 UTC (rev 75912)
@@ -0,0 +1,18 @@
+"""I18N support.
+
+This defines a `MessageFactory` for the I18N domain for the
+application.  This is normally used with the import::
+
+  from i18n import MessageFactory as _
+
+The factory is then used normally.  Two examples::
+
+  text = _('some internationalized text')
+  text = _('helpful-descriptive-message-id', 'default text')
+"""
+__docformat__ = "reStructuredText"
+
+
+from zope import i18nmessageid
+
+MessageFactory = _ = i18nmessageid.MessageFactory("zc.authorizedotnet")

Modified: zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py
===================================================================
--- zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py	2007-05-23 17:30:16 UTC (rev 75911)
+++ zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py	2007-05-23 18:38:00 UTC (rev 75912)
@@ -16,6 +16,88 @@
 import urllib
 import md5
 
+from zc.authorizedotnet.i18n import _
+
+# CONSTANTS for the supported Card Types
+
+AMEX = _('AMEX')
+DISCOVER = _('Discover')
+MASTERCARD = _('MasterCard')
+VISA = _('Visa')
+UNKNOWN_CARD_TYPE = _('Unknown')
+
+
+def identifyCreditCardType(card_num, card_len):
+    """ Identifies the Credit Card type based on information on the
+    following site(s):
+
+    http://en.wikipedia.org/wiki/Credit_card_number
+    http://www.beachnet.com/~hstiles/cardtype.html
+
+    This checks the prefix (first 4 digits) and the length of the card number to
+    identify the type of the card. This method is used because Authorize.net
+    does not provide this information. This method currently identifies only
+    the following four types:
+
+    1. VISA
+    2. MASTERCARD
+    3. Discover
+    4. AMEX
+
+    Before we test, lets create a few dummy credit-card numbers:
+    
+        >>> amex_card_num = '370000000000002'
+        >>> disc_card_num = '6011000000000012'
+        >>> mc_card_num = '5424000000000015'
+        >>> visa_card_num = '4007000000027'
+        >>> unknown_card_num = '400700000002'
+    
+        >>> identifyCreditCardType(amex_card_num, len(amex_card_num)) == AMEX
+        True
+
+        >>> identifyCreditCardType(disc_card_num, len(disc_card_num)) == DISCOVER
+        True
+
+        >>> identifyCreditCardType(mc_card_num, len(mc_card_num)) == MASTERCARD
+        True
+
+        >>> identifyCreditCardType(visa_card_num, len(visa_card_num)) == VISA
+        True
+
+        >>> identifyCreditCardType(unknown_card_num, len(unknown_card_num)) == UNKNOWN_CARD_TYPE
+        True
+
+    """
+    card_type = UNKNOWN_CARD_TYPE
+    card_1_digit = card_num[0]
+    card_2_digits = card_num[:2]
+    card_4_digits = card_num[:4]
+
+    # AMEX
+    if (card_len == 15) and card_2_digits in ('34', '37'):
+        card_type = AMEX
+
+    # MASTERCARD, DISCOVER & VISA
+    elif card_len == 16:
+        # MASTERCARD
+        if card_2_digits in ('51', '52', '53', '54', '55'):
+            card_type = MASTERCARD
+
+        # DISCOVER
+        elif (card_4_digits == '6011') or (card_2_digits == '65'):
+            card_type = DISCOVER
+
+        # VISA
+        elif (card_1_digit == '4'):
+            card_type = VISA
+
+    # VISA
+    elif (card_len == 13) and (card_1_digit == '4'):
+        card_type = VISA
+
+    return card_type
+    
+
 class HTTPSConnection(httpslib.HTTPSConnection):
     """HTTPS connection with a timeout
 
@@ -161,12 +243,6 @@
         fields = response.read().split(self.delimiter)
         result = TransactionResult(fields)
         
-        # get the card_type
-        card_num = kws.get('card_num')
-        if card_num:
-            card_type = self.identifyCardType(card_num)
-            result.card_type = card_type
-        
         if (self.salt is not None
         and not result.validateHash(self.login, self.salt)):
             raise ValueError('MD5 hash is not valid (trans_id = %r)'
@@ -227,72 +303,6 @@
         return urllib.urlencode(fields_pairs)
 
 
-    def identifyCardType(self, card_num):
-        """ Identifies the Credit Card type based on information on the
-        following site(s):
-
-        http://en.wikipedia.org/wiki/Credit_card_number
-        http://www.beachnet.com/~hstiles/cardtype.html
-
-        This checks for the prefix and the length of the card number to
-        identify the type of the card. This method is used because Authorize.net
-        does not provide this information. This method currently identifies only
-        the following four types:
-
-        1. VISA
-        2. MASTERCARD
-        3. Discover
-        4. AMEX
-
-            >>> conn = AuthorizeNetConnection('test.authorize.net',
-            ...                               'login','key')
-            >>> conn.identifyCardType('370000000000002')
-            'AMEX'
-            
-            >>> conn.identifyCardType('6011000000000012')
-            'Discover'
-        
-            >>> conn.identifyCardType('5424000000000015')
-            'MasterCard'
-        
-            >>> conn.identifyCardType('4007000000027')
-            'Visa'
-
-            >>> conn.identifyCardType('400700000002')
-            'Unknown'
-
-        """
-        card_type = 'Unknown'
-        card_len = len(card_num)
-        card_1_digit = card_num[0]
-        card_2_digits = card_num[:2]
-        card_4_digits = card_num[:4]
-        
-        # AMEX
-        if (card_len == 15) and card_2_digits in ('34', '37'):
-            card_type = 'AMEX'
-
-        # MASTERCARD, DISCOVER & VISA
-        elif card_len == 16:
-            # MASTERCARD
-            if card_2_digits in ('51', '52', '53', '54', '55'):
-                card_type = 'MasterCard'
-
-            # DISCOVER
-            elif (card_4_digits == '6011') or (card_2_digits == '65'):
-                card_type = 'Discover'
-
-            # VISA
-            elif (card_1_digit == '4'):
-                card_type = 'Visa'
-
-        # VISA
-        elif (card_len == 13) and (card_1_digit == '4'):
-            card_type = 'Visa'
-
-        return card_type
-
-            
 class CcProcessor(object):
     def __init__(self, server, login, key, salt=None, timeout=None):
         self.connection = AuthorizeNetConnection(
@@ -303,8 +313,16 @@
             raise ValueError('amount must be a string')
 
         type = 'AUTH_ONLY'
-        return self.connection.sendTransaction(type=type, **kws)
 
+        result = self.connection.sendTransaction(type=type, **kws)
+        # get the card_type
+        card_num = kws.get('card_num')
+        if card_num:
+            card_type = identifyCreditCardType(card_num[:4], len(card_num))
+            result.card_type = card_type
+        
+        return result
+
     def captureAuthorized(self, **kws):
         type = 'PRIOR_AUTH_CAPTURE'
         return self.connection.sendTransaction(type=type, **kws)



More information about the Checkins mailing list