Search in sources :

Example 51 with Entitlement

use of org.killbill.billing.entitlement.api.Entitlement in project killbill by killbill.

the class DefaultEntitlementService method processEntitlementNotification.

private void processEntitlementNotification(final EntitlementNotificationKey key, final InternalCallContext internalCallContext, final CallContext callContext) {
    final Entitlement entitlement;
    try {
        entitlement = entitlementInternalApi.getEntitlementForId(key.getEntitlementId(), internalCallContext);
    } catch (final EntitlementApiException e) {
        log.error("Error retrieving entitlementId='{}'", key.getEntitlementId(), e);
        return;
    }
    if (!(entitlement instanceof DefaultEntitlement)) {
        log.error("Error retrieving entitlementId='{}', unexpected entitlement className='{}'", key.getEntitlementId(), entitlement.getClass().getName());
        return;
    }
    final EntitlementNotificationKeyAction entitlementNotificationKeyAction = key.getEntitlementNotificationKeyAction();
    try {
        if (EntitlementNotificationKeyAction.CHANGE.equals(entitlementNotificationKeyAction) || EntitlementNotificationKeyAction.CANCEL.equals(entitlementNotificationKeyAction)) {
            blockAddOnsIfRequired(key, (DefaultEntitlement) entitlement, callContext, internalCallContext);
        } else if (EntitlementNotificationKeyAction.PAUSE.equals(entitlementNotificationKeyAction)) {
            entitlementInternalApi.pause(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
        } else if (EntitlementNotificationKeyAction.RESUME.equals(entitlementNotificationKeyAction)) {
            entitlementInternalApi.resume(key.getBundleId(), internalCallContext.toLocalDate(key.getEffectiveDate()), ImmutableList.<PluginProperty>of(), internalCallContext);
        }
    } catch (final EntitlementApiException e) {
        log.error("Error processing event for entitlementId='{}'", entitlement.getId(), e);
    }
}
Also used : EntitlementNotificationKeyAction(org.killbill.billing.entitlement.engine.core.EntitlementNotificationKeyAction) PluginProperty(org.killbill.billing.payment.api.PluginProperty) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement)

Example 52 with Entitlement

use of org.killbill.billing.entitlement.api.Entitlement in project killbill by killbill.

the class DefaultEntitlementApiBase method getAllEntitlementsForBundle.

public List<Entitlement> getAllEntitlementsForBundle(final UUID bundleId, final InternalTenantContext tenantContext) throws EntitlementApiException {
    final List<EventsStream> eventsStreamForBundle = eventsStreamBuilder.buildForBundle(bundleId, tenantContext);
    final List<Entitlement> entitlements = new LinkedList<Entitlement>();
    for (final EventsStream eventsStream : eventsStreamForBundle) {
        final Entitlement entitlement = new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, tenantContext, internalCallContextFactory);
        entitlements.add(entitlement);
    }
    return entitlements;
}
Also used : EventsStream(org.killbill.billing.entitlement.EventsStream) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LinkedList(java.util.LinkedList)

Example 53 with Entitlement

use of org.killbill.billing.entitlement.api.Entitlement in project killbill by killbill.

the class DefaultEntitlementApiBase method getAllEntitlementsForAccount.

public AccountEntitlements getAllEntitlementsForAccount(final InternalTenantContext tenantContext) throws EntitlementApiException {
    final AccountEventsStreams accountEventsStreams = eventsStreamBuilder.buildForAccount(tenantContext);
    final Map<UUID, Collection<Entitlement>> entitlementsPerBundle = new HashMap<UUID, Collection<Entitlement>>();
    for (final UUID bundleId : accountEventsStreams.getEventsStreams().keySet()) {
        if (entitlementsPerBundle.get(bundleId) == null) {
            entitlementsPerBundle.put(bundleId, new LinkedList<Entitlement>());
        }
        for (final EventsStream eventsStream : accountEventsStreams.getEventsStreams().get(bundleId)) {
            final Entitlement entitlement = new DefaultEntitlement(eventsStream, eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, tenantContext, internalCallContextFactory);
            entitlementsPerBundle.get(bundleId).add(entitlement);
        }
    }
    return new DefaultAccountEntitlements(accountEventsStreams, entitlementsPerBundle);
}
Also used : HashMap(java.util.HashMap) EventsStream(org.killbill.billing.entitlement.EventsStream) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Collection(java.util.Collection) AccountEventsStreams(org.killbill.billing.entitlement.AccountEventsStreams) UUID(java.util.UUID) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement)

