Skip to content

Commit adbe07d

Browse files
author
brianmc
committed
merged changes
2 parents 4f7e9ef + 71dce4b commit adbe07d

File tree

6 files changed

+73
-33
lines changed

6 files changed

+73
-33
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Installations
1111
Run the following to get pyxb and nosetests:
1212
- pip install pyxb
1313
- pip install nosetests
14+
- pip install Magicmock
1415

1516
Testing
1617
--------------------------------------
@@ -19,12 +20,16 @@ Testing
1920

2021
How to Use
2122
--------------------------------------
23+
You need to set your credentials.
24+
Refer to template given in anet_python_sdk_properties.ini
25+
Either copy it to your root directory or make a new one similar to this. If you create one name file anet_python_sdk_properties.ini
26+
2227
The following is a sample which shows how to create a transaction request
2328
and execute it using the create transaction controller.
2429

25-
from contract import binding
30+
from authorizenet import apicontractsv1
2631
from decimal import *
27-
from controller.CreateTransactionController import CreateTransactionController
32+
from authorizenet.apicontrollers import CreateTransactionController
2833

2934
class paymentTransaction(object):
3035

properties.ini renamed to anet_python_sdk_properties_template.ini

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#sandbox cradentials
44
api.login.id : 6rF76h2U93AL
5-
transaction.key : 59j3B2Kq7r8Fyg76
5+
transaction.key : 7jh86zEUhy7228FG
66
md5.hash.key : MD5_HASH_KEY
77

88
#log
@@ -12,6 +12,4 @@ logfilename : logFile.log
1212
#http://proxy.yourcompany.com:80
1313
#https://proxy.yourcompany.com:443
1414

15-
#environments
16-
sandbox : https://apitest.authorize.net/xml/v1/request.api
17-
production : https://api2.authorize.net/xml/v1/request.api
15+

authorizenet/apicontrollersbase.py

+39-24
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
'''
2-
Created on Jul 6, 2015
2+
Created on Nov 1, 2015
33
4-
@author: egodolja
4+
@author: krgupta
55
'''
66
import abc
77
import logging
88
import os
9+
from os.path import expanduser
10+
911
import sys
1012
import xml.dom.minidom
1113

1214
from pip._vendor import requests
1315
from _pyio import __metaclass__
1416
from ConfigParser import SafeConfigParser
17+
import ConfigParser
1518

1619
from authorizenet.constants import constants
1720
from authorizenet import apicontractsv1
@@ -34,27 +37,27 @@ def getresponseclass(self):
3437
pass
3538

3639
@abc.abstractmethod
37-
def getResponse(self):
40+
def getresponse(self):
3841
''' Returns the de-serialized response'''
3942
pass
4043

4144
@abc.abstractmethod
42-
def getResultCode(self):
45+
def getresultcode(self):
4346
''' Returns the result code from the response '''
4447
pass
4548

4649
@abc.abstractmethod
47-
def getMessageType(self):
50+
def getmessagetype(self):
4851
''' Returns the message type enum from the response '''
4952
pass
5053

5154
@abc.abstractmethod
52-
def afterExecute(self):
53-
'''TODO'''
55+
def afterexecute(self):
56+
'''Returns the message received from binding after processing request'''
5457
pass
5558

5659
@abc.abstractmethod
57-
def beforeExecute(self):
60+
def beforeexecute(self):
5861
'''TODO'''
5962
pass
6063

@@ -63,16 +66,28 @@ class APIOperationBase(APIOperationBaseInterface):
6366

6467
parser = SafeConfigParser({"http":"","https":"","ftp":""})
6568

69+
6670
try:
67-
#if #TODO
68-
parser.read(os.path.dirname(__file__) + "/../properties.ini")
71+
#if #TODO
72+
home = os.path.expanduser("~")
73+
homedirpropertiesfilename = os.path.join(home, "anet_python_sdk_properties.ini")
74+
75+
currdir = os.getcwd()
76+
currdirpropertiesfilename = os.path.join(currdir, "anet_python_sdk_properties.ini")
77+
78+
if (os.path.exists(homedirpropertiesfilename)):
79+
parser.read(homedirpropertiesfilename)
80+
elif (os.path.exists(currdirpropertiesfilename)):
81+
parser.read(currdirpropertiesfilename)
82+
else :
83+
print "you do not have anet_python_sdk_properties.ini file neither in home nor in current working directory"
6984
except IOError, error:
7085
sys.exit( error)
7186
else:
7287
logFile = parser.get("properties", "logfilename")
7388
#TODO format and level in config file
7489
logging.basicConfig(filename=logFile, level=logging.DEBUG, format='%(asctime)s %(message)s')
75-
endpoint = parser.get("properties", "sandbox")
90+
endpoint = constants.SANDBOX_TESTMODE
7691

7792
@abc.abstractmethod
7893
def validaterequest(self):
@@ -94,10 +109,10 @@ def validate(self):
94109
self.validaterequest()
95110
return
96111

97-
def _getRequest(self): #protected method
112+
def _getrequest(self): #protected method
98113
return self._request
99114

