Python

Features ›› Account ›› getTransactionHistory ›› Sample Code ››
Parent Previous Next

Get Transaction History


We will first import "request" and "json" module, as we need those module for calling the API

Then, we will import our own url and getRecord functions from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.

We will also be importing a reference data API 'getTransactionTypes', as one of the output for 'getTransactionHistory' is 'transactionType', and we want to show the transactionTypeName instead.

The Reference Data API can be referred under Reference Data Sub-Menu



Next, we create a function getTransactionHistory

Inside the function, we will create the input variables.



Then we build both the header and content object.



Then, we will create the final url, where we will use the url() function to get the API url, and append the headerObj and contentObj to the API url.

We will then post the final_url and store the server response in the variable "response".

The GlobalErrorID is then retrieved and stored in variable "errorCode".



If the errorCode is '010000', it means that there is no error, and we will proceed to check whether there are any records. If there are no records, we will display 'No record Found'.

If there are records, we will use the getRecord() function to check whether there is only 1 record, or more than 1 records, and display the results accordingly.

**NOTE: JSON output for 1 record and more than 1 record is different due to JSON structure. Hence, we use getRecord() function to retrieve the number of records.

If the errorCode is '010041', it means that the OTP provided has expired. We will then display 'OTP has expired. You will be receiving a SMS'.

Else, we will display the ErrorText.



Entire Code:


import requests, json

from functions import url,getRecord

from getTransactionTypes import getTransactionTypes


def getTransactionHistory():

   #Header

   serviceName = 'getTransactionHistory'

   userID = 'bobsmith1'

   PIN = '123456'

   OTP = '123456'

   #Content

   accountID = '4343'

   startDate = '2010-10-10 00:00:00'

   endDate = '2018-10-10 00:00:00'

   numRecordsPerPage = '2'

   pageNum = '1'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   contentObj = {

                        'Content': {

                        'accountID': accountID,

                        'startDate': startDate,

                        'endDate': endDate,

                        'numRecordsPerPage':numRecordsPerPage,

                        'pageNum':pageNum

                        }

                        }

   final_url="{0}?Header={1}&Content={2}".format(url(),json.dumps(headerObj),json.dumps(contentObj))

   response = requests.post(final_url)

   

   serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']

   errorCode = serviceRespHeader['GlobalErrorID']


   if errorCode == '010000':

       history = response.json()['Content']['ServiceResponse']['CDMTransactionDetail']

       if history == {} and  serviceRespHeader['GlobalErrorID']=='010000':

           print('No record found')

       else:

           transaction_history = history['transaction_Detail']

           recordCount = getRecord(transaction_history)  

           if recordCount >1:

               for i in range(0,recordCount,1):

                   transaction_Detail = transaction_history[i]

                   print("\nPayment Mode: {}".format(transaction_Detail['paymentMode']))

                   print("BankID from: {}".format(transaction_Detail['bankID_from']))

                   print("Transaction Amount: {}".format(transaction_Detail['transactionAmount']))

                   print("Transaction Reference Number: {}".format(transaction_Detail['transactionReferenceNumber']))

                   print("Narrative: {}".format(transaction_Detail['narrative']))

                   print("Exchange Rate: {}".format(transaction_Detail['exchangeRate']))

                   print("Channel ID: {}".format(transaction_Detail['channelID']))

                   print("Interim Balance: {}".format(transaction_Detail['interimBalance']))

                   print("AccountFrom: {}".format(transaction_Detail['accountFrom']))

                   print("TransactionID: {}".format(transaction_Detail['transactionID']))

                   print("OfficerID: {}".format(transaction_Detail['officerID']))

                   print("Override Flag: {}".format(transaction_Detail['overrideFlag']))

                   print("Transaction Date: {}".format(transaction_Detail['transactionDate']))

                   print("Currency: {}".format(transaction_Detail['currency']))

                   print("Quote Currency: {}".format(transaction_Detail['quoteCurrency']))

                   print("Transaction Branch: {}".format(transaction_Detail['transactionBranch']))

                   print("Transaction Type: {}".format(getTransactionTypes(transaction_Detail['transactionType'])))

                   print("Quote Currency: {}".format(transaction_Detail['quoteCurrency']))

                   print("Account To: {}".format(transaction_Detail['accountTo']))

                   print("Account to interim Balance: {}".format(transaction_Detail['accountTo_interimBalance']))

                   print("BankID to: {}".format(transaction_Detail['bankID_to']))                    

           else:

               print("\nPayment Mode: {}".format(transaction_history['paymentMode']))

               print("BankID from: {}".format(transaction_history['bankID_from']))

               print("Transaction Amount: {}".format(transaction_history['transactionAmount']))

               print("Transaction Reference Number: {}".format(transaction_history['transactionReferenceNumber']))

               print("Narrative: {}".format(transaction_history['narrative']))

               print("Exchange Rate: {}".format(transaction_history['exchangeRate']))

               print("Channel ID: {}".format(transaction_history['channelID']))

               print("Interim Balance: {}".format(transaction_history['interimBalance']))

               print("AccountFrom: {}".format(transaction_history['accountFrom']))

               print("TransactionID: {}".format(transaction_history['transactionID']))

               print("OfficerID: {}".format(transaction_history['officerID']))

               print("Override Flag: {}".format(transaction_history['overrideFlag']))

               print("Transaction Date: {}".format(transaction_history['transactionDate']))

               print("Currency: {}".format(transaction_history['currency']))

               print("Quote Currency: {}".format(transaction_history['quoteCurrency']))

               print("Transaction Branch: {}".format(transaction_history['transactionBranch']))

               print("Transaction Type: {}".format(getTransactionTypes(transaction_history['transactionType'])))

               print("Quote Currency: {}".format(transaction_history['quoteCurrency']))

               print("Account To: {}".format(transaction_history['accountTo']))

               print("Account to interim Balance: {}".format(transaction_history['accountTo_interimBalance']))

               print("BankID to: {}".format(transaction_history['bankID_to']))  

                               

   elif errorCode == '010041':

       print("OTP has expired.\nYou will receiving a SMS")

   else:

       print(serviceRespHeader['ErrorText'])


getTransactionHistory()


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: What is a Help Authoring tool?