Example 54 with Entitlement

use of org.killbill.billing.entitlement.api.Entitlement in project killbill by killbill.

the class DefaultEntitlementInternalApi method cancel.

@Override
public void cancel(final Iterable<Entitlement> entitlements, @Nullable final LocalDate effectiveDate, final BillingActionPolicy billingPolicy, final Iterable<PluginProperty> properties, final InternalCallContext internalCallContext) throws EntitlementApiException {
    if (!entitlements.iterator().hasNext()) {
        return;
    }
    final CallContext callContext = internalCallContextFactory.createCallContext(internalCallContext);
    final ImmutableMap.Builder<BlockingState, Optional<UUID>> blockingStates = new ImmutableMap.Builder<BlockingState, Optional<UUID>>();
    final Map<DateTime, Collection<NotificationEvent>> notificationEvents = new HashMap<DateTime, Collection<NotificationEvent>>();
    final Collection<EntitlementContext> pluginContexts = new LinkedList<EntitlementContext>();
    final List<WithEntitlementPlugin> callbacks = new LinkedList<WithEntitlementPlugin>();
    final List<SubscriptionBase> subscriptions = new LinkedList<SubscriptionBase>();
    for (final Entitlement entitlement : entitlements) {
        if (entitlement.getState() == EntitlementState.CANCELLED) {
            // If subscription has already been cancelled, we ignore and carry on
            continue;
        }
        final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(entitlement.getBundleId(), entitlement.getBundleExternalKey(), null, effectiveDate, null, false);
        final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
        baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
        final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.CANCEL_SUBSCRIPTION, entitlement.getAccountId(), null, baseEntitlementWithAddOnsSpecifierList, billingPolicy, properties, callContext);
        pluginContexts.add(pluginContext);
        final WithEntitlementPlugin<Entitlement> cancelEntitlementWithPlugin = new WithDateOverrideBillingPolicyEntitlementCanceler((DefaultEntitlement) entitlement, blockingStates, notificationEvents, callContext, internalCallContext);
        callbacks.add(cancelEntitlementWithPlugin);
        subscriptions.add(((DefaultEntitlement) entitlement).getSubscriptionBase());
    }
    final Callable<Void> preCallbacksCallback = new BulkSubscriptionBaseCancellation(subscriptions, billingPolicy, internalCallContext);
    pluginExecution.executeWithPlugin(preCallbacksCallback, callbacks, pluginContexts);
    // Record the new states first, then insert the notifications to avoid race conditions
    blockingStateDao.setBlockingStatesAndPostBlockingTransitionEvent(blockingStates.build(), internalCallContext);
    for (final DateTime effectiveDateForNotification : notificationEvents.keySet()) {
        for (final NotificationEvent notificationEvent : notificationEvents.get(effectiveDateForNotification)) {
            recordFutureNotification(effectiveDateForNotification, notificationEvent, internalCallContext);
        }
    }
}
Also used : HashMap(java.util.HashMap) WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) EventsStreamBuilder(org.killbill.billing.entitlement.engine.core.EventsStreamBuilder) ArrayList(java.util.ArrayList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) UUID(java.util.UUID) DefaultEntitlementContext(org.killbill.billing.entitlement.api.DefaultEntitlementContext) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) Optional(com.google.common.base.Optional) NotificationEvent(org.killbill.notificationq.api.NotificationEvent) CallContext(org.killbill.billing.util.callcontext.CallContext) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedList(java.util.LinkedList) Collection(java.util.Collection) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) DefaultEntitlementContext(org.killbill.billing.entitlement.api.DefaultEntitlementContext)

