Last active
January 30, 2023 10:26
-
-
Save levizwannah/9b521c5f5639f39af433e37346f545e1 to your computer and use it in GitHub Desktop.
STK push using configure and method chaining
This file contains hidden or 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
<?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