@@ -185,6 +185,37 @@ def make_payment(self, recipient, amount, description=None):
185185 match = re .search (r'Transaction id: (?P<txnid>\d+)' , r )
186186 return match .group ('txnid' )
187187
188+ def get_balance (self ):
189+ """
190+ Retrieves the balance for the configured account
191+ """
192+ self .br .open (self .MOBILE_WEB_URL % {'accountno' : self .account })
193+ try :
194+ # Search for the existence of the Register link - indicating a new account
195+ self .br .find_link (text = 'Register' )
196+ raise InvalidAccountException
197+ except mechanize .LinkNotFoundError :
198+ pass
199+
200+ self .br .follow_link (text = 'My Wallet' )
201+ self .br .follow_link (text = 'Balance Inquiry' )
202+ self .br .select_form (nr = 0 )
203+ self .br ['pin' ] = self .pin
204+ r = self .br .submit ().read ()
205+
206+ # Pin valid?
207+ if re .search (r'Invalid PIN' , r ):
208+ raise AuthDeniedException
209+
210+ # An error could occur for other reasons
211+ if re .search (r'Error occured' , r ):
212+ raise RequestErrorException
213+
214+ # If it was successful, we extract the balance
215+ if re .search (r'Your balance is NGN (?P<balance>[\d\.]+)' , r ):
216+ match = re .search (r'Your balance is NGN (?P<balance>[\d\.]+)' , r )
217+ return match .group ('balance' )
218+
188219 def _auth (self ):
189220 _form = urllib .urlencode ({'principal' : self .account , 'password' : self .pin })
190221 self .br .open (self .AUTH_URL , _form )
0 commit comments