initiateTransaction method

Future<UpiResponse> initiateTransaction(
  1. UPIRequestParameters upiRequestParams
)

Initiates a UPI transaction with the given upiRequestParams.

Parameters:

  • upiRequestParams: The parameters required for initiating a UPI transaction.

Returns: A Future that completes with the UPI response.

Implementation

Future<UpiResponse> initiateTransaction(
    UPIRequestParameters upiRequestParams) async {
  try {
    final String? response =
        await _channel.invokeMethod('initiateTransaction', {
      "app": upiRequestParams.paymentApp,
      'pa': upiRequestParams.payeeVpa,
      'pn': upiRequestParams.payeeName,
      'mc': upiRequestParams.payeeMerchantCode,
      'tr': upiRequestParams.transactionId,
      'trref':
          upiRequestParams.transactionRefId ?? upiRequestParams.transactionId,
      'tn': upiRequestParams.description ?? upiRequestParams.transactionId,
      'am': makeDecimal(upiRequestParams.amount!),
      'cu': upiRequestParams.currency ?? "INR",
      'url': "",
    });
    if (response != null && response != "User Cancelled transaction") {
      UpiResponse upiResponse = UpiResponse.fromResponseString(response);
      if (upiResponse.status == "Failure") {
        throw UpiException.fromException(
          PlatformException(
            code: "Failure",
            details: 'Transaction Failure',
            message: 'Transaction Failure',
          ),
        );
      }
      return upiResponse;
    } else if (response == "User Cancelled transaction") {
      throw UpiException.fromException(
        PlatformException(
          code: "Cancelled",
          details: 'User cancelled the transaction',
          message: 'Transaction cancelled',
        ),
      );
    } else {
      throw UpiException.fromException(
        PlatformException(
          code: UpiExceptionType.unknownException.toString(),
          details: 'No response from the payment',
          message: 'No response',
        ),
      );
    }
  } on PlatformException catch (e) {
    throw UpiException.fromException(e);
  } catch (e) {
    if ((e as UpiException).type == UpiExceptionType.cancelledException) {
      throw UpiException.fromException(
        PlatformException(
          code: "Cancelled",
          details: 'User cancelled the transaction',
          message: 'Transaction cancelled',
        ),
      );
    } else {
      throw UpiException(
        type: UpiExceptionType.unknownException,
        message: "Something error",
        details: e.toString(),
        stacktrace: e.toString(),
      );
    }
  }
}