Adding payments functionality to your Python app in 10 minutes using the Authorize.Net API

Nicholas Walsh
Major League Hacking
5 min readOct 3, 2018

--

Learn how to process credit card payments in minutes!

Transactions are a critical part of many modern applications, and while there are countless number of ways to use them, there is no need to reinvent the wheel when it comes to building this functionality. There are awesome APIs (services) out there that help you focus on writing your application code instead of worrying about the code that will handle the transactions! Today I’m going to show you how to use Authorize.Net’s API to send your first credit card transaction, and wrap it as a function for use in other parts of your application.

Here’s the developer portal! As we can see, there are a lot of different options when it comes to sending transactions. For the sake of this tutorial, we’re going to be focusing on sending your first API-powered credit card transaction, and then wrapping it into a function for use in other parts of your Python app.

Setup

0. Sign up for an account

  • Follow this link to create your Authorize.Net account!

Authorize.Net offers a completely free account in their sandbox tier, which is perfect because you’re able to get full API functionality for development purposes, without incurring any costs! No credit card required to get started.

1. Save your API credentials

After a successful account creation, scroll down and record or screenshot your API credentials — you’ll need them for later! Your credentials are sent along with your requests in order to tell Authorize.Net servers what developer account the activity is associated with.

Note: these credentials are secret! Don’t share them publicly or post them online.

Getting Started

2. Find the Hello World example

Click “Hello World” (or go to this link), to see the code needed to process our first payment. Click on the python tab.

3. Install the Authorize.Net python package

Jump over to your terminal. We’re going to install the authorize.net python package using pip with:

$ pip install authorizenet

4. Copy the example code

  • Open up your favorite text editor.
  • Copy the example code from the box that says “Step 2”, and paste it into your new file in your text editor.

5. Insert your account credentials

  • Grab the credentials you recorded before, and insert them into the appropriate spots in the code merchantAuth.name and merchantAuth.transactionKey
  • Save the file as charge-credit-card.py

6. Testing it out

  • Open up your terminal, and run the script with:

$ python charge-credit-card.py

If all went well, you should see a response similar to the following!

Transaction ID: 60107076871

Awesome! As an added bonus, you should be able to check your email now and see a receipt for the purchase!

Modifying the example

Now, we know that our local script is successfully calling the Authorize.Net API, but there are a few more things we’ll want to do to make this a reusable and modular function for our python app.

7. Hiding our private credentials

First, we’re going to want to move our Authorize.Net credentials to another file, to prevent them from accidentally being shared publicly when adding our code to source control like GitHub.

  • In your code editor, save a new file called credentials.py, and fill it in with your API Login ID and Transaction Key as shown below.
api_login_name = ‘API-USER-HERE’
transaction_key = ‘API-PASS-HERE’

This also increases code maintainability — in the event that your API credentials change, you can simply change them once in this dedicated file instead of updating them across many files that may interact with the Authorize.Net API.

We’re going to jump back over to our charge-credit-card.py file and import these credentials.

  • At the top by your imports, we can import our new file with the following line:

import credentials

Now, on the lines where we previously entered our information manually, we’re going to instead change those two lines to the following:

merchantAuth.name = credentials.api_login_name
merchantAuth.transactionKey = credentials.transaction_key

Great! We can test that this works properly by saving both files and running

$ python charge-credit-card.py again.

— -

Converting our code to a function

Wonderful, now it’s time to wrap this code in a function so that we can allow it to accept any arbitrary credit card and transaction data. There are 4 pieces of data that we’ll need to be able to pass to this function:

  • card_number : number of the credit card being charged
  • expiration_date : expiration date of the charged credit card
  • amount : value of the transaction/purchase
  • merchant_id : ID of the business/merchant issuing this charge (visible to the recipient)

9. Define the function

After the authentication code, we’re going to add the following line to instantiate a function named charge with the 4 parameters mentioned prior:

def charge(card_number, expiration_date, amount, merchant_id):

Note: because this is Python, remember to use proper whitespace by indenting the code intended for use inside the function!

10. Change the existing variables that are defined within the function

Find each corresponding line, find it and and replace it with the following.

creditCard.cardNumber = card_number

creditCard.expirationDate = expiration_date

transactionrequest.amount = amount

createtransactionrequest.refId = merchant_id

11. Add some example data

At the bottom, outside the scope of the function, we can define some example variables to pass into our function. Have fun with this!

card_number = “4111111111111111”
expiration_date = “2020–12”
amount = Decimal(‘13.37’)
merchant_id = “Pied-Piper”

Warning: For amount, don’t enter a very large number, because it may get flagged as a ‘suspicious transaction’ within Authorize.Net’s default fraud detection system, and will result in erroring transactions.

12. Invoke the function

And last but not least, we can call our charge function, passing the relevant user information with the following:

charge(card_number, expiration_date, amount, merchant_id)

By now, your code should look exactly like the following:

Once saved, if you jump back over to your terminal, you can test the script just as we did before with:

$ python charge-credit-card.py

Congratulations, you’ve now got the ability to process user transactions with custom data! Reuse this function in any other part of your application that requires credit card payments!

— — —

I hope you enjoyed this tutorial on using Authorize.Net’s Python SDK to power credit card payments in your app.

The full source code is available on GitHub here!

--

--

Developer Relations at Amazon Web Services. Formerly MLH, Datmo, Wolfram Research.❤️ esports, AI/ML, and dunkin donuts coffee.