Giter Site home page Giter Site logo

flavian-anselmo / mpesa-daraja-plugin Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 2.0 30 KB

m-pesa daraja 2.0 wrapper for flutter and dart

Home Page: https://pub.dev/packages/mpesadaraja

License: MIT License

Dart 100.00%
daraja-api-flutter mpesa safaricom-mpesa-apis

mpesa-daraja-plugin's Introduction

mpesa-daraja-plugin-flutter-dart

dart wrapper for mpesa daraja api by safaricom. M-Pesa is a mobile phone-based money transfer, financing and microfinancing service, launched in 2007 by Vodafone for Safaricom and Vodacom the largest mobile network operators in Kenya and Tanzania.

Features

  1. [+]Lipa na mpesa
  2. [inprogress] C2B
  3. [inprogress] B2B
  4. [inprogress] C2B
  5. [inprogress] B2C
  6. [inprogress] TRANSACTION STATUS
  7. [inprogress] ACCOUNT BALANCE
  8. [inprogress] REVERSAL

Getting started

You Will need a few things from Safaricom before development.

  1. Consumer Key
  2. Consumer Secret
  3. Test Credentials for Development/Sanbox environment
  • Login or Register as a Safaricom developer here if you haven't.
  • Add a new App here
  • You will be issued with a Consumer Key and Consumer Secret. You will use these to initiate an Mpesa Instance.
  • Obtain Test Credentials here.
  • The Test Credentials Obtained Are only valid in Sandbox/Development environment. Take note of them.
  • To run in Production Environment you will need real Credentials.
  • To go Live and be issued with real credentials,please refer to this guide
  1. Add dependancy in pubspec.yaml
dependencies:
  mpesadaraja: ^0.1.5

Lipa Na MPesa Online

LIPA NA M-PESA ONLINE also known as M-PESA express (STK Push) is a Merchant/Business initiated C2B (Customer to Business) Payment. This process it takes as described in the detailed sequence diagram below is:

Once you, our merchant integrates to the API, you will be able to send a payment prompt on the customers phone (Popularly known as STK Push Prompt) to your customer's M-PESA registered phone number requesting them to enter their M-PESA pin to authorize and complete a payment.

  1. create MpesaDaraja object and pass the following parameters:
 MpesaDaraja stkpush = MpesaDaraja(
   consumerKey:<>
   consumerSecret:<>
   passKey:<>
 )

or make it a final as shown below:

  final  stkpush = MpesaDaraja(
    consumerKey:<>
    consumerSecret:<>
    passKey:<>
  )
  1. cosumerKey:
  2. consumerSecret
  3. passKey
  • The keys are generated when you create an app at [Daraja 2.0] website Click here to create your keys

  • The keys are a secret, so be sure to use them as environment variables in production code

  1. Use the object created to call lipaNaMpesaStk() function to initialize the process
    • if the function is inside anaother be sure to use a Future with await when caloing the function
    • pass the required parameters in the function as shwon below
 await stk.lipaNaMpesaStk(
    <BusinnessShortCode>,
    <Amount>,
    <PartyA>,
    <PartB>,
    <PhoneNumber>,
    <CallBackUrl>,
    <AccountReference>,
    <TransactionDescription>,
  );

Parameters passed in lipaNaMpesaStk() function

  1. BusinessShortCode: This is organizations shortcode (Paybill or Buygoods - A 5 to 7 digit account number) used to identify an organization and receive the transaction.

  2. Amount: This is the Amount transacted normaly a numeric value. Money that customer pays to the Shorcode. Only whole numbers are supported.

  3. PartyA: The phone number sending money. The parameter expected is a Valid Safaricom Mobile Number that is M-Pesa registered in the format 2547XXXXXXXX

  4. PartyB: The organization receiving the funds. The parameter expected is a 5 to 7 digit as defined on the Shortcode description above. This can be the same as BusinessShortCode value above.

  5. PhoneNUmber: The Mobile Number to receive the STK Pin Prompt. This number can be the same as PartyA value above.

  6. CallBackUrl: A CallBack URL is a valid secure URL that is used to receive notifications from M-Pesa API. It is the endpoint to which the results will be sent by M-Pesa A

  7. AccountReference: Account Reference: This is an Alpha-Numeric parameter that is defined by your system as an Identifier of the transaction for CustomerPayBillOnline transaction type. Along with the business name, this value is also displayed to the customer in the STK Pin Prompt message. Maximum of 12 characters.

  8. TransactionDescription: This is any additional information/comment that can be sent along with the request from your system. Maximum of 13 Characters.

N/B: Do not use my demo keys below since they will not work.

Future<void> main() async {
  final stk = MpesaDaraja(
    consumerKey: 'Dm4oJgziMyOT7WTmJzQfEZS6jjzg1Fkd',
    consumerSecret: 'RGRvsUGkO4jc3NuW',
    passKey: 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919',
  );

  await stk.lipaNaMpesaStk(
    "174379",
    1,
    "254798071520",
    "174379",
    "254798071520",
    "https://mydomain.com/path",
    "accountReference",
    "transactionDesc",
  );
}

Contributers

  1. Anselmo.Jr
  2. Joel
  3. sav4ner

Demo

  • coming soon ....

mpesa-daraja-plugin's People

Contributors

flavian-anselmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mpesa-daraja-plugin's Issues

Pass logged in Phone Number as <Party A> and as <PhoneNumber> from firebase Authentication:

I have set everythong write and I'm trying to call the user's logged in phone number to propmt Mpesa but it's not working:
Future initiatePayment() async {
final stk = MpesaDaraja(
consumerKey: 'my ConsumerKey',
consumerSecret: 'myConsumerSecret',
passKey: 'bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919',
);

int amount = int.parse(calculatedTotalCost.replaceAll(RegExp(r'[^\d]'), ''));

final User? user = FirebaseAuth.instance.currentUser;
String? userPhoneNumber = FirebaseAuth.instance.currentUser?.phoneNumber; // Declare the variable outside the if block

if (user != null) {
userPhoneNumber = user.phoneNumber;
} else {
// Handle the case where the user is not logged in or phone number is not available.
// You can show an error message or take appropriate action.
return;
}

await stk.lipaNaMpesaStk(
"174379",
amount,
userPhoneNumber!,
"174379",
userPhoneNumber,
"https://8570-41-80-116-102.ngrok-free.app",
"FIKISHA RIDERS",
"transactionDesc",
);
}

What can I do?
Uisng my phone Number works perfectly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.