public OnFailurePaymentControlResult executePluginOnFailureCalls(
      final Account account,
      final UUID paymentMethodId,
      final UUID paymentAttemptId,
      final UUID paymentId,
      final String paymentExternalKey,
      final String paymentTransactionExternalKey,
      final PaymentApiType paymentApiType,
      final TransactionType transactionType,
      final HPPType hppType,
      final BigDecimal amount,
      final Currency currency,
      final boolean isApiPayment,
      final List<String> paymentControlPluginNames,
      final Iterable<PluginProperty> pluginProperties,
      final CallContext callContext) {

    final PaymentControlContext inputPaymentControlContext =
        new DefaultPaymentControlContext(
            account,
            paymentMethodId,
            paymentAttemptId,
            paymentId,
            paymentExternalKey,
            paymentTransactionExternalKey,
            paymentApiType,
            transactionType,
            hppType,
            amount,
            currency,
            isApiPayment,
            callContext);

    DateTime candidate = null;
    Iterable<PluginProperty> inputPluginProperties = pluginProperties;

    for (final String pluginName : paymentControlPluginNames) {
      final PaymentControlPluginApi plugin =
          paymentControlPluginRegistry.getServiceForName(pluginName);
      if (plugin != null) {
        try {
          log.debug("Calling onSuccessCall of plugin {}", pluginName);
          final OnFailurePaymentControlResult result =
              plugin.onFailureCall(inputPaymentControlContext, inputPluginProperties);
          log.debug("Successful executed onSuccessCall of plugin {}", pluginName);
          if (candidate == null) {
            candidate = result.getNextRetryDate();
          } else if (result.getNextRetryDate() != null) {
            candidate =
                candidate.compareTo(result.getNextRetryDate()) > 0
                    ? result.getNextRetryDate()
                    : candidate;
          }

          if (result.getAdjustedPluginProperties() != null) {
            inputPluginProperties = result.getAdjustedPluginProperties();
          }

        } catch (final PaymentControlApiException e) {
          log.warn(
              "Error during onFailureCall for plugin='{}', paymentExternalKey='{}'",
              pluginName,
              inputPaymentControlContext.getPaymentExternalKey(),
              e);
          return new DefaultFailureCallResult(candidate, inputPluginProperties);
        }
      }
    }
    return new DefaultFailureCallResult(candidate, inputPluginProperties);
  }
  public OnSuccessPaymentControlResult executePluginOnSuccessCalls(
      final Account account,
      final UUID paymentMethodId,
      final UUID paymentAttemptId,
      final UUID paymentId,
      final String paymentExternalKey,
      final UUID transactionId,
      final String paymentTransactionExternalKey,
      final PaymentApiType paymentApiType,
      final TransactionType transactionType,
      final HPPType hppType,
      final BigDecimal amount,
      final Currency currency,
      final BigDecimal processedAmount,
      final Currency processedCurrency,
      final boolean isApiPayment,
      final List<String> paymentControlPluginNames,
      final Iterable<PluginProperty> pluginProperties,
      final CallContext callContext) {

    final PaymentControlContext inputPaymentControlContext =
        new DefaultPaymentControlContext(
            account,
            paymentMethodId,
            paymentAttemptId,
            paymentId,
            paymentExternalKey,
            transactionId,
            paymentTransactionExternalKey,
            paymentApiType,
            transactionType,
            hppType,
            amount,
            currency,
            processedAmount,
            processedCurrency,
            isApiPayment,
            callContext);

    Iterable<PluginProperty> inputPluginProperties = pluginProperties;
    for (final String pluginName : paymentControlPluginNames) {
      final PaymentControlPluginApi plugin =
          paymentControlPluginRegistry.getServiceForName(pluginName);
      if (plugin != null) {
        try {
          log.debug("Calling onSuccessCall of plugin {}", pluginName);
          final OnSuccessPaymentControlResult result =
              plugin.onSuccessCall(inputPaymentControlContext, inputPluginProperties);
          log.debug("Successful executed onSuccessCall of plugin {}", pluginName);
          if (result.getAdjustedPluginProperties() != null) {
            inputPluginProperties = result.getAdjustedPluginProperties();
          }
          // Exceptions from the control plugins are ignored (and logged) because the semantics on
          // what to do are undefined.
        } catch (final PaymentControlApiException e) {
          log.warn(
              "Error during onSuccessCall for plugin='{}', paymentExternalKey='{}'",
              pluginName,
              inputPaymentControlContext.getPaymentExternalKey(),
              e);
        } catch (final RuntimeException e) {
          log.warn(
              "Error during onSuccessCall for plugin='{}', paymentExternalKey='{}'",
              pluginName,
              inputPaymentControlContext.getPaymentExternalKey(),
              e);
        }
      }
    }
    return new DefaultOnSuccessPaymentControlResult(inputPluginProperties);
  }