[Python] Processing Credit Cards Part 5 (Viewing Transactions)
After being able to complete transactions, the customer service aspect of this begins. Now you have to be able to view transactions and all the details that come along with it.
Getting Transaction Type
There are 3 different transaction types: hold, debit and credit. We’ve covered hold & debit in part 4, but credit is Balanced’s term for refund. There is a secret behind how this data is delivered, however, and it’s pretty simple:
def get_trans_type(self, transaction_number): type = { 'H': {'type': "Hold", 'handle' : balanced.Hold}, 'W': {'type': "Debit", 'handle' : balanced.Debit}, 'C': {'type': "Credit", 'handle' : balanced.Credit} }[transaction_number[0]] return type
When passing the transaction # to the function, it matches the first character of the transaction # (which will always be a H, W or C) and return the type and balanced API handle. The handle to this will make searching for transactions easier as there’s not a one-size-fits-all approach to this, you need to know which type of transaction it is. In getting the type that’s all there is to it.
Getting Transaction Data
Getting the data of the transaction itself is pretty easy once you get the handle.
trans_info = get_trans_type(trans_num) try: trans = balanced.Debit.query.filter(trans_info['handle'].f.transaction_number==trans_num).one() src = trans.source acct = trans.account except: sys.exit("No transaction by ID %s exists." % (trans_num))
The reason for the ‘handle’ configuration in get_trans_type() is put to use here in the filter() call. Most likely (if you follow this series) you’ll be handling nothing but debit transactions (unless you wish to be able to refund amounts), but this basically safeguards you in the event you change business models later.
Wondering what ‘src’ and ‘acct’ are? Well ‘trans’ contains an entire transaction object, including the account and source (i.e.: card) of the transaction. To make things easier on ourselves, we’ll just assign the card to src and account to acct, instead of having to call trans.source/trans.account each time.
Now that we can get transactions, we should probably display it, correct?
Presenting the Data
print "Transaction Details" print "-------------------" print "ID:", trans_num print "Type:", trans_info['type'] print "Description:",trans.description print "Appearing on statement as:",trans.appears_on_statement_as print "Charge Amount:",fmt_currency(trans.amount) print "Fees to Balanced:",fmt_currency(trans.fee) print "Created At:",trans.created_at print "\nAccount Details" print "---------------" print "Name:",acct.name print "E-mail:", acct.email_address print "\nSource Details" print "----------------" print "Card Holder:",src.name print "Last Four:",src.last_four print "Brand:", src.brand print "Expiration: %s/%s" % (src.expiration_month, src.expiration_year) print "Hash:",src.hash
That’s all you really need to do. The above prints out all of the important details of a transaction request. A quick note on the fees and charge amount, the function fmt_currency() was written because, as stated before, currency in Balanced is represented as whole values. If you know you’ll be working with currency that has decimals (i.e.: USD), you can show it in a more user-friendly way by using this:
def fmt_currency(val): return '$%.02f' % (val/100.)
This will convert a whole number (cents) into it’s dollar.cent version.
Overall looking up transactions are easy and a nice way to recall different charges. This data will most likely work for holds and credits as well, but I have yet to test them personally. Consider that homework.
Comment on this Post