mpesa-rust
About
An unofficial Rust wrapper around the Safaricom API for accessing M-Pesa services.
Notes
Warning! WIP, not recommended for use in production
Install
Cargo.toml
[dependencies]
mpesa = "0.2.5"
In your lib or binary crate:
use 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 CLIENT_KEY and
CLIENT_SECRET. Here is how you can get these credentials for the Safaricom sandbox
environment. It's worth noting that these credentials are only valid in the sandbox environment. To go live and get production keys
read the docs here``.
NOTE:
- Only calling
unwrapfor demonstration purposes. Errors are handled appropriately in the lib via theMpesaErrorenum.
These are the following ways you can instantiate Mpesa:
use ;
use env;
let client = new;
assert!
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 Environment type from the string slice:
use Mpesa;
use env;
let client = new;
assert!
If you intend to use in production, you will need to call a the set_initiator_password method from Mpesa after initially
creating the client. Here you provide your initiator password, which overrides the default password used in sandbox "Safcom496!":
use Mpesa;
use env;
let client = new.set_initiator_password;
assert!
Services
The following services are currently available from the Mpesa client as methods that return builders:
- B2C
let response = client
.b2c
.parties
.urls
.amount
.send;
assert!
- B2B
let response = client
.b2b
.parties
.urls
.account_ref
.amount
.send;
assert!
- C2B Register
let response = client
.c2b_register
.short_code
.confirmation_url
.validation_url
.send;
assert!
- C2B Simulate
let response = client
.c2b_simulate
.short_code
.msisdn
.amount
.send;
assert!
- Account Balance
let response = client
.account_balance
.urls
.party_a
.send;
assert!
More will be added progressively, pull requests welcome
Author
Collins Muriuki
- Twitter: @collinsmuriuki_
- Not affiliated with Safaricom in any way
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.