Skip to content

Instantly share code, notes, and snippets.

@dustinmm80
Created September 12, 2012 20:14
Show Gist options
  • Save dustinmm80/3709567 to your computer and use it in GitHub Desktop.
Save dustinmm80/3709567 to your computer and use it in GitHub Desktop.
from django.core.exceptions import ObjectDoesNotExist
from django.template.loader import render_to_string
from django.conf import settings
from pprint import pprint
import requests
from xml.dom import minidom
from apps.donations.models import Donation
AUTH_URL = 'https://api.authorize.net/xml/v1/request.api'
AUTHNET_NS = 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'
def get_transaction_history(arg1=None, arg2=None):
#given an account id, call the auth.net transactions api to get a history
# payload = {
# 'api_login_id': settings.DONATIONS_AUTHNET_LOGIN,
# 'transaction_key': settings.DONATIONS_AUTHNET_PASSWORD
# }
payload = {
'api_login_id': 'xxxxxxxxx',
'transaction_key': 'xxxxxxxx'
}
xml = render_to_string('xml/transaction_history.xml', payload)
headers = {'content-type': 'text/xml', 'content-length': str(len(xml))}
r = requests.post(AUTH_URL, data=xml, headers=headers)
dom = minidom.parseString(r.text.encode('ascii','ignore'))
transactions = dom.getElementsByTagNameNS(AUTHNET_NS, 'transaction')
donation_ids = []
for t in transactions:
firstName = t.getElementsByTagName('firstName')[0].firstChild.data
lastName = t.getElementsByTagName('lastName')[0].firstChild.data
try:
d = Donation.objects.filter(first_name=firstName, last_name=lastName)
if d.exists():
donation_ids.extend(d.values_list('id', flat=True))
except ObjectDoesNotExist:
pass
return Donation.objects.filter(pk__in=donation_ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment