Skip to content

Commit

Permalink
Issue 19: Adding x_customer_ip field for CIM and AIM transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
vcatalano committed Apr 7, 2014
1 parent 89d00f6 commit d89b636
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Here is a simple example of a basic credit card transaction.
Documentation
-------------

Please visit the `Github Page`_ page for full documentation.
Please visit the `Github Page`_ for full documentation.

.. _Github Page: http://vcatalano.github.io/py-authorize/index.html

Expand Down
12 changes: 12 additions & 0 deletions authorize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import xml.etree.ElementTree as E

from authorize.configuration import Configuration
from authorize.address import Address
from authorize.bank_account import BankAccount
Expand All @@ -11,3 +13,13 @@
from authorize.exceptions import AuthorizeInvalidError
from authorize.recurring import Recurring
from authorize.transaction import Transaction


# Monkeypatch the ElementTree module so that we can use CDATA element types
E._original_serialize_xml = E._serialize_xml
def _serialize_xml(write, elem, *args):
if elem.tag == '![CDATA[':
write('<![CDATA[%s]]>' % elem.text)
return
return E._original_serialize_xml(write, elem, *args)
E._serialize_xml = E._serialize['xml'] = _serialize_xml
16 changes: 16 additions & 0 deletions authorize/apis/transaction_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import xml.etree.cElementTree as E

try:
import urllib.parse as urllib
except:
import urllib

from authorize.apis.base_api import BaseAPI
from authorize.schemas import AIMTransactionSchema
from authorize.schemas import CIMTransactionSchema
Expand Down Expand Up @@ -88,6 +93,13 @@ def _cim_base_request(self, xact_type, xact={}):
if 'recurring' in xact:
E.SubElement(xact_type, 'recurringBilling').text = str(xact['recurring']).lower()

if 'extra_options' in xact:
extra_options = {}
if 'customer_ip' in xact['extra_options']:
extra_options['x_customer_ip'] = xact['extra_options']['customer_ip']
options = E.SubElement(request, 'extraOptions')
E.SubElement(options, '![CDATA[').text = urllib.urlencode(extra_options)

return request

def _aim_base_request(self, xact_type, xact={}):
Expand Down Expand Up @@ -131,6 +143,10 @@ def _aim_base_request(self, xact_type, xact={}):
if 'shipping' in xact:
xact_elem.append(create_address('shipTo', xact['shipping']))

if 'extra_options' in xact:
if 'customer_ip' in xact['extra_options']:
E.SubElement(xact_elem, 'customerIP').text = xact['extra_options']['customer_ip']

return request

def _settle_request(self, transaction_id):
Expand Down
10 changes: 7 additions & 3 deletions authorize/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ class CustomerSchema(AddressSchema):
customer_id = colander.SchemaNode(colander.String(),
validator=colander.Length(max=20),
missing=colander.drop)
# customer_ip = colander.SchemaNode(colander.String(),
# validator=colander.Length(max=20),
# missing=colander.drop)


class CreateCustomerSchema(CustomerBaseSchema, CustomerTypeSchema):
Expand Down Expand Up @@ -228,6 +225,12 @@ class OrderSchema(colander.MappingSchema):
missing=colander.drop)


class ExtraOptions(colander.MappingSchema):
customer_ip = colander.SchemaNode(colander.String(),
validator=colander.Length(max=39),
missing=colander.drop)


class TransactionBaseSchema(colander.MappingSchema):
line_items = LineItemsSchema(validator=colander.Length(max=30),
missing=colander.drop)
Expand Down Expand Up @@ -262,6 +265,7 @@ class CIMTransactionSchema(CIMBaseSchema, TransactionBaseSchema):
validator=colander.Regex(
r'^[0-9]{3,4}$', 'The card code is invalid'),
missing=colander.drop)
extra_options = ExtraOptions(missing=colander.drop)


class AIMTransactionSchema(TransactionBaseSchema):
Expand Down
2 changes: 1 addition & 1 deletion authorize/xml_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ def prettify(elem):
"""Return a pretty-printed XML string for the Element."""
rough_string = E.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ").strip()
return reparsed.toprettyxml(indent=' ').strip()
14 changes: 13 additions & 1 deletion docs/transaction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ Full Example
'name': 'UPS 2-Day Shipping',
'description': 'Handle with care',
},
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,
'recurring': True,
})
result.transaction_response.trans_id
Expand Down Expand Up @@ -228,7 +232,11 @@ Full Transactions with Bank Accounts
'name': 'UPS 2-Day Shipping',
'description': 'Handle with care',
},
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,
'recurring': True,
})
result.transaction_response.trans_id
Expand Down Expand Up @@ -309,7 +317,11 @@ Full Transactions Example with CIM Data
'name': 'UPS 2-Day Shipping',
'description': 'Handle with care',
},
'tax_exempt': False,
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,,
'recurring': True,
})
result.transaction_response.trans_id
Expand Down
9 changes: 9 additions & 0 deletions tests/test_live_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
'name': 'UPS 2-Day Shipping',
'description': 'Handle with care',
},
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,
'recurring': True,
}
Expand Down Expand Up @@ -135,6 +138,9 @@
'name': 'The amount for duty',
'description': 'I can''t believe you would pay for duty',
},
'extra_options': {
'customer_ip': 'fe80::f4b6:2a88:70fa:f09f',
},
'tax_exempt': False,
'recurring': True,
'card_code': '443',
Expand Down Expand Up @@ -214,6 +220,9 @@
'name': 'UPS 2-Day Shipping',
'description': 'Handle with care',
},
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,
'recurring': True,
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_transaction_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
'name': 'The amount for duty',
'description': 'I can''t believe you would pay for duty',
},
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,
'recurring': True,
'card_code': '443',
Expand Down Expand Up @@ -135,6 +138,9 @@
'name': 'UPS 2-Day Shipping',
'description': 'Handle with care',
},
'extra_options': {
'customer_ip': '100.0.0.1',
},
'tax_exempt': False,
'recurring': True,
}
Expand Down Expand Up @@ -206,6 +212,8 @@
<recurringBilling>true</recurringBilling>
</profileTransAuthCapture>
</transaction>
<extraOptions>
<![CDATA[x_customer_ip=100.0.0.1]]> </extraOptions>
</createCustomerProfileTransactionRequest>
'''

Expand Down Expand Up @@ -299,6 +307,7 @@
<phoneNumber>520-123-4567</phoneNumber>
<faxNumber>520-456-7890</faxNumber>
</shipTo>
<customerIP>100.0.0.1</customerIP>
</transactionRequest>
</createTransactionRequest>
'''
Expand Down

0 comments on commit d89b636

Please sign in to comment.