Search in sources :

Example 6 with SubscriptionBaseBundle

use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.

the class TestTransfer method testTransferWithAOCancelled.

@Test(groups = "slow")
public void testTransferWithAOCancelled() throws Exception {
    final String baseProduct = "Shotgun";
    final BillingPeriod baseTerm = BillingPeriod.MONTHLY;
    final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
    // CREATE BP
    final SubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList);
    // MOVE 3 DAYS AND CREATE AO1
    clock.addDays(3);
    final String aoProduct1 = "Telescopic-Scope";
    final BillingPeriod aoTerm1 = BillingPeriod.MONTHLY;
    final DefaultSubscriptionBase aoSubscription1 = testUtil.createSubscription(bundle, aoProduct1, aoTerm1, basePriceList);
    assertEquals(aoSubscription1.getState(), EntitlementState.ACTIVE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    testListener.pushExpectedEvent(NextEvent.PHASE);
    clock.addDays(30);
    assertListenerStatus();
    // SET CTD TO TRIGGER CANCELLATION EOT
    final DateTime ctd = baseSubscription.getStartDate().plusDays(30).plusMonths(1);
    subscriptionInternalApi.setChargedThroughDate(baseSubscription.getId(), ctd, internalCallContext);
    // SET CTD TO TRIGGER CANCELLATION EOT
    subscriptionInternalApi.setChargedThroughDate(aoSubscription1.getId(), ctd, internalCallContext);
    // CANCEL ADDON
    testListener.pushExpectedEvent(NextEvent.CANCEL);
    aoSubscription1.cancelWithDate(clock.getUTCNow(), callContext);
    assertListenerStatus();
    clock.addDays(1);
    final DateTime transferRequestedDate = clock.getUTCNow();
    testListener.pushExpectedEvent(NextEvent.TRANSFER);
    transferApi.transferBundle(bundle.getAccountId(), newAccountId, bundle.getExternalKey(), transferRequestedDate, true, false, callContext);
    assertListenerStatus();
    final List<SubscriptionBaseBundle> bundlesForAccountAndKey = subscriptionInternalApi.getBundlesForAccountAndKey(newAccountId, bundle.getExternalKey(), internalCallContext);
    assertEquals(bundlesForAccountAndKey.size(), 1);
    final SubscriptionBaseBundle newBundle = bundlesForAccountAndKey.get(0);
    final List<SubscriptionBase> subscriptions = subscriptionInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, internalCallContext);
    assertEquals(subscriptions.size(), 1);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 7 with SubscriptionBaseBundle

use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.

the class SubscriptionChecker method checkBundleNoAudits.

public SubscriptionBaseBundle checkBundleNoAudits(final UUID bundleId, final UUID expectedAccountId, final String expectedKey, final InternalTenantContext context) throws SubscriptionBaseApiException {
    final SubscriptionBaseBundle bundle = subscriptionApi.getBundleFromId(bundleId, context);
    Assert.assertNotNull(bundle);
    Assert.assertEquals(bundle.getAccountId(), expectedAccountId);
    Assert.assertEquals(bundle.getExternalKey(), expectedKey);
    return bundle;
}
Also used : SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)

Example 8 with SubscriptionBaseBundle

use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.

the class DefaultEntitlementApi method createBaseEntitlement.

