[Checkins] SVN: zc.authorizedotnet/trunk/src/zc/authorizedotnet/ Added support for line items.

Albertas Agejevas alga at pov.lt
Thu May 3 16:38:43 EDT 2007


Log message for revision 75083:
  Added support for line items.
  

Changed:
  U   zc.authorizedotnet/trunk/src/zc/authorizedotnet/README.txt
  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-03 19:41:49 UTC (rev 75082)
+++ zc.authorizedotnet/trunk/src/zc/authorizedotnet/README.txt	2007-05-03 20:38:42 UTC (rev 75083)
@@ -197,6 +197,23 @@
     'approved'
 
 
+Line items
+----------
+
+An itemized listing of the order can be included in the authorization
+data as a sequcence of sequences.
+
+    >>> result = cc.authorize(amount='2.98', card_num='4007000000027',
+    ...                       exp_date='0530',
+    ...                       line_items=[
+    ...                       # id  name      description qty  unit price tax
+    ...                       ('1', 'G-1000', 'Gadget',   '1', '1.99',    'Y'),
+    ...                       ('2', 'A-150',  'Accessory','1', '0.99',    'Y'),
+    ...                       ])
+    >>> result.response
+    'approved'
+
+
 The MD5 Hash Security Feature
 -----------------------------
 
@@ -238,11 +255,7 @@
         ...
     ValueError: amount must be a string
 
-TODO
-----
 
- - explain and demonstrate duplicate transaction window
-
 NOTES
 -----
 

Modified: zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py
===================================================================
--- zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py	2007-05-03 19:41:49 UTC (rev 75082)
+++ zc.authorizedotnet/trunk/src/zc/authorizedotnet/processing.py	2007-05-03 20:38:42 UTC (rev 75083)
@@ -143,9 +143,7 @@
             # ... turn on test mode (that's the only time that card works)
             kws['test_request'] = 'TRUE'
 
-        fields = dict(('x_'+key, value) for key, value in kws.iteritems())
-        fields.update(self.standard_fields)
-        body = urllib.urlencode(fields)
+        body = self.formatRequest(kws)
 
         if self.server.startswith('localhost:'):
             server, port = self.server.split(':')
@@ -169,7 +167,59 @@
 
         return result
 
+    def formatRequest(self, params):
+        r"""Encode the argument dict into HTTP request form data.
 
+            >>> conn = AuthorizeNetConnection('test.authorize.net',
+            ...                               'login','key')
+            >>> def display(result):
+            ...     print '&\\\n'.join(result.split('&'))
+            >>> display(conn.formatRequest({'card_num': '4007000000027',
+            ...                             'exp_date': '0530'}))
+            x_login=login&\
+            x_method=CC&\
+            x_card_num=4007000000027&\
+            x_tran_key=key&\
+            x_version=3.1&\
+            x_delim_char=%7C&\
+            x_exp_date=0530&\
+            x_relay_response=FALSE&\
+            x_delim_data=TRUE
+
+        The 'line_items' parameter is handled in a special way.  It is
+        expected to be a sequence of sequences.  Each inner sequence
+        represents a line_item parameter.  There can be up to 30 of
+        them in a single transaction.
+
+            >>> display(conn.formatRequest({'line_items': [
+            ...  # item# name       description    qty unitprice taxable
+            ...    ('1', 'MD-1000', 'Main device', '1', '99.95', 'Y'),
+            ...    ('2', 'AC-100', 'Accessory', '2', '14.95', ''),
+            ...    ]}))
+            x_login=login&\
+            x_version=3.1&\
+            x_delim_char=%7C&\
+            x_method=CC&\
+            x_relay_response=FALSE&\
+            x_tran_key=key&\
+            x_delim_data=TRUE&\
+            x_line_item=1%3C%7C%3EMD-1000%3C%7C%3EMain+device%3C%7C%3E1%3C%7C%3E99.95%3C%7C%3EY&\
+            x_line_item=2%3C%7C%3EAC-100%3C%7C%3EAccessory%3C%7C%3E2%3C%7C%3E14.95%3C%7C%3E
+
+        '%3C%7C%3E' is '<|>', the delimiter of line_item fields.
+        """
+
+        line_items = []
+        if 'line_items' in params:
+            line_items = params.pop('line_items')
+        fields = dict(('x_'+key, value) for key, value in params.iteritems())
+        fields.update(self.standard_fields)
+        fields_pairs = fields.items()
+        for item in line_items:
+            fields_pairs.append(('x_line_item', '<|>'.join(item)))
+        return urllib.urlencode(fields_pairs)
+
+
 class CcProcessor(object):
     def __init__(self, server, login, key, salt=None, timeout=None):
         self.connection = AuthorizeNetConnection(



More information about the Checkins mailing list