Search in sources :

Example 51 with SubscriptionBaseApiException

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

the class DefaultSubscriptionApi method getActiveSubscriptionBundleForExternalKey.

@Override
public SubscriptionBundle getActiveSubscriptionBundleForExternalKey(final String externalKey, final TenantContext context) throws SubscriptionApiException {
    final InternalTenantContext internalContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(context);
    try {
        final UUID activeSubscriptionIdForKey = entitlementUtils.getFirstActiveSubscriptionIdForKeyOrNull(externalKey, internalContext);
        if (activeSubscriptionIdForKey == null) {
            throw new SubscriptionApiException(new SubscriptionBaseApiException(ErrorCode.SUB_GET_INVALID_BUNDLE_KEY, externalKey));
        }
        final InternalTenantContext internalContextWithAccountRecordId = internalCallContextFactory.createInternalTenantContext(activeSubscriptionIdForKey, ObjectType.SUBSCRIPTION, context);
        final UUID bundleId = subscriptionBaseInternalApi.getBundleIdFromSubscriptionId(activeSubscriptionIdForKey, internalContextWithAccountRecordId);
        return getSubscriptionBundle(bundleId, context);
    } catch (final SubscriptionBaseApiException e) {
        throw new SubscriptionApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 52 with SubscriptionBaseApiException

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

the class DefaultSubscriptionApi method updateExternalKey.

@Override
public void updateExternalKey(final UUID bundleId, final String newExternalKey, final CallContext callContext) throws EntitlementApiException {
    logUpdateExternalKey(log, bundleId, newExternalKey);
    final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(bundleId, ObjectType.BUNDLE, callContext);
    final ImmutableAccountData account;
    try {
        final UUID accountId = subscriptionBaseInternalApi.getAccountIdFromBundleId(bundleId, internalCallContext);
        account = accountApi.getImmutableAccountDataById(accountId, internalCallContext);
    } catch (final SubscriptionBaseApiException e) {
        throw new EntitlementApiException(e);
    } catch (AccountApiException e) {
        throw new EntitlementApiException(e);
    }
    final LocalDate effectiveDate = internalCallContext.toLocalDate(callContext.getCreatedDate());
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(bundleId, newExternalKey, new ArrayList<EntitlementSpecifier>(), effectiveDate, effectiveDate, false);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.UPDATE_BUNDLE_EXTERNAL_KEY, account.getId(), null, baseEntitlementWithAddOnsSpecifierList, null, ImmutableList.<PluginProperty>of(), callContext);
    final WithEntitlementPlugin<Void> updateExternalKeyWithPlugin = new WithEntitlementPlugin<Void>() {

        final InternalCallContext internalCallContextWithValidAccountId = internalCallContextFactory.createInternalCallContext(account.getId(), callContext);

        @Override
        public Void doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
            subscriptionBaseInternalApi.updateExternalKey(bundleId, newExternalKey, internalCallContextWithValidAccountId);
            return null;
        }
    };
    pluginExecution.executeWithPlugin(updateExternalKeyWithPlugin, 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) LocalDate(org.joda.time.LocalDate) AccountApiException(org.killbill.billing.account.api.AccountApiException) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext)

Example 53 with SubscriptionBaseApiException

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

the class DefaultSubscriptionApi method getSubscriptionBundle.

@Override
public SubscriptionBundle getSubscriptionBundle(final UUID bundleId, final TenantContext tenantContext) throws SubscriptionApiException {
    final UUID accountId = internalCallContextFactory.getAccountId(bundleId, ObjectType.BUNDLE, tenantContext);
    final InternalTenantContext internalTenantContextWithValidAccountRecordId = internalCallContextFactory.createInternalTenantContext(accountId, tenantContext);
    final SubscriptionBaseBundle baseBundle;
    try {
        baseBundle = subscriptionBaseInternalApi.getBundleFromId(bundleId, internalTenantContextWithValidAccountRecordId);
    } catch (final SubscriptionBaseApiException e) {
        throw new SubscriptionApiException(e);
    }
    final List<Entitlement> bundleEntitlements;
    try {
        bundleEntitlements = entitlementInternalApi.getAllEntitlementsForBundle(bundleId, internalTenantContextWithValidAccountRecordId);
    } catch (final EntitlementApiException e) {
        throw new SubscriptionApiException(e);
    }
    // Build subscriptions
    final List<Subscription> bundleSubscriptions = new LinkedList<Subscription>();
    for (final Entitlement entitlement : bundleEntitlements) {
        if (entitlement instanceof DefaultEntitlement) {
            bundleSubscriptions.add(new DefaultSubscription((DefaultEntitlement) entitlement));
        } else {
            throw new ShouldntHappenException("Entitlement should be a DefaultEntitlement instance");
        }
    }
    final String bundleExternalKey = bundleSubscriptions.get(0).getBundleExternalKey();
    final SubscriptionBundleTimeline timeline = new DefaultSubscriptionBundleTimeline(accountId, bundleId, bundleExternalKey, bundleEntitlements, internalTenantContextWithValidAccountRecordId);
    return new DefaultSubscriptionBundle(bundleId, accountId, bundleExternalKey, bundleSubscriptions, timeline, baseBundle.getOriginalCreatedDate(), baseBundle.getCreatedDate(), baseBundle.getUpdatedDate());
}
Also used : LinkedList(java.util.LinkedList) ShouldntHappenException(org.killbill.billing.util.customfield.ShouldntHappenException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) UUID(java.util.UUID) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 54 with SubscriptionBaseApiException

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