@Override
public Entitlement createBaseEntitlement(final UUID accountId, final PlanPhaseSpecifier planPhaseSpecifier, final String externalKey, final List<PlanPhasePriceOverride> overrides, @Nullable final LocalDate entitlementEffectiveDate, @Nullable final LocalDate billingEffectiveDate, final boolean isMigrated, final Iterable<PluginProperty> properties, final CallContext callContext) throws EntitlementApiException {
    logCreateEntitlement(log, null, planPhaseSpecifier, overrides, entitlementEffectiveDate, billingEffectiveDate);
    final EntitlementSpecifier entitlementSpecifier = new DefaultEntitlementSpecifier(planPhaseSpecifier, overrides);
    final List<EntitlementSpecifier> entitlementSpecifierList = new ArrayList<EntitlementSpecifier>();
    entitlementSpecifierList.add(entitlementSpecifier);
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(null, externalKey, entitlementSpecifierList, entitlementEffectiveDate, billingEffectiveDate, isMigrated);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.CREATE_SUBSCRIPTION, accountId, null, baseEntitlementWithAddOnsSpecifierList, null, properties, callContext);
    final WithEntitlementPlugin<Entitlement> createBaseEntitlementWithPlugin = new WithEntitlementPlugin<Entitlement>() {

        @Override
        public Entitlement doCall(final EntitlementApi entitlementApi, final EntitlementContext updatedPluginContext) throws EntitlementApiException {
            final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(accountId, callContext);
            try {
                if (entitlementUtils.getFirstActiveSubscriptionIdForKeyOrNull(externalKey, contextWithValidAccountRecordId) != null) {
                    throw new EntitlementApiException(new SubscriptionBaseApiException(ErrorCode.SUB_CREATE_ACTIVE_BUNDLE_KEY_EXISTS, externalKey));
                }
                final SubscriptionBaseBundle bundle = subscriptionBaseInternalApi.createBundleForAccount(accountId, externalKey, contextWithValidAccountRecordId);
                final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = getFirstBaseEntitlementWithAddOnsSpecifier(updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers());
                final EntitlementSpecifier specifier = getFirstEntitlementSpecifier(baseEntitlementWithAddOnsSpecifier);
                final DateTime billingRequestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate(), contextWithValidAccountRecordId);
                final SubscriptionBase subscription = subscriptionBaseInternalApi.createSubscription(bundle.getId(), specifier.getPlanPhaseSpecifier(), specifier.getOverrides(), billingRequestedDate, isMigrated, contextWithValidAccountRecordId);
                final DateTime entitlementRequestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getEntitlementEffectiveDate(), contextWithValidAccountRecordId);
                final BlockingState newBlockingState = new DefaultBlockingState(subscription.getId(), BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, EntitlementService.ENTITLEMENT_SERVICE_NAME, false, false, false, entitlementRequestedDate);
                entitlementUtils.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableList.<BlockingState>of(newBlockingState), subscription.getBundleId(), contextWithValidAccountRecordId);
                return new DefaultEntitlement(accountId, subscription.getId(), eventsStreamBuilder, entitlementApi, pluginExecution, blockingStateDao, subscriptionBaseInternalApi, checker, notificationQueueService, entitlementUtils, dateHelper, clock, securityApi, internalCallContextFactory, callContext);
            } catch (final SubscriptionBaseApiException e) {
                throw new EntitlementApiException(e);
            }
        }
    };
    return pluginExecution.executeWithPlugin(createBaseEntitlementWithPlugin, pluginContext);
}
Also used : WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) DateTime(org.joda.time.DateTime) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) EntitlementLoggingHelper.logPauseResumeEntitlement(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logPauseResumeEntitlement) EntitlementLoggingHelper.logTransferEntitlement(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logTransferEntitlement) EntitlementLoggingHelper.logCreateEntitlement(org.killbill.billing.entitlement.logging.EntitlementLoggingHelper.logCreateEntitlement) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 9 with SubscriptionBaseBundle

use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.

the class DefaultEntitlementApi method getDryRunStatusForChange.

