mpesa_sdk_dart 3.0.0-dev.1 copy "mpesa_sdk_dart: ^3.0.0-dev.1" to clipboard
mpesa_sdk_dart: ^3.0.0-dev.1 copied to clipboard

A production-ready Dart SDK for M-Pesa API (Mozambique) with typed responses and robust error handling.

mpesa_sdk_dart #

Pub Version GitHub

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 #

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 by MpesaClient.
  • MpesaTransaction.* static calls are replaced by instance methods on MpesaClient.
  • Request model fields moved from inputXxx naming to clear Dart naming, while preserving the original API JSON keys internally.
  • Methods now return MpesaResult<MpesaApiResponse> and throw typed exceptions instead of leaking raw http.Response.

License #

MIT

12
likes
0
points
14
downloads

Publisher

unverified uploader

Weekly Downloads

A production-ready Dart SDK for M-Pesa API (Mozambique) with typed responses and robust error handling.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

asn1lib, http, pointycastle

More

Packages that depend on mpesa_sdk_dart