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;
}
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;
}
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());
}
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);
}
Aggregations