Search in sources :

Example 1 with PriorPaymentControlResult

use of org.killbill.billing.control.plugin.api.PriorPaymentControlResult in project killbill by killbill.

the class ControlPluginRunner method executePluginPriorCalls.

public PriorPaymentControlResult executePluginPriorCalls(final Account account, final UUID paymentMethodId, final String pluginName, final UUID paymentAttemptId, final UUID paymentId, final String paymentExternalKey, final UUID paymentTransactionId, 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) 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, null, pluginProperties);
    // Those values are adjusted prior each call with the result of what previous call to plugin returned
    UUID inputPaymentMethodId = paymentMethodId;
    String inputPaymentMethodName = pluginName;
    BigDecimal inputAmount = amount;
    Currency inputCurrency = currency;
    Iterable<PluginProperty> inputPluginProperties = pluginProperties;
    final UUID accountId = account != null ? account.getId() : null;
    for (final String controlPluginName : paymentControlPluginNames) {
        final PaymentControlContext inputPaymentControlContext = new DefaultPaymentControlContext(accountId, inputPaymentMethodId, controlPluginName, paymentAttemptId, paymentId, paymentExternalKey, paymentTransactionId, paymentTransactionExternalKey, paymentApiType, transactionType, hppType, inputAmount, inputCurrency, processedAmount, processedCurrency, isApiPayment, callContext);
        final PaymentControlPluginApi plugin = paymentControlPluginRegistry.getServiceForName(controlPluginName);
        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", controlPluginName);
            continue;
        }
        log.debug("Calling priorCall of plugin {}", controlPluginName);
        prevResult = plugin.priorCall(inputPaymentControlContext, inputPluginProperties);
        log.debug("Successful executed priorCall of plugin {}", controlPluginName);
        if (prevResult == null) {
            // Nothing returned by the plugin
            continue;
        }
        if (prevResult.getAdjustedPaymentMethodId() != null) {
            // unless the property isAllowedToOverwritePaymentMethodId was explicitly configured to allow this
            if (!paymentConfig.isAllowedToOverwritePaymentMethodId() && paymentMethodId != null) {
                throw new PaymentControlApiException(String.format("Not allowed to overwrite paymentMethodId '%s' for payment '%s'", paymentMethodId, paymentId));
            }
            inputPaymentMethodId = prevResult.getAdjustedPaymentMethodId();
        }
        if (prevResult.getAdjustedPluginName() != null) {
            inputPaymentMethodName = prevResult.getAdjustedPluginName();
        }
        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(controlPluginName);
        }
    }
    // Rebuild latest result to include inputPluginProperties
    prevResult = new DefaultPriorPaymentControlResult(prevResult != null && prevResult.isAborted(), inputPaymentMethodId, inputPaymentMethodName, inputAmount, inputCurrency, inputPluginProperties);
    return prevResult;
}
Also used : PaymentControlPluginApi(org.killbill.billing.control.plugin.api.PaymentControlPluginApi) DefaultPriorPaymentControlResult(org.killbill.billing.payment.retry.DefaultPriorPaymentControlResult) BigDecimal(java.math.BigDecimal) PaymentControlApiException(org.killbill.billing.control.plugin.api.PaymentControlApiException) PluginProperty(org.killbill.billing.payment.api.PluginProperty) Currency(org.killbill.billing.catalog.api.Currency) DefaultPriorPaymentControlResult(org.killbill.billing.payment.retry.DefaultPriorPaymentControlResult) PriorPaymentControlResult(org.killbill.billing.control.plugin.api.PriorPaymentControlResult) PaymentControlContext(org.killbill.billing.control.plugin.api.PaymentControlContext) UUID(java.util.UUID)

Example 2 with PriorPaymentControlResult

use of org.killbill.billing.control.plugin.api.PriorPaymentControlResult in project killbill by killbill.

the class OperationControlCallback method executePluginPriorCalls.

private PriorPaymentControlResult executePluginPriorCalls(final List<String> paymentControlPluginNames, final PaymentControlContext paymentControlContextArg) throws PaymentControlApiException {
    final PriorPaymentControlResult result = controlPluginRunner.executePluginPriorCalls(paymentStateContext.getAccount(), paymentControlContextArg.getPaymentMethodId(), paymentControlContextArg.getPaymentPluginName(), paymentStateControlContext.getAttemptId(), paymentStateContext.getPaymentId(), paymentStateContext.getPaymentExternalKey(), paymentStateContext.getTransactionId(), paymentStateContext.getPaymentTransactionExternalKey(), PaymentApiType.PAYMENT_TRANSACTION, paymentStateContext.getTransactionType(), null, paymentControlContextArg.getAmount(), paymentControlContextArg.getCurrency(), paymentControlContextArg.getProcessedAmount(), paymentControlContextArg.getProcessedCurrency(), paymentStateControlContext.isApiPayment(), paymentControlPluginNames, paymentStateContext.getProperties(), paymentStateContext.getCallContext());
    adjustStateContextForPriorCall(paymentStateContext, result);
    return result;
}
Also used : PriorPaymentControlResult(org.killbill.billing.control.plugin.api.PriorPaymentControlResult)

Example 3 with PriorPaymentControlResult

