/mpesa_sdk_dart

Dart | Flutter library for M-Pesa API (Mozambique)

Primary LanguageDartMIT LicenseMIT

mpesa_sdk_dart

Pub Version GitHub GitHub repo size

Dart package for M-Pesa API (Mozambique)

Ready Methods/APIs

  • C2B
  • B2B
  • B2C
  • TRANSACTION STATUS
  • ACCOUNT BALANCE
  • 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
Igor Sambo

Copyright and license

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.