public PaymentSimulationResult simulatePayment(final PaymentParameters params) {

    PaymentStatus status;
    AccountHistoryTransferVO transferVO = null;

    try {
      final PrepareParametersResult result = prepareParameters(params);
      status = result.getStatus();

      if (status == null) {
        final DoExternalPaymentDTO dto =
            paymentHelper.toExternalPaymentDTO(params, result.getFrom(), result.getTo());
        if (!validateTransferType(dto)) {
          webServiceHelper.trace(
              PaymentStatus.INVALID_PARAMETERS
                  + ". Reason: The service client doesn't have permission to the specified transfer type: "
                  + dto.getTransferType());
          status = PaymentStatus.INVALID_PARAMETERS;
        } else {
          // Simulate the payment
          final Transfer transfer = (Transfer) paymentService.simulatePayment(dto);
          transferVO = accountHelper.toVO(WebServiceContext.getMember(), transfer, null);
          status = paymentHelper.toStatus(transfer);
        }
      }
    } catch (final Exception e) {
      webServiceHelper.error(e);
      status = paymentHelper.toStatus(e);
    }

    if (!status.isSuccessful()) {
      webServiceHelper.error("Simulate payment status: " + status);
    }
    return new PaymentSimulationResult(status, transferVO);
  }
 private boolean validateTransferType(final DoExternalPaymentDTO dto) {
   final Collection<TransferType> possibleTypes = paymentHelper.listPossibleTypes(dto);
   return possibleTypes != null && possibleTypes.contains(dto.getTransferType());
 }
  public PaymentResult doPayment(final PaymentParameters params) {
    AccountHistoryTransferVO transferVO = null;
    PaymentStatus status;
    AccountStatus fromMemberStatus = null;
    AccountStatus toMemberStatus = null;
    try {
      final PrepareParametersResult result = prepareParameters(params);
      status = result.getStatus();

      if (status == null) {
        // Status null means no "pre-payment" errors (like validation, pin, channel...)
        // Perform the payment
        final DoExternalPaymentDTO dto =
            paymentHelper.toExternalPaymentDTO(params, result.getFrom(), result.getTo());

        // Validate the transfer type
        if (!validateTransferType(dto)) {
          status = PaymentStatus.INVALID_PARAMETERS;
          webServiceHelper.trace(
              status
                  + ". Reason: The service client doesn't have permission to the specified transfer type: "
                  + dto.getTransferType());
        } else {
          final Transfer transfer = (Transfer) paymentService.insertExternalPayment(dto);
          status = paymentHelper.toStatus(transfer);
          transferVO = accountHelper.toVO(WebServiceContext.getMember(), transfer, null);

          if (WebServiceContext.getClient()
                  .getPermissions()
                  .contains(ServiceOperation.ACCOUNT_DETAILS)
              && params.isReturnStatus()) {
            if (WebServiceContext.getMember() == null) {
              fromMemberStatus =
                  accountService.getStatus(
                      new GetTransactionsDTO(dto.getFrom(), dto.getTransferType().getFrom()));
              toMemberStatus =
                  accountService.getStatus(
                      new GetTransactionsDTO(dto.getTo(), dto.getTransferType().getTo()));
            } else if (WebServiceContext.getMember()
                .equals(paymentHelper.resolveFromMember(params))) {
              fromMemberStatus =
                  accountService.getStatus(
                      new GetTransactionsDTO(dto.getFrom(), dto.getTransferType().getFrom()));
            } else {
              toMemberStatus =
                  accountService.getStatus(
                      new GetTransactionsDTO(dto.getTo(), dto.getTransferType().getTo()));
            }
          }
        }
      }
    } catch (final Exception e) {
      webServiceHelper.error(e);
      status = paymentHelper.toStatus(e);
    }

    if (!status.isSuccessful()) {
      webServiceHelper.error("Payment status: " + status);
    }

    return new PaymentResult(
        status,
        transferVO,
        accountHelper.toVO(fromMemberStatus),
        accountHelper.toVO(toMemberStatus));
  }
  public List<PaymentResult> doBulkPayment(final List<PaymentParameters> params) {
    final List<PaymentResult> results =
        new ArrayList<PaymentResult>(params == null ? 0 : params.size());
    if (params != null) {
      // We should lock at once all from accounts for all payments, but only if all accounts are
      // passed ok
      boolean hasError = false;
      final List<AccountDTO> allAccounts = new ArrayList<AccountDTO>();
      for (int i = 0; i < params.size(); i++) {
        final PaymentParameters param = params.get(i);
        final PrepareParametersResult result = prepareParameters(param);

        if (result.getStatus() == null) {
          try {
            final DoExternalPaymentDTO dto =
                paymentHelper.toExternalPaymentDTO(param, result.getFrom(), result.getTo());
            if (!validateTransferType(dto)) {
              results.add(new PaymentResult(PaymentStatus.INVALID_PARAMETERS, null));
              webServiceHelper.error(
                  "The specified transfer type is invalid: " + dto.getTransferType());
              hasError = true;
            } else {
              allAccounts.add(new AccountDTO(result.getFrom(), dto.getTransferType().getFrom()));
              results.add(new PaymentResult(PaymentStatus.NOT_PERFORMED, null));
            }
          } catch (final Exception e) {
            webServiceHelper.error(e);
            hasError = true;
            results.add(new PaymentResult(paymentHelper.toStatus(e), null));
          }
        } else {
          hasError = true;
          results.add(new PaymentResult(result.getStatus(), null));
          webServiceHelper.error(
              "Bulk payment validation status [" + i + "]: " + result.getStatus());
        }
      }
      if (!hasError) {
        // No validation error. Lock all accounts and perform each payment
        try {
          accountService.lockAccounts(true, allAccounts);
        } catch (final Exception e) {
          WebServiceFaultsEnum.CURRENTLY_UNAVAILABLE.throwFault(e);
        }
        for (int i = 0; i < params.size(); i++) {
          final PaymentParameters param = params.get(i);
          PaymentResult result;
          if (hasError) {
            result = new PaymentResult(PaymentStatus.NOT_PERFORMED, null);
          } else {
            result = doPayment(param);
            if (!result.getStatus().isSuccessful()) {
              hasError = true;
            }
          }
          results.set(i, result);
        }
      }
    }
    return results;
  }