mpesa_sdk_dart 1.0.0

  • Readme
  • Changelog
  • Installing
  • 61

mpesa_sdk_dart #

Pub Version GitHub GitHub repo size

Dart package for M-Pesa API (Mozambique)

Ready Methods/APIs

  • [x] C2B
  • [x] B2B
  • [x] B2C
  • [x] TRANSACTION STATUS
  • [ ] ACCOUNT BALANCE
  • [x] REVERSAL

Requisites #

We highly recommend reading Mpesa API docs first!

You Will need a few things from there before development.

  1. Api Key
  2. Public Key
  • Login or Register as a M-Pesa developer here if you haven't.
  • You will be issued with an API Key and a Public Key. You will use these to generate your access token.

Usage #

Add dependency in pubspec.yaml

dependencies:
  mpesa_sdk_dart: [ADD_LATEST_VERSION_HERE]

Import in your Flutter app or plain dart app.

import 'package:mpesa_sdk_dart/mpesa_sdk_dart.dart';

Generate a token #

String token = MpesaConfig.getBearerToken(
    'API_KEY_HERE',
    'PUBLIC_KEY_HERE',
  );

C2B Api Call #

Initialize your payload

PaymentRequest payload = PaymentRequest(
    inputTransactionReference: 'inputTransactionReference',
    inputCustomerMsisdn: '25884xxxxxxx',
    inputAmount: inputAmount,
    inputThirdPartyReference: 'inputThirdPartyReference',
    inputServiceProviderCode: 'inputServiceProviderCode',
  );

Perform the api call

MpesaTransaction.c2b(token, payload);

B2C Api Call #

Initialize your payload

PaymentRequest payload = PaymentRequest(
    inputTransactionReference: 'inputTransactionReference',
    inputCustomerMsisdn: '25884xxxxxxx',
    inputAmount: inputAmount,
    inputThirdPartyReference: 'inputThirdPartyReference',
    inputServiceProviderCode: 'inputServiceProviderCode',
  );

Perform the api call

MpesaTransaction.b2c(token, payload);

Reversal Api Call #

Initialize your payload

ReversalRequest payload = ReversalRequest(
    inputTransactionID: 'input_TransactionID',
    inputSecurityCredential: 'input_SecurityCredential',
    inputInitiatorIdentifier: 'input_InitiatorIdentifier',
    inputThirdPartyReference: 'input_ThirdPartyReference',
    inputServiceProviderCode: 'input_ServiceProviderCode',
    inputReversalAmount: montant, // Optional
  );

Perform the api call

MpesaTransaction.reversal(token, payload);

B2B Api Call #

Initialize your payload

TransferRequest payload = TransferRequest(
    inputTransactionReference: 'input_TransactionReference',
    inputAmount: inputAmount,
    inputThirdPartyReference: 'input_ThirdPartyReference',
    inputPrimaryPartyCode: 'input_PrimaryPartyCode',
    inputReceiverPartyCode: 'input_ReceiverPartyCode',
  );

Perform the api call

MpesaTransaction.b2b(token, payload);

Query Transaction Status Api Call #

Perform the api call

MpesaTransaction.getTransactionStatus(
    token,
    'input_ThirdPartyReference',
    'input_QueryReference',
    'input_ServiceProviderCode',
  );

Handle Response #

All transaction methods returned an http response. So what you have to do is assign your call to a property of type Response (From http package). Note that this is an async task.

Response response = await MpesaTransaction.c2b(token, payload);
print(response.body);

if(response.statusCode == 201) {  // if is resource created!
  // Do something!
}

Credits #

Contributors
Ergito Vilanculos

MIT License

Copyright (c) 2020 Ergito Vilanculos

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Changelog #

All notable changes to this project will be documented in this file.

[Unreleased] #

[1.0.0] - 07/Jun/2020 #

Added #

  • B2C transactions
  • B2B transactions
  • Reversal transactions
  • Query transactions status
  • Homepage config

Changed #

  • Updated README

Fixed #

  • Fix typos

removed #

  • Usage of [new] keyword

[0.0.1] - 05/Jun/2020 #

Added #

  • Initial release

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  mpesa_sdk_dart: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:mpesa_sdk_dart/mpesa_sdk_dart.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
61
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Health suggestions

Fix lib/src/payment_response.dart. (-0.50 points)

Analysis of lib/src/payment_response.dart reported 1 hint:

line 33 col 39: Unnecessary new keyword.

Fix lib/src/reversal_request.dart. (-0.50 points)

Analysis of lib/src/reversal_request.dart reported 1 hint:

line 35 col 39: Unnecessary new keyword.

Fix lib/src/rsa_key_helper.dart. (-0.50 points)

Analysis of lib/src/rsa_key_helper.dart reported 1 hint:

line 133 col 22: Use contains instead of indexOf

Fix additional 5 files with analysis or formatting issues. (-0.50 points)

Additional issues in the following files:

  • lib/src/transfer_request.dart (1 hint)
  • lib/example/main.dart (Run dartfmt to format lib/example/main.dart.)
  • lib/src/mpesa_config.dart (Run dartfmt to format lib/src/mpesa_config.dart.)
  • lib/src/mpesa_transaction.dart (Run dartfmt to format lib/src/mpesa_transaction.dart.)
  • lib/src/payment_request.dart (Run dartfmt to format lib/src/payment_request.dart.)

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and mpesa_sdk_dart.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
asn1lib ^0.6.4 0.6.5
http ^0.12.1 0.12.1
pointycastle ^1.0.2 1.0.2
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nnbd
http_parser 3.1.4
meta 1.2.1
path 1.7.0
pedantic 1.9.1
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nnbd