PyAuthorize

Introduction

There are a handful of Python implementations of the Authorize.net AIM API out there already, but we wrote our own. Why? Well honestly when we wrote it we didn't find any of the ones that already exist. Maybe we're just really bad at using Google, but it's the truth. Regardless, we like it because it's under 300 lines of code and extremely easy to use and modify.

Installing

You have a couple options for getting the code:
  1. Get the code from GitHub
  2. Download the package from PyPI
  3. Install using pip
    sudo pip install pyauthorize
  4. Install using easy_install
    sudo easy_install pyauthorize

Usage:

Initializing the processor

import pyauthorize
# Set x_test_request=False to use in production mode
pp = pyauthorize.PaymentProcessor(x_login='your auth.net login', x_tran_key='your auth.net api key', x_test_request=True)

Transaction Types

auth_and_capture

Use this method to do a standard capture of funds.
Required fields: card_num, exp_date, amount
Optional fields: is_avs_required (True/False), is_ccv_required (True/False), invoice_number, first_name, last_name, customer_id, description
If is_avs_required is True: address and zip are also required.
If is_ccv_required is True: card_code is also required.
pp.card_num = '4111111111111111'
pp.exp_date = '112012'
pp.amount = '12.00'
pp.auth_and_capture()
result = pp.process()

auth_only

Use this method to get an authorization but not actually capture funds.
Required fields: card_num, exp_date, amount
Optional fields: is_avs_required (True/False), is_ccv_required (True/False), invoice_number, first_name, last_name, customer_id, description
If is_avs_required is True: address and zip are also required.
If is_ccv_required is True: card_code is also required.
pp.card_num = '4111111111111111'
pp.exp_date = '112012'
pp.amount = '12.00'
pp.auth_only()
result = pp.process()

prior_auth_capture

Use this method to capture funds that were authorized in a previous auth_only transaction.
Required fields: transaction
Optional fields: amount (only if you wish to capture less than the original auth was for.)
pp.transaction = '123123123'
pp.amount = '10.00'
pp.prior_auth_capture()
result = pp.process()

void

Use this method to void a capture that hasn't settled yet.
Required fields: transaction
pp.transaction = '123123123'
pp.void()
result = pp.process()

credit

Use this method to process a credit (refund) on a previously settled transaction. The total amount of all refunds against a single transaction number cannot exceed the amount originally charged.
Required fields: transaction, card_num (full or last four digits), amount.
pp.transaction = '123123123'
pp.card_num = '1111'
pp.amount = '8.00'
pp.credit()
result = pp.process()

process_void_or_credit

Attempts to process a void first, if the void fails it will process a credit. This is useful when you don't know if a transaction is settled yet but you wish to refund the full amount.
Required fields: transaction, card_num (full or last four digits), amount.
Returns: A two part tuple. The first value is a boolean indicating if the transaction was successful or not. The second field will be either 'Credit' or 'Void'.
pp.transaction = '123123123'
pp.card_num = '1111'
pp.amount = '12.00'
is_processed, transaction_type = pp.process_void_or_credit()

Adding extra fields

The client includes what we encountered as the most common fields as well as field validation for those values based on the Authorize.net AIM integration guide. You are free, however, to include any of the fields specified in the manual even if the field is not included in PyAuthorize. Just add those additional values to the PaymentProcessor.transaction_data dictionary. If you are using these other fields often, please log a feature request with us and we will add the validation, or you are free to add it yourself and contribute your diff to the project.
pp.card_num = '4111111111111111'
pp.exp_date = '112012'
pp.amount = '12.00'

# Include additional fields not specified in PyAuthorize
pp.transaction_data['tax'] = '1.20'
pp.transaction_data['freight'] = '4.95'

pp.auth_and_capture()
result = pp.process()

Testing

A note on running the tests that are included: In order to run all the tests, you will have to edit lines 44 and 45 of pyauthorize_test.py and add your Authorize.net login details. Without setting these values, some tests will fail.

Contributing

We welcome your contributions to the project, in whatever capacity you are able to help. Whether it is through feature requests, bug reports, code contributions, or simply some constructive feedback, we are glad to hear from you. See below for information on how to contact us.

Contacting Us

The best way to contact us is by sending us an email at open-source@analytemedia.com. We promise to get back to you in a jiffy! You can also email your patches to us there.
Comments