2017 © Pedro Peláez
 

library mpesa

Safaricom M-Pesa integration

image

mxgel/mpesa

Safaricom M-Pesa integration

  • Wednesday, June 27, 2018
  • by Murwa
  • Repository
  • 1 Watchers
  • 1 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 16 Versions
  • 5 % Grown

The README.md

README

This is a non-official implementation of Safaricom's Mpesa G2 API, (*1)

How do I get set up?

The preferred way is installation via composer:-, (*2)

composer require mxgel/mpesa, (*3)

That's all., (*4)

Auth

To communicate with the daraja APIs, you'll always require an access token. This has been simplified for you in this library. All you have to do is provide an instance of Mxgel\MPesa\Auth\Auth to the execute method. A piece of code like:-, (*5)

    /**
     * @var Auth
     */
    private $auth;

    /**
     * @return \Mxgel\MPesa\Auth\Auth
     */
    public function getAuth(): Auth
    {
        if (!$this->auth) {
            return $this->auth = new Mxgel\MPesa\Auth\Auth([
                'key'    => config('key'),
                'secret' => config('secret'),
            ]);
        }

        return $this->auth;
    }

MPESA Express

This API is used to initiate online payment on behalf of a customer., (*6)

Example

Sample code (in Laravel) to initiate the payment can be something like this (use Mxgel\MPesa\Requests\LNMO)., (*7)

$request = LNMO::make($amount, $phoneNumber, $shortCode, 'Wallet top up');
$request->setPassKey(config('LNMO_passkey'))
    ->setBusinessShortCode(config('LNMO_short_code'))
    ->setCallBackURL($callback);


$resp = $request->execute($this->getAuth());
Log::info('STk data', $request->toArray());

//NOTE: For a real application, save this details in a data store

We now need to write a callback that will receive data from safaricom upon request completion by the customer. Remember to save request details above in a data store, (*8)

Here is a sample of the callback in laravel., (*9)

For easy handling of the received data, we use Mxgel\MPesa\Responses\LNMOCallbackResponse class. This strips away unnecessary data and just give us the juice., (*10)

// Assuming u have an Express model, u can have this.

Log::debug('LNMO Data', $this->request->all());
$resp = new LNMOCallbackResponse($this->request->get('Body'));
Express::whereMerchantRequestId($resp->getMerchantRequestID())
    ->whereCheckoutRequestId($resp->getCheckoutRequestID())
    ->whereConfirmed(false)                 // Ensure it's yet to be updated
    ->update([
        'receipt_number' => $resp->getMpesaReceiptNumber(),
        'confirmed'      => $resp->completed(),
    ]);

Sometimes you might want to write a cron job to update un-confirmed transactions. Luckily, we can use the status check api, and it's a breeze. Use Mxgel\MPesa\Requests\LNMQ class like so, (*11)

$request = new LNMQ([
    'businessShortCode' => $business_short_code,
    'checkoutRequestID' => $checkout_request_id,
    'passKey'           => config('LNMO_passkey'),
]);

// Remember our auth object? Yes, we need it.
$resp = $request->execute($this->getAuth());
Log::info("Execute status with data: ", $request->toArray());
Log::info("Executed status check with response", $resp->toArray());
if ($resp->completed()) {
    // Your code here...
}

At this point, your STK should be fine., (*12)

The Versions

27/06 2018

dev-documentation

dev-documentation

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

dev-master

9999999-dev

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

v1.13

1.13.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

v1.12

1.12.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

v1.11

1.11.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

v1.10

1.10.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

v1.9

1.9.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

16/05 2018

v1.8

1.8.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

12/05 2018

v1.7

1.7.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

v1.6

1.6.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

v1.5

1.5.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

v1.4

1.4.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

v1.3

1.3.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

1.2

1.2.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

1.1

1.1.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa

11/05 2018

1.0

1.0.0.0

Safaricom M-Pesa integration

  Sources   Download

MIT

The Requires

 

by Peter Murwa