Skip to content

Instantly share code, notes, and snippets.

@KabakiAntony
Last active November 16, 2023 08:49
Show Gist options
  • Save KabakiAntony/74ae3fd5741b7ab29ae713edb7b980a9 to your computer and use it in GitHub Desktop.
Save KabakiAntony/74ae3fd5741b7ab29ae713edb7b980a9 to your computer and use it in GitHub Desktop.
declaration of the MpesaHandler class
class MpesaHandler:
now = None
shortcode = None
consumer_key = None
consumer_secret = None
access_token_url = None
access_token = None
stk_push_url = None
my_callback_url = None
query_status_url = None
timestamp = None
passkey = None
def __init__(self):
self.now = datetime.now()
self.shortcode = env("SAF_SHORTCODE")
self.consumer_key = env("SAF_CONSUMER_KEY")
self.consumer_secret = env("SAF_CONSUMER_SECRET")
self.access_token_url = env("SAF_ACCESS_TOKEN_API")
self.passkey = env("SAF_PASS_KEY")
self.stk_push_url = env("SAF_STK_PUSH_API")
self.query_status_url = env("SAF_STK_PUSH_QUERY_API")
self.my_callback_url = env("CALLBACK_URL")
self.password = self.generate_password()
try:
self.access_token = self.get_mpesa_access_token()
if self.access_token is None:
raise Exception("Request for access token failed")
else:
self.access_token_expiration = time.time() + 3599
except Exception as e:
# log this errors
print(str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment