Search in sources :

Example 1 with PaymentTransactionInfoPlugin

use of org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin in project killbill by killbill.

the class PaymentProcessor method toPayment.

// Used in single get APIs (getPayment / getPaymentByExternalKey)
private Payment toPayment(final PaymentModelDao paymentModelDao, final boolean withPluginInfo, final boolean withAttempts, final Iterable<PluginProperty> properties, final TenantContext context, final InternalTenantContext tenantContext) throws PaymentApiException {
    final PaymentPluginApi plugin = getPaymentProviderPlugin(paymentModelDao.getPaymentMethodId(), true, tenantContext);
    final List<PaymentTransactionInfoPlugin> pluginTransactions = withPluginInfo ? getPaymentTransactionInfoPlugins(plugin, paymentModelDao, properties, context) : null;
    return toPayment(paymentModelDao, pluginTransactions, withAttempts, tenantContext);
}
Also used : PaymentPluginApi(org.killbill.billing.payment.plugin.api.PaymentPluginApi) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin)

Example 2 with PaymentTransactionInfoPlugin

use of org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin in project killbill by killbill.

the class PaymentProcessor method invokeJanitor.

private PaymentModelDao invokeJanitor(final PaymentModelDao curPaymentModelDao, final Collection<PaymentTransactionModelDao> curTransactionsModelDao, @Nullable final Iterable<PaymentTransactionInfoPlugin> pluginTransactions, final InternalTenantContext internalTenantContext) {
    // Need to filter for optimized codepaths looking up by account_record_id
    final Iterable<PaymentTransactionModelDao> filteredTransactions = Iterables.filter(curTransactionsModelDao, new Predicate<PaymentTransactionModelDao>() {

        @Override
        public boolean apply(final PaymentTransactionModelDao curPaymentTransactionModelDao) {
            return curPaymentTransactionModelDao.getPaymentId().equals(curPaymentModelDao.getId());
        }
    });
    PaymentModelDao newPaymentModelDao = curPaymentModelDao;
    final Collection<PaymentTransactionModelDao> transactionsModelDao = new LinkedList<PaymentTransactionModelDao>();
    for (final PaymentTransactionModelDao curPaymentTransactionModelDao : filteredTransactions) {
        PaymentTransactionModelDao newPaymentTransactionModelDao = curPaymentTransactionModelDao;
        final PaymentTransactionInfoPlugin paymentTransactionInfoPlugin = findPaymentTransactionInfoPlugin(newPaymentTransactionModelDao, pluginTransactions);
        if (paymentTransactionInfoPlugin != null) {
            // Make sure to invoke the Janitor task in case the plugin fixes its state on the fly
            // See https://github.com/killbill/killbill/issues/341
            final boolean hasChanged = incompletePaymentTransactionTask.updatePaymentAndTransactionIfNeededWithAccountLock(newPaymentModelDao, newPaymentTransactionModelDao, paymentTransactionInfoPlugin, internalTenantContext);
            if (hasChanged) {
                newPaymentModelDao = paymentDao.getPayment(newPaymentModelDao.getId(), internalTenantContext);
                newPaymentTransactionModelDao = paymentDao.getPaymentTransaction(newPaymentTransactionModelDao.getId(), internalTenantContext);
            }
        }
        transactionsModelDao.add(newPaymentTransactionModelDao);
    }
    curTransactionsModelDao.clear();
    curTransactionsModelDao.addAll(transactionsModelDao);
    return newPaymentModelDao;
}
Also used : PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) PaymentModelDao(org.killbill.billing.payment.dao.PaymentModelDao) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) LinkedList(java.util.LinkedList)

Example 3 with PaymentTransactionInfoPlugin

use of org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin in project killbill by killbill.

the class PaymentEnteringStateCallback method enteringState.

@Override
public void enteringState(final State newState, final Operation.OperationCallback operationCallback, final OperationResult operationResult, final LeavingStateCallback leavingStateCallback) {
    logger.debug("Entering state {} with result {}", newState.getName(), operationResult);
    // If the transaction was not created -- for instance we had an exception in leavingState callback then we bail; if not, then update state:
    if (paymentStateContext.getPaymentTransactionModelDao() != null && paymentStateContext.getPaymentTransactionModelDao().getId() != null) {
        final PaymentTransactionInfoPlugin paymentInfoPlugin = paymentStateContext.getPaymentTransactionInfoPlugin();
        final TransactionStatus transactionStatus = PaymentTransactionInfoPluginConverter.toTransactionStatus(paymentInfoPlugin);
        // The bus event will be posted from the transaction
        daoHelper.processPaymentInfoPlugin(transactionStatus, paymentInfoPlugin, newState.getName());
    } else if (!paymentStateContext.isApiPayment()) {
        //
        // If there is NO transaction to update (because payment transaction did not occur), then there is something wrong happening (maybe a missing defaultPaymentMethodId, ...)
        // so, if the call does NOT originates from api then we still want to send a bus event so the system can react to it if needed.
        //
        final BusInternalEvent event = new DefaultPaymentErrorEvent(paymentStateContext.getAccount().getId(), null, null, paymentStateContext.getAmount(), paymentStateContext.getCurrency(), null, paymentStateContext.getTransactionType(), null, "Early abortion of payment transaction", paymentStateContext.getInternalCallContext().getAccountRecordId(), paymentStateContext.getInternalCallContext().getTenantRecordId(), paymentStateContext.getInternalCallContext().getUserToken());
        try {
            daoHelper.getEventBus().post(event);
        } catch (EventBusException e) {
            logger.warn("Failed to post event {}", event, e);
        }
    }
}
Also used : PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) TransactionStatus(org.killbill.billing.payment.api.TransactionStatus) EventBusException(org.killbill.bus.api.PersistentBus.EventBusException) DefaultPaymentErrorEvent(org.killbill.billing.payment.api.DefaultPaymentErrorEvent) BusInternalEvent(org.killbill.billing.events.BusInternalEvent)