100-
def buildRequest(self):
115+
def buildrequest(self):
101116
logging.debug('building request..')
102117
#TODO requestType = type( self._request)
103118
requestType = self._requestType
@@ -109,8 +124,8 @@ def buildRequest(self):
109124

110125
return xmlRequest
111126

112-
def getPrettyXmlRequest(self):
113-
xmlRequest = self.buildRequest()
127+
def getprettyxmlrequest(self):
128+
xmlRequest = self.buildrequest()
114129
requestDom = xml.dom.minidom.parseString(xmlRequest)
115130
logging.debug('Request is: %s' % requestDom.toprettyxml())
116131

@@ -119,26 +134,26 @@ def getPrettyXmlRequest(self):
119134
def execute(self):
120135
logging.debug('Executing http post to url: %s', self.endpoint)
121136

122-
self.beforeExecute()
137+
self.beforeexecute()
123138

124139
proxyDictionary = {'http' : self.parser.get("properties", "http"),
125140
'https' : self.parser.get("properties" , "https"),
126141
'ftp' : self.parser.get("properties", "ftp")}
127142

128143
#requests is http request
129144
try:
130-
xmlRequest = self.buildRequest()
145+
xmlRequest = self.buildrequest()
131146
self._httpResponse = requests.post(self.endpoint, data=xmlRequest, headers=constants.headers, proxies=proxyDictionary)
132147
except Exception as httpException:
133-
logging.error( 'Error retrieving http response from: %s for request: %s', self.endpoint, self.getPrettyXmlRequest())
148+
logging.error( 'Error retrieving http response from: %s for request: %s', self.endpoint, self.getprettyxmlrequest())
134149
logging.error( 'Exception: %s, %s', type(httpException), httpException.args )
135150

136151

137152
if self._httpResponse:
138153
#encoding of response should be changed to retrieve text of response
139154
self._httpResponse.encoding = constants.response_encoding
140155
self._httpResponse = self._httpResponse.text[3:] #strip BOM
141-
self.afterExecute()
156+
self.afterexecute()
142157
try:
143158
self._response = apicontractsv1.CreateFromDocument(self._httpResponse)
144159
except Exception as createfromdocumentexception:
@@ -156,21 +171,21 @@ def execute(self):
156171
else:
157172
print "Did not receive http response"
158173

159-
def getResponse(self):
174+
def getresponse(self):
160175
return self._response
161176

162-
def getResultCode(self):
177+
def getresultcode(self):
163178
if self._response:
164179
return self._response.resultCode
165180

166-
def getMessageType(self):
181+
def getmessagetype(self):
167182
if self._response:
168183
return self._response.message
169184

170-
def afterExecute(self ):
185+
def afterexecute(self ):
171186
return
172187

173-
def beforeExecute(self):
188+
def beforeexecute(self):
174189
return
175190

176191
def __init__(self, apiRequest, requestType):

authorizenet/constants.py

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
@author: egodolja
55
'''
66
class constants(object):
7+
"""All the constants are defined here
8+
Define all your constants instead of using magic numbers in the
9+
code.
10+
"""
711

812
'''Environments'''
913
SANDBOX_TESTMODE = 'https://apitest.authorize.net/xml/v1/request.api'

setup.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""
33

44
# Always prefer setuptools over distutils
5-
from setuptools import setup, find_packages
5+
from setuptools import setup
6+
from setuptools import find_packages
67
# To use a consistent encoding
78
from codecs import open
89
from os import path
@@ -75,6 +76,11 @@
7576
#'dev': ['check-manifest'],
7677
#'test': ['coverage'],
7778
},
79+
environment_variables={
80+
#api.login.id : xyz
81+
#transaction.key : xyz
82+
#md5.hash.key : MD5_HASH_KEY
83+
},
7884

7985
# If there are data files included in your packages that need to be
8086
# installed, specify them here. If using Python 2.6 or less, then these

tests/apitestbase.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ class ApiTestBase(unittest.TestCase):
1919
def setUp(self):
2020
self.amount = str(round(random.random()*100, 2))
2121
parser = SafeConfigParser()
22-
parser.read(os.path.dirname(__file__)+ "/../properties.ini")
22+
home = os.path.expanduser("~")
23+
homedirpropertiesfilename = os.path.join(home, "anet_python_sdk_properties.ini")
24+
25+
currdir = os.getcwd()
26+
currdirpropertiesfilename = os.path.join(currdir, "anet_python_sdk_properties.ini")
27+
28+
if (os.path.exists(homedirpropertiesfilename)):
29+
parser.read(homedirpropertiesfilename)
30+
elif (os.path.exists(currdirpropertiesfilename)):
31+
parser.read(currdirpropertiesfilename)
32+
else :
33+
print "you do not have anet_python_sdk_properties.ini file neither in home nor in current working directory"
2334

2435
self.api_login_id = parser.get("properties", "api.login.id")
2536
self.transaction_key = parser.get("properties", "transaction.key")
@@ -73,5 +84,6 @@ def setUp(self):
7384
self.billTo.state = "WA"
7485
self.billTo.zip = "98122"
7586
self.billTo.country = "USA"
76-
87+
88+
7789

0 commit comments

Comments
 (0)