-
Notifications
You must be signed in to change notification settings - Fork 12
/
mpesa-callback-url.php
58 lines (35 loc) · 1.17 KB
/
mpesa-callback-url.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
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Headers: Origin, X-Requested-With,X-Auth-Token, Content-Type, Accept');
$stkCallbackResponse = file_get_contents('php://input');
/*
$logFile = "logs/IPN-".time().".json";
$log = fopen($logFile, "a");
fwrite($log, $stkCallbackResponse);
fclose($log);
*/
$ipn_data = json_decode($stkCallbackResponse,true);
$new_status = 'COMPLETED';
$checkout_request_id = $ipn_data['Body']['stkCallback']['CheckoutRequestID'];
$result_code = $ipn_data['Body']['stkCallback']['ResultCode'];
$mpesa_confirmation_code = $ipn_data['Body']['stkCallback']['CallbackMetadata']['Item']['1']['Value'];
if($result_code != 0){
$new_status = 'CANCELLED';
}
//Process Database Saving of data
include 'lib/class.connect.php';
$dbConnect = new Connect();
$dbConnect = $dbConnect->db();
//Check If signature matches
$save = mysqli_query($dbConnect,
"UPDATE
mpesa_transactions
SET
txn_status = '$new_status',
mpesa_confirmation_id = '$mpesa_confirmation_code'
WHERE
checkout_request_id = '$checkout_request_id'") or mysqli_error($dbConnect);
$dbConnect->close();
exit();
?>