Example 4 with PaymentTransactionInfoPlugin

use of org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin in project killbill by killbill.

the class TestPaymentAutomatonDAOHelper method testCreateNewPaymentTransaction.

@Test(groups = "slow")
public void testCreateNewPaymentTransaction() throws Exception {
    // Create a payment and transaction based on the context
    final PaymentAutomatonDAOHelper daoHelper = createDAOHelper(null, paymentExternalKey, paymentTransactionExternalKey, amount, currency);
    daoHelper.createNewPaymentTransaction();
    final PaymentModelDao payment1 = daoHelper.getPayment();
    Assert.assertEquals(payment1.getExternalKey(), paymentExternalKey);
    Assert.assertNull(payment1.getStateName());
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getTransactionExternalKey(), paymentTransactionExternalKey);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getAmount().compareTo(amount), 0);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getCurrency(), currency);
    // Verify we can update them
    final PaymentTransactionInfoPlugin paymentInfoPlugin = Mockito.mock(PaymentTransactionInfoPlugin.class);
    Mockito.when(paymentInfoPlugin.getAmount()).thenReturn(new BigDecimal("82010.222"));
    Mockito.when(paymentInfoPlugin.getCurrency()).thenReturn(Currency.CAD);
    Mockito.when(paymentInfoPlugin.getStatus()).thenReturn(PaymentPluginStatus.PROCESSED);
    Mockito.when(paymentInfoPlugin.getGatewayErrorCode()).thenReturn(UUID.randomUUID().toString().substring(0, 5));
    Mockito.when(paymentInfoPlugin.getGatewayError()).thenReturn(UUID.randomUUID().toString());
    daoHelper.processPaymentInfoPlugin(TransactionStatus.SUCCESS, paymentInfoPlugin, "SOME_STATE");
    final PaymentModelDao payment2 = daoHelper.getPayment();
    Assert.assertEquals(payment2.getExternalKey(), paymentExternalKey);
    Assert.assertEquals(payment2.getStateName(), "SOME_STATE");
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getPaymentId(), payment2.getId());
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getTransactionExternalKey(), paymentTransactionExternalKey);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getTransactionStatus(), TransactionStatus.SUCCESS);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getAmount().compareTo(amount), 0);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getCurrency(), currency);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getProcessedAmount().compareTo(paymentInfoPlugin.getAmount()), 0);
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getProcessedCurrency(), paymentInfoPlugin.getCurrency());
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getGatewayErrorCode(), paymentInfoPlugin.getGatewayErrorCode());
    Assert.assertEquals(paymentStateContext.getPaymentTransactionModelDao().getGatewayErrorMsg(), paymentInfoPlugin.getGatewayError());
}
Also used : PaymentModelDao(org.killbill.billing.payment.dao.PaymentModelDao) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 5 with PaymentTransactionInfoPlugin

use of org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin in project killbill by killbill.

the class TestExternalPaymentProviderPlugin method testProcessPayment.

@Test(groups = "fast")
public void testProcessPayment() throws Exception {
    final List<PluginProperty> properties = ImmutableList.<PluginProperty>of();
    final UUID accountId = UUID.randomUUID();
    final UUID paymentId = UUID.randomUUID();
    final UUID kbTransactionId = UUID.randomUUID();
    final UUID paymentMethodId = UUID.randomUUID();
    final BigDecimal amount = BigDecimal.TEN;
    final PaymentTransactionInfoPlugin paymentInfoPlugin = plugin.purchasePayment(accountId, paymentId, kbTransactionId, paymentMethodId, amount, Currency.BRL, properties, callContext);
    Assert.assertEquals(paymentInfoPlugin.getAmount(), amount);
    Assert.assertNull(paymentInfoPlugin.getGatewayError());
    Assert.assertNull(paymentInfoPlugin.getGatewayErrorCode());
    Assert.assertEquals(paymentInfoPlugin.getStatus(), PaymentPluginStatus.PROCESSED);
    final List<PaymentTransactionInfoPlugin> retrievedPaymentTransactionInfoPlugin = plugin.getPaymentInfo(accountId, paymentId, properties, callContext);
// getPaymentInfo mock is not implemented (yet)
//Assert.assertEquals(retrievedPaymentTransactionInfoPlugin.get(0).getStatus(), PaymentPluginStatus.PROCESSED);
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

PaymentTransactionInfoPlugin (org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin)16 PaymentTransactionModelDao (org.killbill.billing.payment.dao.PaymentTransactionModelDao)6 BigDecimal (java.math.BigDecimal)5 PaymentModelDao (org.killbill.billing.payment.dao.PaymentModelDao)5 DefaultNoOpPaymentInfoPlugin (org.killbill.billing.payment.provider.DefaultNoOpPaymentInfoPlugin)5 Test (org.testng.annotations.Test)5 LinkedList (java.util.LinkedList)4 PluginProperty (org.killbill.billing.payment.api.PluginProperty)4 Predicate (com.google.common.base.Predicate)3 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)3 PaymentPluginApi (org.killbill.billing.payment.plugin.api.PaymentPluginApi)3 UUID (java.util.UUID)2 PaymentPluginApiException (org.killbill.billing.payment.plugin.api.PaymentPluginApiException)2 EventBusException (org.killbill.bus.api.PersistentBus.EventBusException)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 List (java.util.List)1 OperationException (org.killbill.automaton.OperationException)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 Currency (org.killbill.billing.catalog.api.Currency)1