@Override public boolean cancelWithPolicy(BillingActionPolicy policy, CallContext context) throws SubscriptionBaseApiException { return sub.cancelWithPolicy(policy, context); }
@Override public Entitlement cancelEntitlementWithDateOverrideBillingPolicy(final LocalDate localCancelDate, final BillingActionPolicy billingPolicy, final CallContext callContext) throws EntitlementApiException { // Get the latest state from disk refresh(callContext); if (eventsStream.isEntitlementCancelled()) { throw new EntitlementApiException(ErrorCode.SUB_CANCEL_BAD_STATE, getId(), EntitlementState.CANCELLED); } // Make sure to compute the entitlement effective date first to avoid timing issues for IMM cancellations // (we don't want an entitlement cancel date one second or so after the subscription cancel date or add-ons cancellations // computations won't work). final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(getAccountId(), callContext); final LocalDate effectiveLocalDate = new LocalDate(localCancelDate, eventsStream.getAccountTimeZone()); final DateTime effectiveDate = dateHelper.fromLocalDateAndReferenceTime(effectiveLocalDate, getSubscriptionBase().getStartDate(), contextWithValidAccountRecordId); try { // Cancel subscription base first, to correctly compute the add-ons entitlements we need to cancel (see below) getSubscriptionBase().cancelWithPolicy(billingPolicy, callContext); } catch (SubscriptionBaseApiException e) { throw new EntitlementApiException(e); } final BlockingState newBlockingState = new DefaultBlockingState(getId(), BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_CANCELLED, EntitlementService.ENTITLEMENT_SERVICE_NAME, true, true, false, effectiveDate); entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(newBlockingState, contextWithValidAccountRecordId); blockAddOnsIfRequired(effectiveDate, callContext, contextWithValidAccountRecordId); return entitlementApi.getEntitlementForId(getId(), callContext); }
@Test(groups = "fast") public void testChangeSubscriptionFutureCancelled() throws SubscriptionBaseApiException { SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); final PlanPhase trialPhase = subscription.getCurrentPhase(); // MOVE TO NEXT PHASE final PlanPhase currentPhase = subscription.getCurrentPhase(); testListener.pushExpectedEvent(NextEvent.PHASE); final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31)); clock.addDeltaFromReality(it.toDurationMillis()); assertListenerStatus(); // SET CTD TO CANCEL IN FUTURE final DateTime expectedPhaseTrialChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), trialPhase.getDuration()); final Duration ctd = testUtil.getDurationMonth(1); final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(expectedPhaseTrialChange, ctd); subscriptionInternalApi.setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext); subscription = subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); subscription.cancelWithPolicy(BillingActionPolicy.END_OF_TERM, callContext); try { subscription.changePlanWithDate("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, clock.getUTCNow(), callContext); } catch (SubscriptionBaseApiException e) { assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_FUTURE_CANCELLED.getCode()); } assertListenerStatus(); }