-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
// access token | ||
$consumerKey = ''; //Fill with your app Consumer Key | ||
$consumerSecret = ''; // Fill with your app Secret | ||
|
||
$headers = ['Content-Type:application/json; charset=utf8']; | ||
|
||
$access_token_url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; | ||
$curl = curl_init($access_token_url); | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | ||
curl_setopt($curl, CURLOPT_HEADER, FALSE); | ||
curl_setopt($curl, CURLOPT_USERPWD, $consumerKey.':'.$consumerSecret); | ||
$result = curl_exec($curl); | ||
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
$result = json_decode($result); | ||
$access_token = $result->access_token; | ||
curl_close($curl); | ||
|
||
# initiating the transaction | ||
|
||
# define the variales | ||
$initiate_url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest'; | ||
|
||
# provide the following details, this part is found on your test credentials on the developer account | ||
$BusinessShortCode = ''; | ||
$Passkey = ''; | ||
|
||
|
||
/* | ||
This are your info, for | ||
$PartyA should be the ACTUAL clients phone number or your phone number, format 2547******** | ||
$AccountRefference, it maybe invoice number, account number etc on production systems, but for test just put anything | ||
TransactionDesc can be anything, probably a better description of or the transaction | ||
$Amount this is the total invoiced amount, Any amount here will be | ||
actually deducted from a clients side/your test phone number once the PIN has been entered to authorize the transaction. | ||
for developer/test accounts, this money will be reversed automatically by midnight. | ||
*/ | ||
|
||
$PartyA = ''; // This is your phone number, | ||
$AccountReference = ''; | ||
$TransactionDesc = ''; | ||
$Amount = ''; | ||
|
||
|
||
$Timestamp = date('YmdGis'); | ||
$CallBackURL = 'http://kidonda.us/projects/MPESA_API/callback_url.php'; | ||
|
||
# Get the base64 encoded string -> $password. The passkey is the M-PESA Public Key | ||
$Password = base64_encode($BusinessShortCode.$Passkey.$Timestamp); | ||
|
||
$stkheader = ['Content-Type:application/json','Authorization:Bearer '.$access_token]; | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $initiate_url); | ||
curl_setopt($curl, CURLOPT_HTTPHEADER, $stkheader); //setting custom header | ||
|
||
$curl_post_data = array( | ||
//Fill in the request parameters with valid values | ||
'BusinessShortCode' => $BusinessShortCode, | ||
'Password' => $Password, | ||
'Timestamp' => $Timestamp, | ||
'TransactionType' => 'CustomerPayBillOnline', | ||
'Amount' => $Amount, | ||
'PartyA' => $PartyA, | ||
'PartyB' => $BusinessShortCode, | ||
'PhoneNumber' => $PartyA, | ||
'CallBackURL' => $CallBackURL, | ||
'AccountReference' => $AccountReference, | ||
'TransactionDesc' => $TransactionDesc | ||
); | ||
|
||
$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); | ||
print_r($curl_response); | ||
|
||
echo $curl_response; | ||
?> |