Skip to content

Commit

Permalink
Add confirm api mpesa endpoint | remove print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekirapapaul committed Aug 9, 2020
1 parent ea81885 commit 8cd0540
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
12 changes: 3 additions & 9 deletions mpesa/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
INITIATOR_PASS = os.environ.get('INITIATOR_PASS')
CERTIFICATE_FILE = os.environ.get('CERTIFICATE_FILE')
HOST_NAME = os.environ.get('HOST_NAME')

PASS_KEY = os.environ.get('PASS_KEY')
shortcode = os.environ.get('SHORT_CODE')

def encryptInitiatorPassword():
PASS = "foobar1234"
Expand All @@ -43,18 +44,13 @@ def encryptInitiatorPassword():

def generate_pass_key(cipher):
time_now = datetime.datetime.now().strftime("%Y%m%d%H%I%S")
PASS_KEY = "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919"
shortcode = "601754"
s = shortcode + PASS_KEY + time_now
encoded = b64encode(s.encode('utf-8')).decode('utf-8')


def get_token():
api_URL = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"

consumer_key = "9ntpVdAfDvNqjt6VzFkbNYbOx5wmKonx"
consumer_secret = "Bv7Z7IjRNAetpDAr"

r = requests.get(api_URL, auth=HTTPBasicAuth(consumer_key, consumer_secret))
jonresponse = json.loads(r.content)
access_token = jonresponse['access_token']
Expand Down Expand Up @@ -84,8 +80,6 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None):
access_token = get_token()
time_now = datetime.datetime.now().strftime("%Y%m%d%H%I%S")

PASS_KEY = "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919"
shortcode = "174379"
s = shortcode + PASS_KEY + time_now
encoded = b64encode(s.encode('utf-8')).decode('utf-8')

Expand All @@ -104,7 +98,7 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None):
"PartyA": phone_number,
"PartyB": shortcode,
"PhoneNumber": phone_number,
"CallBackURL": "{}/api/confirm/".format(HOST_NAME),
"CallBackURL": "{}/mpesa/confirm/".format(HOST_NAME),
"AccountReference": phone_number,
"TransactionDesc": "Payment for {}".format(phone_number)
}
Expand Down
3 changes: 2 additions & 1 deletion mpesa/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from .views import SubmitView, CheckTransaction
from .views import SubmitView, CheckTransaction, ConfirmView

mpesa_urls = [
url(r'^submit/', SubmitView.as_view(), name='submit'),
url(r'^confirm/', ConfirmView.as_view(), name='confirm'),
url(r'^checktransaction/', CheckTransaction.as_view(), name='check_transaction'),
]
9 changes: 5 additions & 4 deletions mpesa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def post(self, request):
amount = data['amount']
entity_id = data['entity_id']

print(phone_number)
print(amount)
transactionId = sendSTK(phone_number, amount, entity_id)
# b2c()
message = {"status": "ok" , "transaction_id" : transactionId}
Expand Down Expand Up @@ -101,19 +99,22 @@ class ConfirmView(APIView):

def post(self, request):
# save the data
print("confirm view reached")
request_data = json.dumps(request.data)
request_data = json.loads(request_data)
body = request_data.get('Body')
print("Body " + json.dumps(body))
resultcode = body.get('stkCallback').get('ResultCode')
# Perform your processing here e.g. print it out...
if resultcode == 0:
print('Payment successful')
requestId = body.get('stkCallback').get('CheckoutRequestID')
metadata = body.get('stkCallback').get('CallbackMetadata').get('Item')
for data in metadata:
if data.get('Name') == "MpesaReceiptNumber":
receipt_number = data.get('Value')
transaction = PaymentTransaction.objects.get(
checkoutRequestID=requestId)
if transaction:
transaction.trans_id = receipt_number
transaction.isFinished = True
transaction.isSuccessFull = True
transaction.save()
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django-mpesa
version = 0.1
version = 1.1
description = A python library that interfaces safaricoms mpesa apis
long_description = file: README.rst
url = https://www.vorane.com/
Expand Down

0 comments on commit 8cd0540

Please sign in to comment.