ssiva/mpesa-laravel-sdk

This package provides a seamless integration of M-PESA Daraja APIs in Laravel applications.

v1.1.5 2023-04-13 16:22 UTC

This package is auto-updated.

Last update: 2024-10-25 18:13:49 UTC


README

This package provides a seamless integration of M-PESA Daraja APIs in Laravel applications, allowing developers to easily handle:

  • B2C (Business to Customer)
  • C2B (Customer to Business)
  • B2B (Business to Business)
  • Account Balance inquiries
  • Transaction reversals queries
  • Transaction status queries.

It simplifies the implementation process by providing an intuitive interface for making API requests, handling responses, and managing errors. With this package, developers can seamlessly integrate M-PESA Daraja APIs in their Laravel projects, saving time and effort.

Installation and Setup

  • Run the command below

    composer require ssiva/mpesa-laravel-sdk
    
  • Run the command below to publish the package config file config/mpesa.php:

    php artisan vendor:publish --tag=mpesa_config

Configuration

Set up the config values as required

  1. Account API Online Config

         MPESA_SHORTCODE=
         MPESA_RESULT_URL=
         MPESA_TIMEOUT_URL=
         MPESA_INITIATOR_NAME=
         MPESA_SECURITY_CREDENTIAL=
         MPESA_SECURITY_CERT=
         MPESA_ACCOUNT_IDENTIFIER=
  2. LipaNaMpesa API Online Config

         MPESA_SHORTCODE=
         MPESA_CALLBACK_URL=
         MPESA_PASSKEY=
         MPESA_ONLINE_TRANSACTION_TYPE=
  3. B2C API Config

         MPESA_SHORTCODE=
         MPESA_RESULT_URL=
         MPESA_TIMEOUT_URL=
         MPESA_INITIATOR_NAME=
         MPESA_SECURITY_CREDENTIAL=
         MPESA_SECURITY_CERT=
         MPESA_ACCOUNT_IDENTIFIER=
         MPESA_B2C_COMMAND=
  4. B2B API Config

         MPESA_SHORTCODE=
         MPESA_RESULT_URL=
         MPESA_TIMEOUT_URL=
         MPESA_INITIATOR_NAME=
         MPESA_SECURITY_CREDENTIAL=
         MPESA_SECURITY_CERT=
         MPESA_ACCOUNT_IDENTIFIER=
         MPESA_B2B_COMMAND=
         MPESA_B2B_SENDER_ID=
         MPESA_B2B_RECEIVER_ID=
  5. C2B API Config

         MPESA_SHORTCODE=
         MPESA_C2B_COMMAND=
         MPESA_C2B_CONFIRMATION_URL=
         MPESA_C2B_VALIDATION_URL=
         MPESA_C2B_RESPONSE_TYPE=

Usage Examples

<?php
namespace YOURNAMESPACE;

use MpesaDaraja; 
use Ssiva\MpesaDaraja\Mpesa;

class CheckoutController extends Controller {
   
   public function darajaExamples(
        $mpesaDaraja = new MpesaDaraja();
        
        // authenticate
        $mpesaDaraja::authenticate();
        
        // STK Push
        $stkParams = [
            'Amount' => '2',
            'PartyA' => '2547XXXXXXXX',
            'PhoneNumber' => '2547XXXXXXXX',
            'AccountReference' => '13',
            'TransactionDesc' => 'Shopping',
        ];
       $mpesaDaraja::stkPush($stkParams);
       
       // stk push status query
       $stkQueryParams = [
         'CheckoutRequestID' => "ws_CO_290320231617432767XXXXXXXX",
       ];
       $mpesaDaraja::stkPushQuery($stkQueryParams);
       
       // transaction status query
       $statusParams = [
         'Remarks' => "Status test for RCC3LAPCEL",
         "TransactionID" => "RCC3LAPCEL",
         "Occasion" => "Optional Value for Occasion"
       ];
       $mpesaDaraja::transactionStatus($statusParams);

   }
}