15
15
PASS_KEY = api_settings .PASS_KEY
16
16
SHORT_CODE = api_settings .SHORT_CODE
17
17
SAFARICOM_API = api_settings .SAFARICOM_API
18
+ TRANSACTION_TYPE = api_settings .TRANSACTION_TYPE
18
19
19
20
20
21
# Applies for LipaNaMpesaOnline Payment method
@@ -26,7 +27,6 @@ def generate_pass_key():
26
27
27
28
def get_token ():
28
29
api_url = "{}/oauth/v1/generate?grant_type=client_credentials" .format (SAFARICOM_API )
29
- print (api_url )
30
30
31
31
r = requests .get (api_url , auth = HTTPBasicAuth (consumer_key , consumer_secret ))
32
32
if r .status_code == 200 :
@@ -38,7 +38,7 @@ def get_token():
38
38
return False
39
39
40
40
41
- def sendSTK (phone_number , amount , orderId = 0 , transaction_id = None , shortcode = None ):
41
+ def sendSTK (phone_number , amount , orderId = 0 , transaction_id = None , shortcode = None , account_number = None ):
42
42
code = shortcode or SHORT_CODE
43
43
access_token = get_token ()
44
44
if access_token is False :
@@ -54,18 +54,26 @@ def sendSTK(phone_number, amount, orderId=0, transaction_id=None, shortcode=None
54
54
"Authorization" : "Bearer %s" % access_token ,
55
55
"Content-Type" : "application/json" ,
56
56
}
57
+
58
+ transaction_type = TRANSACTION_TYPE or "CustomerBuyGoodsOnline"
59
+ # If account number is set, change transaction type to paybill
60
+ if account_number :
61
+ transaction_type = "CustomerPayBillOnline"
62
+ elif transaction_type == "CustomerPayBillOnline" and account_number == None :
63
+ account_number = phone_number
64
+
57
65
request = {
58
66
"BusinessShortCode" : code ,
59
67
"Password" : encoded ,
60
68
"Timestamp" : time_now ,
61
- "TransactionType" : "CustomerPayBillOnline" ,
69
+ "TransactionType" : transaction_type ,
62
70
"Amount" : str (int (amount )),
63
71
"PartyA" : phone_number ,
64
72
"PartyB" : code ,
65
73
"PhoneNumber" : phone_number ,
66
74
"CallBackURL" : "{}/mpesa/confirm/" .format (HOST_NAME ),
67
- "AccountReference" : code ,
68
- "TransactionDesc" : "Payment for {}" .format (phone_number )
75
+ "AccountReference" : account_number or code ,
76
+ "TransactionDesc" : "{}" .format (phone_number )
69
77
}
70
78
71
79
print (request )
0 commit comments