Skip to content

Instantly share code, notes, and snippets.

@levizwannah
Last active January 30, 2023 10:25
Show Gist options
  • Save levizwannah/0d75710c5b86d39060fb718076045b17 to your computer and use it in GitHub Desktop.
Save levizwannah/0d75710c5b86d39060fb718076045b17 to your computer and use it in GitHub Desktop.
STK push for Till number using properties
<?php
require(__DIR__ . "/vendor/autoload.php");
// or read getting started if the vendor is not available
use LeviZwannah\MpesaSdk\Mpesa;
$config = [
"key" => "consumer Key",
"secret" => "consumer secret",
"code" => "business short code",
"passkey" => "passkey for stk push"
];
$mpesa = Mpesa::new()->configure($config);
$stk = $mpesa->stk();
$stk->till(1234567);
$stk->phone("07123456789") // handles +2547, 07, and 2547
->amount(1)
->callback("https://callback.url")
->reference("optional account number")
->description("optional transaction description");
$stk->buygoods();
$stk->push(); // or $stk();
// check error
$error = $stk->error();
if($error){
echo "Error with code: $error->code and message: $error->message occurred";
}
// check acceptance
if(!$stk->accepted()){
echo "Request not accepted. Look at the response to see";
}
// get the response
$response = $stk->response();
// ...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment