All Projects → collinsmuriuki → mpesa-rust

collinsmuriuki / mpesa-rust

Licence: MIT License
An abstraction/wrapper on top of Safaricom's M-PESA daraja api in Rust

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to mpesa-rust

mpesa-rest-api
mpesa rest api converts the mpesa api to a RESTful API that is easy for developers to use instead of the current SOAP web service provided by mpesa.
Stars: ✭ 24 (+41.18%)
Mutual labels:  mpesa, safaricom
sapamapay
New Safaricom MPESA (Daraja) API
Stars: ✭ 31 (+82.35%)
Mutual labels:  mpesa, safaricom
safaricom-daraja-nodejs
Standalone M-Pesa payments service using the new Daraja Payment API by Safaricom.
Stars: ✭ 31 (+82.35%)
Mutual labels:  mpesa, safaricom
ex pesa
Payment Library For Most Public Payment API's in Kenya and hopefully Africa. Let us get this moneybag
Stars: ✭ 19 (+11.76%)
Mutual labels:  mpesa
pesa-js
Pesa SDK
Stars: ✭ 19 (+11.76%)
Mutual labels:  mpesa
python-daraja
Python Wrapper for interacting with the MPESA Daraja API. More Features to be implemented
Stars: ✭ 20 (+17.65%)
Mutual labels:  mpesa
loopy
Loopy is an unofficial cli client for CBA Loop for the lazy bones
Stars: ✭ 23 (+35.29%)
Mutual labels:  mpesa
php-pesa
Open payment integration toolkit for PHP
Stars: ✭ 26 (+52.94%)
Mutual labels:  mpesa
Chowder
Chowder for Android M-Pesa payments.
Stars: ✭ 31 (+82.35%)
Mutual labels:  safaricom
tz-mpesa-ussd-push
Vodacom Tanzania USSD Push API Client
Stars: ✭ 18 (+5.88%)
Mutual labels:  mpesa
mpesa-dart
A dart wrapper around mpesa daraja api
Stars: ✭ 19 (+11.76%)
Mutual labels:  mpesa
JustJava-Android
JustJava is a mock food ordering and delivery application for a coffee shop.
Stars: ✭ 59 (+247.06%)
Mutual labels:  mpesa

mpesa-rust

Version Documentation mpesa travis-ci License: MIT

About

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

Install

Cargo.toml

[dependencies]
mpesa = "0.4.2"

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:

[dependencies]
mpesa = { version = "0.4.2", default_features = false, features = ["b2b", "express_request"] }

In your lib or binary crate:

use mpesa::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.

NOTE:

  • Only calling unwrap for demonstration purposes. Errors are handled appropriately in the lib via the MpesaError enum.

These are the following ways you can instantiate Mpesa:

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

let client = Mpesa::new(
      env::var("CLIENT_KEY")?,
      env::var("CLIENT_SECRET")?,
      Environment::Sandbox,
);

assert!(client.is_connected())

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 and Uppercase string slices also valid):

use mpesa::Mpesa;
use std::env;

let client = Mpesa::new(
      env::var("CLIENT_KEY")?,
      env::var("CLIENT_SECRET")?,
      "sandbox".parse()?, // "production"
);
assert!(client.is_connected())

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::Mpesa;
use std::env;

let client = Mpesa::new(
      env::var("CLIENT_KEY")?,
      env::var("CLIENT_SECRET")?,
      "production".parse()?,
);

client.set_initiator_password("new_password");

assert!(client.is_connected())

Services

The following services are currently available from the Mpesa client as methods that return builders:

  • B2C
let response = client
    .b2c("testapi496")
    .party_a("600496")
    .party_b("254708374149")
    .result_url("https://testdomain.com/ok")
    .timeout_url("https://testdomain.com/err")
    .amount(1000)
    .send();
assert!(response.is_ok())
  • B2B
let response = client
    .b2b("testapi496")
    .party_a("600496")
    .party_b("254708374149")
    .result_url("https://testdomain.com/ok")
    .timeout_url("https://testdomain.com/err")
    .account_ref("254708374149")
    .amount(1000)
    .send();
assert!(response.is_ok())
  • C2B Register
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
let response = client
    .c2b_simulate()
    .short_code("600496")
    .msisdn("254700000000")
    .amount(1000)
    .send();
assert!(response.is_ok())
  • Account Balance
let response = client
    .account_balance("testapi496")
    .result_url("https://testdomain.com/ok")
    .timeout_url("https://testdomain.com/err")
    .party_a("600496")
    .send();
assert!(response.is_ok())
  • Mpesa Express Request / STK push / Lipa na M-PESA online
let response = client
    .express_request("174379")
    .phone_number("254708374149")
    .amount(500)
    .callback_url("https://test.example.com/api")
    .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 © 2021 Collins Muriuki.
This project is MIT licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].