Struct mpesa::Mpesa[][src]

pub struct Mpesa { /* fields omitted */ }
Expand description

Mpesa client that will facilitate communication with the Safaricom API

Implementations

impl<'a> Mpesa[src]

pub fn new(
    client_key: String,
    client_secret: String,
    environment: Environment
) -> Self
[src]

Constructs a new Mpesa instance.

Example

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

pub fn set_initiator_password(&self, initiator_password: &str)[src]

Optional in development but required for production, you will need to call this method and set your production initiator password. If in development, default initiator password is already pre-set

use mpesa::Mpesa;

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

client.set_initiator_password("your_initiator_password");

pub fn is_connected(&self) -> bool[src]

Checks if the client can be authenticated

pub fn b2c(&'a self, initiator_name: &'a str) -> B2cBuilder<'a>[src]

B2C Builder

Creates a B2cBuilder for building a B2C transaction struct. The builder is consumed and request made by calling its send method. See more from Safaricom the API docs here.

Requires an initiator_name, the credential/ username used to authenticate the transaction request

Example

let response = client
    .b2c("testapi496")
    .party_a("600496")
    .party_b("600000")
    .result_url("https://testdomain.com/err")
    .timeout_url("https://testdomain.com/ok")
    .amount(1000)
    .remarks("Your Remark") // optional, defaults to "None"
    .occasion("Your Occasion") // optional, defaults to "None"
    .command_id(mpesa::CommandId::BusinessPayment) // optional, defaults to `CommandId::BusinessPayment`
    .send();

pub fn b2b(&'a self, initiator_name: &'a str) -> B2bBuilder<'a>[src]

B2B Builder

Creates a B2bBuilder for building B2B transaction struct.

See more from the Safaricom API docs here

Requires an initiator_name, the credential/ username used to authenticate the transaction request

Example

let response = client.b2b("testapi496")
   .party_a("600496")
   .party_b("600000")
   .result_url("https://testdomain.com/err")
   .timeout_url("https://testdomain.com/ok")
   .account_ref("254708374149")
   .amount(1000)
   .command_id(mpesa::CommandId::BusinessToBusinessTransfer) // optional, defaults to `CommandId::BusinessToBusinessTransfer`
   .remarks("None") // optional, defaults to "None"
   .sender_id(mpesa::IdentifierTypes::ShortCode) // optional, defaults to `IdentifierTypes::ShortCode`
   .receiver_id(mpesa::IdentifierTypes::ShortCode) // optional, defaults to `IdentifierTypes::ShortCode`
   .send();

pub fn c2b_register(&'a self) -> C2bRegisterBuilder<'a>[src]

C2B Register builder

Creates a C2bRegisterBuilder for registering URLs to the 3rd party shortcode.

See more from the Safaricom API docs here

Example

let response = client
   .c2b_register()
   .short_code("600496")
   .confirmation_url("https://testdomain.com/true")
   .validation_url("https://testdomain.com/valid")
   .response_type(mpesa::ResponseTypes::Complete) // optional, defaults to `ResponseTypes::Complete`
   .send();

pub fn c2b_simulate(&'a self) -> C2bSimulateBuilder<'a>[src]

C2B Simulate Builder

Creates a C2bSimulateBuilder for simulating C2B transactions

See more here

Example

let response = client.c2b_simulate()
   .short_code("600496")
   .msisdn("254700000000")
   .amount(1000)
   .command_id(mpesa::CommandId::CustomerPayBillOnline) // optional, defaults to `CommandId::CustomerPayBillOnline`
   .bill_ref_number("Your_BillRefNumber>") // optional, defaults to "None"
   .send();

pub fn account_balance(
    &'a self,
    initiator_name: &'a str
) -> AccountBalanceBuilder<'a>
[src]

Account Balance Builder

Creates an AccountBalanceBuilder for enquiring the balance on an MPESA BuyGoods. Requires an initiator_name.

See more from the Safaricom API docs here

Example

let response = client
   .account_balance("testapi496")
   .result_url("https://testdomain.com/err")
   .timeout_url("https://testdomain.com/ok")
   .party_a("600496")
   .command_id(mpesa::CommandId::AccountBalance) // optional, defaults to `CommandId::AccountBalance`
   .identifier_type(mpesa::IdentifierTypes::ShortCode) // optional, defaults to `IdentifierTypes::ShortCode`
   .remarks("Your Remarks") // optional, defaults to "None"
   .send();

pub fn express_request(
    &'a self,
    business_short_code: &'a str
) -> MpesaExpressRequestBuilder<'a>
[src]

Mpesa Express Request/ STK push Builder

Creates a MpesaExpressRequestBuilder struct Requires a business_short_code - The organization shortcode used to receive the transaction

See more from the Safaricom API docs here

Example

 let response = client
    .express_request("174379")
    .phone_number("254708374149")
    .party_a("254708374149")
    .party_b("174379")
    .amount(500)
    .callback_url("https://test.example.com/api")
    .transaction_type(CommandId::CustomerPayBillOnline) // Optional, defaults to `CommandId::CustomerPayBillOnline`
    .transaction_desc("Description") // Optional, defaults to "None"
    .send();

Trait Implementations

impl Debug for Mpesa[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl MpesaSecurity for Mpesa[src]

fn gen_security_credentials(&self) -> MpesaResult<String>[src]

Generates security credentials M-Pesa Core authenticates a transaction by decrypting the security credentials. Security credentials are generated by encrypting the base64 encoded initiator password with M-Pesa’s public key, a X509 certificate. Returns base64 encoded string. Read more

Auto Trait Implementations

impl !RefUnwindSafe for Mpesa

impl Send for Mpesa

impl !Sync for Mpesa

impl Unpin for Mpesa

impl UnwindSafe for Mpesa

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.