-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.php
438 lines (342 loc) · 14.3 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?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,$email) {
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`, `email`) VALUES ('$msisdn','$sessionid','$option','$donor_name', '$amount', '$network', '$walletno', '$volunteer_name', '$age', '$email')";
$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=="email"){
$progressq="UPDATE `progress` SET `sessionId`='$sessionid', `email`='$data' WHERE `ID`='$msisdn' ";
}
$progress=$conn->prepare($progressq);
$progress->execute();
}
function insertTransaction($amount,$donor_name,$walletno,$network,$TRAFFIC_ID) {
include('include/connect.php');
//Generate Client transaction ID
$clienttrans = "BAKE" . rand(100000, 999999);
$clienttransid = $clienttrans;
$status="PROGRESS";
$progressq="INSERT INTO `transactions`(`clientid`, `ussdtrafficid`, `donor_name`, `amount`, `status`, `reference`, `telcotransid`, `wallet_num`, `wallet_network`) VALUES ('$clienttransid','$TRAFFIC_ID','$donor_name','$amount','$status','donation payment','NULL','$walletno','$network')";
$progress=$conn->prepare($progressq);
$progress->execute();
sendMobileMoney($clienttransid,$walletno,$network,$amount,$TRAFFIC_ID);
}
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,$email) {
include('include/connect.php');
$progressq="INSERT INTO `volunteers`(`full_name`, `mobile_number`, `age`, `email`) VALUES ('$full_name','$mobile_number','$age','$email')";
$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= 'your_senderid';
$frog_username= 'your_frog_username';
$frog_password= 'your_frog_password';
//using the FROG legacy API to send simple SMS message
$messageApi='https://frog.wigal.com.gh/ismsweb/sendmsg';
$params='username='.$frog_username.'&password='.$frog_password.'&from='.$from.'&to='.$reciepients.'&message='.$message;
alllogs_log("SMS Parameter log on " , $time , $params);
$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);
alllogs_log("SMS request log on " , $time , $return);
curl_close($ch);
}
function sendEmai($email,$message)
{
include('include/connect.php');
$time= date("Y/m/d h:i:s");
$reciepients= $email;
$message= $message;
$from= 'your_senderid';
$frog_username= 'your_frog_username';
$frog_password= 'your_frog_password';
$mesgid = rand(10, 99);
//using the FROG V2 API to send simple Email message
$messageApi='https://frog.wigal.com.gh/api/v2/sendmsg/';
$messagedata = array(
'username'=> $frog_username,
'password'=> $frog_password,
'senderid'=> $from,
'destinations'=> [
[
'destination'=> $reciepients,
'msgid'=> $mesgid
]
],
'message'=> $message,
'service'=> 'EMAIL',
'subject'=> 'Thank you Message',
'fromemail'=> 'support@wigal.com.gh',
'smstype'=> ''
);
$jsonbody = json_encode($messagedata);
alllogs_log("Email Parameter log on " , $time , $jsonbody);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $messageApi,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $jsonbody,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept-Encoding: UTF-8'
),
));
$response = curl_exec($curl);
// @file_put_contents('logs.txt',"\n" . "Email request log on ". $time . "=> " . $response . "\n",FILE_APPEND);
alllogs_log("Email request log on " , $time , $response);
curl_close($curl);
// echo $response;
}
function sendVoice($mobile_number,$message)
{
include('include/connect.php');
$time= date("Y/m/d h:i:s");
$reciepients= $mobile_number;
$message= $message;
$from= 'your_senderid';
$frog_username= 'your_frog_username';
$frog_password= 'your_frog_password';
$mesgid = rand(10, 99);
//using the FROG V2 API to send simple Voice message
$messageApi='https://frog.wigal.com.gh/api/v2/sendmsg/';
$messagedata = array(
'username'=> $frog_username,
'password'=> $frog_password,
'senderid'=> $from,
'destinations'=> [
[
'destination'=> $reciepients,
'msgid'=> $mesgid
]
],
'message'=> $message,
'service'=> 'VOICE',
'subject'=> 'Thank you Message',
'fromemail'=> '',
'smstype'=> ''
);
$jsonbody = json_encode($messagedata);
alllogs_log("Voice Parameter log on " , $time , $jsonbody);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $messageApi,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $jsonbody,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept-Encoding: UTF-8'
),
));
$response = curl_exec($curl);
// @file_put_contents('logs.txt',"\n" . "Voice request log on ". $time . "=> " . $response . "\n",FILE_APPEND);
alllogs_log("Voice request log on " , $time , $response);
curl_close($curl);
// echo $response;
}
function sendMobileMoney($clienttransid,$walletno,$network,$amount,$TRAFFIC_ID)
{
$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,
"ussdtrafficid"=>$TRAFFIC_ID //Provide this if you want the OTP to be removed from your Payment prompt
);
$jsonbody = json_encode($params);
// @file_put_contents('logs.txt',"Payment request log on ". $time ."=> ". $jsonbody . "\n",FILE_APPEND);
alllogs_log("Payment request log on " , $time , $jsonbody);
//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);
alllogs_log("Payment response log on " , $time , $response);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
} else {
$report = json_decode($response);
if($report->status == 'OK')
{
updateTransaction($clienttransid,"PENDING");
// echo $response;
}
}
}
function alllogs_log($log_msg, $time, $data)
{
$log_filename = "logs";
if (!file_exists($log_filename))
{
// create directory/folder uploads.
mkdir($log_filename, 0777, true);
}
$log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
// if you don't add `FILE_APPEND`, the file will be erased each time you add a log
file_put_contents($log_file_data, $log_msg . $time ."=> ". $data . "\n", FILE_APPEND);
}
?>