Python

Parent Previous Next

Calculate Partial Loan Repayment


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

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



Next, we create a function calculatePartialLoanRepayment

Inside the function, we will create the input variables.




Then we build 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 t and contentObj o 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 display 'the result.

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


def calculatePartialLoanRepayment():

   #Header

   serviceName = 'calculatePartialLoanRepayment'

   userID = 'alan1960'

   PIN = '123'

   OTP = '123'


   accountID = "1040"

   repaymentAmount = "5000"

   keepMaturityDate = "true"

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   

   contentObj = {

                       'Content': {

                       'accountID': accountID,

                       'repaymentAmount': repaymentAmount,

                       'keepMaturityDate': keepMaturityDate

                       }

                       }

   

   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':

       PartialRepaymentResponseObj = response.json()['Content']['ServiceResponse']['PartialRepaymentResponse']

       print('Status: {}'.format(PartialRepaymentResponseObj['Status']))

       print('NewLoanAmount: {}'.format(PartialRepaymentResponseObj['NewLoanAmount']))

       print('BalanceAfterRepayment: {}'.format(PartialRepaymentResponseObj['BalanceAfterRepayment']))

       print('PenaltyRate: {}'.format(PartialRepaymentResponseObj['PenaltyRate']))

       print('NewMaturityDate: {}'.format(PartialRepaymentResponseObj['NewMaturityDate']))

       print('NewMonthsRemaining: {}'.format(PartialRepaymentResponseObj['NewMonthsRemaining']))


       print('OldMonthlyInstallment: {}'.format(PartialRepaymentResponseObj['OldMonthlyInstallment']))

       print('OldMaturityDate: {}'.format(PartialRepaymentResponseObj['OldMaturityDate']))

       print('OldLoanTerm: {}'.format(PartialRepaymentResponseObj['OldLoanTerm']))

       print('NewMonthlyInstallment: {}'.format(PartialRepaymentResponseObj['NewMonthlyInstallment']))

       print('BalanceBeforeRepayment: {}'.format(PartialRepaymentResponseObj['BalanceBeforeRepayment']))

       print('SettlementAccountBalance: {}'.format(PartialRepaymentResponseObj['SettlementAccountBalance']))


       print('PenaltyAmount: {}'.format(PartialRepaymentResponseObj['PenaltyAmount']))

       print('SettlementAccountID: {}'.format(PartialRepaymentResponseObj['SettlementAccountID']))

       print('NewInterest: {}'.format(PartialRepaymentResponseObj['NewInterest']))

       print('InterestRate: {}'.format(PartialRepaymentResponseObj['InterestRate']))

       print('NewLoanTerm: {}'.format(PartialRepaymentResponseObj['NewLoanTerm']))

       print('RepaymentAmountAfterPenalty: {}'.format(PartialRepaymentResponseObj['RepaymentAmountAfterPenalty']))

       

   elif errorCode == '010041':

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


   else:

      print(serviceRespHeader['ErrorText'])

           

calculatePartialLoanRepayment()


   


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Produce online help for Qt applications