initializeMpesaSTKPush static method

Future initializeMpesaSTKPush({
  1. required String businessShortCode,
  2. required TransactionType transactionType,
  3. required double amount,
  4. required String partyA,
  5. required String partyB,
  6. required Uri callBackURL,
  7. required String accountReference,
  8. String? transactionDesc,
  9. required String phoneNumber,
  10. required Uri baseUri,
  11. required String passKey,
})

Implementation

static Future<dynamic> initializeMpesaSTKPush(
    {

    ///BusinessShortCode is the org paybill
    ///Which is same as PartyB
    ///Phone Number should be a registered MPESA number
    ///Which is same as PartyA

    required String businessShortCode,
    required TransactionType transactionType,
    required double amount,
    required String partyA,
    required String partyB,
    required Uri callBackURL,
    required String accountReference,
    String? transactionDesc,
    required String phoneNumber,
    required Uri baseUri,
    required String passKey}) async {
  /*Inject some sanity*/
  if (amount < 1.0) {
    throw "error: you provided $amount  as the amount which is not valid.";
  }
  if (phoneNumber.length < 9) {
    throw "error: $phoneNumber  doesn\'t seem to be a valid phone number";
  }
  if (!phoneNumber.startsWith('254')) {
    throw "error: $phoneNumber need be in international format";
  }

  /*Mine the secrets from Config*/

  if (!_consumerSecretSet || !_consumerKeySet) {
    throw "error: ensure consumer key & secret is set. Use MpesaFlutterPlugin.setConsumer...";
  }
  var rawTimeStamp = new DateTime.now();
  var formatter = new DateFormat('yyyyMMddHHmmss');
  String actualTimeStamp = formatter.format(rawTimeStamp);

  return RequestHandler(
          consumerKey: _mConsumerKeyVariable,
          consumerSecret: _mConsumerSecretVariable,
          baseUrl: baseUri.host)
      .mSTKRequest(
          mAccountReference: accountReference,
          mAmount: amount,
          mBusinessShortCode: businessShortCode,
          mCallBackURL: callBackURL,
          mPhoneNumber: phoneNumber,
          mTimeStamp: actualTimeStamp,
          mTransactionDesc: transactionDesc,
          nPassKey: passKey,
          partyA: partyA,
          partyB: partyB,
          mTransactionType:
              transactionType == TransactionType.CustomerPayBillOnline
                  ? "CustomerPayBillOnline"
                  : "CustomerBuyGoodsOnline");
}