Search in sources :

Example 6 with ApiEventCancel

use of org.killbill.billing.subscription.events.user.ApiEventCancel in project killbill by killbill.

the class TestSubscriptionBillingEvents method testWithCancelation_Before_EffSubDtV2.

@Test(groups = "fast")
public void testWithCancelation_Before_EffSubDtV2() throws Exception {
    final DateTime createDate = new DateTime(2011, 1, 2, 0, 0, DateTimeZone.UTC);
    final DefaultSubscriptionBase subscriptionBase = new DefaultSubscriptionBase(new SubscriptionBuilder().setAlignStartDate(createDate));
    final UUID subscriptionId = UUID.randomUUID();
    final List<SubscriptionBaseEvent> inputEvents = new LinkedList<SubscriptionBaseEvent>();
    inputEvents.add(new ApiEventCreate(new ApiEventBuilder().setApiEventType(CREATE).setEventPlan("gold-monthly").setEventPlanPhase("gold-monthly-trial").setEventPriceList("DEFAULT").setFromDisk(true).setUuid(UUID.randomUUID()).setSubscriptionId(subscriptionId).setCreatedDate(createDate).setUpdatedDate(createDate).setEffectiveDate(createDate).setTotalOrdering(1).setActive(true)));
    final DateTime evergreenPhaseDate = createDate.plusDays(30);
    inputEvents.add(new PhaseEventData(new PhaseEventBuilder().setPhaseName("gold-monthly-evergreen").setUuid(UUID.randomUUID()).setSubscriptionId(subscriptionId).setCreatedDate(evergreenPhaseDate).setUpdatedDate(evergreenPhaseDate).setEffectiveDate(evergreenPhaseDate).setTotalOrdering(1).setActive(true)));
    final DateTime cancelDate = new DateTime(2011, 2, 13, 0, 0, DateTimeZone.UTC);
    inputEvents.add(new ApiEventCancel(new ApiEventBuilder().setApiEventType(ApiEventType.CANCEL).setEventPlan(null).setEventPlanPhase(null).setEventPriceList(null).setFromDisk(true).setUuid(UUID.randomUUID()).setSubscriptionId(subscriptionId).setCreatedDate(createDate).setUpdatedDate(null).setEffectiveDate(cancelDate).setTotalOrdering(2).setActive(true)));
    subscriptionBase.rebuildTransitions(inputEvents, catalog);
    final List<SubscriptionBillingEvent> result = subscriptionBase.getSubscriptionBillingEvents(catalog.getCatalog());
    Assert.assertEquals(result.size(), 3);
    Assert.assertEquals(result.get(0).getType(), SubscriptionBaseTransitionType.CREATE);
    Assert.assertEquals(result.get(0).getEffectiveDate().compareTo(createDate), 0);
    Assert.assertEquals(result.get(0).getPlan().getName().compareTo("gold-monthly"), 0);
    Assert.assertEquals(toDateTime(result.get(0).getPlan().getCatalog().getEffectiveDate()).compareTo(EFF_V1), 0);
    Assert.assertEquals(result.get(1).getType(), SubscriptionBaseTransitionType.PHASE);
    Assert.assertEquals(result.get(1).getEffectiveDate().compareTo(evergreenPhaseDate), 0);
    Assert.assertEquals(result.get(1).getPlan().getName().compareTo("gold-monthly"), 0);
    Assert.assertEquals(toDateTime(result.get(1).getPlan().getCatalog().getEffectiveDate()).compareTo(EFF_V1), 0);
    // Cancel event
    Assert.assertEquals(result.get(2).getType(), SubscriptionBaseTransitionType.CANCEL);
    Assert.assertEquals(result.get(2).getEffectiveDate().compareTo(cancelDate), 0);
    Assert.assertNull(result.get(2).getPlan());
// Nothing after cancel -> we correctly discarded subsequent catalog update events after the cancel
}
Also used : ApiEventCreate(org.killbill.billing.subscription.events.user.ApiEventCreate) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) DateTime(org.joda.time.DateTime) LinkedList(java.util.LinkedList) PhaseEventData(org.killbill.billing.subscription.events.phase.PhaseEventData) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) UUID(java.util.UUID) PhaseEventBuilder(org.killbill.billing.subscription.events.phase.PhaseEventBuilder) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) Test(org.testng.annotations.Test)

Example 7 with ApiEventCancel

use of org.killbill.billing.subscription.events.user.ApiEventCancel in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method addCancellationAddOnForEventsIfRequired.

