Skip to content

Commit

Permalink
Enrich MPESA Response (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmogusu committed Jan 3, 2023
1 parent 6fd5f6d commit a72cd29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 16 additions & 3 deletions django_daraja/mpesa/utils.py
Expand Up @@ -20,9 +20,16 @@


class MpesaResponse(Response):
response_description = ""
error_code = None
request_id = ''
response_code = ''
response_description = ''
customer_message = ''
conversation_id = ''
originator_conversation_id = ''
error_code = ''
error_message = ''
merchant_request_id = ''
checkout_request_id = ''


def mpesa_response(r):
Expand All @@ -35,9 +42,16 @@ def mpesa_response(r):

r.__class__ = MpesaResponse
json_response = r.json()
r.request_id = json_response.get('requestId', '')
r.response_code = json_response.get('ResponseCode', '')
r.response_description = json_response.get('ResponseDescription', '')
r.customer_message = json_response.get('CustomerMessage', '')
r.conversation_id = json_response.get('ConversationID', '')
r.originator_conversation_id = json_response.get('OriginatorConversationID', '')
r.error_code = json_response.get('errorCode')
r.error_message = json_response.get('errorMessage', '')
r.merchant_request_id = json_response.get('MerchantRequestID', '')
r.checkout_request_id = json_response.get('CheckoutRequestID', '')
return r


Expand Down Expand Up @@ -157,7 +171,6 @@ def mpesa_access_token():
else:
delta = timezone.now() - access_token.created_at
minutes = (delta.total_seconds()//60)
print('minutes: ', minutes)
if minutes > 50:
# Access token expired
access_token = generate_access_token()
Expand Down
4 changes: 4 additions & 0 deletions tests/test_stk_push.py
Expand Up @@ -23,7 +23,11 @@ def test_stk_push_success(self):
account_reference = 'reference'
transaction_desc = 'Description'
response = self.cl.stk_push(phone_number, amount, account_reference, transaction_desc, self.callback_url)
self.assertEqual(response.response_code, '0')
self.assertEqual(response.response_description, 'Success. Request accepted for processing')
self.assertTrue(len(response.merchant_request_id) > 0)
self.assertTrue(len(response.checkout_request_id) > 0)
self.assertTrue(len(response.customer_message) > 0)

def test_stk_push_empty_reference(self):
'''
Expand Down

0 comments on commit a72cd29

Please sign in to comment.