Search in sources :

Example 21 with SubscriptionCatalog

use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method uncancel.

@Override
public boolean uncancel(final DefaultSubscriptionBase subscription, final CallContext context) throws SubscriptionBaseApiException {
    if (!subscription.isFutureCancelled()) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_UNCANCEL_BAD_STATE, subscription.getId().toString());
    }
    try {
        final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalCallContext);
        final SubscriptionBaseEvent uncancelEvent = new ApiEventUncancel(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(context.getCreatedDate()).setFromDisk(true));
        final List<SubscriptionBaseEvent> uncancelEvents = new ArrayList<SubscriptionBaseEvent>();
        uncancelEvents.add(uncancelEvent);
        // 
        // Used to compute effective for next phase (which was set unactive during cancellation).
        // In case of a pending subscription we don't want to pass an effective date prior the CREATE event as we would end up with the wrong
        // transition in PlanAligner (next transition would be CREATE instead of potential next PHASE)
        // 
        final DateTime planAlignerEffectiveDate = subscription.getState() == EntitlementState.PENDING ? subscription.getStartDate() : context.getCreatedDate();
        final TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, planAlignerEffectiveDate, catalog, internalCallContext);
        final PhaseEvent nextPhaseEvent = (nextTimedPhase != null) ? PhaseEventData.createNextPhaseEvent(subscription.getId(), nextTimedPhase.getPhase().getName(), nextTimedPhase.getStartPhase()) : null;
        if (nextPhaseEvent != null) {
            uncancelEvents.add(nextPhaseEvent);
        }
        dao.uncancelSubscription(subscription, uncancelEvents, internalCallContext);
        subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), catalog);
        return true;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) ArrayList(java.util.ArrayList) TimedPhase(org.killbill.billing.subscription.alignment.TimedPhase) ApiEventUncancel(org.killbill.billing.subscription.events.user.ApiEventUncancel) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime)

Example 22 with SubscriptionCatalog

use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.

the class DefaultSubscriptionBaseApiService method undoChangePlan.

@Override
public boolean undoChangePlan(final DefaultSubscriptionBase subscription, final CallContext context) throws SubscriptionBaseApiException {
    if (!subscription.isPendingChangePlan()) {
        throw new SubscriptionBaseApiException(ErrorCode.SUB_UNDO_CHANGE_BAD_STATE, subscription.getId().toString());
    }
    try {
        final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalCallContext);
        final SubscriptionBaseEvent undoChangePlanEvent = new ApiEventUndoChange(new ApiEventBuilder().setSubscriptionId(subscription.getId()).setEffectiveDate(context.getCreatedDate()).setFromDisk(true));
        final List<SubscriptionBaseEvent> undoChangePlanEvents = new ArrayList<SubscriptionBaseEvent>();
        undoChangePlanEvents.add(undoChangePlanEvent);
        // 
        // Used to compute effective for next phase (which was set unactive during cancellation).
        // In case of a pending subscription we don't want to pass an effective date prior the CREATE event as we would end up with the wrong
        // transition in PlanAligner (next transition would be CREATE instead of potential next PHASE)
        // 
        final DateTime planAlignerEffectiveDate = subscription.getState() == EntitlementState.PENDING ? subscription.getStartDate() : context.getCreatedDate();
        final TimedPhase nextTimedPhase = planAligner.getNextTimedPhase(subscription, planAlignerEffectiveDate, catalog, internalCallContext);
        final PhaseEvent nextPhaseEvent = (nextTimedPhase != null) ? PhaseEventData.createNextPhaseEvent(subscription.getId(), nextTimedPhase.getPhase().getName(), nextTimedPhase.getStartPhase()) : null;
        if (nextPhaseEvent != null) {
            undoChangePlanEvents.add(nextPhaseEvent);
        }
        dao.undoChangePlan(subscription, undoChangePlanEvents, internalCallContext);
        subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), catalog);
        return true;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : ApiEventUndoChange(org.killbill.billing.subscription.events.user.ApiEventUndoChange) PhaseEvent(org.killbill.billing.subscription.events.phase.PhaseEvent) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) ArrayList(java.util.ArrayList) TimedPhase(org.killbill.billing.subscription.alignment.TimedPhase) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime)

Aggregations

SubscriptionCatalog (org.killbill.billing.subscription.catalog.SubscriptionCatalog)22 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)20 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)10 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)10 DateTime (org.joda.time.DateTime)9 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)9 ArrayList (java.util.ArrayList)6 Plan (org.killbill.billing.catalog.api.Plan)5 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)5 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)4 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)4 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)4 LinkedList (java.util.LinkedList)3 PlanPhasePriceOverridesWithCallContext (org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext)3 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)3 ApiEventBuilder (org.killbill.billing.subscription.events.user.ApiEventBuilder)3 CallContext (org.killbill.billing.util.callcontext.CallContext)3 TimedPhase (org.killbill.billing.subscription.alignment.TimedPhase)2 DefaultSubscriptionBaseBundle (org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle)2 PhaseEvent (org.killbill.billing.subscription.events.phase.PhaseEvent)2