use of org.killbill.billing.control.plugin.api.PriorPaymentControlResult in project killbill by killbill.

the class TestControlPluginRunner method testPriorCallWithUnknownPlugin.

@Test(groups = "fast")
public void testPriorCallWithUnknownPlugin() throws Exception {
    final Account account = Mockito.mock(Account.class);
    final UUID paymentMethodId = UUIDs.randomUUID();
    final UUID paymentId = UUIDs.randomUUID();
    final String paymentExternalKey = UUIDs.randomUUID().toString();
    final UUID paymentTransactionId = UUIDs.randomUUID();
    final String paymentTransactionExternalKey = UUIDs.randomUUID().toString();
    final BigDecimal amount = BigDecimal.ONE;
    final Currency currency = Currency.USD;
    final ImmutableList<String> paymentControlPluginNames = ImmutableList.<String>of("not-registered");
    final ImmutableList<PluginProperty> pluginProperties = ImmutableList.<PluginProperty>of();
    final ControlPluginRunner controlPluginRunner = new ControlPluginRunner(new DefaultPaymentControlProviderPluginRegistry(), paymentConfig);
    final PriorPaymentControlResult paymentControlResult = controlPluginRunner.executePluginPriorCalls(account, paymentMethodId, null, null, paymentId, paymentExternalKey, paymentTransactionId, paymentTransactionExternalKey, PaymentApiType.PAYMENT_TRANSACTION, TransactionType.AUTHORIZE, null, amount, currency, null, null, true, paymentControlPluginNames, pluginProperties, callContext);
    Assert.assertEquals(paymentControlResult.getAdjustedAmount(), amount);
    Assert.assertEquals(paymentControlResult.getAdjustedCurrency(), currency);
    Assert.assertEquals(paymentControlResult.getAdjustedPaymentMethodId(), paymentMethodId);
    Assert.assertEquals(paymentControlResult.getAdjustedPluginProperties(), pluginProperties);
    Assert.assertFalse(paymentControlResult.isAborted());
}
Also used : Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) DefaultPaymentControlProviderPluginRegistry(org.killbill.billing.payment.provider.DefaultPaymentControlProviderPluginRegistry) Currency(org.killbill.billing.catalog.api.Currency) PriorPaymentControlResult(org.killbill.billing.control.plugin.api.PriorPaymentControlResult) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with PriorPaymentControlResult

use of org.killbill.billing.control.plugin.api.PriorPaymentControlResult in project killbill by killbill.

the class TestControlPluginRunner method testPriorCallWithNullAccountAndPaymentMethodId.

// This tests the PSP notification processing case
@Test(groups = "fast")
public void testPriorCallWithNullAccountAndPaymentMethodId() throws Exception {
    final UUID paymentId = UUIDs.randomUUID();
    final String paymentExternalKey = UUIDs.randomUUID().toString();
    final UUID paymentTransactionId = UUIDs.randomUUID();
    final String paymentTransactionExternalKey = UUIDs.randomUUID().toString();
    final BigDecimal amount = BigDecimal.ONE;
    final Currency currency = Currency.USD;
    final ImmutableList<String> paymentControlPluginNames = ImmutableList.<String>of("not-registered");
    final ImmutableList<PluginProperty> pluginProperties = ImmutableList.<PluginProperty>of();
    final ControlPluginRunner controlPluginRunner = new ControlPluginRunner(new DefaultPaymentControlProviderPluginRegistry(), paymentConfig);
    final PriorPaymentControlResult paymentControlResult = controlPluginRunner.executePluginPriorCalls(null, null, null, null, paymentId, paymentExternalKey, paymentTransactionId, paymentTransactionExternalKey, PaymentApiType.PAYMENT_TRANSACTION, TransactionType.AUTHORIZE, null, amount, currency, null, null, true, paymentControlPluginNames, pluginProperties, callContext);
    Assert.assertNotNull(paymentControlResult);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) DefaultPaymentControlProviderPluginRegistry(org.killbill.billing.payment.provider.DefaultPaymentControlProviderPluginRegistry) Currency(org.killbill.billing.catalog.api.Currency) PriorPaymentControlResult(org.killbill.billing.control.plugin.api.PriorPaymentControlResult) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

PriorPaymentControlResult (org.killbill.billing.control.plugin.api.PriorPaymentControlResult)4 BigDecimal (java.math.BigDecimal)3 UUID (java.util.UUID)3 Currency (org.killbill.billing.catalog.api.Currency)3 PluginProperty (org.killbill.billing.payment.api.PluginProperty)3 DefaultPaymentControlProviderPluginRegistry (org.killbill.billing.payment.provider.DefaultPaymentControlProviderPluginRegistry)2 Test (org.testng.annotations.Test)2 Account (org.killbill.billing.account.api.Account)1 PaymentControlApiException (org.killbill.billing.control.plugin.api.PaymentControlApiException)1 PaymentControlContext (org.killbill.billing.control.plugin.api.PaymentControlContext)1 PaymentControlPluginApi (org.killbill.billing.control.plugin.api.PaymentControlPluginApi)1 DefaultPriorPaymentControlResult (org.killbill.billing.payment.retry.DefaultPriorPaymentControlResult)1