# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
import StringIO
import pycurl
import urllib
class CredexExt(object):
def __init__(self, url, merch_id, username, password):
self._url = url
self._username = username
self._password = password
self._merch_id = merch_id
def request_loan(self, amount, cust_fname, cust_lname,
bill_addr_address, bill_addr_city, bill_addr_state, bill_addr_zip,
ship_addr_address, ship_addr_city, ship_addr_state, ship_addr_zip,
cust_email, product_info):
"""
Request a loan.
"""
params = {
'inv_action': 'AUTH_ONLY',
'inv_value': amount,
'cust_fname': cust_fname,
'cust_lname': cust_lname,
'bill_addr_address': bill_addr_address,
'bill_addr_city': bill_addr_city,
'bill_addr_state': bill_addr_state,
'bill_addr_zip': bill_addr_zip,
'ship_addr_address': ship_addr_address,
'ship_addr_city': ship_addr_city,
'ship_addr_state': ship_addr_state,
'ship_addr_zip': ship_addr_zip,
'cust_email': cust_email,
'version': '0.3',
'udf02': product_info,
}
response = self._post(params)
return response
def _post(self, data):
data = dict(data)
data.update({
'response_format': 'XML',
'dg_username': self._username,
'dg_password': self._password,
'merch_id': self._merch_id,
})
curl = pycurl.Curl()
#curl.setopt(pycurl.VERBOSE, 1)
curl.setopt(pycurl.SSL_VERIFYPEER, 1)
curl.setopt(pycurl.SSL_VERIFYHOST, 2)
headers = ['Accept: application/xml',
'Content-Type: application/x-www-form-urlencoded']
curl.setopt(pycurl.HTTPHEADER, headers)
b = StringIO.StringIO()
curl.setopt(pycurl.WRITEFUNCTION, b.write)
curl.setopt(pycurl.URL, self._url)
curl.setopt(pycurl.POSTFIELDS, urllib.urlencode(data))
curl.perform()
return b.getvalue()
if __name__ == '__main__':
url = 'https://demo.credex.net:10001/gateway/transact.cfm'
merch_id = '1234'
username = 'username'
password = 'pass'
c = CredexExt(url, merch_id, username, password)
kwargs = {
'cust_fname': 'Mary',
'cust_lname': 'Bernard',
'bill_addr_address': 'PO Box 190648',
'bill_addr_city': 'Anchorage',
'bill_addr_state': 'AK',
'bill_addr_zip': '99519',
'ship_addr_address': 'PO Box 190648',
'ship_addr_city': 'Anchorage',
'ship_addr_state': 'AK',
'ship_addr_zip': '99519',
'cust_email': 'mary@email.com',
'product_info': 'product info'}
r = c.request_loan(1500.0, **kwargs)
print r