Ready to get started?

Learn more about the CData Python Connector for Authorize.Net or download a free trial:

Download Now

Use SQLAlchemy ORMs to Access Authorize.Net Data in Python

The CData Python Connector for Authorize.Net enables you to create Python applications and scripts that use SQLAlchemy Object-Relational Mappings of Authorize.Net data.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems effectively. With the CData Python Connector for Authorize.Net and the SQLAlchemy toolkit, you can build Authorize.Net-connected Python applications and scripts. This article shows how to use SQLAlchemy to connect to Authorize.Net data to query Authorize.Net data.

With built-in optimized data processing, the CData Python Connector offers unmatched performance for interacting with live Authorize.Net data in Python. When you issue complex SQL queries from Authorize.Net, the CData Connector pushes supported SQL operations, like filters and aggregations, directly to Authorize.Net and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to Authorize.Net Data

Connecting to Authorize.Net data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

You can obtain the necessary connection properties on the Security Settings -> General Settings page after logging into your Merchant Account.

  • UseSandbox: The Authorize.Net API to be used to process transactions. If you are using a production account, this property can be left blank. If you are using a developer test account, set this to 'TRUE'.
  • LoginID: The API login Id associated with your payment gateway account. This property is used to authenticate that you are authorized to submit website transactions. Note that this value is not the same as the login Id that you use to log in to the Merchant Interface.
  • TransactionKey: The transaction key associated with your payment gateway account. This property is used to authenticate that you are authorized to submit website transactions.

Follow the procedure below to install SQLAlchemy and start accessing Authorize.Net through Python objects.

Install Required Modules

Use the pip utility to install the SQLAlchemy toolkit:

pip install sqlalchemy

Be sure to import the module with the following:

import sqlalchemy

Model Authorize.Net Data in Python

You can now connect with a connection string. Use the create_engine function to create an Engine for working with Authorize.Net data.

engine = create_engine("authorizenet///?LoginId=MyLoginId&TransactionKey=MyTransactionKey")

Declare a Mapping Class for Authorize.Net Data

After establishing the connection, declare a mapping class for the table you wish to model in the ORM (in this article, we will model the SettledBatchList table). Use the sqlalchemy.ext.declarative.declarative_base function and create a new class with some or all of the fields (columns) defined.

base = declarative_base()
class SettledBatchList(base):
	__tablename__ = "SettledBatchList"
	MarketType = Column(String,primary_key=True)
	TotalCharge = Column(String)
	...

Query Authorize.Net Data

With the mapping class prepared, you can use a session object to query the data source. After binding the Engine to the session, provide the mapping class to the session query method.

Using the query Method

engine = create_engine("authorizenet///?LoginId=MyLoginId&TransactionKey=MyTransactionKey")
factory = sessionmaker(bind=engine)
session = factory()
for instance in session.query(SettledBatchList).filter_by(IncludeStatistics="True"):
	print("MarketType: ", instance.MarketType)
	print("TotalCharge: ", instance.TotalCharge)
	print("---------")

Alternatively, you can use the execute method with the appropriate table object. The code below works with an active session.

Using the execute Method

SettledBatchList_table = SettledBatchList.metadata.tables["SettledBatchList"]
for instance in session.execute(SettledBatchList_table.select().where(SettledBatchList_table.c.IncludeStatistics == "True")):
	print("MarketType: ", instance.MarketType)
	print("TotalCharge: ", instance.TotalCharge)
	print("---------")

For examples of more complex querying, including JOINs, aggregations, limits, and more, refer to the Help documentation for the extension.

Free Trial & More Information

Download a free, 30-day trial of the Authorize.Net Python Connector to start building Python apps and scripts with connectivity to Authorize.Net data. Reach out to our Support Team if you have any questions.