@Override
 public void serviceChanged(final ServiceEvent event) {
   if (context == null
       || (event.getType() != ServiceEvent.REGISTERED
           && event.getType() != ServiceEvent.UNREGISTERING)) {
     // We are not initialized or uninterested
     return;
   }
   final ServiceReference serviceReference = event.getServiceReference();
   for (final OSGIServiceRegistration cur : allRegistrationHandlers) {
     if (listenForServiceType(serviceReference, event.getType(), cur.getServiceType(), cur)) {
       break;
     }
   }
 }
  private <T> boolean listenForServiceType(
      final ServiceReference serviceReference,
      final int eventType,
      final Class<T> claz,
      final OSGIServiceRegistration<T> registration) {
    // Make sure we can retrieve the plugin name
    final String serviceName =
        (String) serviceReference.getProperty(OSGIPluginProperties.PLUGIN_NAME_PROP);
    if (serviceName == null || !checkSanityPluginRegistrationName(serviceName)) {
      // Quite common for non Killbill bundles
      logger.debug(
          "Ignoring registered OSGI service {} with no {} property",
          claz.getName(),
          OSGIPluginProperties.PLUGIN_NAME_PROP);
      return true;
    }

    final Object theServiceObject = context.getService(serviceReference);
    // Is that for us? We look for a subclass here for greater flexibility (e.g. HttpServlet for a
    // Servlet service)
    if (theServiceObject == null || !claz.isAssignableFrom(theServiceObject.getClass())) {
      return false;
    }
    final T theService = (T) theServiceObject;

    final OSGIServiceDescriptor desc =
        new DefaultOSGIServiceDescriptor(
            serviceReference.getBundle().getSymbolicName(),
            bundleRegistry.getPluginName(serviceReference.getBundle()),
            serviceName);
    switch (eventType) {
      case ServiceEvent.REGISTERED:
        final T wrappedService =
            ContextClassLoaderHelper.getWrappedServiceWithCorrectContextClassLoader(
                theService, registration.getServiceType(), serviceName, metricsRegistry);
        registration.registerService(desc, wrappedService);
        bundleRegistry.registerService(desc, registration.getServiceType().getName());
        break;
      case ServiceEvent.UNREGISTERING:
        registration.unregisterService(desc.getRegistrationName());
        bundleRegistry.unregisterService(desc, registration.getServiceType().getName());
        break;
      default:
        break;
    }
    return true;
  }
 private PaymentPluginApiWithTestControl getTestPluginPaymentApi() {
   final PaymentPluginApiWithTestControl result =
       (PaymentPluginApiWithTestControl)
           paymentPluginApiOSGIServiceRegistration.getServiceForName(
               BeatrixIntegrationModule.OSGI_PLUGIN_NAME);
   Assert.assertNotNull(result);
   return result;
 }
Пример #4
0
  @BeforeClass(groups = "slow")
  public void beforeClass() throws Exception {
    super.beforeClass();

    this.testInvoicePluginApi = new TestInvoicePluginApi();
    pluginRegistry.registerService(
        new OSGIServiceDescriptor() {
          @Override
          public String getPluginSymbolicName() {
            return "TaxInvoicePluginApi";
          }

          @Override
          public String getRegistrationName() {
            return "TaxInvoicePluginApi";
          }
        },
        testInvoicePluginApi);
  }
