PHP code example of knox / mpesa-rest
1. Go to this page and download the library: Download knox/mpesa-rest library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
knox / mpesa-rest example snippets
use MPESA;
use Illuminate\Http\Request;
class PaymentsController extends Controller
{
public function registerURL(){
$mpesa = MPESA::registerC2bUrl();
}
}
<!-- @param(phone_number, amount, payment_type, @remarks) -->
mpesa = MPESA::b2c('254700123456',10,'PromotionPayment', 'No Remarks');
<!-- @param(short_code, amount, command, sender_identifier, receiver_identifier, @remarks, @account = '') -->
mpesa = MPESA::b2b('600000',100,'BusinessPayBill',4,4, 'No Remarks',123456);
<!-- @param(short_code, amount, command, sender_identifier, receiver_identifier, @remarks, @account = '') -->
mpesa = MPESA::b2b('600000',100,'BusinessBuyGoods',4,4, 'No Remarks');
<!-- @param() -->
mpesa = MPESA::registerC2bUrl();
<!-- @param(phone, amount, command, account, @short_code) -->
mpesa = MPESA::c2bSimulate('254700123456',100,'CustomerPayBillOnline','123456');
mpesa = MPESA::c2bSimulate('254700123456',1000,'CustomerBuyGoodsOnline');
<!-- @param(@remarks) -->
mpesa = MPESA::getAccountBalance();
<!-- @param(transaction, amount, receiver = null, receiver_identifier = 11, @remarks, @occassion) -->
mpesa = MPESA::doReversal('ND893KKHX1', 100, 602984, 4);
<!-- @param(phone, amount, account, @description) -->
mpesa = MPESA::stkPush('254700123456', 1000, 'Account 123');
<!-- @param(checkout_request_id) -->
mpesa = MPESA::stkQuery('ws_CO_14092017184227664');
<!-- @param(transaction, party = '', identifier_type = '4', @remarks, @occassion) -->
mpesa = MPESA::getTransactionStatus('ND893KKHX1', null,4);
public function c2bConfirmation(Request $request)
{
$response = json_decode($request->getContent(), true);
$mpesa_transaction_id = $response['TransID'];
$date_time = Carbon::parse($response['TransTime']);
$amount = $response['TransAmount'];
$account = strtoupper(preg_replace('/\s+/', '', $response['BillRefNumber']));
$merchant_transaction_id = $response['ThirdPartyTransID'];
$phone = $response['MSISDN'];
$payer = preg_replace('!\s+!', ' ', ucwords(strtolower($response['FirstName'] . ' ' . $response['MiddleName'] . ' ' . $response['LastName'])));
}