Search in sources :

Example 1 with SubscriptionBaseWithAddOnsSpecifier

use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.

the class TestSubscriptionHelper method createSubscription.

private DefaultSubscriptionBase createSubscription(final boolean noEvents, @Nullable final SubscriptionBaseBundle bundle, final String productName, final BillingPeriod term, final String planSet, final PhaseType phaseType, final LocalDate requestedDate) throws SubscriptionBaseApiException {
    final VersionedCatalog catalog;
    try {
        catalog = catalogInternalApi.getFullCatalog(true, true, internalCallContext);
    } catch (CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
    // Make sure the right account information is used
    final InternalCallContext internalCallContext = bundle == null ? this.internalCallContext : internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), ObjectType.ACCOUNT, this.internalCallContext.getUpdatedBy(), this.internalCallContext.getCallOrigin(), this.internalCallContext.getContextUserType(), this.internalCallContext.getUserToken(), this.internalCallContext.getTenantRecordId());
    boolean bundleExists = false;
    if (bundle != null) {
        try {
            bundleExists = (subscriptionApi.getBundleFromId(bundle.getId(), internalCallContext) != null);
        } catch (final SubscriptionBaseApiException ignored) {
        }
    }
    if (!noEvents && (requestedDate == null || requestedDate.compareTo(clock.getUTCToday()) <= 0)) {
        testListener.pushExpectedEvent(NextEvent.CREATE);
    }
    final ImmutableList<EntitlementSpecifier> entitlementSpecifiers = ImmutableList.<EntitlementSpecifier>of(new EntitlementSpecifier() {

        @Override
        public PlanPhaseSpecifier getPlanPhaseSpecifier() {
            return new PlanPhaseSpecifier(productName, term, planSet, phaseType);
        }

        @Override
        public Integer getBillCycleDay() {
            return null;
        }

        @Override
        public String getExternalKey() {
            return UUID.randomUUID().toString();
        }

        @Override
        public List<PlanPhasePriceOverride> getOverrides() {
            return null;
        }
    });
    final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(bundle == null || !bundleExists ? null : bundle.getId(), bundle == null ? null : bundle.getExternalKey(), entitlementSpecifiers, requestedDate, false);
    final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = subscriptionApi.createBaseSubscriptionsWithAddOns(catalog, ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), false, internalCallContext).get(0);
    final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) subscriptionBaseWithAddOns.getSubscriptionBaseList().get(0);
    assertNotNull(subscription);
    testListener.assertListenerStatus();
    mockNonEntityDao.addTenantRecordIdMapping(subscription.getId(), internalCallContext);
    mockNonEntityDao.addAccountRecordIdMapping(subscription.getId(), internalCallContext);
    mockNonEntityDao.addTenantRecordIdMapping(subscription.getBundleId(), internalCallContext);
    mockNonEntityDao.addAccountRecordIdMapping(subscription.getBundleId(), internalCallContext);
    return subscription;
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) SubscriptionBaseWithAddOnsSpecifier(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 2 with SubscriptionBaseWithAddOnsSpecifier

use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.

the class TestUserApiCreate method testCreateSubscriptionWithBCD.

@Test(groups = "slow")
public void testCreateSubscriptionWithBCD() throws SubscriptionBaseApiException {
    final DateTime init = clock.getUTCNow();
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(bundle.getAccountId(), ObjectType.ACCOUNT, this.internalCallContext.getUpdatedBy(), this.internalCallContext.getCallOrigin(), this.internalCallContext.getContextUserType(), this.internalCallContext.getUserToken(), this.internalCallContext.getTenantRecordId());
    final Iterable<EntitlementSpecifier> specifiers = ImmutableList.<EntitlementSpecifier>of(new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("shotgun-monthly"), 18, null, null));
    final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(bundle.getId(), bundle.getExternalKey(), specifiers, init.toLocalDate(), false);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BCD_CHANGE);
    final List<SubscriptionBaseWithAddOns> subscriptionBaseWithAddOns = subscriptionInternalApi.createBaseSubscriptionsWithAddOns(catalog.getCatalog(), ImmutableList.<SubscriptionBaseWithAddOnsSpecifier>of(subscriptionBaseWithAddOnsSpecifier), false, internalCallContext);
    testListener.assertListenerStatus();
    assertEquals(subscriptionBaseWithAddOns.size(), 1);
    assertEquals(subscriptionBaseWithAddOns.get(0).getSubscriptionBaseList().size(), 1);
    final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) subscriptionBaseWithAddOns.get(0).getSubscriptionBaseList().get(0);
    assertNotNull(subscription);
    assertNotNull(subscription.getBillCycleDayLocal());
    assertEquals(subscription.getBillCycleDayLocal().intValue(), 18);
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) SubscriptionBaseWithAddOnsSpecifier(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 3 with SubscriptionBaseWithAddOnsSpecifier

use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.

the class DefaultEntitlementApi method createBaseEntitlementsWithAddOns.

private List<UUID> createBaseEntitlementsWithAddOns(final OperationType operationType, final UUID accountId, final Iterable<BaseEntitlementWithAddOnsSpecifier> originalBaseEntitlementWithAddOnsSpecifiers, final boolean renameCancelledBundleIfExist, final Iterable<PluginProperty> properties, final CallContext callContext) throws EntitlementApiException {
    logCreateEntitlementsWithAOs(log, originalBaseEntitlementWithAddOnsSpecifiers);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(operationType, accountId, null, originalBaseEntitlementWithAddOnsSpecifiers, null, properties, callContext);
    final WithEntitlementPlugin<List<UUID>> createBaseEntitlementsWithAddOns = new WithEntitlementPlugin<List<UUID>>() {

        @Override
        public List<UUID> doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
            final InternalCallContext contextWithValidAccountRecordId = internalCallContextFactory.createInternalCallContext(accountId, callContext);
            final VersionedCatalog catalog;
            try {
                catalog = catalogInternalApi.getFullCatalog(true, true, contextWithValidAccountRecordId);
            } catch (final CatalogApiException e) {
                throw new EntitlementApiException(e);
            }
            final Map<UUID, Optional<EventsStream>> eventsStreamForBaseSubscriptionPerBundle = new HashMap<UUID, Optional<EventsStream>>();
            final Map<String, Optional<UUID>> bundleKeyToIdMapping = new HashMap<String, Optional<UUID>>();
            final Iterable<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifiersAfterPlugins = updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers();
            final Collection<SubscriptionBaseWithAddOnsSpecifier> subscriptionBaseWithAddOnsSpecifiers = new LinkedList<SubscriptionBaseWithAddOnsSpecifier>();
            DateTime upTo = null;
            for (final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier : baseEntitlementWithAddOnsSpecifiersAfterPlugins) {
                // Entitlement
                final DateTime entitlementRequestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getEntitlementEffectiveDate(), updatedPluginContext.getCreatedDate(), contextWithValidAccountRecordId);
                upTo = upTo == null || upTo.compareTo(entitlementRequestedDate) < 0 ? entitlementRequestedDate : upTo;
                final UUID bundleId = populateCaches(baseEntitlementWithAddOnsSpecifier, eventsStreamForBaseSubscriptionPerBundle, bundleKeyToIdMapping, catalog, callContext, contextWithValidAccountRecordId);
                if (bundleId != null) {
                    final Optional<EventsStream> eventsStreamForBaseSubscription = eventsStreamForBaseSubscriptionPerBundle.get(bundleId);
                    if (eventsStreamForBaseSubscription.isPresent()) {
                        // Verify if the operation is valid for that bundle
                        preCheckAddEntitlement(bundleId, entitlementRequestedDate, baseEntitlementWithAddOnsSpecifier, eventsStreamForBaseSubscription.get());
                    }
                }
                final SubscriptionBaseWithAddOnsSpecifier subscriptionBaseWithAddOnsSpecifier = new SubscriptionBaseWithAddOnsSpecifier(baseEntitlementWithAddOnsSpecifier.getBundleId(), baseEntitlementWithAddOnsSpecifier.getBundleExternalKey(), baseEntitlementWithAddOnsSpecifier.getEntitlementSpecifier(), baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate(), baseEntitlementWithAddOnsSpecifier.isMigrated());
                subscriptionBaseWithAddOnsSpecifiers.add(subscriptionBaseWithAddOnsSpecifier);
            }
            // Verify if operation is allowed by looking for is_block_change on Account
            // Note that to fully check for block_change we should also look for BlockingState at the BUNDLE/SUBSCRIPTION level in case some of the input contain a BP that already exists.
            checkForAccountBlockingChange(accountId, upTo, contextWithValidAccountRecordId);
            final List<SubscriptionBaseWithAddOns> subscriptionsWithAddOns;
            try {
                subscriptionsWithAddOns = subscriptionBaseInternalApi.createBaseSubscriptionsWithAddOns(catalog, subscriptionBaseWithAddOnsSpecifiers, renameCancelledBundleIfExist, contextWithValidAccountRecordId);
            } catch (final SubscriptionBaseApiException e) {
                throw new EntitlementApiException(e);
            }
            // Update the context for plugins (assume underlying ordering is respected)
            for (int i = 0; i < subscriptionsWithAddOns.size(); i++) {
                final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = subscriptionsWithAddOns.get(i);
                updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers(i).setBundleId(subscriptionBaseWithAddOns.getBundle().getId());
                updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers(i).setBundleExternalKey(subscriptionBaseWithAddOns.getBundle().getExternalKey());
            }
            return createEntitlementEvents(baseEntitlementWithAddOnsSpecifiersAfterPlugins, subscriptionsWithAddOns, updatedPluginContext, contextWithValidAccountRecordId);
        }
    };
    return pluginExecution.executeWithPlugin(createBaseEntitlementsWithAddOns, pluginContext);
}
Also used : SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) SubscriptionBaseWithAddOnsSpecifier(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier) WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) HashMap(java.util.HashMap) EventsStream(org.killbill.billing.entitlement.EventsStream) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) DateTime(org.joda.time.DateTime) VersionedCatalog(org.killbill.billing.catalog.api.VersionedCatalog) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) Optional(com.google.common.base.Optional) LinkedList(java.util.LinkedList) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException)

