@@ -25,17 +25,25 @@ def generate_pass_key():
25
25
26
26
27
27
def get_token ():
28
- api_URL = "{}/oauth/v1/generate?grant_type=client_credentials" .format (SAFARICOM_API )
28
+ api_url = "{}/oauth/v1/generate?grant_type=client_credentials" .format (SAFARICOM_API )
29
+ print (api_url )
29
30
30
- r = requests .get (api_URL , auth = HTTPBasicAuth (consumer_key , consumer_secret ))
31
- jonresponse = json .loads (r .content )
32
- access_token = jonresponse ['access_token' ]
33
- return access_token
31
+ r = requests .get (api_url , auth = HTTPBasicAuth (consumer_key , consumer_secret ))
32
+ if r .status_code == 200 :
33
+ jonresponse = json .loads (r .content )
34
+ access_token = jonresponse ['access_token' ]
35
+ return access_token
36
+ elif r .status_code == 400 :
37
+ print ('Invalid credentials.' )
38
+ return False
34
39
35
40
36
41
def sendSTK (phone_number , amount , orderId = 0 , transaction_id = None , shortcode = None ):
37
42
code = shortcode or SHORT_CODE
38
43
access_token = get_token ()
44
+ if access_token is False :
45
+ raise Exception ("Invalid Consumer key or secret or both" )
46
+
39
47
time_now = datetime .datetime .now ().strftime ("%Y%m%d%H%I%S" )
40
48
41
49
s = code + PASS_KEY + time_now
@@ -56,10 +64,11 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None, shortcode=None
56
64
"PartyB" : code ,
57
65
"PhoneNumber" : phone_number ,
58
66
"CallBackURL" : "{}/mpesa/confirm/" .format (HOST_NAME ),
59
- "AccountReference" : phone_number ,
67
+ "AccountReference" : code ,
60
68
"TransactionDesc" : "Payment for {}" .format (phone_number )
61
69
}
62
70
71
+ print (request )
63
72
response = requests .post (api_url , json = request , headers = headers )
64
73
json_response = json .loads (response .text )
65
74
if json_response .get ('ResponseCode' ):
@@ -71,8 +80,9 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None, shortcode=None
71
80
transaction .save ()
72
81
return transaction .id
73
82
else :
74
- transaction = PaymentTransaction .objects .create (phone_number = phone_number , checkoutRequestID = checkout_id ,
75
- amount = amount , order_id = orderId )
83
+ transaction = PaymentTransaction .objects .create (phone_number = phone_number ,
84
+ checkoutRequestID = checkout_id ,
85
+ amount = amount , order_id = orderId )
76
86
transaction .save ()
77
87
return transaction .id
78
88
else :
@@ -101,6 +111,14 @@ def check_payment_status(checkout_request_id, shortcode=None):
101
111
response = requests .post (api_url , json = request , headers = headers )
102
112
json_response = json .loads (response .text )
103
113
if 'ResponseCode' in json_response and json_response ["ResponseCode" ] == "0" :
114
+ requestId = json_response .get ('CheckoutRequestID' )
115
+ transaction = PaymentTransaction .objects .get (
116
+ checkoutRequestID = requestId )
117
+ if transaction :
118
+ transaction .isFinished = True
119
+ transaction .isSuccessFull = True
120
+ transaction .save ()
121
+
104
122
result_code = json_response ['ResultCode' ]
105
123
response_message = json_response ['ResultDesc' ]
106
124
return {
0 commit comments