johnmahugu

python stkpush

Mar 8th, 2021
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #!/usr/bin/python
  2. import requests
  3. import json
  4. import base64
  5. response1 = requests.get("https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",auth=('<CONSUMER_KEY>','<CONSUMER_SECRET>')).text
  6. res=json.loads(response1)
  7. access_token = res['access_token']
  8. api_url = "https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
  9. headers = { "Authorization": "Bearer "+access_token}
  10. timestamp="20210307100100"
  11. shortcode="<SHORTCODE>"
  12. passkey="<PASSKEY>"
  13. def passout(word):
  14.     output=[]
  15.     for each in str(base64.b64encode(bytes(word, 'utf-8'))).split('b\''):
  16.         output.append(each.split("'"))
  17.     return output[1][0]
  18.    
  19.    
  20. password=passout(shortcode+passkey+timestamp)
  21.  
  22. ammount=int(input('enter ammount   '))
  23. mobile=input('enter mobile in format 7XXXXXXXX   ')
  24. request ={
  25.       "BusinessShortCode": shortcode,
  26.       "Password": password,
  27.       "Timestamp": timestamp,
  28.       "TransactionType": "CustomerPayBillOnline",
  29.       "Amount": ammount,
  30.       "PartyA": "254"+mobile,
  31.       "PartyB": shortcode,
  32.       "PhoneNumber": "254"+mobile,
  33.       "CallBackURL": "http://mpesa-requestbin.herokuapp.com/1hx6r081",
  34.       "AccountReference": "KESH",
  35.       "TransactionDesc": "kesh test"
  36.     }
  37.  
  38. response = requests.post(api_url, json = request, headers=headers)
  39.  
  40. print (response.text)
  41.  
Add Comment
Please, Sign In to add comment