forked from wigalgh/smartussd_api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
300 lines (222 loc) · 10.1 KB
/
functions.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*Below are the various functions the ussdflow script depends on
NB: These functions are self explanatory, so ensure to pay strict attention during your implementation.*/
function deleteTracking($msisdn) {
include('include/connect.php');
//query to fetch all data of msisdn from tracking Table//
$resettq="DELETE FROM tracking WHERE ID='$msisdn' ";
$resett=$conn->prepare($resettq);
$resett->execute();
}
function insertTracking($msisdn,$sessionid,$mode,$username,$time,$userdata,$track) {
include('include/connect.php');
//query to insert msisdn into the tracking table//
$new_menuq="INSERT INTO tracking VALUES('$msisdn','$sessionid','$mode','$username','$time','$userdata','$track')";
$new_menu=$conn->prepare($new_menuq);
$new_menu->execute();
}
function deleteProgress($msisdn) {
include('include/connect.php');
//query to delect msisdn from progress table//
$resett1q="DELETE FROM progress WHERE ID='$msisdn' ";
$resett1=$conn->prepare($resett1q);
$resett1->execute();
}
function insertProgress($msisdn,$sessionid,$option,$donor_name,$amount,$network,$walletno,$volunteer_name,$age,$constituency) {
include('include/connect.php');
//query to insert passed parameters into the progress table//
$progressq="INSERT INTO `progress`(`ID`, `sessionId`, `option`, `donor_name`, `amount`, `network`, `walletno`, `volunteer_name`, `age`, `constituency`) VALUES ('$msisdn','$sessionid','$option','$donor_name', '$amount', '$network', '$walletno', '$volunteer_name', '$age', '$constituency')";
$progress=$conn->prepare($progressq);
$progress->execute();
}
function getTracking($msisdn) {
include('include/connect.php');
//query to fetch all data of msisdn from tracking Table//
$check_menuq="SELECT * FROM tracking WHERE ID='$msisdn' ";
$check_menu=$conn->prepare($check_menuq);
$check_menu->execute();
$row=$check_menu->fetch();
return $row;
}
function getProgress($msisdn) {
include('include/connect.php');
//query to fetch all data of msisdn from progress Table//
$selectprogq="SELECT * FROM progress WHERE ID='$msisdn' ";
$selectprog=$conn->prepare($selectprogq);
$selectprog->execute();
$rowopt=$selectprog->fetch();
return $rowopt;
}
function updateTracking($msisdn,$sessionid,$mode,$username,$time,$userdata,$track) {
include('include/connect.php');
//query to update msisdn with passed parameters into the tracking table//
$menu_updateq="UPDATE tracking SET sessionId='$sessionid',mode='$mode',username='$username',`time`='$time',userData='$userdata',track='$track' WHERE ID='$msisdn'";
$menu_update=$conn->prepare($menu_updateq);
$menu_update->execute();
}
function updateProgress($msisdn,$sessionid,$field,$data) {
include('include/connect.php');
//query to update individual field based on the option passed//
$progressq="";
if($field=="option"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `option`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="donor_name"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `donor_name`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="amount"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `amount`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="network"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `network`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="walletno"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `walletno`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="volunteer_name"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `volunteer_name`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="age"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `age`='$data' WHERE `ID`='$msisdn' ";
}
if($field=="constituency"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `constituency`='$data' WHERE `ID`='$msisdn' ";
}
$progress=$conn->prepare($progressq);
$progress->execute();
}
function insertTransaction($amount,$donor_name,$walletno,$network) {
include('include/connect.php');
//Generate Client transaction ID
$clienttrans = "BAKE" . rand(100000, 999999);
$clienttransid = $clienttrans;
$status="PROGRESS";
$progressq="INSERT INTO `transactions`(`clientid`, `donor_name`, `amount`, `status`, `reference`, `telcotransid`, `wallet_num`, `wallet_network`) VALUES ('$clienttransid','$donor_name','$amount','$status','donation payment','NULL','$walletno','$network')";
$progress=$conn->prepare($progressq);
$progress->execute();
sendMobileMoney($clienttransid,$walletno,$network,$amount);
}
function updateTransaction($clientid,$status) {
include('include/connect.php');
$menu_updateq="UPDATE transactions SET status='$status' WHERE clientid='$clientid'";
$menu_update=$conn->prepare($menu_updateq);
$menu_update->execute();
if ($status=="PAID") {
$transdetails=gettrans($clientid);
$mmdetails=getmmtrans($clientid);
$donor_name=$transdetails['donor_name'];
$amount=$transdetails['amount'];
$clientid=$transdetails['clientid'];
$wallet_network=$transdetails['wallet_network'];
$wallet_number=$transdetails['wallet_num'];
$mmtransid=$mmdetails['telcotransid'];
$menu_updateq="UPDATE transactions SET status='$status', telcotransid= '$mmtransid' WHERE clientid='$clientid'";
$menu_update=$conn->prepare($menu_updateq);
$menu_update->execute();
insertdonation($donor_name,$amount,$clientid,$mmtransid,$wallet_network,$wallet_number);
}
}
function insertdonation($donor_name,$amount,$clientid,$mmtransid,$wallet_network,$wallet_number) {
include('include/connect.php');
$progressq="INSERT INTO `donations`(`name`, `amount`, `clientid`, `mmtransid`, `wallet_network`, `wallet_number`) VALUES ('$donor_name','$amount','$clientid','$mmtransid','$wallet_network','$wallet_number')";
$progress=$conn->prepare($progressq);
$progress->execute();
}
function insertvolunteer($full_name,$mobile_number,$age,$constituency) {
include('include/connect.php');
$progressq="INSERT INTO `volunteers`(`full_name`, `mobile_number`, `age`, `constituency`) VALUES ('$full_name','$mobile_number','$age','$constituency')";
$progress=$conn->prepare($progressq);
$progress->execute();
}
function gettrans($clientid) {
include('include/connect.php');
$selectprogq="SELECT * FROM transactions WHERE clientid='$clientid' ";
$selectprog=$conn->prepare($selectprogq);
$selectprog->execute();
$rowopt=$selectprog->fetch();
return $rowopt;
}
function getmmtrans($clientid) {
include('include/connect.php');
$selectprogq="SELECT * FROM mm_transactions WHERE clienttransid='$clientid' and status='PAID' ";
$selectprog=$conn->prepare($selectprogq);
$selectprog->execute();
$rowopt=$selectprog->fetch();
return $rowopt;
}
function insertmmTransaction($clienttransid,$clientreference,$telcotransid,$transactionid,$status,$statusdate,$reason) {
include('include/connect.php');
$progressq="INSERT INTO `mm_transactions`(`clienttransid`, `clientreference`, `telcotransid`, `transactionid`, `status`, `statusdate`, `reason`) VALUES ('$clienttransid','$clientreference','$telcotransid','$transactionid','$status','$statusdate','$reason')";
$progress=$conn->prepare($progressq);
$progress->execute();
}
function sendSMS($msisdn,$message)
{
include('include/connect.php');
$time=date("Y/m/d h:i:s");
$reciepients=$msisdn;
$message=$message;
$from='WIGAL';
$smsusername='your_isms_username';
$smspassword='your_isms_password';
$sent_by="WIGAL";
//initiate api to send message
$messageApi='https://isms.wigalsolutions.com/ismsweb/sendmsg/';
$params='username='.$smsusername.'&password='.$smspassword.'&from='.$from.'&to='.$reciepients.'&message='.$message;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$messageApi);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
$return=curl_exec($ch);
@file_put_contents('logs.txt',"\n" . "SMS request log on ". $time . "=> " . $return . "\n",FILE_APPEND);
curl_close($ch);
}
function sendMobileMoney($clienttransid,$walletno,$network,$amount)
{
$time=date("Y/m/d h:i:s");
$params = array(
'amount'=>$amount,
'appid'=>your_redde_appid,
'clientreference'=>"Donation Payment by client",
'clienttransid'=>$clienttransid,
'description'=>"Donation Payment",
'nickname'=>"Bakeside Campaign",
'paymentoption'=>$network,
'vouchercode'=>"",
'walletnumber'=>$walletno
);
$jsonbody = json_encode($params);
@file_put_contents('logs.txt',"Payment request log on ". $time ."=> ". $jsonbody . "\n",FILE_APPEND);
//initiate api to send Mobile Money Request
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.reddeonline.com/v1/receive/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $jsonbody,
CURLOPT_HTTPHEADER => array(
"ApiKey: your_redde_apikey",
"Content-Type: application/json"
),
)
);
$response = curl_exec($curl);
@file_put_contents('logs.txt',"Payment response log on ".$time ."=> ". $response . "\n",FILE_APPEND);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
} else {
$report = json_decode($response);
if($report->status == 'OK')
{
updateTransaction($clienttransid,"PENDING");
// echo $response;
}
}
}
?>