MOMO.rs is a Rust library for the MOMO payment gateway.
Installation
[]
= "0.1.7"
or you can use cargo add
cargo add mtnmomo
MTN Mobile Money API
This 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:
use Momo;
use Uuid;
use dotenv;
use env;
async
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.
example making a request to pay:
use ;
use Uuid;
use dotenv;
use env;
async
The above code will request a payment of 100 EUR from the customer with the phone number "46733123450" (MTN sandbox test number). In the sandbox environment, this test number will simulate a successful payment. 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.
Testing with MTN Sandbox
When using the sandbox environment, you should use MTN's predefined test phone numbers:
46733123450- Successful payment46733123451- Payment rejection46733123452- Payment expiry46733123453- Ongoing payment46733123454- Delayed payment (succeeds after 30 seconds)
Callback Server
This library includes an integrated callback server for handling MTN MoMo webhooks. The callback server provides a secure HTTPS endpoint that processes payment notifications from MTN MoMo API.
Installation with Callback Server
[]
= { = "0.1.7", = ["callback-server"] }
Basic Callback Server Usage
use ;
use StreamExt;
async
Environment Configuration
The callback server can be configured using environment variables:
TLS Certificate Setup
For production use, you'll need valid TLS certificates. You can obtain free certificates from Let's Encrypt:
# Install certbot
# Get certificate for your domain
# Copy certificates to your application directory
For development/testing, you can create self-signed certificates:
# Generate private key
# Generate self-signed certificate
Callback URLs
When making API calls, use your callback server URLs:
// For payments
let callback_url = "https://your-domain.com/collection_request_to_pay/REQUEST_TO_PAY";
let result = collection.request_to_pay.await;
// For disbursements
let callback_url = "https://your-domain.com/disbursement_deposit_v1/DISBURSEMENT_DEPOSIT_V1";
let result = disbursement.deposit.await;
Available Endpoints
The callback server automatically creates endpoints for all MTN MoMo services:
- Collection:
/collection_request_to_pay/{callback_type} - Collection Withdrawals:
/collection_request_to_withdraw_v1/{callback_type},/collection_request_to_withdraw_v2/{callback_type} - Invoices:
/collection_invoice/{callback_type},/collection_payment/{callback_type} - Disbursements:
/disbursement_deposit_v1/{callback_type},/disbursement_deposit_v2/{callback_type} - Remittances:
/remittance_cash_transfer/{callback_type},/remittance_transfer/{callback_type} - Health Check:
/health(GET)
Features
- 🔒 TLS/HTTPS Support: Secure server with certificate-based encryption
- 📡 Complete Callback Coverage: Handles all MTN MoMo callback types
- 💊 Health Monitoring: Built-in health check endpoint for load balancers
- 🛡️ Production Ready: Graceful shutdown, structured logging, comprehensive error handling
- ⚙️ Environment Configuration: Configurable via environment variables
- 🔧 Extensible: Easy-to-extend callback handlers for custom business logic