1. Go to this page and download the library: Download savannabits/daraja library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
savannabits / daraja example snippets
use Savannabits\Daraja\Daraja;
$shortcode = "600737";//Your Paybill or till number here
$confirmationURL = 'your dynamic validation url here';
$validationURL = 'your dynamic validation url here'; // Optional. Leave null if you don't want validation
$environment = "sandbox"; // or "live"
$responseType = "Cancelled"; //Default Response type in case the validation URL is unreachable or is undefined. Either Cancelled or Completed as per the safaricom documentation
$response = Daraja::getInstance()
->setCredentials("CONSUMER KEY","CONSUMER SECRET",$environment)
->registerCallbacks($shortcode, $confirmationURL, $validationURL,$responseType);
use Savannabits\Daraja\Daraja;
/**
* Here is the c2b Function documentation to help you understand the params:
* Use this function to initiate a C2B transaction
* @param $ShortCode | 6 digit M-Pesa Till Number or PayBill Number
* @param $CommandID | Unique command for each transaction type. either "CustomerPayBillOnline" or "CustomerBuyGoodsOnline"
* @param $Amount | The amount been transacted.
* @param $Msisdn | MSISDN (phone number) sending the transaction, start with country code without the plus(+) sign.
* @param $BillRefNumber | Bill Reference Number (Optional).
* @return mixed|string
*/
$shortcode = "YOUR SHORTCODE";
$commandID = "CustomerPayBillOnline";
$amount = 100;
$msisdn = "07xxxxxxxx"; // See safaricom daraja documentation and check your credentials for the specific number given for testing.
$billRefNumber = "THE PAYBILL ACCOUNT NO."; // e.g "MAMA MBOGA 212"
$response = Daraja::getInstance()
->setCredentials("YOUR CONSUMER KEY","YOUR CONSUMER SECRET","sandbox")
->c2b($shortcode, $commandID, $amount, $msisdn, $billRefNumber);
/**
* Use this function to initiate an STKPush Simulation
* @param $BusinessShortCode | The organization shortcode used to receive the transaction.
* @param $LipaNaMpesaPasskey | The password for encrypting the request. This is generated by base64 encoding BusinessShortcode, Passkey and Timestamp.
* @param $TransactionType | The transaction type to be used for this request. Only CustomerPayBillOnline is supported.
* @param $Amount | The amount to be transacted.
* @param $PartyA | The MSISDN sending the funds.
* @param $PartyB | The organization shortcode receiving the funds
* @param $PhoneNumber | The MSISDN sending the funds.
* @param $CallBackURL | The url to where responses from M-Pesa will be sent to.
* @param $AccountReference | Used with M-Pesa PayBills.
* @param $TransactionDesc | A description of the transaction.
* @param $Remark | Remarks
* @return mixed|string
*/
\Savannabits\Daraja\Daraja::getInstance()
->setCredentials("CONSUMER KEY","CONSUMER SECRET","sandbox")
->STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remark);
use Savannabits\Daraja\Daraja;
class MyController extends \App\Http\Controllers\Controller {
public function c2bValidationCallback(Request $request) {
// Perform your validations here and set the status
$status = true; // Or false based on whether you want to accept or reject the transaction.
Daraja::getInstance()->finishTransaction($status);
}
public function c2bConfirmationCallback(Request $request) {
//Get Response data
$response = Daraja::getInstance()->getDataFromCallback();
// $response = $request->all(); //Alternatively...
// Do what you want with the data
// ...
// Finish Transaction
Daraja::getInstance()->finishTransaction(true);
}
public function stkConfirmationCallback(Request $request) {
$mpesa = new Daraja();
// Get data from safaricom response.
$data = $mpesa->getDataFromCallback();
\Log::info($data);
// Handle the data
//...
//Finish Transaction by sending acknowledgment to safaricom
$mpesa->finishTransaction(true);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.