Search in sources :

Example 91 with Account

use of org.killbill.billing.account.api.Account in project killbill by killbill.

the class PluginControlPaymentProcessor method retryPaymentTransaction.

public void retryPaymentTransaction(final UUID attemptId, final List<String> paymentControlPluginNames, final InternalCallContext internalCallContext) {
    final PaymentAttemptModelDao attempt = paymentDao.getPaymentAttempt(attemptId, internalCallContext);
    log.info("Retrying attemptId='{}', paymentExternalKey='{}', transactionExternalKey='{}'. paymentControlPluginNames='{}'", attemptId, attempt.getPaymentExternalKey(), attempt.getTransactionExternalKey(), paymentControlPluginNames);
    final PaymentModelDao paymentModelDao = paymentDao.getPaymentByExternalKey(attempt.getPaymentExternalKey(), internalCallContext);
    final UUID paymentId = paymentModelDao != null ? paymentModelDao.getId() : null;
    final CallContext callContext = buildCallContext(internalCallContext);
    final String transactionType = TransactionType.PURCHASE.name();
    Account account = null;
    Payment payment = null;
    PaymentTransaction paymentTransaction = null;
    try {
        account = accountInternalApi.getAccountById(attempt.getAccountId(), internalCallContext);
        final State state = paymentControlStateMachineHelper.getState(attempt.getStateName());
        final Iterable<PluginProperty> pluginProperties = PluginPropertySerializer.deserialize(attempt.getPluginProperties());
        logEnterAPICall(log, transactionType, account, attempt.getPaymentMethodId(), paymentId, null, attempt.getAmount(), attempt.getCurrency(), attempt.getPaymentExternalKey(), attempt.getTransactionExternalKey(), null, paymentControlPluginNames);
        payment = pluginControlledPaymentAutomatonRunner.run(state, false, attempt.getTransactionType(), ControlOperation.valueOf(attempt.getTransactionType().toString()), account, attempt.getPaymentMethodId(), paymentId, attempt.getPaymentExternalKey(), attempt.getTransactionExternalKey(), attempt.getAmount(), attempt.getCurrency(), pluginProperties, paymentControlPluginNames, callContext, internalCallContext);
        paymentTransaction = Iterables.<PaymentTransaction>find(Lists.<PaymentTransaction>reverse(payment.getTransactions()), new Predicate<PaymentTransaction>() {

            @Override
            public boolean apply(final PaymentTransaction input) {
                return attempt.getTransactionExternalKey().equals(input.getExternalKey());
            }
        });
    } catch (final AccountApiException e) {
        log.warn("Failed to retry attemptId='{}', paymentControlPlugins='{}'", attemptId, toPluginNamesOnError(paymentControlPluginNames), e);
    } catch (final PaymentApiException e) {
        // Log exception unless nothing left to be paid
        if (e.getCode() == ErrorCode.PAYMENT_PLUGIN_API_ABORTED.getCode() && paymentControlPluginNames != null && paymentControlPluginNames.size() == 1 && InvoicePaymentControlPluginApi.PLUGIN_NAME.equals(paymentControlPluginNames.get(0))) {
            log.warn("Failed to retry attemptId='{}', paymentControlPlugins='{}'. Invoice has already been paid", attemptId, toPluginNamesOnError(paymentControlPluginNames));
        } else {
            log.warn("Failed to retry attemptId='{}', paymentControlPlugins='{}'", attemptId, toPluginNamesOnError(paymentControlPluginNames), e);
        }
    } catch (final PluginPropertySerializerException e) {
        log.warn("Failed to retry attemptId='{}', paymentControlPlugins='{}'", attemptId, toPluginNamesOnError(paymentControlPluginNames), e);
    } catch (final MissingEntryException e) {
        log.warn("Failed to retry attemptId='{}', paymentControlPlugins='{}'", attemptId, toPluginNamesOnError(paymentControlPluginNames), e);
    } finally {
        logExitAPICall(log, transactionType, account, payment != null ? payment.getPaymentMethodId() : null, payment != null ? payment.getId() : null, paymentTransaction != null ? paymentTransaction.getId() : null, paymentTransaction != null ? paymentTransaction.getProcessedAmount() : null, paymentTransaction != null ? paymentTransaction.getProcessedCurrency() : null, payment != null ? payment.getExternalKey() : null, paymentTransaction != null ? paymentTransaction.getExternalKey() : null, paymentTransaction != null ? paymentTransaction.getTransactionStatus() : null, paymentControlPluginNames, null);
    }
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) Account(org.killbill.billing.account.api.Account) PaymentModelDao(org.killbill.billing.payment.dao.PaymentModelDao) PluginPropertySerializerException(org.killbill.billing.payment.dao.PluginPropertySerializer.PluginPropertySerializerException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) CallContext(org.killbill.billing.util.callcontext.CallContext) Predicate(com.google.common.base.Predicate) PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) Payment(org.killbill.billing.payment.api.Payment) State(org.killbill.automaton.State) AccountApiException(org.killbill.billing.account.api.AccountApiException) MissingEntryException(org.killbill.automaton.MissingEntryException) UUID(java.util.UUID)

Example 92 with Account

use of org.killbill.billing.account.api.Account in project killbill by killbill.

the class TestOverdueHelper method createAccount.

