Search in sources :

Example 36 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class JaxRsResourceBase method addBlockingState.

public Response addBlockingState(final BlockingStateJson json, final String id, final BlockingStateType type, final String requestedDate, final List<String> pluginPropertiesString, final String createdBy, final String reason, final String comment, final HttpServletRequest request) throws SubscriptionApiException, EntitlementApiException, AccountApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final UUID blockableId = UUID.fromString(id);
    final boolean isBlockBilling = (json.isBlockBilling() != null && json.isBlockBilling());
    final boolean isBlockEntitlement = (json.isBlockEntitlement() != null && json.isBlockEntitlement());
    final boolean isBlockChange = (json.isBlockChange() != null && json.isBlockChange());
    final LocalDate resolvedRequestedDate = toLocalDate(requestedDate);
    final BlockingState input = new DefaultBlockingState(blockableId, type, json.getStateName(), json.getService(), isBlockChange, isBlockEntitlement, isBlockBilling, null);
    subscriptionApi.addBlockingState(input, resolvedRequestedDate, pluginProperties, callContext);
    return Response.status(Status.OK).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) LocalDate(org.joda.time.LocalDate) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Example 37 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class MockEntitlementModuleForPayment method installBlockingApi.

@Override
public void installBlockingApi() {
    super.installBlockingApi();
    final BlockingState blockingState = new DefaultBlockingState(null, BlockingStateType.ACCOUNT, "CLEAR_STATE_NAME", "test", false, false, false, new DateTime(DateTimeZone.UTC));
    Mockito.when(blockingApi.getBlockingAllForAccount(Mockito.<InternalTenantContext>any())).thenReturn(ImmutableList.<BlockingState>of(blockingState));
    Mockito.when(blockingApi.getBlockingStateForService(Mockito.<UUID>any(), Mockito.<BlockingStateType>any(), Mockito.anyString(), Mockito.<InternalTenantContext>any())).thenReturn(blockingState);
}
Also used : DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) DateTime(org.joda.time.DateTime)

Example 38 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class DefaultEntitlementApiBase method blockUnblockBundle.

private UUID blockUnblockBundle(final UUID bundleId, final String stateName, final String serviceName, @Nullable final LocalDate localEffectiveDate, boolean blockBilling, boolean blockEntitlement, boolean blockChange, @Nullable final SubscriptionBase inputBaseSubscription, final InternalCallContext internalCallContext) throws EntitlementApiException {
    final DateTime effectiveDate = dateHelper.fromLocalDateAndReferenceTime(localEffectiveDate, internalCallContext.getCreatedDate(), internalCallContext);
    final BlockingState state = new DefaultBlockingState(bundleId, BlockingStateType.SUBSCRIPTION_BUNDLE, stateName, serviceName, blockChange, blockEntitlement, blockBilling, effectiveDate);
    entitlementUtils.setBlockingStatesAndPostBlockingTransitionEvent(ImmutableList.<BlockingState>of(state), bundleId, internalCallContext);
    return state.getId();
}
Also used : DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) BlockingState(org.killbill.billing.entitlement.api.BlockingState) DateTime(org.joda.time.DateTime) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Example 39 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState 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)

Example 40 with DefaultBlockingState

use of org.killbill.billing.junction.DefaultBlockingState in project killbill by killbill.

the class DefaultEntitlementApi method createEntitlementEvents.

private List<UUID> createEntitlementEvents(final Iterable<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifiersAfterPlugins, final List<SubscriptionBaseWithAddOns> subscriptionsWithAddOns, final CallContext updatedPluginContext, final InternalCallContext contextWithValidAccountRecordId) throws EntitlementApiException {
    final List<UUID> createdSubscriptionIds = new LinkedList<UUID>();
    final Map<BlockingState, UUID> blockingStateMap = new HashMap<BlockingState, UUID>();
    int i = 0;
    for (final Iterator<BaseEntitlementWithAddOnsSpecifier> it = baseEntitlementWithAddOnsSpecifiersAfterPlugins.iterator(); it.hasNext(); i++) {
        final BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = it.next();
        for (final SubscriptionBase subscriptionBase : subscriptionsWithAddOns.get(i).getSubscriptionBaseList()) {
            final BlockingState blockingState = new DefaultBlockingState(subscriptionBase.getId(), BlockingStateType.SUBSCRIPTION, DefaultEntitlementApi.ENT_STATE_START, KILLBILL_SERVICES.ENTITLEMENT_SERVICE.getServiceName(), false, false, false, dateHelper.fromLocalDateAndReferenceTime(baseEntitlementWithAddOnsSpecifier.getEntitlementEffectiveDate(), updatedPluginContext.getCreatedDate(), contextWithValidAccountRecordId));
            blockingStateMap.put(blockingState, subscriptionsWithAddOns.get(i).getBundle().getId());
            createdSubscriptionIds.add(subscriptionBase.getId());
        }
    }
    entitlementUtils.setBlockingStateAndPostBlockingTransitionEvent(blockingStateMap, contextWithValidAccountRecordId);
    return createdSubscriptionIds;
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) HashMap(java.util.HashMap) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState) UUID(java.util.UUID) LinkedList(java.util.LinkedList) DefaultBlockingState(org.killbill.billing.junction.DefaultBlockingState)

Aggregations

DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)63 UUID (java.util.UUID)46 DateTime (org.joda.time.DateTime)38 BlockingState (org.killbill.billing.entitlement.api.BlockingState)37 Test (org.testng.annotations.Test)35 LocalDate (org.joda.time.LocalDate)34 Account (org.killbill.billing.account.api.Account)23 ArrayList (java.util.ArrayList)21 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)19 DateTimeZone (org.joda.time.DateTimeZone)14 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)14 SubscriptionBaseTransition (org.killbill.billing.subscription.api.user.SubscriptionBaseTransition)11 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)9 Entitlement (org.killbill.billing.entitlement.api.Entitlement)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 WithEntitlementPlugin (org.killbill.billing.entitlement.api.EntitlementPluginExecution.WithEntitlementPlugin)7 EntitlementContext (org.killbill.billing.entitlement.plugin.api.EntitlementContext)7 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)7 BigDecimal (java.math.BigDecimal)6 BillingEvent (org.killbill.billing.junction.BillingEvent)6