Bill Payment
We will first import "request" and "json" module, as we need those module for calling the API
Then, we will import our own url function from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.
We will create a function billPayment.
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 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 billPayment():
#Header
serviceName = 'billPayment'
userID = 'bobsmith1'
PIN = '123456'
OTP = '123456'
#Content
accountFrom = '4399'
accountTo = '3399'
transactionAmount = '100'
transactionReferenceNumber = '12332'
narrative = '2'
headerObj = {
'Header': {
'serviceName': serviceName,
'userID': userID,
'PIN': PIN,
'OTP': OTP
}
}
contentObj = {
'Content': {
'accountFrom': accountFrom,
'accountTo': accountTo,
'transactionAmount': transactionAmount,
'transactionReferenceNumber': transactionReferenceNumber ,
'narrative': narrative
}
}
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':
ServerResponse = response.json()['Content']['ServiceResponse']
print("Balance After Transferring: ${:.2f} ".format(float(ServerResponse['BalanceAfter']['_content_'])))
print("Transaction ID: ", ServerResponse['TransactionID']['_content_'])
print("Balance Before Transferring: ${:.2f}".format( float(ServerResponse['BalanceBefore']['_content_'])))
elif errorCode == '010041':
print("OTP has expired.\nYou will receiving a SMS")
else:
print(serviceRespHeader['ErrorText'])
billPayment()
Created with the Personal Edition of HelpNDoc: Easy to use tool to create HTML Help files and Help web sites