initiateTransaction method

Future<M4eTransactionResult> initiateTransaction (M4eUniqueId userId, M4eTransactionForm transaction)

Requires [M4eTransactionForm] as parameter

Throws [M4eServerException] if call to server fails

Throws [M4eServerException] with NO_INTERNET_CONNECTION error code device is not connected to the internet

Implementation

Future<M4eTransactionResult> initiateTransaction(
    M4eUniqueId userId, M4eTransactionForm transaction) async {
  if (transaction.from == transaction.to) {
    throw M4eExceptionMessages.kSameTransactionWalletException;
  }

  if ((await _connectionChecker.hasConnection) ?? false) {
    /// extracts util methods from transaction service
    final _utilService = TransactionServiceUtil(walletApi: _walletApi);

    try {
      /// map [M4eTransactionForm] to [InternalTransactionForm] type
      final _fromWallet = await transaction.from.map(
        m4eWalletProvider: (provider) {
          return _utilService.m4eProviderTransactionMapper(
            walletProvider: transaction.from,
          );
        },
        mobileMoneyWalletProvider: (provider) {
          return _utilService.mobileMoneyTransactionMapper(
            userId: userId,
            transaction: transaction,
            walletProvider: transaction.from,
          );
        },
      );

      final _toWallet = await transaction.to.map(
        m4eWalletProvider: (provider) {
          return _utilService.m4eProviderTransactionMapper(
            walletProvider: transaction.to,
          );
        },
        mobileMoneyWalletProvider: (provider) {
          return _utilService.mobileMoneyTransactionMapper(
            userId: userId,
            transaction: transaction,
            walletProvider: transaction.to,
          );
        },
      );

      // create [InternalTransactionForm]
      final _modifiedTransaction = InternalTransactionForm(
        from: _fromWallet,
        to: _toWallet,
        amount: transaction.amount,
        at: transaction.at,
        meta: transaction.meta,
      );

      final _transaction =
          await _transactionApi.initiateTransaction(_modifiedTransaction);

      return _transaction;
    } catch (e) {
      rethrow;
    }
  }

  throw M4eExceptionMessages.kNoInternetConnectionException;
}