keronei / mpesa_flutter_plugin

Simple plugin for implementing Lipa Na MPESA Online on a flutter app.
https://pub.dev/packages/mpesa_flutter_plugin
Other
35 stars 19 forks source link

The plugin is not initializing properly on every mpesa request. #7

Closed ryann254 closed 2 years ago

ryann254 commented 2 years ago

I have integrated this plugin in my flutter app and when I first load up the app it works well, but on repeated trials it does not work. What I mean is that yes it launching the STK push for payment but when the payment is done, it's not redirecting safaricom to my callback url so that the response can be stored there. The only way repeated trials are working is when I change the account reference, save the app then run it again. Here's the code`
// Create the lipaNaMpesa method here. Future lipaNaMpesa({String type}) async { dynamic transactionInitialisation;

try {
  transactionInitialisation =
      await MpesaFlutterPlugin.initializeMpesaSTKPush(
    businessShortCode: '174379',
    transactionType: TransactionType.CustomerPayBillOnline,
    amount: 1.0,
    partyA: phonenumber,
    partyB: '174379',
    callBackURL: Uri(
      scheme: 'https',
      host: globals.apiKey,
      path: 'mpesacallbackURL',
    ),
    transactionDesc: 'Parking pass',
    accountReference: 'Parking Kenya Limited',
    phoneNumber: phonenumber,
    baseUri: Uri(scheme: "https", host: "sandbox.safaricom.co.ke"),
    passKey:
        'test passKey',
  );

 // Here I'm checking if the STK push launched successfully.
  if (transactionInitialisation['ResponseCode'] == '0') {
   // Here I'm waiting for the user to enter his pin before checking whether the response is in the database.
    timer = new Timer(const Duration(seconds: 4), () async {
      try {
       // Check if the transaction is in the db
        var response = await fetchTransaction();

        // If the transaction is there...show success message.
      } catch (err) {
        // If it's not then...show error message.
      }
    });
  }

  return transactionInitialisation;
} catch (e) {
  log("CAUGHT EXCEPTION: " + e.toString());
}

}`