2017 © Pedro Peláez
 

library mpesa-rest

Laravel Package implementation of the MPESA REST API

image

knox/mpesa-rest

Laravel Package implementation of the MPESA REST API

  • Friday, May 25, 2018
  • by knox
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

MPESA REST API for Laravel

Laravel Package for the MPESA REST API, (*1)

Installation

Add this package using Composer

From the command line inside your project directory, simply type:, (*2)

composer require knox/mpesa-rest, (*3)

Update your config

NB: from laravel 5.5 due to autodiscovery the below can be skipped, (*4)

Add the service provider to the providers array in config/app.php:, (*5)

Knox\MPESA\MpesaServiceProvider::class, (*6)

Add the facade to the aliases array in config/app.php:, (*7)

'MPESA' => Knox\MPESA\Facades\MPESA::class, (*8)

Publish the package configuration

Publish the configuration file and migrations by running the provided console command:, (*9)

php artisan vendor:publish --provider="Knox\MPESA\MpesaServiceProvider", (*10)

Setup

Environmental Variables

MPESA_ENV='live' for production and 'test' for sandboxbr/ MPESA_VERSION='v1' or 'v2'br/ MPESA_CONSUMER_KEY=consumer keybr/ MPESA_CONSUMER_SECRET=consumer secretbr/ MPESA_IDENTIFIER=shortcode shortcode or tillbr/ MPESA_SHORT_CODE=shortcodebr/ MPESA_PASSKEY=passkeybr/, (*11)

MPESA_INITIATOR_NAME=initiator usernamebr/ MPESA_INITIATOR_PASSWORD=initiator passwordbr/, (*12)

MPESA_B2C_TIMEOUT_URL=url in your sitebr/ MPESA_B2C_RESULT_URL=url in your sitebr/ MPESA_B2B_TIMEOUT_URL=url in your sitebr/ MPESA_B2B_RESULT_URL=url in your sitebr/, (*13)

MPESA_STK_CALLBACK_URL=url in your sitebr/ MPESA_C2B_VALIDATION_URL=url in your sitebr/ MPESA_C2B_CONFIRMATION_URL=url in your sitebr/, (*14)

MPESA_ACCOUNT_BALANCE_TIMEOUT_URL=url in your sitebr/ MPESA_ACCOUNT_BALANCE_CONFIRMATION_URL=url in your sitebr/, (*15)

MPESA_REVERSAL_TIMEOUT_URL=url in your sitebr/ MPESA_REVERSAL_CONFIRMATION_URL=url in your sitebr/, (*16)

MPESA_TRANSACTION_STATUS_TIMEOUT_URL=url in your sitebr/ MPESA_TRANSACTION_STATUS_CONFIRMATION_URL=url in your sitebr/, (*17)

MPESA_IDENTITY_CALLBACK_URL=url in your sitebr/, (*18)

Usage

At the top of your controller include the facadebr/ use MPESA;, (*19)

Registration of C2B Urls

If you haven't registered the mpesa url callbacks then use the belowbr/, (*20)

use MPESA;
use Illuminate\Http\Request;

class PaymentsController extends Controller
{
    public function registerURL(){
        $mpesa = MPESA::registerC2bUrl();
    }
}

Posible Operations


mpesa = MPESA::b2c('254700123456',10,'PromotionPayment', 'No Remarks');


mpesa = MPESA::b2b('600000',100,'BusinessPayBill',4,4, 'No Remarks',123456);


mpesa = MPESA::b2b('600000',100,'BusinessBuyGoods',4,4, 'No Remarks');


mpesa = MPESA::registerC2bUrl();


mpesa = MPESA::c2bSimulate('254700123456',100,'CustomerPayBillOnline','123456');
mpesa = MPESA::c2bSimulate('254700123456',1000,'CustomerBuyGoodsOnline');


mpesa = MPESA::getAccountBalance();


mpesa = MPESA::doReversal('ND893KKHX1', 100, 602984, 4);


mpesa = MPESA::stkPush('254700123456', 1000, 'Account 123');


mpesa = MPESA::stkQuery('ws_CO_14092017184227664');


mpesa = MPESA::getTransactionStatus('ND893KKHX1', null,4);

Example response handler

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'])));
}

All Done

Feel free to report any issues, (*21)

The Versions

25/05 2018

dev-master

9999999-dev

Laravel Package implementation of the MPESA REST API

  Sources   Download

The Requires

 

by Timothy Radier

08/12 2017

v1.0

1.0.0.0

Laravel Package implementation of the MPESA REST API

  Sources   Download

The Requires

 

by Timothy Radier