Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save levizwannah/9b521c5f5639f39af433e37346f545e1 to your computer and use it in GitHub Desktop.
Save levizwannah/9b521c5f5639f39af433e37346f545e1 to your computer and use it in GitHub Desktop.
STK push using configure and method chaining
<?php
require(__DIR__ . "/vendor/autoload.php");
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 configure
$stkConfig = [
"phone" => $phone,
"amount" => $amount,
"callback" => $callbackUrl,
"reference" => $accountNumber,
"description" => $transDescription,
"till" => 1234567
];
// method chaining
$stk->configure($stkConfig)->buygoods()->push();
$error = $stk->error();
if($error){
echo "Error with code: $error->code and message: $error->message occurred";
}
if(!$stk->accepted()){
echo "Request not accepted. Look at the response to see";
}
$response = $stk->response();
// ...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment