MpesaStk
Lipa na M-Pesa Online Payment API is used to initiate a M-Pesa transaction on behalf of a customer using STK Push. This is the same technique mySafaricom App uses whenever the app is used to make payments.
Installation
Add this line to your application's Gemfile:
gem 'mpesa_stk'
and run the bundle install command
Or install it yourself as:
gem install mpesa_stk
Getting Started
This gem has a Redis dependency, so make sure it running
$ redis-server
you need to setup your environment variables, checkout .sample.env for the values you need.
or run
$ cp .sample.env .env
open .env on your editor and add the missing variable
key=""
secret=""
business_short_code=""
business_passkey=""
callback_url=""
keyandsecretof the app created on your developer account.business_short_codeandbusiness_pass_keythis can be found in Test Credentials.callback_urlthe url of your application where response will be sent.make sure its a reachable/active url
Testing
$ irb
2.5.0 :001 > require 'mpesa_stk'
2.5.0 :002 > MpesaStk::PushPayment.call("500", "<YOUR PHONE NUMBER: 254711222333>")
expected irb output after the command
{
"MerchantRequestID"=>"7909-1302368-1",
"CheckoutRequestID"=>"ws_CO_DMZ_40472724_16062018092359957",
"ResponseCode"=>"0",
"ResponseDescription"=>"Success. Request accepted for processing",
"CustomerMessage"=>"Success. Request accepted for processing"
}
the above response means the response has been successfully sent to Safaricom for processing and you should be able to see the checkout/express prompt on the sender number.
Mpesa Checkout/Express
This is the expected output on the mobile phone

Callback url
After the pin code is entered on the checkout/express prompt. you will receive a request on the provided callback_url with the status of the action
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mboya/mpesa_stk.
To Contribute to this gem,
- Comment on the issue you would like to work on solving.
- Mark the issue as in progress by adding an
in-progresslabel. - Fork the project to your github repository (This project only accepts PRs from forks)
- Submit the PR after the implementation all unfinished PRs for an issue should have a WIP indicated beside it
- Every PR should have a link to the issue being solved
- Checkout this github best practices for more info.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the MpesaStk project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Sample application
Check out a rails sample application here