[−][src]Crate mpesa
About
A Rust wrapper around the Safaricom API for accessing M-Pesa services.
Disclaimer
Warning! WIP, not production ready
Install & Usage
Cargo.toml
[dependencies]
mpesa = "0.2.0"
In your lib or binary crate:
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 CLIENT_KEY,
CLIENT_SECRET and INIT_PASSWORD (initiator password). Here is how you can get these credentials for the Safaricom sandbox
environment.
There are two ways you can instantiate Mpesa
:
NOTE: only calling unwrap
for demonstration purposes. Errors are handled appropriately in the lib via the MpesaError
enum.
use mpesa::{Mpesa, Environment};
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
Environment::Sandbox,
env::var("INIT_PASSWORD").unwrap(),
);
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::Mpesa;
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
"sandbox".parse().unwrap(),
env::var("INIT_PASSWORD").unwrap(),
);
Services
The following services are currently available from the Mpesa
client as methods that return builders:
- B2C
use mpesa::Mpesa;
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
"sandbox".parse().unwrap(),
env::var("INIT_PASSWORD").unwrap(),
);
let response = client
.b2c("testapi496")
.parties("600496", "254708374149")
.urls("https://testdomain.com/err", "https://testdomain.com/res")
.amount(1000)
.send();
assert!(response.is_ok())
- B2B
use mpesa::Mpesa;
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
"sandbox".parse().unwrap(),
env::var("INIT_PASSWORD").unwrap(),
);
let response = client
.b2b("testapi496")
.parties("600496", "600000")
.urls("https://testdomain.com/err", "https://testdomain.com/api")
.account_ref("254708374149")
.amount(1000)
.send();
assert!(response.is_ok())
- C2B Register
use mpesa::Mpesa;
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
"sandbox".parse().unwrap(),
env::var("INIT_PASSWORD").unwrap(),
);
let response = client
.c2b_register()
.short_code("600496")
.confirmation_url("https://testdomain.com/true")
.validation_url("https://testdomain.com/valid")
.send();
assert!(response.is_ok())
- C2B Simulate
use mpesa::Mpesa;
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
"sandbox".parse().unwrap(),
env::var("INIT_PASSWORD").unwrap(),
);
let response = client
.c2b_simulate()
.short_code("600496")
.msisdn("254700000000")
.amount(1000)
.send();
assert!(response.is_ok())
- Account Balance
use mpesa::Mpesa;
use std::env;
let client = Mpesa::new(
env::var("CLIENT_KEY").unwrap(),
env::var("CLIENT_SECRET").unwrap(),
"sandbox".parse().unwrap(),
env::var("INIT_PASSWORD").unwrap(),
);
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
- Twitter: @collinsmuriuki_
License
This project is MIT licensed
Modules
services |
Structs
Mpesa | Mpesa client that will facilitate communication with the Safaricom API |
Enums
CommandId | Mpesa command ids |
Environment | Enum to map to desired environment so as to access certificate
and the base url
Required to construct a new |
IdentifierTypes | Identifier types - both sender and receiver - identify an M-Pesa transaction’s sending and receiving party as either a shortcode, a till number or a MSISDN (phone number). There are three identifier types that can be used with M-Pesa APIs. |
MpesaError | Mpesa error stack |
Traits
MpesaSecurity | Trait responsible for implementation of security configs for Mpesa |