Project

mpesarb

0.01
The project is in a healthy, maintained state
the gem will allow you to simply integrate with mpesa apis
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
 Dependencies

Development

Runtime

 Project Readme

Mpesa

Welcome to mpesa API gem. The gem defines the API resources and helps developers to make requests cleanly.

When you receive a JSON response from an API endpoint, it's really easy to convert this to a Ruby hash. But hashes don't feel very Ruby-ish when you're working with them.

To help on this gem parse the responses to OpenStruct Objects so a developer can interact with the response just like regular ruby objects we are used to.

This Gem provides an interface that developers can use to convert JSON to OpenStruct objects, this is useful when you want to parse results callbacks data.

Installation

Install via Gemfile

gem 'mpesarb', '~> 0.3.0'

Or

bundle add mpesarb

Usage

Initialization

client = Mpesa::Client.new(key: 'ZtkRW6ATbVtFpNml5w5SfG26Adfyagn9', secret: 'dosFI1yQ8bvHEVFw', env: 'sandbox')

response = client.auth
response.access_token # XiKf3D6UrY0J8S2aeOQ7R7w0BuA5
response.expires_in # 3599

Parameters

Required

  • key - API consumer key - Required
  • secret - API consumer secret - Required

Optional

  • env - API environment. Default production
  • adapter - Faraday HTTP adapter. Default :net_http
  • shortcode - Mpesa shortcode
  • pass_key - LPNMO pass Key( used by STK API)

Register Urls

Register C2B Urls( confirmation and validation url)

 response = client.register(
   shortcode: "44445",
   confirmation_url: 'http://example.com/confirm',
   validation_url: 'http://example.com/validate'
 )
 response.OriginatorCoversationID #   "807-15591582-1"
 response.ResponseCode # "0"
 response.ResponseDescription # "success"  

STK (LPNMO)

Lipa na mpesa online(Stk Push)

response = client.stk(
  shortcode: "174379",
  pass_key: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", # Optional if passed in client initialization
  amount: "10",
  phone: "254705112855",
  callback_url: "https://test.com",
  reference: "REF",
  trans_desc: "desc"
)

response.CheckoutRequestID # "ws_CO_040920212326513616"
response.inspect # to see all available attributes

B2C (Payout)

response = client.payout(
  shortcode: "600998", # Optional if added in client initialization
  initiator_username: "testapi",
  initiator_password: "Safaricom998!",
  command_id: "BusinessPayment",
  phone: "254708374149",
  amount: "100",
  result_url: "https://example.com/result",
  timeout_url: "https://example.com/result",
  occasion: "some desc",
  remarks: "remarks"
)

response.ConversationID # AG_20210904_0000574b3c8d93651740
response.OriginatorConversationID #  "28831-15819693-1"
respose.ResponseDescription # "Accept the service request successfully."

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gathuku/mpesa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Mpesa project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.