Accept Credit Cards in Django with Authorize.Net

credit cards Accepting credit cards in your Django applications using the Authorize.Net payment gateway is easier than you might think. Using the quix.pay Python module to do the heavy lifting with Authorize.Net, all you need to get started is a simple Django form.
You will need to either download quix.pay and install it using the setup.py script or download and install quix.pay using the easy_install command:
easy_install quix.pay

The quix.pay Python module provides credit card processing using online payment gateways including Authorize.Net. For those who are interested, I wrote up a more detailed look at quix.pay here: Authorize.NET Payment Gateway in Python
You should also have the "API Login ID" and "Transaction Key" for your Authorize.Net merchant account or developer account. This would have been provided to you by Authorize.Net when you signed up.
The Django code below shows a very simple credit card form. Along with the amount to charge the credit card, this is the minimum data you need to pass along to Authorize.Net (depending on your account security settings).
from django import forms

class PaymentForm(forms.Form):
    first_name = forms.CharField(max_length=50)
    last_name = forms.CharField(max_length=50)
    card_number = forms.CharField(max_length=16)
    expiration_month = forms.CharField(max_length=2)
    expiration_year = forms.CharField(max_length=4)
    security_code = forms.CharField(max_length=4)
This next snippet of Django code shows how you could process the above form data in your Django view.
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django import forms
from quix.pay.gateway.authorizenet import AimGateway
from quix.pay.transaction import CreditCard
from forms import PaymentForm

def process_payment_form(request):

    if request.POST:
        form = PaymentForm(request.POST)
        if form.is_valid():
            card = CreditCard(
                number = form.cleaned_data['card_number'],
                month = form.cleaned_data['expiration_month'],
                year = form.cleaned_data['expiration_year'],
                first_name = form.cleaned_data['first_name'],
                last_name = form.cleaned_data['last_name'],
                code = form.cleaned_data['security_code']  
            )
            gateway = AimGateway('YOUR API LOGIN ID', 'YOUR TRANSACTION KEY')
            gateway.use_test_mode = True
            gateway.use_test_url = True
            # use gateway.authorize() for an "authorize only" transaction
            response = gateway.sale('0.99', card)
            if response.status == response.APPROVED:
                # this is where you store data from the response object into
                # your models. The response.trans_id would be used to capture,
                # void, or credit the sale later. 
                return HttpResponseRedirect('/order_success.html')
            else:
                status = "%s - %s" % (response.status_strings[response.status], 
                    response.message)
                form._errors[forms.forms.NON_FIELD_ERRORS] = form.error_class([status])
    else:  
        form = PaymentForm()
    
    return render_to_response('base.html', {'form': form,}, 
        context_instance=RequestContext(request))
Now, in a real-world application you would likely clean that up a bit. One method I like using is to build the payment processing right into the form's validation. You can even check the various responses from Authorize.Net to put the validation on the appropriate field (such as an invalid security code).
I also like break out the gateway configuration details into the settings.py file.
I'll try to post an example of the above mentioned changes if I can find the time in the next couple of days. But, this should be enough to get you started. Enjoy.

No comments:

Post a Comment