Пример #5
0
 @BeforeMethod(groups = "slow")
 public void beforeMethod() throws Exception {
   super.beforeMethod();
   mockPaymentProviderPlugin = (MockPaymentProviderPlugin) registry.getServiceForName(PLUGIN_NAME);
 }
  public PriorPaymentControlResult executePluginPriorCalls(
      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)
      throws PaymentControlApiException {
    // Return as soon as the first plugin aborts, or the last result for the last plugin
    PriorPaymentControlResult prevResult =
        new DefaultPriorPaymentControlResult(
            false, amount, currency, paymentMethodId, pluginProperties);

    // Those values are adjusted prior each call with the result of what previous call to plugin
    // returned
    UUID inputPaymentMethodId = paymentMethodId;
    BigDecimal inputAmount = amount;
    Currency inputCurrency = currency;
    Iterable<PluginProperty> inputPluginProperties = pluginProperties;
    PaymentControlContext inputPaymentControlContext =
        new DefaultPaymentControlContext(
            account,
            paymentMethodId,
            paymentAttemptId,
            paymentId,
            paymentExternalKey,
            paymentTransactionExternalKey,
            paymentApiType,
            transactionType,
            hppType,
            amount,
            currency,
            isApiPayment,
            callContext);

    for (final String pluginName : paymentControlPluginNames) {
      final PaymentControlPluginApi plugin =
          paymentControlPluginRegistry.getServiceForName(pluginName);
      if (plugin == null) {
        // First call to plugin, we log warn, if plugin is not registered
        log.warn("Skipping unknown payment control plugin {} when fetching results", pluginName);
        continue;
      }
      log.debug("Calling priorCall of plugin {}", pluginName);
      prevResult = plugin.priorCall(inputPaymentControlContext, inputPluginProperties);
      log.debug("Successful executed priorCall of plugin {}", pluginName);
      if (prevResult.getAdjustedPaymentMethodId() != null) {
        inputPaymentMethodId = prevResult.getAdjustedPaymentMethodId();
      }
      if (prevResult.getAdjustedAmount() != null) {
        inputAmount = prevResult.getAdjustedAmount();
      }
      if (prevResult.getAdjustedCurrency() != null) {
        inputCurrency = prevResult.getAdjustedCurrency();
      }
      if (prevResult.getAdjustedPluginProperties() != null) {
        inputPluginProperties = prevResult.getAdjustedPluginProperties();
      }
      if (prevResult.isAborted()) {
        throw new PaymentControlApiAbortException(pluginName);
      }
      inputPaymentControlContext =
          new DefaultPaymentControlContext(
              account,
              inputPaymentMethodId,
              paymentAttemptId,
              paymentId,
              paymentExternalKey,
              paymentTransactionExternalKey,
              paymentApiType,
              transactionType,
              hppType,
              inputAmount,
              inputCurrency,
              isApiPayment,
              callContext);
    }
    // Rebuild latest result to include inputPluginProperties
    prevResult =
        new DefaultPriorPaymentControlResult(
            prevResult.isAborted(),
            inputPaymentMethodId,
            inputAmount,
            inputCurrency,
            inputPluginProperties);
    return prevResult;
  }
  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);
  }
  @BeforeMethod(groups = "fast")
  public void beforeMethod() throws Exception {

    super.beforeMethod();

    account = testHelper.createTestAccount("*****@*****.**", true);

    paymentOptions =
        new PaymentOptions() {
          @Override
          public boolean isExternalPayment() {
            return false;
          }

          @Override
          public List<String> getPaymentControlPluginNames() {
            return ImmutableList.of(
                TestPaymentGatewayApiControlPlugin.PLUGIN_NAME,
                TestPaymentGatewayApiValidationPlugin.VALIDATION_PLUGIN_NAME);
          }
        };

    plugin = new TestPaymentGatewayApiControlPlugin();

    controlPluginRegistry.registerService(
        new OSGIServiceDescriptor() {
          @Override
          public String getPluginSymbolicName() {
            return null;
          }

          @Override
          public String getPluginName() {
            return TestPaymentGatewayApiControlPlugin.PLUGIN_NAME;
          }

          @Override
          public String getRegistrationName() {
            return TestPaymentGatewayApiControlPlugin.PLUGIN_NAME;
          }
        },
        plugin);

    validationPlugin = new TestPaymentGatewayApiValidationPlugin();
    controlPluginRegistry.registerService(
        new OSGIServiceDescriptor() {
          @Override
          public String getPluginSymbolicName() {
            return null;
          }

          @Override
          public String getPluginName() {
            return TestPaymentGatewayApiValidationPlugin.VALIDATION_PLUGIN_NAME;
          }

          @Override
          public String getRegistrationName() {
            return TestPaymentGatewayApiValidationPlugin.VALIDATION_PLUGIN_NAME;
          }
        },
        validationPlugin);
  }