I.T. Security and Linux Administration

Sep 30 2012   1:03PM GMT

[Python] Processing Credit Cards Part 3 (Filtering Data)

Eric Hansen Eric Hansen Profile: Eric Hansen

To continue with my series of processing cards, we covered how to add clients and credit cards last time.  This time, we’re going to cover searching Balanced for various information and filtering data.

Before continuing, it should be noted that Balanced does not have a very well thought out API on this aspect yet.  They are working on it, however.

Searching For Accounts

This is potentially the easiest search and filtering you can do.  Here’s a basic Python function to do this:

    def search_email(self, crit):
        info = None

            info = balanced.Account.query.filter(email_address=crit)[0]

        return info

Basically what this does is either return None if there’s no account/client by such e-mail address, or return a structure of data containing a bunch of info like name, e-mail, URIs for getting data for user, etc…

The purpose of the “[0]” at the end is because otherwise you’d get a result similar to this:

<Page<class ‘balanced.resources.Account’>{’email_address’: ‘fake@email.tld’}>

Each query is returned in a paging system, as you’re only allowed to return so much data at a time, this also helps organize data so you’re not automatically swarmed with a bunch of data.  You could instead use “.one()” in place of “[0]” which returns the same data, but this will return an exception:

 >>> print balanced.Account.query.one()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/dist-packages/balanced/resources.py", line 123, in one
    raise MultipleResultsFound('Multiple items were found for one()')
balanced.exc.MultipleResultsFound: Multiple items were found for one()

The reason for this exception is because you’re attempting to access one record when multiple are returned (in this case you’ll want to use “.all()” and then filter through each entry in a for loop, which I’ll show in a bit).

Getting an account is fine, but what’s the point if you can’t do anything with it, right?  Well, lets say we want a specific card of a buyer, how do we do that?

Filtering Card Data

If you remember from our last tutorial, in order to create a client for Balanced, we had to associate a card with them (which, I later found out isn’t necessary but it helps in the long run anyways).  Now lets get all of their cards (using the previous function to aid in the process):

    Test function written for #balanced to provide a list of CC's for one account only.
    Useful for many things.  Could be possible to shorten try: block to one line, not sure.
    def get_acct_ccs(self, addr):
        cards = None

            buyer = self.search_email(addr)
            cards = list(balanced.Card.query.filter(card_uri=buyer.cards_uri))

        return cards

What we are doing here is getting the buyer’s account information, and searching for all the user’s cards by the buyer’s card URI.  Calling the query inside of list() is another way of calling .filter(…).all() but it has the added bonus of being easily iterated without extra work going into it, as each card instance is in the list.

A card structure looks like this:

Card(expiration_month=3, account=Account(holds_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/holds’, name=u’Philipp Lenard’, roles=[u’buyer’], created_at=datetime.datetime(2012, 9, 13, 18, 43, 51, 423482, tzinfo=<iso8601.iso8601.Utc object at 0x903a64c>), uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7′, bank_accounts_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/bank_accounts’, refunds_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/refunds’, meta={}, debits_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/debits’, transactions_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/transactions’, email_address=u’philipp.lenard157@msn.web’, id=u’AC7uGozq6tesxvCACn2IE9u7′, credits_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/credits’, cards_uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/cards’), hash=None, name=u’Philipp Lenard’, expiration_year=2014, created_at=datetime.datetime(2012, 9, 13, 18, 43, 51, 269988, tzinfo=<iso8601.iso8601.Utc object at 0x903a64c>), brand=u’Visa’, uri=u’/v1/marketplaces/TEST-MP6Z5z89tGG99Wx9ZMQ8KjmG/accounts/AC7uGozq6tesxvCACn2IE9u7/cards/CC7uv1Kkr76J17ctz9sytC3g’, card_type=u’visa’, is_valid=True, meta={}, last_four=u’1881′, id=u’CC7uv1Kkr76J17ctz9sytC3g’)

There is a lot of data here, but it can all be boiled down pretty easily.  If you want to find a specific card by the last four, you would then just loop through the data:

cards = get_acct_ccs('some@email.tld')
for card in cards:
    if card.last_four == '1111':
        print "success!"

Just replace with ‘1111’ with whatever the last four of the card is.  You can do this with a multitude of data, even transactions which I will cover later.  To get an idea as to what the different fields are, and what their values mean, check out the balanced-api GitHub: https://github.com/balanced/balanced-api/tree/master/resources

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: