Expand description
§MTN Mobile Money Rust package
MTNMomo Rust package
provides for an easy way to connect to MTN MoMo API, it provides for the following products:
- Collection
- Disbursements
- Remittance
- Provisioning in case of sandbox environment how to use:
§Examples
use mtnmomo::Momo;
use uuid::Uuid;
use dotenv::dotenv;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let mtn_url = env::var("MTN_URL").expect("MTN_COLLECTION_URL must be set"); // https://sandbox.momodeveloper.mtn.com
let primary_key = env::var("MTN_COLLECTION_PRIMARY_KEY").expect("PRIMARY_KEY must be set");
let secondary_key = env::var("MTN_COLLECTION_SECONDARY_KEY").expect("SECONDARY_KEY must be set");
let callback_host = env::var("MTN_CALLBACK_HOST").expect("MTN_CALLBACK_HOST must be set");
let momo = Momo::new_with_provisioning(mtn_url, primary_key.clone(), &callback_host).await.unwrap();
let collection = momo.collection(primary_key, secondary_key);
}
After initializing the Momo struct, you can then use the collection, disbursement or remittance methods to initialize the respective products. The products have methods that you can use to interact with the API. For example, to request a payment from a customer, you can use the request_to_pay method of the Collection product.
§Important notes
mtnmomo::Momo::new_with_provisioning
is used to initialize the Momo struct with the sandbox environment.
mtnmomo::Momo::new
is used to initialize the Momo struct with the production environment.
§Examples:
If you want to request a payment from a customer, you can use the request_to_pay method of the Collection product.
use mtnmomo::{Momo, Party, PartyIdType, Currency, RequestToPay};
use uuid::Uuid;
use dotenv::dotenv;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let mtn_url = env::var("MTN_URL").expect("MTN_COLLECTION_URL must be set"); // https://sandbox.momodeveloper.mtn.com
let primary_key = env::var("MTN_COLLECTION_PRIMARY_KEY").expect("PRIMARY_KEY must be set");
let secondary_key = env::var("MTN_COLLECTION_SECONDARY_KEY").expect("SECONDARY_KEY must be set");
let callback_host = env::var("MTN_CALLBACK_HOST").expect("MTN_CALLBACK_HOST must be set");
let momo = Momo::new_with_provisioning(mtn_url, primary_key.clone(), &callback_host).await.unwrap();
let collection = momo.collection(primary_key, secondary_key);
let payer : Party = Party {
party_id_type: PartyIdType::MSISDN,
party_id: "234553".to_string(),
};
let request = RequestToPay::new("100".to_string(), Currency::EUR, payer, "test_payer_message".to_string(), "test_payee_note".to_string());
let result = collection.request_to_pay(request, None).await;
}
The above code will request a payment of 100 EUR from the customer with the phone number “234553”. The customer will receive a prompt on their phone to confirm the payment. If the customer confirms the payment, the payment will be processed and the customer will receive a confirmation message. If the customer declines the payment, the payment will not be processed and the customer will receive a message informing them that the payment was declined.
Re-exports§
pub use callback::CallbackResponse;
pub use callback::MomoUpdates;
pub use callback::Reason;
pub use common::id::DepositId;
pub use common::id::InvoiceId;
pub use common::id::PaymentId;
pub use common::id::RefundId;
pub use common::id::TransactionId;
pub use common::id::TranserId;
pub use common::id::WithdrawId;
Modules§
Type Aliases§
- Access
Token Request - Access
Type - BCAuthorize
Response - Balance
- Basic
User Info Json Response - BcAuthorize
Request - Callback
Type - Cash
Transfer Request - Cash
Transfer Result - Create
Payment Request - Currency
- Delete
Invoice Request - Delivery
Notification Request - Environment
- Invoice
Delete Request - Invoice
Request - Invoice
Result - Momo
Collection - Momo
Disbursements - Momo
Provisioning - Momo
Remittance - Money
- OAuth2
Token Response - Party
- Party
IdType - Payment
Result - PreApproval
Request - PreApproval
Result - Refund
Request - Request
ToPay - Request
ToPay Result - Token
Response - Transfer
Request - Transfer
Result