Crate async_mpesa

Source
Expand description

§async-mpesa

The async-mpesa crate provides a convenient way to access the mpesa api.

The client used to call the apis is asynchronous.

§Making a request

You first need to configure the client by providing the api url and key by default the client is configured to use the correct url so providing your own will be unnecessary.

The access token if not provided is sourced in the MPESA_ACCESS_TOKEN environment variable that you will need to set manually.

let config = MpesaConfig::new()
    .with_access_token(access_token)
    .with_api_url(api_url);

A client is needed to make requests and can be used to create multiple requests.

let client = Client::with_config(config);

Next to create a request you need to use the specific builder method for the api you are trying to access, here is an example for the stk push api which is called by the ExpressPushRequestArgs::default method.

let request = ExpressPushRequestArgs::default()
   .PartyA("")
   .PartyB("")
   .Amount("")
   .Password(shortcode, passkey, timestamp)
   .AccountReference("")
   .TransactionType("")
   .BusinessShortCode("")
   .CallbackURL("")
   .TransactionDesc("")
   .Timestamp("")
   .PhoneNumber("")
   .build()
   .unwrap();

To make a request and get a response you also need the specific method for the builder method used.

The response is deserialized into a response struct and if it fails it is deserialized into the error struct using the fields names specified in the mpesa docs.

let response = client
   .stkpush()
   .create(request)
   .await
   .unwrap();

println!("{:?}", response);

Modules§

config
error
types

Structs§

AccountBalance
Client to call the account balance API.
B2C
Client to call the b2c API
Bbuygoods
Client to call the business buy goods API
Bpaybill
Client to call the business paybill API
Client
ExpressQuery
Client to call the stkpush transaction status query API
Qr
Client to call the qr code generator API
Reversal
Client to call the transaction reversal API
STKPush
Client to call the stkpush API
SingleInvoice
Client to call the single invoicing API.
Tax
Client to call the business tax remittance API
TransactionStatus
Client to call the transcation status API