private List<DefaultSubscriptionBase> addCancellationAddOnForEventsIfRequired(final Collection<SubscriptionBaseEvent> events, final CatalogEntity baseProduct, final UUID bundleId, final DateTime effectiveDate, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = new ArrayList<DefaultSubscriptionBase>();
    final List<SubscriptionBase> subscriptions = dao.getSubscriptions(bundleId, ImmutableList.<SubscriptionBaseEvent>of(), internalTenantContext);
    for (final SubscriptionBase subscription : subscriptions) {
        final DefaultSubscriptionBase cur = (DefaultSubscriptionBase) subscription;
        if (cur.getState() == EntitlementState.CANCELLED || cur.getCategory() != ProductCategory.ADD_ON) {
            continue;
        }
        final Plan addonCurrentPlan = cur.getCurrentPlan();
        if (baseProduct == null || addonUtils.isAddonIncludedFromProdName(baseProduct.getName(), addonCurrentPlan, effectiveDate, internalTenantContext) || !addonUtils.isAddonAvailableFromProdName(baseProduct.getName(), addonCurrentPlan, effectiveDate, internalTenantContext)) {
            //
            // Perform AO cancellation using the effectiveDate of the BP
            //
            final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(cur.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
            subscriptionsToBeCancelled.add(cur);
            events.add(cancelEvent);
        }
    }
    return subscriptionsToBeCancelled;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 8 with ApiEventCancel

use of org.killbill.billing.subscription.events.user.ApiEventCancel in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method getEventsOnCancelPlan.

@Override
public List<SubscriptionBaseEvent> getEventsOnCancelPlan(final DefaultSubscriptionBase subscription, final DateTime effectiveDate, final DateTime processedDate, final boolean addCancellationAddOnForEventsIfRequired, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<SubscriptionBaseEvent> cancelEvents = new ArrayList<SubscriptionBaseEvent>();
    final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
    cancelEvents.add(cancelEvent);
    if (subscription.getCategory() == ProductCategory.BASE && addCancellationAddOnForEventsIfRequired) {
        final Product currentBaseProduct = cancelEvent.getEffectiveDate().compareTo(clock.getUTCNow()) <= 0 ? null : subscription.getCurrentPlan().getProduct();
        addCancellationAddOnForEventsIfRequired(cancelEvents, currentBaseProduct, subscription.getBundleId(), effectiveDate, internalTenantContext);
    }
    return cancelEvents;
}
Also used : ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 9 with ApiEventCancel

use of org.killbill.billing.subscription.events.user.ApiEventCancel in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method getEventsOnCancelPlan.

@Override
public List<SubscriptionBaseEvent> getEventsOnCancelPlan(final DefaultSubscriptionBase subscription, final DateTime effectiveDate, final boolean addCancellationAddOnForEventsIfRequired, final SubscriptionCatalog fullCatalog, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<SubscriptionBaseEvent> cancelEvents = new ArrayList<SubscriptionBaseEvent>();
    final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
    cancelEvents.add(cancelEvent);
    if (subscription.getCategory() == ProductCategory.BASE && addCancellationAddOnForEventsIfRequired) {
        final Product currentBaseProduct = cancelEvent.getEffectiveDate().compareTo(clock.getUTCNow()) <= 0 ? null : subscription.getCurrentPlan().getProduct();
        addCancellationAddOnForEventsIfRequired(cancelEvents, currentBaseProduct, subscription.getBundleId(), effectiveDate, fullCatalog, internalTenantContext);
    }
    return cancelEvents;
}
Also used : ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) Product(org.killbill.billing.catalog.api.Product) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 10 with ApiEventCancel

use of org.killbill.billing.subscription.events.user.ApiEventCancel in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method addCancellationAddOnForEventsIfRequired.

private List<DefaultSubscriptionBase> addCancellationAddOnForEventsIfRequired(final Collection<SubscriptionBaseEvent> events, final Product baseProduct, final UUID bundleId, final DateTime effectiveDate, final SubscriptionCatalog catalog, final InternalTenantContext internalTenantContext) throws CatalogApiException {
    final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = new ArrayList<DefaultSubscriptionBase>();
    final List<DefaultSubscriptionBase> subscriptions = dao.getSubscriptions(bundleId, ImmutableList.<SubscriptionBaseEvent>of(), catalog, internalTenantContext);
    for (final SubscriptionBase subscription : subscriptions) {
        final DefaultSubscriptionBase cur = (DefaultSubscriptionBase) subscription;
        if (cur.getState() == EntitlementState.CANCELLED || cur.getCategory() != ProductCategory.ADD_ON) {
            continue;
        }
        final Plan addonCurrentPlan = cur.getCurrentPlan();
        if (baseProduct == null || addonUtils.isAddonIncluded(baseProduct, addonCurrentPlan) || !addonUtils.isAddonAvailable(baseProduct, addonCurrentPlan)) {
            // 
            // Perform AO cancellation using the effectiveDate of the BP
            // 
            final SubscriptionBaseEvent cancelEvent = new ApiEventCancel(new ApiEventBuilder().setSubscriptionId(cur.getId()).setEffectiveDate(effectiveDate).setFromDisk(true));
            subscriptionsToBeCancelled.add(cur);
            events.add(cancelEvent);
        }
    }
    return subscriptionsToBeCancelled;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) ArrayList(java.util.ArrayList) ApiEventCancel(org.killbill.billing.subscription.events.user.ApiEventCancel) Plan(org.killbill.billing.catalog.api.Plan) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Aggregations

SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)12 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)12 ApiEventCancel (org.killbill.billing.subscription.events.user.ApiEventCancel)12 LinkedList (java.util.LinkedList)7 ArrayList (java.util.ArrayList)6 DateTime (org.joda.time.DateTime)6 ApiEventCreate (org.killbill.billing.subscription.events.user.ApiEventCreate)5 UUID (java.util.UUID)4 Plan (org.killbill.billing.catalog.api.Plan)4 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)4 Test (org.testng.annotations.Test)4 Product (org.killbill.billing.catalog.api.Product)3 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)3 PhaseEventBuilder (org.killbill.billing.subscription.events.phase.PhaseEventBuilder)3 PhaseEventData (org.killbill.billing.subscription.events.phase.PhaseEventData)3 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)2 ApiEventChange (org.killbill.billing.subscription.events.user.ApiEventChange)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1