Search in sources :

Example 6 with ApiEvent

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

the class DefaultSubscriptionBase method rebuildTransitions.

public void rebuildTransitions(final List<SubscriptionBaseEvent> inputEvents, final Catalog catalog) throws CatalogApiException {
    if (inputEvents == null) {
        return;
    }
    this.events = inputEvents;
    filterOutDuplicateCancelEvents(events);
    UUID nextUserToken = null;
    UUID nextEventId = null;
    DateTime nextCreatedDate = null;
    EntitlementState nextState = null;
    String nextPlanName = null;
    String nextPhaseName = null;
    Integer nextBillingCycleDayLocal = null;
    UUID prevEventId = null;
    DateTime prevCreatedDate = null;
    EntitlementState previousState = null;
    PriceList previousPriceList = null;
    Plan previousPlan = null;
    PlanPhase previousPhase = null;
    Integer previousBillingCycleDayLocal = null;
    transitions = new LinkedList<SubscriptionBaseTransition>();
    for (final SubscriptionBaseEvent cur : inputEvents) {
        if (!cur.isActive()) {
            continue;
        }
        ApiEventType apiEventType = null;
        boolean isFromDisk = true;
        nextEventId = cur.getId();
        nextCreatedDate = cur.getCreatedDate();
        switch(cur.getType()) {
            case PHASE:
                final PhaseEvent phaseEV = (PhaseEvent) cur;
                nextPhaseName = phaseEV.getPhase();
                break;
            case BCD_UPDATE:
                final BCDEvent bcdEvent = (BCDEvent) cur;
                nextBillingCycleDayLocal = bcdEvent.getBillCycleDayLocal();
                break;
            case API_USER:
                final ApiEvent userEV = (ApiEvent) cur;
                apiEventType = userEV.getApiEventType();
                isFromDisk = userEV.isFromDisk();
                switch(apiEventType) {
                    case TRANSFER:
                    case CREATE:
                        prevEventId = null;
                        prevCreatedDate = null;
                        previousState = null;
                        previousPlan = null;
                        previousPhase = null;
                        previousPriceList = null;
                        nextState = EntitlementState.ACTIVE;
                        nextPlanName = userEV.getEventPlan();
                        nextPhaseName = userEV.getEventPlanPhase();
                        break;
                    case CHANGE:
                        nextPlanName = userEV.getEventPlan();
                        nextPhaseName = userEV.getEventPlanPhase();
                        break;
                    case CANCEL:
                        nextState = EntitlementState.CANCELLED;
                        nextPlanName = null;
                        nextPhaseName = null;
                        break;
                    case UNCANCEL:
                    default:
                        throw new SubscriptionBaseError(String.format("Unexpected UserEvent type = %s", userEV.getApiEventType().toString()));
                }
                break;
            default:
                throw new SubscriptionBaseError(String.format("Unexpected Event type = %s", cur.getType()));
        }
        Plan nextPlan = null;
        PlanPhase nextPhase = null;
        PriceList nextPriceList = null;
        nextPlan = (nextPlanName != null) ? catalog.findPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
        nextPhase = (nextPhaseName != null) ? catalog.findPhase(nextPhaseName, cur.getEffectiveDate(), getAlignStartDate()) : null;
        nextPriceList = (nextPlan != null) ? catalog.findPriceListForPlan(nextPlanName, cur.getEffectiveDate(), getAlignStartDate()) : null;
        final SubscriptionBaseTransitionData transition = new SubscriptionBaseTransitionData(cur.getId(), id, bundleId, bundleExternalKey, cur.getType(), apiEventType, cur.getEffectiveDate(), prevEventId, prevCreatedDate, previousState, previousPlan, previousPhase, previousPriceList, previousBillingCycleDayLocal, nextEventId, nextCreatedDate, nextState, nextPlan, nextPhase, nextPriceList, nextBillingCycleDayLocal, cur.getTotalOrdering(), cur.getCreatedDate(), nextUserToken, isFromDisk);
        transitions.add(transition);
        previousState = nextState;
        previousPlan = nextPlan;
        previousPhase = nextPhase;
        previousPriceList = nextPriceList;
        prevEventId = nextEventId;
        prevCreatedDate = nextCreatedDate;
        previousBillingCycleDayLocal = nextBillingCycleDayLocal;
    }
}
Also used : ApiEventType(org.killbill.billing.subscription.events.user.ApiEventType) PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) BCDEvent(org.killbill.billing.subscription.events.bcd.BCDEvent) Plan(org.killbill.billing.catalog.api.Plan) DateTime(org.joda.time.DateTime) EntitlementState(org.killbill.billing.entitlement.api.Entitlement.EntitlementState) SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) ApiEvent(org.killbill.billing.subscription.events.user.ApiEvent) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) UUID(java.util.UUID) PriceList(org.killbill.billing.catalog.api.PriceList) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 7 with ApiEvent

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

