How to use the Mpesa Express API (Daraja) with PHP:

Sample 1

Here is an example of how you can use the Mpesa Express API (Daraja) with PHP:

				
					<?php // Replace YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, and YOUR_ACCESS_TOKEN with your actual values
define("CONSUMER_KEY", "YOUR_CONSUMER_KEY");
define("CONSUMER_SECRET", "YOUR_CONSUMER_SECRET");
define("ACCESS_TOKEN", "YOUR_ACCESS_TOKEN");

// Set up the API endpoint and parameters
$url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest";
$partyA = 254700000000; // Replace with the phone number of the customer
$partyB = CONSUMER_KEY;
$amount = 100; // Replace with the amount to be charged
$callbackURL = "https://example.com/callback";
$accountReference = "12345678"; // Replace with your own account reference
$transactionDesc = "Purchase"; // Replace with a description of the transaction

$timestamp = date("YmdHis");
$password = base64_encode(CONSUMER_KEY . CONSUMER_SECRET . $timestamp);

// Set up the request body
$requestBody = array(
    "BusinessShortCode" => CONSUMER_KEY,
    "Password" => $password,
    "Timestamp" => $timestamp,
    "TransactionType" => "CustomerPayBillOnline",
    "Amount" => $amount,
    "PartyA" => $partyA,
    "PartyB" => $partyB,
    "PhoneNumber" => $partyA,
    "CallBackURL" => $callbackURL,
    "AccountReference" => $accountReference,
    "TransactionDesc" => $transactionDesc
);

// Set up the authorization header
$headers = array(
    "Authorization: Bearer " . ACCESS_TOKEN,
    "Content-Type: application/json"
);

// Initialize cURL
$ch = curl_init();

// Set the API endpoint, request body, authorization header, and other options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestBody));

// Execute the request and get the response
$response = curl_exec($ch);

// Close cURL
curl_close($ch);

// Decode the response and print the result
$responseData = json_decode($response, true);
print_r($responseData);

				
			

This code sends a POST request to the Mpesa Express API using cURL, with the necessary authorization header, request body, and parameters. The API responds with a JSON object containing the result of the request.

You can then use this response to check the status of the request and handle any errors that may have occurred.

Sample 2

				
					<?php // Replace YOUR_CONSUMER_KEY and YOUR_CONSUMER_SECRET with your actual consumer key and secret
define("CONSUMER_KEY", "YOUR_CONSUMER_KEY");
define("CONSUMER_SECRET", "YOUR_CONSUMER_SECRET");

// Replace YOUR_ACCESS_TOKEN with your actual access token
define("ACCESS_TOKEN", "YOUR_ACCESS_TOKEN");

// Set up the API endpoint and request body
$url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest";
$requestBody = array(
    "BusinessShortCode" => "123456", // Replace with your business short code
    "Password" => base64_encode("123456" . "YOUR_TIMESTAMP" . "YOUR_PASSKEY"), // Replace YOUR_TIMESTAMP and YOUR_PASSKEY with your actual timestamp and passkey
    "Timestamp" => "YOUR_TIMESTAMP",
    "TransactionType" => "CustomerPayBillOnline",
    "Amount" => "100", // Replace with the actual amount to be paid
    "PartyA" => "254708374149", // Replace with the customer's phone number
    "PartyB" => "123456", // Replace with your business short code
    "PhoneNumber" => "254708374149", // Replace with the customer's phone number
    "CallBackURL" => "https://your-callback-url.com/callback", // Replace with your actual callback URL
    "AccountReference" => "Invoice 12345", // Replace with an invoice or account reference
    "TransactionDesc" => "Payment for goods and services" // Replace with a description of the transaction
);

// Set up the authorization header
$headers = array(
    "Authorization: Bearer " . ACCESS_TOKEN
);

// Initialize cURL
$ch = curl_init();

// Set the API endpoint, request body, authorization header, and other options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestBody));

// Execute the request and get the response
$response = curl_exec($ch);

// Close cURL
curl_close($ch);

// Print the response
echo $response;

				
			

This code sends a POST request to the Mpesa Express API endpoint using cURL, with the necessary authorization header, request body, and other options. The request body contains the necessary parameters for initiating a mobile payment, such as the business short code, amount to be paid, customer’s phone number, and callback URL.

The API responds with a JSON object containing the status of the request and other details. You can then use this information to verify the status of the payment

What's your reaction?
0Smile0Shocked0Cool0Sad0Laugh