the class DefaultEntitlementApi method populateEventsStreamForBaseSubscriptionPerBundleCache.

private void populateEventsStreamForBaseSubscriptionPerBundleCache(final UUID bundleId, final Map<UUID, Optional<EventsStream>> eventsStreamForBaseSubscriptionPerBundle, final TenantContext callContext, final InternalCallContext contextWithValidAccountRecordId) throws EntitlementApiException {
    if (eventsStreamForBaseSubscriptionPerBundle.get(bundleId) == null) {
        final List<SubscriptionBase> subscriptionsByBundle;
        try {
            subscriptionsByBundle = subscriptionBaseInternalApi.getSubscriptionsForBundle(bundleId, null, contextWithValidAccountRecordId);
            if (subscriptionsByBundle == null || subscriptionsByBundle.isEmpty()) {
                throw new EntitlementApiException(ErrorCode.SUB_NO_ACTIVE_SUBSCRIPTIONS, bundleId);
            }
        } catch (final SubscriptionBaseApiException e) {
            throw new EntitlementApiException(e);
        }
        final boolean isStandalone = Iterables.any(subscriptionsByBundle, new Predicate<SubscriptionBase>() {

            @Override
            public boolean apply(final SubscriptionBase input) {
                return ProductCategory.STANDALONE.equals(input.getCategory());
            }
        });
        if (!isStandalone) {
            final EventsStream eventsStreamForBaseSubscription = eventsStreamBuilder.buildForBaseSubscription(bundleId, callContext);
            eventsStreamForBaseSubscriptionPerBundle.put(bundleId, Optional.<EventsStream>of(eventsStreamForBaseSubscription));
        } else {
            eventsStreamForBaseSubscriptionPerBundle.put(bundleId, Optional.<EventsStream>absent());
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) EventsStream(org.killbill.billing.entitlement.EventsStream) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 55 with SubscriptionBaseApiException

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

the class DefaultEntitlementApi method transferEntitlementsOverrideBillingPolicy.

@Override
public UUID transferEntitlementsOverrideBillingPolicy(final UUID sourceAccountId, final UUID destAccountId, final String bundleExternalKey, @Nullable final LocalDate effectiveDate, final BillingActionPolicy billingPolicy, final Iterable<PluginProperty> properties, final CallContext context) throws EntitlementApiException {
    logTransferEntitlement(log, sourceAccountId, destAccountId, bundleExternalKey, effectiveDate, billingPolicy);
    final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleExternalKey, new ArrayList<EntitlementSpecifier>(), effectiveDate, effectiveDate, false);
    final List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final EntitlementContext pluginContext = new DefaultEntitlementContext(OperationType.TRANSFER_BUNDLE, sourceAccountId, destAccountId, baseEntitlementWithAddOnsSpecifierList, billingPolicy, properties, context);
    final WithEntitlementPlugin<UUID> transferWithPlugin = new WithEntitlementPlugin<UUID>() {

        @Override
        public UUID doCall(final EntitlementApi entitlementApi, final DefaultEntitlementContext updatedPluginContext) throws EntitlementApiException {
            final boolean cancelImm;
            switch(billingPolicy) {
                case IMMEDIATE:
                    cancelImm = true;
                    break;
                case END_OF_TERM:
                    cancelImm = false;
                    break;
                default:
                    throw new RuntimeException("Unexpected billing policy " + billingPolicy);
            }
            final InternalCallContext contextWithSourceAccountRecordId = internalCallContextFactory.createInternalCallContext(sourceAccountId, context);
            try {
                final UUID activeSubscriptionIdForKey = entitlementUtils.getFirstActiveSubscriptionIdForKeyOrNull(bundleExternalKey, contextWithSourceAccountRecordId);
                final UUID bundleId = activeSubscriptionIdForKey != null ? subscriptionBaseInternalApi.getBundleIdFromSubscriptionId(activeSubscriptionIdForKey, contextWithSourceAccountRecordId) : null;
                final UUID baseBundleAccountId = bundleId != null ? subscriptionBaseInternalApi.getAccountIdFromBundleId(bundleId, contextWithSourceAccountRecordId) : null;
                if (baseBundleAccountId == null || !baseBundleAccountId.equals(sourceAccountId)) {
                    throw new EntitlementApiException(new SubscriptionBaseApiException(ErrorCode.SUB_GET_INVALID_BUNDLE_KEY, bundleExternalKey));
                }
                final DefaultBaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = getFirstBaseEntitlementWithAddOnsSpecifier(updatedPluginContext.getBaseEntitlementWithAddOnsSpecifiers());
                final DateTime requestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getBillingEffectiveDate(), updatedPluginContext.getCreatedDate(), contextWithSourceAccountRecordId);
                final SubscriptionBaseBundle newBundle = subscriptionBaseTransferApi.transferBundle(sourceAccountId, destAccountId, bundleExternalKey, requestedDate, true, cancelImm, context);
                // Update the context for plugins
                baseEntitlementWithAddOnsSpecifier.setBundleId(newBundle.getId());
                baseEntitlementWithAddOnsSpecifier.setBundleExternalKey(newBundle.getExternalKey());
                final Map<BlockingState, UUID> blockingStates = new HashMap<BlockingState, UUID>();
                // Note that there is no un-transfer at the moment, so we effectively add a blocking state on disk for all subscriptions
                for (final SubscriptionBase subscriptionBase : subscriptionBaseInternalApi.getSubscriptionsForBundle(bundleId, null, contextWithSourceAccountRecordId)) {
                    final BlockingState blockingState = new DefaultBlockingState(subscriptionBase.getId(), BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_CANCELLED, KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName(), true, true, false, requestedDate);
                    blockingStates.put(blockingState, subscriptionBase.getBundleId());
                }
                entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(blockingStates, contextWithSourceAccountRecordId);
                // Add blocking events for transferred subscriptions..
                final InternalCallContext contextWithDestAccountRecordId = internalCallContextFactory.createInternalCallContext(destAccountId, context);
                blockingStates.clear();
                final DateTime entitlementRequestedDate = dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getEntitlementEffectiveDate(), updatedPluginContext.getCreatedDate(), contextWithDestAccountRecordId);
                for (final SubscriptionBase subscriptionBase : subscriptionBaseInternalApi.getSubscriptionsForBundle(newBundle.getId(), null, contextWithDestAccountRecordId)) {
                    final BlockingState newBlockingState = new DefaultBlockingState(subscriptionBase.getId(), BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName(), false, false, false, entitlementRequestedDate);
                    blockingStates.put(newBlockingState, subscriptionBase.getBundleId());
                }
                entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(blockingStates, contextWithDestAccountRecordId);
                return newBundle.getId();
            } catch (final SubscriptionBaseTransferApiException e) {
                throw new EntitlementApiException(e);
            } catch (final SubscriptionBaseApiException e) {
                throw new EntitlementApiException(e);
            }
        }
    };
    return pluginExecution.executeWithPlugin(transferWithPlugin, pluginContext);
}
Also used : WithEntitlementPlugin(org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin) HashMap(java.util.HashMap) 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) UUID(java.util.UUID) EntitlementContext(org.killbill.billing.entitlement.plugin.api.EntitlementContext) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) SubscriptionBaseTransferApiException(org.killbill.billing.subscription.api.transfer.SubscriptionBaseTransferApiException)

Aggregations

SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)72 ArrayList (java.util.ArrayList)29 DateTime (org.joda.time.DateTime)29 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)28 UUID (java.util.UUID)27 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)24 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)24 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)19 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)19 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)17 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)16 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)14 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)14 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)11 Plan (org.killbill.billing.catalog.api.Plan)11 LinkedList (java.util.LinkedList)10 SubscriptionCatalog (org.killbill.billing.subscription.catalog.SubscriptionCatalog)10 AccountApiException (org.killbill.billing.account.api.AccountApiException)8 PlanPhasePriceOverridesWithCallContext (org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext)8 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)8