-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
120 lines (98 loc) · 3.76 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
<?php
// declare variables
$consumerKey = 'yGq4s18LSbNb38iVczvUAgX7HpLp6xCl';
$consumerSecret = 'b3EIQYhcVdanDHnr';
$shortcode='603021';
$amount='1';
$phone='254708374149';
//get access token
function generateToken()
{
$url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
$credentials = base64_encode('ToO5SO5ncy1Ru9oSpbCBRsUEfH9idEYy:fg51hQnivrlGlTMF');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$credentials));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$json_decode = json_decode($curl_response);
$access_token = $json_decode->access_token;
return $access_token;
}
//register urls
function registerURL($shortcode)
{
$url = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.generateToken()));
$curl_post_data = array(
'ShortCode' => $shortcode,
'ResponseType' => 'Completed',
'ConfirmationURL' => 'http://cac12eeb18f7.ngrok.io/m-pesa/confirmation/',
'ValidationURL' => 'http://cac12eeb18f7.ngrok.io/m-pesa/validation/'
);
$data_string = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$curl_response = curl_exec($curl);
return $curl_response;
}
function simulatC2B($amount, $phone)
{
registerURL(603021);
global $shortcode;
$url = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.generateToken()));
$curl_post_data = array(
'ShortCode' => $shortcode,
'CommandID' => 'CustomerPayBillOnline',
'Amount' => $amount,
'Msisdn' => $phone,
'CallBackURL' => 'http://cac12eeb18f7.ngrok.io/m-pesa/callback/',
'BillRefNumber' => '00000'
);
$data_string = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$curl_response = curl_exec($curl);
return $curl_response;
}
function payment()
{
$url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
$amount='4';
$BusinessShortCode = 174379;
$LipaNaMpesaPasskey = 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919';
$date = new DateTime();
$timestamp = $date->format('YmdHis');
$password=base64_encode($BusinessShortCode.$LipaNaMpesaPasskey.$timestamp);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.generateToken()));
$curl_post_data = array(
'BusinessShortCode' => $BusinessShortCode,
'Password' => $password,
'Timestamp' => $timestamp,
'TransactionType' => 'CustomerPayBillOnline',
'Amount' => '1',
'PartyA' => '254719578752',
'PartyB' => '174379',
'PhoneNumber' => '254719578752',
'CallBackURL' => 'http://cac12eeb18f7.ngrok.io/m-pesa/callback/',
'AccountReference' => 'testing',
'TransactionDesc' => 'test'
);
$data_string = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$curl_response = curl_exec($curl);
return $curl_response;
}
?>