Dart package for M-Pesa API (Mozambique)
Ready Methods/APIs
- C2B
- B2B
- B2C
- TRANSACTION STATUS
- REVERSAL
We highly recommend reading Mpesa API docs first!
You Will need a few things from there before development.
- Api Key
- 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.
Add dependency in pubspec.yaml
dependencies:
mpesa_sdk_dart: <latest_version>
Import in your Flutter app or plain dart app.
import 'package:mpesa_sdk_dart/mpesa_sdk_dart.dart';
String token = MpesaConfig.getBearerToken(
'API_KEY_HERE',
'PUBLIC_KEY_HERE',
);
PaymentRequest payload = PaymentRequest(
inputTransactionReference: 'inputTransactionReference',
inputCustomerMsisdn: '25884xxxxxxx',
inputAmount: inputAmount,
inputThirdPartyReference: 'inputThirdPartyReference',
inputServiceProviderCode: 'inputServiceProviderCode',
);
MpesaTransaction.c2b(token, apiHost, payload);
PaymentRequest payload = PaymentRequest(
inputTransactionReference: 'inputTransactionReference',
inputCustomerMsisdn: '25884xxxxxxx',
inputAmount: inputAmount,
inputThirdPartyReference: 'inputThirdPartyReference',
inputServiceProviderCode: 'inputServiceProviderCode',
);
MpesaTransaction.b2c(token, apiHost, payload);
ReversalRequest payload = ReversalRequest(
inputTransactionID: 'input_TransactionID',
inputSecurityCredential: 'input_SecurityCredential',
inputInitiatorIdentifier: 'input_InitiatorIdentifier',
inputThirdPartyReference: 'input_ThirdPartyReference',
inputServiceProviderCode: 'input_ServiceProviderCode',
inputReversalAmount: montant, // Optional
);
MpesaTransaction.reversal(token, apiHost, payload);
TransferRequest payload = TransferRequest(
inputTransactionReference: 'input_TransactionReference',
inputAmount: inputAmount,
inputThirdPartyReference: 'input_ThirdPartyReference',
inputPrimaryPartyCode: 'input_PrimaryPartyCode',
inputReceiverPartyCode: 'input_ReceiverPartyCode',
);
MpesaTransaction.b2b(token, apiHost, payload);
MpesaTransaction.getTransactionStatus(
token,
apiHost,
'input_ThirdPartyReference',
'input_QueryReference',
'input_ServiceProviderCode',
);
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, apiHost, payload);
print(response.body);
if(response.statusCode == 201) { // if is resource created!
// Do something!
}
MIT License
Copyright (c) 2020-2021 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.