forked from brianosoro/STKPush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTKPush.php
118 lines (88 loc) · 3.26 KB
/
STKPush.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
<?php
include_once ("constants.php");
//======================================================================
// SAFARICOM STK PUSH
//======================================================================
//-----------------------------------------------------
// Author : Brian Osoro
// Company : Symatech Labs Ltd
// Website : www.symatechlabs.com
// Blog : www.brianosoro.com
// Twitter : @brayanosoro
// Email : [email protected] / [email protected]
//-----------------------------------------------------
class STKPush
{
public $timestamp;
function __construct()
{
$this->timestamp = date('YmdHis');
}
public function generateToken()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, TOKEN_URL);
$credentials = base64_encode(CONSUMER_KEY . ':' . CONSUMER_SECRET);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . $credentials
)); //setting a custom header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
return json_decode($curl_response, true) ['access_token'];
}
public function proccessRequest($amount, $phoneNumber)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, URL);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Authorization:Bearer ' . TOKEN
)); //setting custom header
$password = base64_encode(SHORT_CODE . PASS_KEY . $this->timestamp);
$curl_post_data = array(
//Fill in the request parameters with valid values
"BusinessShortCode" => SHORT_CODE,
"Password" => $password,
"Timestamp" => $this->timestamp,
"TransactionType" => "CustomerPayBillOnline",
"Amount" => $amount,
"PartyA" => $phoneNumber,
"PartyB" => SHORT_CODE,
"PhoneNumber" => $phoneNumber,
"CallBackURL" => CALLBACK_URL,
"AccountReference" => "Order 43",
"TransactionDesc" => "Paybill Online"
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));
return curl_exec($curl);
}
public function callbackResponse()
{
$jsonArray = json_decode(trim(file_get_contents('php://input')) , true);
$jsonObject = $jsonArray['Body']['stkCallback']['CallbackMetadata']['Item'];
$transactions = new Transactions();
foreach ($jsonObject as $key => $value)
{
if ($value[Name] == "Amount")
{
$amount = $value[Value];
}
else if ($value[Name] == "MpesaReceiptNumber")
{
$MpesaReceiptNumber = $value[Value];
}
else if ($value[Name] == "TransactionDate")
{
$TransactionDate = $value[Value];
}
else if ($value[Name] == "PhoneNumber")
{
$PhoneNumber = $value[Value];
}
}
$transactions->insert($amount, $MpesaReceiptNumber , $TransactionDate , $PhoneNumber);
}
}
?>