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