Last active
August 26, 2022 15:13
-
-
Save britisharmy/0fdd58919192d21795b22eaaf0810e78 to your computer and use it in GitHub Desktop.
This file contains 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
Step 1 | |
Install using composer this package | |
composer require kopokopo/k2-connect-php | |
Step 2 | |
Get production credentials from your KopoKopo account. Feed the requisite parameters and you are ready to go. | |
<?php | |
/** | |
K2 SDK | |
*/ | |
use Kopokopo\SDK\K2; | |
/** | |
Autoload | |
*/ | |
require 'vendor/autoload.php'; | |
/** | |
Display Errors | |
*/ | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
error_reporting(E_ALL); | |
/** | |
Options | |
*/ | |
$options = [ | |
'clientId' => '', | |
'clientSecret' => '', | |
'apiKey' => '', | |
'baseUrl' => 'https://api.kopokopo.com' | |
]; | |
$K2 = new K2($options); | |
/** | |
Create Token | |
*/ | |
$tokens = $K2->TokenService(); | |
/** | |
Use Token | |
*/ | |
$result = $tokens->getToken(); | |
if ($result['status'] == 'success') { | |
$data = $result['data']; | |
echo "My access token is: " . $data['accessToken'] . " It expires in: " . $data['expiresIn'] . "<br>"; | |
} | |
/** | |
STK Push | |
**Rate Limit Not Known | |
*/ | |
$stk = $K2->StkService(); | |
$response = $stk->initiateIncomingPayment([ | |
'paymentChannel' => 'M-PESA STK Push', | |
'tillNumber' => '', | |
'firstName' => '', | |
'lastName' => '', | |
'phoneNumber' => '', | |
'amount' => 15, | |
'currency' => 'KES', | |
'email' => '', | |
'callbackUrl' => '', | |
'accessToken' => $data['accessToken'], | |
]); | |
/** | |
Response Log | |
*/ | |
echo "<pre>"; | |
print_r($response); | |
echo "</pre>"; | |
/** | |
Log STK Push Outcome | |
*/ | |
if ($response['status'] == 'success') { | |
$data = $result['data']; | |
return response('Request sent to your phone', 200); | |
} else { | |
return response($response['status'], 400); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment