startTransaction method

Future<UpiResponse> startTransaction({
  1. required UpiApp app,
  2. required String receiverUpiId,
  3. required String receiverName,
  4. required String transactionRefId,
  5. String? transactionNote,
  6. double amount = 1,
  7. bool flexibleAmount = false,
  8. String currency = "INR",
  9. String? url,
  10. String? merchantId,
})

This method is used to initiate a transaction with given parameters.

Implementation

Future<UpiResponse> startTransaction({
  /// app refers to app name provided using [UpiApp] class.
  required UpiApp app,

  /// receiverUpiId is the UPI ID of the Payee (who will receive the money).
  /// Double check this value or you may end up paying the wrong person.
  required String receiverUpiId,

  /// receiverName is the name of the Payee( who will receive the  money)
  required String receiverName,

  /// transactionRefId is a unique literal which you can use to find the transaction later easily.
  /// It is mandatory for Merchant transactions and dynamic URL generation.
  /// In response the "txnRef" that you will receive must match this value.
  required String transactionRefId,

  /// transactionNote provides short description of transaction.
  String? transactionNote,

  /// amount is the actual amount in decimal format (in INR by default) that should be paid.
  /// amount = 0 if you want user to enter the amount.
  double amount = 1,

  /// If true allows user to enter the amount
  bool flexibleAmount = false,

  /// currency refers to the currency code. Currently only "INR" is supported by UPI.
  String currency = "INR",

  /// url when used, MUST BE related to the particular transaction and
  /// MUST NOT be used to send unsolicited information that are not relevant to the transaction.
  String? url,

  /// Payee merchant code. If present then needs to be passed as it is.
  String? merchantId,
}) {
  if (receiverUpiId.split("@").length != 2) {
    return Future.error(UpiIndiaInvalidParametersException("Incorrect UPI ID provided"));
  }

//    if (receiverAccountNumber != null && !RegExp(r'^\d{9,18}$').hasMatch(receiverAccountNumber)) {
//      return Future.error(UpiIndiaInvalidParametersException(
//        "Invalid Account number provided! Indian Account number length varies between 9 to 18 only",
//      ));
//    }
//
//    if(receiverIfscCode != null && !RegExp(r'^[A-Za-z]{4}[a-zA-Z0-9]{7}$').hasMatch(receiverIfscCode)){
//      return Future.error(UpiIndiaInvalidParametersException(
//        "Invalid IFSC code provided!",
//      ));
//    }

  if (amount < 1 || amount > 100000) {
    return Future.error(
      UpiIndiaInvalidParametersException(
        "Incorrect amount provided. Range is between 1 to 1,00,000",
      ),
    );
  }

  if (!RegExp(r'^\d{1,}(\.\d{0,2})?$').hasMatch(amount.toString())) {
    return Future.error(UpiIndiaInvalidParametersException(
      "Incorrect amount format. Make sure to use only two digits after decimal",
    ));
  }

  if (currency != "INR") {
    return Future.error(UpiIndiaInvalidParametersException("Only INR is currently supported"));
  }

  return _channel.invokeMethod('startTransaction', {
    "app": app.packageName,
    'receiverUpiId': receiverUpiId,
    'receiverName': receiverName,
    'transactionRefId': transactionRefId,
    'transactionNote': transactionNote,
    'amount': flexibleAmount ? '' : amount.toString(),
    'currency': currency,
    'merchantId': merchantId,
  }).then((response) {
    print("UPI_INDIA_FINAL_RESPONSE: $response");
    return UpiResponse(response);
  }).catchError((e) {
    print("UPI_INDIA_FINAL_RESPONSE: ${e.message}");
    switch (e.code) {
      case "app_not_installed":
        throw UpiIndiaAppNotInstalledException();

      case "null_response":
        throw UpiIndiaNullResponseException();

      case "user_canceled":
        throw UpiIndiaUserCancelledException();

      case "invalid_parameters":
        throw UpiIndiaInvalidParametersException();

      default:
        throw e;
    }
  }, test: (e) => e is PlatformException);
}