use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.
the class DefaultSubscriptionBase method getBillingAlignment.
@Override
public BillingAlignment getBillingAlignment(final PlanPhaseSpecifier spec, final DateTime transitionTime, final VersionedCatalog publicCatalog) throws SubscriptionBaseApiException {
try {
final SubscriptionCatalog catalog = DefaultSubscriptionCatalogApi.wrapCatalog(publicCatalog, clock);
final SubscriptionBaseTransition transition = (getState() == EntitlementState.PENDING) ? getPendingTransition() : getLastTransitionForCurrentPlan();
final BillingAlignment alignment = catalog.billingAlignment(spec, transitionTime, transition.getEffectiveTransitionTime());
return alignment;
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
}
use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.
the class DefaultSubscriptionBaseService method processEventReady.
@Override
public void processEventReady(final SubscriptionBaseEvent event, final int seqId, final InternalCallContext context) {
if (!event.isActive()) {
return;
}
try {
final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(context);
final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) dao.getSubscriptionFromId(event.getSubscriptionId(), catalog, context);
if (subscription == null) {
log.warn("Error retrieving subscriptionId='{}'", event.getSubscriptionId());
return;
}
final SubscriptionBaseTransitionData transition = subscription.getTransitionFromEvent(event, seqId);
if (transition == null) {
log.warn("Skipping event ='{}', no matching transition was built", event.getType());
return;
}
boolean eventSent = false;
if (event.getType() == EventType.PHASE) {
eventSent = onPhaseEvent(subscription, event, catalog, context);
} else if (event.getType() == EventType.API_USER && subscription.getCategory() == ProductCategory.BASE) {
final CallContext callContext = internalCallContextFactory.createCallContext(context);
eventSent = onBasePlanEvent(subscription, event, catalog, callContext);
} else if (event.getType() == EventType.BCD_UPDATE) {
eventSent = false;
}
if (!eventSent) {
// Methods above invoking the DAO will send this event directly from the transaction
final BusEvent busEvent = new DefaultEffectiveSubscriptionEvent(transition, subscription.getAlignStartDate(), context.getUserToken(), context.getAccountRecordId(), context.getTenantRecordId());
eventBus.post(busEvent);
}
} catch (final EventBusException e) {
log.warn("Failed to post event {}", event, e);
} catch (final CatalogApiException e) {
log.warn("Failed to post event {}", event, e);
}
}
use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method cancelWithRequestedDate.
@Override
public boolean cancelWithRequestedDate(final DefaultSubscriptionBase subscription, final DateTime requestedDateWithMs, final CallContext context) throws SubscriptionBaseApiException {
final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
try {
final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalCallContext);
final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : context.getCreatedDate();
return doCancelPlan(ImmutableMap.<DefaultSubscriptionBase, DateTime>of(subscription, effectiveDate), catalog, internalCallContext);
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
}
use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method doChangePlan.
private void doChangePlan(final DefaultSubscriptionBase subscription, final EntitlementSpecifier spec, final DateTime effectiveDate, final CallContext context) throws SubscriptionBaseApiException, CatalogApiException {
final InternalCallContext internalCallContext = createCallContextFromBundleId(subscription.getBundleId(), context);
final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(spec.getOverrides(), context);
final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalCallContext);
final PlanPhaseSpecifier planPhaseSpecifier = spec.getPlanPhaseSpecifier();
final StaticCatalog versionCatalog = catalog.versionForDate(effectiveDate);
final Plan newPlan = versionCatalog.createOrFindPlan(planPhaseSpecifier, overridesWithContext);
final PhaseType initialPhaseType = planPhaseSpecifier.getPhaseType();
if (ProductCategory.ADD_ON.toString().equalsIgnoreCase(newPlan.getProduct().getCategory().toString())) {
if (newPlan.getPlansAllowedInBundle() != -1 && newPlan.getPlansAllowedInBundle() > 0 && addonUtils.countExistingAddOnsWithSamePlanName(dao.getSubscriptions(subscription.getBundleId(), null, catalog, internalCallContext), newPlan.getName()) >= newPlan.getPlansAllowedInBundle()) {
// the plan can be changed to the new value, because it has reached its limit by bundle
throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_AO_MAX_PLAN_ALLOWED_BY_BUNDLE, newPlan.getName());
}
}
if (newPlan.getProduct().getCategory() != subscription.getCategory()) {
throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_INVALID, subscription.getId());
}
final List<DefaultSubscriptionBase> addOnSubscriptionsToBeCancelled = new ArrayList<DefaultSubscriptionBase>();
final List<SubscriptionBaseEvent> addOnCancelEvents = new ArrayList<SubscriptionBaseEvent>();
final List<SubscriptionBaseEvent> changeEvents = getEventsOnChangePlan(subscription, newPlan, newPlan.getPriceList().getName(), effectiveDate, true, addOnSubscriptionsToBeCancelled, addOnCancelEvents, initialPhaseType, spec.getBillCycleDay(), catalog, internalCallContext);
dao.changePlan(subscription, changeEvents, addOnSubscriptionsToBeCancelled, addOnCancelEvents, catalog, internalCallContext);
subscription.rebuildTransitions(dao.getEventsForSubscription(subscription.getId(), internalCallContext), catalog);
}
use of org.killbill.billing.subscription.catalog.SubscriptionCatalog in project killbill by killbill.
the class DefaultSubscriptionInternalApi method createBaseSubscriptionsWithAddOns.
@Override
public List<SubscriptionBaseWithAddOns> createBaseSubscriptionsWithAddOns(final VersionedCatalog publicCatalog, final Iterable<SubscriptionBaseWithAddOnsSpecifier> subscriptionWithAddOnsSpecifiers, final boolean renameCancelledBundleIfExist, final InternalCallContext context) throws SubscriptionBaseApiException {
try {
final SubscriptionCatalog catalog = DefaultSubscriptionCatalogApi.wrapCatalog(publicCatalog, clock);
final CallContext callContext = internalCallContextFactory.createCallContext(context);
return super.createBaseSubscriptionsWithAddOns(subscriptionWithAddOnsSpecifiers, renameCancelledBundleIfExist, catalog, addonUtils, accountIdCacheController, bundleIdCacheController, callContext, context);
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
}
Aggregations