About
An unofficial Rust wrapper around the Safaricom API for accessing M-Pesa services.
Install
Cargo.toml
[]
= "0.4.0"
Optionally, you can disable default-features, which is basically the entire suite of MPESA APIs to conditionally select from either ["b2b", "b2c" ,"account_balance", "c2b_register", "c2b_simulate", "express_request"] services.
Example:
[]
= { = "0.4.0", = false, = ["b2b", "express_request"] }
In your lib or binary crate:
use Mpesa;
Usage
Creating a Mpesa 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.
These are the following ways you can instantiate Mpesa:
NOTE:
- Only calling
unwrapfor demonstration purposes. Errors are handled appropriately in the lib via theMpesaErrorenum. - Use of
dotenvis optional.
use ;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
assert!
Since the Environment enum implements FromStr and TryFrom, you can pass the name of the environment as a &str and call the parse() or try_into()
method to create an Environment type from the string slice (Pascal case or Uppercase string slices also valid):
use Mpesa;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = 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;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
client.set_initiator_password;
assert!
Services
The following services are currently available from the Mpesa client as methods that return builders:
- B2C
use ;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
let response: = client
.b2c
.party_a
.party_b
.result_url
.timeout_url
.amount
.send;
assert!
- B2B
use ;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
let response: = client
.b2b
.party_a
.party_b
.result_url
.timeout_url
.account_ref
.amount
.send;
assert!
- C2B Register
use ;
use Value;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
let response: = client
.c2b_register
.short_code
.confirmation_url
.validation_url
.send;
assert!
- C2B Simulate
use ;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
let response: = client
.c2b_simulate
.short_code
.msisdn
.amount
.send;
assert!
- Account Balance
use ;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
let response: = client
.account_balance
.result_url
.timeout_url
.party_a
.send;
assert!
- Mpesa Express Request / STK push/ Lipa na M-PESA online
use ;
use env;
use dotenv;
dotenv.ok;
let client: Mpesa = new;
let response: = client
.express_request
.phone_number
.party_a
.party_b
.amount
.callback_url
.send;
assert!
More will be added progressively, pull requests welcome
Author
Collins Muriuki
- Twitter: @collinsmuriuki_
- Not affiliated with Safaricom.
License
This project is MIT licensed