mpesa-rust

Version Documentation mpesa travis-ci License: MIT

About

A Rust wrapper around the Safaricom API for accessing M-Pesa services.

Notes

Warning! WIP, not production ready

Install & Usage

Cargo.toml

md [dependencies] mpesa = "0.2.0"

In your lib or binary crate: rs use mpesa::Mpesa;

Usage

Creating a Client

You will first need to create an instance of the Mpesa instance (the client). You are required to provide a CLIENTKEY, CLIENTSECRET and INIT_PASSWORD (initiator password). Here is how you can get these credentials for the Safaricom sandbox environment.

NOTE: only calling unwrap for demonstration purposes. Errors are handled appropriately in the lib via the MpesaError enum.

There are two ways you can instantiate Mpesa:

```rust use mpesa::{Mpesa, Environment}; use std::env;

let client = Mpesa::new( env::var("CLIENTKEY")?, env::var("CLIENTSECRET")?, Environment::Sandbox, env::var("INIT_PASSWORD")?, ); ```

Since the Environment enum implements FromStr, you can pass the name of the environment as a &str and call the parse() method to create an Enviroment type from the string slice:

```rust use mpesa::Mpesa; use std::env;

let client = Mpesa::new( env::var("CLIENTKEY")?, env::var("CLIENTSECRET")?, "sandbox".parse()?, env::var("INIT_PASSWORD")?, );`` ```

Services

The following services are currently available from the Mpesa client as methods that return builders: * B2C rust let response = client .b2c("testapi496") .parties("600496", "254708374149") .urls("https://testdomain.com/err", "https://testdomain.com/res") .amount(1000) .send(); assert!(response.is_ok())

let response = client .c2bsimulate() .shortcode("600496") .msisdn("254700000000") .amount(1000) .send(); assert!(response.is_ok()) ```

rust let response = client .account_balance("testapi496") .urls("https://testdomain.com/err", "https://testdomain.com/ok") .party_a("600496") .send(); assert!(response.is_ok())

More will be added progressively, pull requests welcome

Author

Collins Muriuki

Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

Copyright © 2020 Collins Muriuki.
This project is MIT licensed.