mpesa_sdk_dart
A modern, typed Dart SDK for M-Pesa API (Mozambique).
Features
- C2B, B2C, B2B, Reversal, and Transaction Status operations
- Typed request/response models
- Typed exceptions for auth, API, network, and serialization failures
- One primary client API (
MpesaClient) with explicit configuration
Requirements
- Dart SDK
>=3.6.0 <4.0.0 - M-Pesa API Key and Public Key from M-Pesa Developer Portal
Installation
dependencies:
mpesa_sdk_dart: <latest_version>
Quick Start
import 'package:mpesa_sdk_dart/mpesa_sdk_dart.dart';
Future<void> main() async {
final client = MpesaClient(
credentials: const MpesaCredentials(
apiKey: 'YOUR_API_KEY',
publicKey: 'YOUR_PUBLIC_KEY',
),
environment: MpesaEnvironment.sandbox,
);
final response = await client.c2b(
PaymentRequest(
transactionReference: 'T12344C',
customerMsisdn: '258847522988',
amount: 10,
thirdPartyReference: '11114',
serviceProviderCode: '171717',
),
);
if (!response.data.isSuccessCode) {
throw StateError('Transaction failed: ${response.data.outputResponseDesc}');
}
client.close();
}
Error Handling
try {
final result = await client.b2c(request);
// success
} on MpesaAuthException catch (error) {
// invalid credentials/authorization
} on MpesaApiException catch (error) {
// non-2xx response from M-Pesa
} on MpesaNetworkException catch (error) {
// timeout or connectivity
} on MpesaSerializationException catch (error) {
// invalid/non-JSON response payload
}
Migration from v2
MpesaConfig.getBearerToken(...)is removed from public API. Token generation is handled internally byMpesaClient.MpesaTransaction.*static calls are replaced by instance methods onMpesaClient.- Request model fields moved from
inputXxxnaming to clear Dart naming, while preserving the original API JSON keys internally. - Methods now return
MpesaResult<MpesaApiResponse>and throw typed exceptions instead of leaking rawhttp.Response.
License
MIT