Example 55 with Entitlement

use of org.killbill.billing.entitlement.api.Entitlement in project killbill by killbill.

the class TestBlockingApi method testBlockingAcrossTypes.

@Test(groups = "slow")
public void testBlockingAcrossTypes() throws Exception {
    final String stateNameBlock = "stateBlock";
    final String stateNameUnBlock = "stateUnBlock";
    final String service = "SVC_BLOC_TYPES";
    final boolean blockChange = false;
    final boolean blockEntitlement = true;
    final boolean blockBilling = true;
    final Account account = createAccount(getAccountData(7));
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state1 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, stateNameBlock, service, blockChange, blockEntitlement, blockBilling, clock.getUTCNow());
    subscriptionApi.addBlockingState(state1, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.ANNUAL, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID baseEntitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Entitlement baseEntitlement = entitlementApi.getEntitlementForId(baseEntitlementId, callContext);
    assertEquals(baseEntitlement.getState(), EntitlementState.BLOCKED);
    // Add blocking at bundle level.
    clock.addDays(1);
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state2 = new DefaultBlockingState(baseEntitlement.getBundleId(), BlockingStateType.SUBSCRIPTION_BUNDLE, stateNameBlock, service, blockChange, blockEntitlement, blockBilling, null);
    subscriptionApi.addBlockingState(state2, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    baseEntitlement = entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    assertEquals(baseEntitlement.getState(), EntitlementState.BLOCKED);
    // Remove blocking at account level
    clock.addDays(1);
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state3 = new DefaultBlockingState(account.getId(), BlockingStateType.ACCOUNT, stateNameUnBlock, service, false, false, false, clock.getUTCNow());
    subscriptionApi.addBlockingState(state3, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    baseEntitlement = entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    assertEquals(baseEntitlement.getState(), EntitlementState.BLOCKED);
    // Remove blocking at bundle level.
    clock.addDays(1);
    testListener.pushExpectedEvent(NextEvent.BLOCK);
    final BlockingState state4 = new DefaultBlockingState(baseEntitlement.getBundleId(), BlockingStateType.SUBSCRIPTION_BUNDLE, stateNameUnBlock, service, false, false, false, null);
    subscriptionApi.addBlockingState(state4, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    baseEntitlement = entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
    assertEquals(baseEntitlement.getState(), EntitlementState.ACTIVE);
    final List<BlockingState> blockingAll = blockingInternalApi.getBlockingActiveForAccount(catalog, null, internalCallContext);
    final List<BlockingState> history = ImmutableList.<BlockingState>copyOf(Collections2.<BlockingState>filter(blockingAll, new Predicate<BlockingState>() {

        @Override
        public boolean apply(final BlockingState input) {
            return input.getService().equals(service);
        }
    }));
    Assert.assertEquals(history.size(), 4);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) Account(org.killbill.billing.account.api.Account) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) Entitlement(org.killbill.billing.entitlement.api.Entitlement) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) Predicate(com.google.common.base.Predicate) Test(org.testng.annotations.Test)

Aggregations

Entitlement (org.killbill.billing.entitlement.api.Entitlement)96 LocalDate (org.joda.time.LocalDate)76 Account (org.killbill.billing.account.api.Account)71 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)68 Test (org.testng.annotations.Test)68 UUID (java.util.UUID)64 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)64 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)53 BigDecimal (java.math.BigDecimal)37 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)34 DateTime (org.joda.time.DateTime)32 Invoice (org.killbill.billing.invoice.api.Invoice)27 PluginProperty (org.killbill.billing.payment.api.PluginProperty)22 ArrayList (java.util.ArrayList)20 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)18 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)16 ApiOperation (io.swagger.annotations.ApiOperation)14 ApiResponses (io.swagger.annotations.ApiResponses)14 TimedResource (org.killbill.commons.metrics.TimedResource)14 Produces (javax.ws.rs.Produces)13