the class DefaultSubscriptionBaseApiService method handleBasePlanEvent.

@Override
public int handleBasePlanEvent(final DefaultSubscriptionBase subscription, final SubscriptionBaseEvent event, final CallContext context) throws CatalogApiException {
    final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
    if (((ApiEvent) event).getApiEventType() == ApiEventType.CANCEL || ((ApiEvent) event).getApiEventType() == ApiEventType.CHANGE) {
        final Product baseProduct = (subscription.getState() == EntitlementState.CANCELLED) ? null : subscription.getCurrentPlan().getProduct();
        final List<SubscriptionBaseEvent> cancelEvents = new LinkedList<SubscriptionBaseEvent>();
        final List<DefaultSubscriptionBase> subscriptionsToBeCancelled = computeAddOnsToCancel(cancelEvents, baseProduct, subscription.getBundleId(), event.getEffectiveDate(), internalCallContext);
        dao.cancelSubscriptionsOnBasePlanEvent(subscription, event, subscriptionsToBeCancelled, cancelEvents, internalCallContext);
        return subscriptionsToBeCancelled.size();
    } else {
        dao.notifyOnBasePlanEvent(subscription, event, internalCallContext);
        return 0;
    }
}
Also used : ApiEvent(org.killbill.billing.subscription.events.user.ApiEvent) Product(org.killbill.billing.catalog.api.Product) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) LinkedList(java.util.LinkedList) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 8 with ApiEvent

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

the class TestSubscriptionHelper method checkNextPhaseChange.

public void checkNextPhaseChange(final DefaultSubscriptionBase subscription, final int expPendingEvents, final DateTime expPhaseChange) {
    final List<SubscriptionBaseEvent> events = dao.getPendingEventsForSubscription(subscription.getId(), internalCallContext);
    assertNotNull(events);
    printEvents(events);
    assertEquals(events.size(), expPendingEvents);
    if (events.size() > 0 && expPhaseChange != null) {
        boolean foundPhase = false;
        boolean foundChange = false;
        for (final SubscriptionBaseEvent cur : events) {
            if (cur instanceof PhaseEvent) {
                assertEquals(foundPhase, false);
                foundPhase = true;
                assertEquals(cur.getEffectiveDate(), expPhaseChange);
            } else if (cur instanceof ApiEvent) {
                final ApiEvent uEvent = (ApiEvent) cur;
                assertEquals(ApiEventType.CHANGE, uEvent.getApiEventType());
                assertEquals(foundChange, false);
                foundChange = true;
            } else {
                assertFalse(true);
            }
        }
    }
}
Also used : PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) ApiEvent(org.killbill.billing.subscription.events.user.ApiEvent) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Aggregations

SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)8 ApiEvent (org.killbill.billing.subscription.events.user.ApiEvent)8 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)4 DateTime (org.joda.time.DateTime)3 BCDEvent (org.killbill.billing.subscription.events.bcd.BCDEvent)3 LinkedList (java.util.LinkedList)2 UUID (java.util.UUID)2 Plan (org.killbill.billing.catalog.api.Plan)2 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)2 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)2 ApiEventType (org.killbill.billing.subscription.events.user.ApiEventType)2 Interval (org.joda.time.Interval)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)1 Duration (org.killbill.billing.catalog.api.Duration)1 PhaseType (org.killbill.billing.catalog.api.PhaseType)1 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)1 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)1 PriceList (org.killbill.billing.catalog.api.PriceList)1 Product (org.killbill.billing.catalog.api.Product)1