Giter VIP home page Giter VIP logo

codeigniter-mpesa's Introduction

CodeIgniter Mpesa

This should act as a drop-in solution for CodeIgniter 3 applications that use Mpesa functionality. Currently, only lipa na Mpesa (STK push) has been implemented as it is the most widely used. This is an opensource project though, so feel free to add the rest of the functionality.

Structure

config/mpesa.php contains Mpesa configuration you may need. By default, it comes already setup with sandbox credentials, so all you need to change is the CONSUMER_KEY and CONSUMER_SECRET which you can get from Safaricom when you create a new app

libraries/Mpesa_lib.php is the Mpesa API abstraction implementation that contains all the methods you can use

Getting started

Copy and paste the all directories into your application/ directory

Setting up

Get your consumer key and secret from Safaricom Daraja

Replace the values in config/mpesa.php to your corresponding credentials

Using the Library

Import the library into your controller

  // Load the mpesa library
  $this->load->library('mpesa_lib');
 
 // $this->mpesa_lib is how you will be accessing the library from now on

Call the lipa_na_mpesa method in your controller

  // $phone - The phone number initiating the transaction - your customer's phone
  // $amount - A non-zero amount (integer)
  $this->mpesa_lib->lipa_na_mpesa($phone, $amount)

This should trigger an STK push request on your customer's device. You can add this to an API request endpoint that accepts a phone number and amount and pass those into the lipa_na_mpesa method

Handling the transaction

If a transaction succeeds or fails, you'd want to know about it. That's where your callback url comes in. This is where Mpesa will send more information about a transaction to your application.

Ideally, you can create a controller specifically for handling the transaction requests and map your callback url to it. For example: controllers/payments/Handler.php would probably result in your callback url being something on the lines of site.com/payments/handler.

More details on how this works can again be found through Daraja.

The callback urls can be set through config/mpesa.php

Details on what kind of information is returned by the Mpesa API upon completion can be found here.

Conclusion

With that, you should be able to implement both the request and handling of Mpesa requests.

codeigniter-mpesa's People

Contributors

allanjeremy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.