A Rust library to interact with the MPesa API, simplifying B2C payment integrations and more.

Overview

MPesa SDK (Rust Library)

A Rust library to interact with the MPesa API, simplifying B2C payment integrations and more.

Features

  • Configuration management for API and public keys.
  • Easy B2C Payment initiation.
  • Customizable headers and parameters for requests.
  • Built-in encryption and token generation for request authentication.
  • Comprehensive error handling with the MPesaError enum.

Getting Started

Prerequisites

  • Rust compiler and Cargo.
  • An MPesa Developer account to obtain API and public keys.

Installation

Include this in your Cargo.toml:

[dependencies]
mpesa_sdk = "0.1.0"

Then, build your project with:

$ cargo build

Basic Usage

use mpesa_sdk::{MPesaClient, data::B2CInput};

let client = MPesaClient::new("YOUR_API_KEY", "YOUR_PUBLIC_KEY");
let mut payment_request = B2CInput::new();

payment_request.set_transaction_reference("TRANSACTION_REF");
payment_request.set_customer_msisdn("258848255237");
payment_request.set_amount("1000");
payment_request.set_third_party_reference("THIRD_PARTY_REF");
payment_request.set_service_provider_code("171717");

let response = client.b2c_payment(&payment_request);

match response {
    Ok(resp) => println!("{:?}", resp),
    Err(e) => eprintln!("Error: {:?}", e),
}

Error Handling

The SDK uses the MPesaError enum for comprehensive error feedback. When making requests, results can be pattern-matched against these errors:

match result {
    Ok(data) => {
        // Process success scenario
    },
    Err(MPesaError::NetworkError(_)) => {
        // Handle network errors
    },
    Err(MPesaError::SerializationError(_)) => {
        // Handle serialization errors
    },
    // ... other specific errors
    Err(_) => {
        // Handle general or unknown errors
    }
}

For a full list of error variants, refer to the MPesaError documentation.

Contributing

We welcome contributions! Whether it's bug fixes, feature additions, or documentation improvements, your input is valuable. For significant changes, please open an issue for discussion before making a pull request.

License

This SDK is under the MIT License.


This README aims to provide clear, concise instructions to quickly get users started. Over time, consider enhancing the documentation by including more in-depth tutorials, best practices, or use-case examples to further guide users.

You might also like...
A smart-contract api and client for revm

revmup A smart contract and client API for revm. Features: Auto-generate contracts that interact directly with revm without needing ethers provider Co

Bindings for the etherscan API and other block explorers.

foundry-block-explorers Bindings for the etherscan.io web API and other block explorers. Examples use ethers_core::types::Chain; use foundry_block_exp

High-level networking library that extends the bevy_replicon library to allow snapshot interpolation and client-side prediction

bevy_replicon_snap A Snapshot Interpolation plugin for the networking solution bevy_replicon in the Bevy game engine. This library is a very rough pro

Rust API guidelines

Rust API guidelines This is a set of recommendations on how to design and present APIs for the Rust programming language. They are authored largely by

Rust NACL Wrapper API

Rust NACL Wrapper API NaCl (pronounced "salt") is a new easy-to-use high-speed software library for network communication, encryption, decryption, sig

A diffusers API in Burn (Rust)

diffusers-burn: A diffusers API in Rust/Burn ⚠️ This is still in development - contributors welcome! The diffusers-burn crate is a conversion of diffu

A Boring(SSL)-compatible API abstraction for Rust cryptographic implementations.
A Boring(SSL)-compatible API abstraction for Rust cryptographic implementations.

A Boring(SSL)-compatible API abstraction for Rust cryptographic implementations. What is Superboring? Superboring hides the complexity, diversity and

A Simple Rust NFT API + Smart Contract

Rust NFT API Purpose Rust NFT API is a simple RESTful API developed in Rust that offers functionalities for creating, retrieving, and listing Non-Fung

A Rust implementation of ECMAScript's Temporal API

Temporal in Rust ⚠️ This crate is highly experimental and NOT stable. We cannot make any guarantee that the API will be stable until 0.1.0. Do not use

Owner
Ismael GraHms
Are you spying on me ?
Ismael GraHms
Freelance payment protocol written in Rust.. (with multi-sig signing for dispute settling)

Freelance Escrow payment protocol ?? About The freelance protocol is a protocol built on decentralized and open systems such as blockchain and decentr

Femi Bolaji 4 Jan 3, 2023
A substrate pallet that enables Manta's decentialized anynonymous payment (DAP) protocol.

This is a pallet that enables decentialized anynonymous payment (DAP) protocol. The best way to use this repo is to invoke it with a manta-runtime, available from either manta-node or cumulus.

Manta Network 27 Nov 23, 2022
Demonstrating how to use Rust to interact with the Solana blockchain.

Solana Scripts This is a library of Rust scripts for specific purposes: Generate a new wallet Create an SPL token Mint an SPL token Get an associated

Rónán 40 May 28, 2023
A Rust implementation of Trojan with QUIC tunnel, Lite-TLS and more.

Trojan-Oxide A Rust implementation of Trojan with QUIC tunnel, Lite-TLS and more. Overview Full support for the original Trojan Protocol, including TC

null 13 Oct 17, 2022
Bitcoin PSBT signer, descriptor exporter and more for offline devices.

KeeChain Description KeeChain is a Bitcoin application to transform your offline computer in an AirGap Signing Device (aka Hardware Wallet). Getting s

Yuki Kishimoto 3 Dec 15, 2022
reth-indexer reads directly from the reth db and indexes the data into a postgres database all decoded with a simple config file and no extra setup alongside exposing a API ready to query the data.

reth-indexer reth-indexer reads directly from the reth db and indexes the data into a postgres database all decoded with a simple config file and no e

Josh Stevens 306 Jul 12, 2023
An open source Rust high performance cryptocurrency trading API with support for multiple exchanges and language wrappers. written in rust(🦀) with ❤️

Les.rs - Rust Cryptocurrency Exchange Library An open source Rust high performance cryptocurrency trading API with support for multiple exchanges and

Crabby AI 4 Jan 9, 2023
Rust API Client for ImageKit.io a file storage and image processing service

Rust API Client for ImageKit.io a file storage and image processing service Usage You must retrieve your Public and Private Keys from the ImageKit Dev

Esteban Borai 4 Jul 31, 2022
A node API for the dprint TypeScript and JavaScript code formatter

dprint-node A node API for the dprint TypeScript and JavaScript code formatter. It's written in Rust for blazing fast speed. Usage Pass a file path an

Devon Govett 431 Dec 24, 2022
An API and test-app that exposes zcash functionality for app consumption

Zingolib This repo provides both a library for zingoproxyclient and zingo-mobile, as well as an included cli application to interact with zcashd via l

ZingoLabs 5 Dec 15, 2022