├── .gitignore ├── README.MD └── app.py /.gitignore: -------------------------------------------------------------------------------- 1 | b2c.json 2 | lnmo.json -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | This is a simple implementation of Daraja API in Flask 2 | 3 | On every Resource consumption, check the details that need to be edited to 4 | reflect your iwn test credentials. 5 | 6 | * If you like this code sample, don't forget to star it here in Github 7 | Video Tutorials link: https://www.youtube.com/playlist?list=PLcKuwRUZRXZKfB0-5idYOo8JQKKLq_Kz- -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This is a simple implementation of Daraja API in Flask 3 | 4 | On every Resource consumption, check the details that need to be edited to 5 | reflect your iwn test credentials. 6 | 7 | * If you like this code sample, don't forget to star it here in Github 8 | Video Tutorials link: https://www.youtube.com/playlist?list=PLcKuwRUZRXZKfB0-5idYOo8JQKKLq_Kz- 9 | ''' 10 | 11 | from flask import Flask, request 12 | import requests 13 | from requests.auth import HTTPBasicAuth 14 | import json 15 | from datetime import datetime 16 | import base64 17 | 18 | app = Flask(__name__) 19 | 20 | base_url = '' 21 | consumer_key = '' 22 | consumer_secret = '' 23 | 24 | 25 | @app.route('/') 26 | def home(): 27 | return 'Hello World!' 28 | 29 | @app.route('/access_token') 30 | def get_access_token(): 31 | consumer_key = consumer_key 32 | consumer_secret = consumer_secret 33 | endpoint = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials' 34 | 35 | r = requests.get(endpoint, auth=HTTPBasicAuth(consumer_key, consumer_secret)) 36 | data = r.json() 37 | return data['access_token'] 38 | 39 | @app.route('/register') 40 | def register_urls(): 41 | endpoint = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl' 42 | access_token = _access_token() 43 | my_endpoint = base_url + "c2b/" 44 | headers = { "Authorization": "Bearer %s" % access_token } 45 | r_data = { 46 | "ShortCode": "600383", 47 | "ResponseType": "Completed", 48 | "ConfirmationURL": my_endpoint + 'con', 49 | "ValidationURL": my_endpoint + 'val' 50 | } 51 | 52 | response = requests.post(endpoint, json = r_data, headers = headers) 53 | return response.json() 54 | 55 | 56 | @app.route('/simulate') 57 | def test_payment(): 58 | endpoint = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate' 59 | access_token = _access_token() 60 | headers = { "Authorization": "Bearer %s" % access_token } 61 | 62 | data_s = { 63 | "Amount": 100, 64 | "ShortCode": "600383", 65 | "BillRefNumber": "test", 66 | "CommandID": "CustomerPayBillOnline", 67 | "Msisdn": "254708374149" 68 | } 69 | 70 | res = requests.post(endpoint, json= data_s, headers = headers) 71 | return res.json() 72 | 73 | @app.route('/b2c') 74 | def make_payment(): 75 | endpoint = 'https://sandbox.safaricom.co.ke/mpesa/b2c/v1/paymentrequest' 76 | access_token = _access_token() 77 | headers = { "Authorization": "Bearer %s" % access_token } 78 | my_endpoint = base_url + "/b2c/" 79 | 80 | data = { 81 | "InitiatorName": "apitest342", 82 | "SecurityCredential": "SQFrXJpsdlADCsa986yt5KIVhkskagK+1UGBnfSu4Gp26eFRLM2eyNZeNvsqQhY9yHfNECES3xyxOWK/mG57Xsiw9skCI9egn5RvrzHOaijfe3VxVjA7S0+YYluzFpF6OO7Cw9qxiIlynYS0zI3NWv2F8HxJHj81y2Ix9WodKmCw68BT8KDge4OUMVo3BDN2XVv794T6J82t3/hPwkIRyJ1o5wC2teSQTgob1lDBXI5AwgbifDKe/7Y3p2nn7KCebNmRVwnsVwtcjgFs78+2wDtHF2HVwZBedmbnm7j09JO9cK8glTikiz6H7v0vcQO19HcyDw62psJcV2c4HDncWw==", 83 | "CommandID": "BusinessPayment", 84 | "Amount": "200", 85 | "PartyA": "601342", 86 | "PartyB": "254708374149", 87 | "Remarks": "Pay Salary", 88 | "QueueTimeOutURL": my_endpoint + "timeout", 89 | "ResultURL": my_endpoint + "result", 90 | "Occasion": "Salary" 91 | } 92 | 93 | res = requests.post(endpoint, json = data, headers = headers) 94 | return res.json() 95 | 96 | @app.route('/lnmo') 97 | def init_stk(): 98 | endpoint = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest' 99 | access_token = _access_token() 100 | headers = { "Authorization": "Bearer %s" % access_token } 101 | my_endpoint = base_url + "/lnmo" 102 | Timestamp = datetime.now() 103 | times = Timestamp.strftime("%Y%m%d%H%M%S") 104 | password = "174379" + "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919" + times 105 | datapass = base64.b64encode(password.encode('utf-8')) 106 | 107 | data = { 108 | "BusinessShortCode": "174379", 109 | "Password": datapass, 110 | "Timestamp": times, 111 | "TransactionType": "CustomerPayBillOnline", 112 | "PartyA": "", # fill with your phone number 113 | "PartyB": "174379", 114 | "PhoneNumber": "", # fill with your phone number 115 | "CallBackURL": my_endpoint, 116 | "AccountReference": "TestPay", 117 | "TransactionDesc": "HelloTest", 118 | "Amount": 2 119 | } 120 | 121 | res = requests.post(endpoint, json = data, headers = headers) 122 | return res.json() 123 | 124 | @app.route('/lnmo', methods=['POST']) 125 | def lnmo_result(): 126 | data = request.get_data() 127 | f = open('lnmo.json', 'a') 128 | f.write(data) 129 | f.close() 130 | 131 | @app.route('/b2c/result', methods=['POST']) 132 | def result_b2c(): 133 | data = request.get_data() 134 | f = open('b2c.json', 'a') 135 | f.write(data) 136 | f.close() 137 | 138 | @app.route('/b2c/timeout', methods=['POST']) 139 | def b2c_timeout(): 140 | data = request.get_json() 141 | f = open('b2ctimeout.json', 'a') 142 | f.write(data) 143 | f.close() 144 | 145 | @app.route('/c2b/val', methods=['POST']) 146 | def validate(): 147 | data = request.get_data() 148 | f = open('data_v.json', 'a') 149 | f.write(data) 150 | f.close() 151 | 152 | @app.route('/c2b/con', methods=['POST']) 153 | def confirm(): 154 | data = request.get_json() 155 | f = open('data_c.json', 'a') 156 | f.write(data) 157 | f.close() 158 | 159 | 160 | def _access_token(): 161 | consumer_key = consumer_key 162 | consumer_secret = consumer_secret 163 | endpoint = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials' 164 | 165 | r = requests.get(endpoint, auth=HTTPBasicAuth(consumer_key, consumer_secret)) 166 | data = r.json() 167 | return data['access_token'] 168 | 169 | 170 | if __name__ == "__main__": 171 | app.run(host='0.0.0.0', port=5000, debug=True) --------------------------------------------------------------------------------