@Override
public List<EntitlementAOStatusDryRun> getDryRunStatusForChange(final UUID bundleId, final String targetProductName, @Nullable final LocalDate effectiveDate, final TenantContext context) throws EntitlementApiException {
    final InternalTenantContext internalContext = internalCallContextFactory.createInternalTenantContext(bundleId, ObjectType.BUNDLE, context);
    try {
        final SubscriptionBaseBundle bundle = subscriptionBaseInternalApi.getBundleFromId(bundleId, internalContext);
        final SubscriptionBase baseSubscription = subscriptionBaseInternalApi.getBaseSubscription(bundleId, internalContext);
        final InternalTenantContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(bundle.getAccountId(), context);
        final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(effectiveDate, contextWithValidAccountRecordId);
        return subscriptionBaseInternalApi.getDryRunChangePlanStatus(baseSubscription.getId(), targetProductName, requestedDate, contextWithValidAccountRecordId);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) DateTime(org.joda.time.DateTime) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 10 with SubscriptionBaseBundle

use of org.killbill.billing.subscription.api.user.SubscriptionBaseBundle in project killbill by killbill.

the class DefaultSubscriptionApi method addBlockingState.

@Override
public void addBlockingState(final BlockingState inputBlockingState, @Nullable final LocalDate inputEffectiveDate, final Iterable<PluginProperty> properties, final CallContext callContext) throws EntitlementApiException {
    logAddBlockingState(log, inputBlockingState, inputEffectiveDate);
    // This is in no way an exhaustive arg validation, but to to ensure plugin would not hijack private entitlement state or service name
    if (inputBlockingState.getService() == null || inputBlockingState.getService().equals(EntitlementService.ENTITLEMENT_SERVICE_NAME)) {
        throw new EntitlementApiException(ErrorCode.SUB_BLOCKING_STATE_INVALID_ARG, "Need to specify a valid serviceName");
    }
    if (inputBlockingState.getStateName() == null || inputBlockingState.getStateName().equals(DefaultEntitlementApi.ENT_STATE_CANCELLED) || inputBlockingState.getStateName().equals(DefaultEntitlementApi.ENT_STATE_BLOCKED) || inputBlockingState.getStateName().equals(DefaultEntitlementApi.ENT_STATE_CLEAR)) {
        throw new EntitlementApiException(ErrorCode.SUB_BLOCKING_STATE_INVALID_ARG, "Need to specify a valid stateName");
    }
    final InternalCallContext internalCallContextWithValidAccountId;
    final ImmutableAccountData account;
    final UUID accountId;
    final UUID bundleId;
    final String externalKey;
    try {
        switch(inputBlockingState.getType()) {
            case ACCOUNT:
                internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(inputBlockingState.getBlockedId(), ObjectType.ACCOUNT, callContext);
                account = accountApi.getImmutableAccountDataById(inputBlockingState.getBlockedId(), internalCallContextWithValidAccountId);
                externalKey = account.getExternalKey();
                accountId = account.getId();
                bundleId = null;
                break;
            case SUBSCRIPTION_BUNDLE:
                internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(inputBlockingState.getBlockedId(), ObjectType.BUNDLE, callContext);
                final SubscriptionBaseBundle bundle = subscriptionBaseInternalApi.getBundleFromId(inputBlockingState.getBlockedId(), internalCallContextWithValidAccountId);
                externalKey = bundle.getExternalKey();
                bundleId = bundle.getId();
                accountId = bundle.getAccountId();
                break;
            case SUBSCRIPTION:
                internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(inputBlockingState.getBlockedId(), ObjectType.SUBSCRIPTION, callContext);
                final Entitlement entitlement = entitlementInternalApi.getEntitlementForId(inputBlockingState.getBlockedId(), internalCallContextWithValidAccountId);
                bundleId = entitlement.getBundleId();
                accountId = entitlement.getAccountId();
                externalKey = null;
                break;
            default:
                throw new IllegalStateException("Invalid blockingStateType " + inputBlockingState.getType());
        }
    } catch (final AccountApiException e) {
        throw new EntitlementApiException(e);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    }
    final DateTime effectiveDate = inputEffectiveDate == null ? clock.getUTCNow() : internalCallContextWithValidAccountId.toUTCDateTime(inputEffectiveDate);
    final DefaultBlockingState blockingState = new DefaultBlockingState(inputBlockingState, effectiveDate);
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(bundleId, externalKey, new ArrayList<EntitlementSpecifier>(), internalCallContextWithValidAccountId.toLocalDate(effectiveDate), internalCallContextWithValidAccountId.toLocalDate(effectiveDate), false);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.ADD_BLOCKING_STATE, accountId, null, baseEntitlementWithAddOnsSpecifierList, null, properties, callContext);
    final WithEntitlementPlugin<Void> addBlockingStateWithPlugin = new WithEntitlementPlugin<Void>() {

        @Override
        public Void doCall(final EntitlementApi entitlementApi, final EntitlementContext updatedPluginContext) throws EntitlementApiException {
            entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(blockingState, internalCallContextWithValidAccountId);
            return null;
        }
    };
    pluginExecution.executeWithPlugin(addBlockingStateWithPlugin, pluginContext);
}
Also used : ImmutableAccountData(org.killbill.billing.account.api.ImmutableAccountData) WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) ArrayList(java.util.ArrayList) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DateTime(org.joda.time.DateTime) AccountApiException(org.killbill.billing.account.api.AccountApiException) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext)

Aggregations

SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)26 DateTime (org.joda.time.DateTime)17 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)16 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)10 UUID (java.util.UUID)9 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)9 ArrayList (java.util.ArrayList)8 Test (org.testng.annotations.Test)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)7 Plan (org.killbill.billing.catalog.api.Plan)7 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)6 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)6 LinkedList (java.util.LinkedList)4 AccountApiException (org.killbill.billing.account.api.AccountApiException)4 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)4 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)4 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)4 DefaultSubscriptionBaseBundle (org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle)4 ImmutableList (com.google.common.collect.ImmutableList)3