Example 4 with SubscriptionBaseWithAddOnsSpecifier

use of org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier in project killbill by killbill.

the class DefaultSubscriptionBaseCreateApi method createBaseSubscriptionsWithAddOns.

List<SubscriptionBaseWithAddOns> createBaseSubscriptionsWithAddOns(final Iterable<SubscriptionBaseWithAddOnsSpecifier> baseAndAddOnEntitlementsSpecifiers, final boolean renameCancelledBundleIfExist, final SubscriptionCatalog catalog, final AddonUtils addonUtils, final CacheController<UUID, UUID> accountIdCacheController, final CacheController<UUID, UUID> bundleIdCacheController, final CallContext callContext, final InternalCallContext context) throws SubscriptionBaseApiException, CatalogApiException {
    // Prepare the subscription specifiers from the entitlement specifiers
    final Collection<SubscriptionAndAddOnsSpecifier> baseAndAddOnSubscriptionsSpecifiers = new ArrayList<SubscriptionAndAddOnsSpecifier>();
    for (final SubscriptionBaseWithAddOnsSpecifier baseAndAddOnEntitlementsSpecifier : baseAndAddOnEntitlementsSpecifiers) {
        prepareSubscriptionAndAddOnsSpecifier(baseAndAddOnSubscriptionsSpecifiers, baseAndAddOnEntitlementsSpecifier, renameCancelledBundleIfExist, catalog, addonUtils, accountIdCacheController, callContext, context);
    }
    // Create the subscriptions
    final List<SubscriptionBaseWithAddOns> subscriptionBaseWithAddOns = apiService.createPlansWithAddOns(callContext.getAccountId(), baseAndAddOnSubscriptionsSpecifiers, catalog, callContext);
    // Populate the caches
    for (final SubscriptionBaseWithAddOns subscriptionBaseWithAO : subscriptionBaseWithAddOns) {
        for (final SubscriptionBase subscriptionBase : subscriptionBaseWithAO.getSubscriptionBaseList()) {
            bundleIdCacheController.putIfAbsent(subscriptionBase.getId(), subscriptionBaseWithAO.getBundle().getId());
        }
    }
    return subscriptionBaseWithAddOns;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) SubscriptionBaseWithAddOnsSpecifier(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier) ArrayList(java.util.ArrayList) SubscriptionAndAddOnsSpecifier(org.killbill.billing.subscription.api.user.SubscriptionAndAddOnsSpecifier)

Aggregations

SubscriptionBaseWithAddOns (org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns)4 SubscriptionBaseWithAddOnsSpecifier (org.killbill.billing.subscription.api.SubscriptionBaseWithAddOnsSpecifier)4 ArrayList (java.util.ArrayList)3 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 DateTime (org.joda.time.DateTime)2 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)2 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)2 VersionedCatalog (org.killbill.billing.catalog.api.VersionedCatalog)2 EntitlementSpecifier (org.killbill.billing.entitlement.api.EntitlementSpecifier)2 Optional (com.google.common.base.Optional)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 UUID (java.util.UUID)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1 EventsStream (org.killbill.billing.entitlement.EventsStream)1 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)1 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)1 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)1