public Account createAccount(final LocalDate dateOfLastUnPaidInvoice) throws SubscriptionBaseApiException, AccountApiException {
    final UUID accountId = UUID.randomUUID();
    final Account account = Mockito.mock(Account.class);
    Mockito.when(account.getId()).thenReturn(accountId);
    Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
    Mockito.when(accountInternalApi.getAccountById(Mockito.eq(account.getId()), Mockito.<InternalTenantContext>any())).thenReturn(account);
    final Invoice invoice = Mockito.mock(Invoice.class);
    Mockito.when(invoice.getInvoiceDate()).thenReturn(dateOfLastUnPaidInvoice);
    Mockito.when(invoice.getBalance()).thenReturn(BigDecimal.TEN);
    Mockito.when(invoice.getId()).thenReturn(UUID.randomUUID());
    final InvoiceItem item = Mockito.mock(InvoiceItem.class);
    final List<InvoiceItem> items = new ArrayList<InvoiceItem>();
    items.add(item);
    Mockito.when(invoice.getInvoiceItems()).thenReturn(items);
    final List<Invoice> invoices = new ArrayList<Invoice>();
    invoices.add(invoice);
    Mockito.when(invoiceInternalApi.getUnpaidInvoicesByAccountId(Mockito.<UUID>any(), Mockito.<LocalDate>any(), Mockito.<InternalTenantContext>any())).thenReturn(invoices);
    final Tag tag = Mockito.mock(Tag.class);
    Mockito.when(tag.getObjectId()).thenReturn(accountId);
    Mockito.when(tag.getObjectType()).thenReturn(ObjectType.ACCOUNT);
    Mockito.when(tag.getTagDefinitionId()).thenReturn(new UUID(0, 6));
    final List<Tag> tags = new ArrayList<Tag>();
    tags.add(tag);
    Mockito.when(tagInternalApi.getTags(Mockito.eq(account.getId()), Mockito.eq(ObjectType.ACCOUNT), Mockito.<InternalTenantContext>any())).thenReturn(tags);
    return account;
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ArrayList(java.util.ArrayList) Tag(org.killbill.billing.util.tag.Tag) UUID(java.util.UUID)

Example 93 with Account

use of org.killbill.billing.account.api.Account in project killbill by killbill.

the class TestBillingStateCalculator method beforeMethod.

@Override
@BeforeMethod(groups = "fast")
public void beforeMethod() throws Exception {
    super.beforeMethod();
    final Account account = Mockito.mock(Account.class);
    Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
    Mockito.when(accountApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account);
}
Also used : Account(org.killbill.billing.account.api.Account) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 94 with Account

use of org.killbill.billing.account.api.Account in project killbill by killbill.

the class TestOverdueWrapper method testWrapperBasic.

@Test(groups = "slow")
public void testWrapperBasic() throws Exception {
    final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes());
    final DefaultOverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, DefaultOverdueConfig.class);
    ((MockOverdueConfigCache) overdueConfigCache).loadOverwriteDefaultOverdueConfig(config);
    Account account;
    OverdueWrapper wrapper;
    OverdueState state;
    state = config.getOverdueStatesAccount().findState("OD1");
    account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(31));
    wrapper = overdueWrapperFactory.createOverdueWrapperFor(account, internalCallContext);
    wrapper.refresh(clock.getUTCNow(), internalCallContext);
    testOverdueHelper.checkStateApplied(state);
    state = config.getOverdueStatesAccount().findState("OD2");
    account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(41));
    wrapper = overdueWrapperFactory.createOverdueWrapperFor(account, internalCallContext);
    wrapper.refresh(clock.getUTCNow(), internalCallContext);
    testOverdueHelper.checkStateApplied(state);
    state = config.getOverdueStatesAccount().findState("OD3");
    account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(51));
    wrapper = overdueWrapperFactory.createOverdueWrapperFor(account, internalCallContext);
    wrapper.refresh(clock.getUTCNow(), internalCallContext);
    testOverdueHelper.checkStateApplied(state);
}
Also used : Account(org.killbill.billing.account.api.Account) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig) MockOverdueConfigCache(org.killbill.billing.overdue.caching.MockOverdueConfigCache) OverdueState(org.killbill.billing.overdue.api.OverdueState) Test(org.testng.annotations.Test)

Example 95 with Account

use of org.killbill.billing.account.api.Account in project killbill by killbill.

the class DefaultAccountApiBase method getAccountByKey.

protected Account getAccountByKey(final String key, final InternalTenantContext context) throws AccountApiException {
    final AccountModelDao accountModelDao = accountDao.getAccountByKey(key, context);
    if (accountModelDao == null) {
        throw new AccountApiException(ErrorCode.ACCOUNT_DOES_NOT_EXIST_FOR_KEY, key);
    }
    final Account account = new DefaultAccount(accountModelDao);
    accountCacheController.putIfAbsent(account.getId(), new DefaultImmutableAccountData(account));
    return account;
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) Account(org.killbill.billing.account.api.Account) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) DefaultImmutableAccountData(org.killbill.billing.account.api.DefaultImmutableAccountData) AccountApiException(org.killbill.billing.account.api.AccountApiException)

Aggregations

Account (org.killbill.billing.account.api.Account)305 Test (org.testng.annotations.Test)198 LocalDate (org.joda.time.LocalDate)139 BigDecimal (java.math.BigDecimal)90 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)87 Invoice (org.killbill.billing.invoice.api.Invoice)84 DateTime (org.joda.time.DateTime)71 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)63 UUID (java.util.UUID)62 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)59 ApiOperation (io.swagger.annotations.ApiOperation)48 ApiResponses (io.swagger.annotations.ApiResponses)48 ArrayList (java.util.ArrayList)48 Produces (javax.ws.rs.Produces)48 AccountData (org.killbill.billing.account.api.AccountData)47 TimedResource (org.killbill.commons.metrics.TimedResource)47 Path (javax.ws.rs.Path)42 PluginProperty (org.killbill.billing.payment.api.PluginProperty)42 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)41 Payment (org.killbill.billing.payment.api.Payment)40