@Override public List<SubscriptionBaseWithAddOns> createBaseSubscriptionsWithAddOns(final Iterable<SubscriptionBaseWithAddOnsSpecifier> subscriptionWithAddOnsSpecifiers, final boolean renameCancelledBundleIfExist, final InternalCallContext context) throws SubscriptionBaseApiException { try { final Catalog catalog = catalogInternalApi.getFullCatalog(true, true, context); 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); } }
private void cancelSubscriptionsIfRequired(final DateTime effectiveDate, final ImmutableAccountData account, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueException { if (nextOverdueState.getOverdueCancellationPolicy() == OverdueCancellationPolicy.NONE) { return; } final CallContext callContext = internalCallContextFactory.createCallContext(context); try { final BillingActionPolicy actionPolicy; switch (nextOverdueState.getOverdueCancellationPolicy()) { case END_OF_TERM: actionPolicy = BillingActionPolicy.END_OF_TERM; break; case IMMEDIATE: actionPolicy = BillingActionPolicy.IMMEDIATE; break; default: throw new IllegalStateException("Unexpected OverdueCancellationPolicy " + nextOverdueState.getOverdueCancellationPolicy()); } final List<Entitlement> toBeCancelled = new LinkedList<Entitlement>(); computeEntitlementsToCancel(account, toBeCancelled, callContext); try { entitlementInternalApi.cancel(toBeCancelled, context.toLocalDate(effectiveDate), actionPolicy, ImmutableList.<PluginProperty>of(), context); } catch (final EntitlementApiException e) { throw new OverdueException(e); } } catch (final EntitlementApiException e) { throw new OverdueException(e); } }
eventSent = onPhaseEvent(subscription, event, fullCatalog, context); } else if (event.getType() == EventType.API_USER && subscription.getCategory() == ProductCategory.BASE) { final CallContext callContext = internalCallContextFactory.createCallContext(context); eventSent = onBasePlanEvent(subscription, event, fullCatalog, callContext); } else if (event.getType() == EventType.BCD_UPDATE) {
@Override public DateTime getDryRunChangePlanEffectiveDate(final SubscriptionBase subscription, final EntitlementSpecifier spec, final DateTime requestedDateWithMs, final BillingActionPolicy requestedPolicy, final InternalCallContext context) throws SubscriptionBaseApiException, CatalogApiException { final TenantContext tenantContext = internalCallContextFactory.createTenantContext(context); final CallContext callContext = internalCallContextFactory.createCallContext(context); // verify the number of subscriptions (of the same kind) allowed per bundle final Catalog catalog = catalogInternalApi.getFullCatalog(true, true, context); final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : null; final DateTime effectiveCatalogDate = effectiveDate != null ? effectiveDate : context.getCreatedDate(); final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(spec.getOverrides(), callContext); final Plan plan = catalog.createOrFindPlan(spec.getPlanPhaseSpecifier(), overridesWithContext, effectiveCatalogDate, subscription.getStartDate()); if (ProductCategory.ADD_ON.toString().equalsIgnoreCase(plan.getProduct().getCategory().toString())) { if (plan.getPlansAllowedInBundle() != -1 && plan.getPlansAllowedInBundle() > 0 && addonUtils.countExistingAddOnsWithSamePlanName(getSubscriptionsForBundle(subscription.getBundleId(), null, catalog, addonUtils, callContext, context), plan.getName()) >= plan.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, plan.getName()); } } return apiService.dryRunChangePlan((DefaultSubscriptionBase) subscription, spec, effectiveDate, requestedPolicy, tenantContext); }
private CallContext buildCallContext(final InternalCallContext context) { return internalCallContextFactory.createCallContext(context); }
@Override public List<SubscriptionBaseWithAddOns> createBaseSubscriptionsWithAddOns(final Iterable<SubscriptionBaseWithAddOnsSpecifier> subscriptionWithAddOnsSpecifiers, final boolean renameCancelledBundleIfExist, final InternalCallContext context) throws SubscriptionBaseApiException { try { final Catalog catalog = catalogInternalApi.getFullCatalog(true, true, context); 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); } }
@Override public InvoicePayment recordRefund(final UUID paymentId, final BigDecimal amount, final boolean isInvoiceAdjusted, final Map<UUID, BigDecimal> invoiceItemIdsWithAmounts, final String transactionExternalKey, final InternalCallContext context) throws InvoiceApiException { if (amount.compareTo(BigDecimal.ZERO) <= 0) { throw new InvoiceApiException(ErrorCode.PAYMENT_REFUND_AMOUNT_NEGATIVE_OR_NULL, paymentId, amount); } final InvoicePaymentModelDao refund = dao.createRefund(paymentId, amount, isInvoiceAdjusted, invoiceItemIdsWithAmounts, transactionExternalKey, context); // See https://github.com/killbill/killbill/issues/265 final CallContext callContext = internalCallContextFactory.createCallContext(context); final DefaultInvoice invoice = getInvoiceByIdInternal(refund.getInvoiceId(), context); final UUID accountId = invoice.getAccountId(); final WithAccountLock withAccountLock = new WithAccountLock() { @Override public Iterable<DefaultInvoice> prepareInvoices() throws InvoiceApiException { return ImmutableList.<DefaultInvoice>of(invoice); } }; invoiceApiHelper.dispatchToInvoicePluginsAndInsertItems(accountId, false, withAccountLock, ImmutableList.<PluginProperty>of(), callContext); return new DefaultInvoicePayment(refund); }
private void cancelSubscriptionsIfRequired(final DateTime effectiveDate, final ImmutableAccountData account, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueException { if (nextOverdueState.getOverdueCancellationPolicy() == OverdueCancellationPolicy.NONE) { return; } final CallContext callContext = internalCallContextFactory.createCallContext(context); try { final BillingActionPolicy actionPolicy; switch (nextOverdueState.getOverdueCancellationPolicy()) { case END_OF_TERM: actionPolicy = BillingActionPolicy.END_OF_TERM; break; case IMMEDIATE: actionPolicy = BillingActionPolicy.IMMEDIATE; break; default: throw new IllegalStateException("Unexpected OverdueCancellationPolicy " + nextOverdueState.getOverdueCancellationPolicy()); } final List<Entitlement> toBeCancelled = new LinkedList<Entitlement>(); computeEntitlementsToCancel(account, toBeCancelled, callContext); try { entitlementInternalApi.cancel(toBeCancelled, context.toLocalDate(effectiveDate), actionPolicy, ImmutableList.<PluginProperty>of(), context); } catch (final EntitlementApiException e) { throw new OverdueException(e); } } catch (final EntitlementApiException e) { throw new OverdueException(e); } }
eventSent = onPhaseEvent(subscription, event, fullCatalog, context); } else if (event.getType() == EventType.API_USER && subscription.getCategory() == ProductCategory.BASE) { final CallContext callContext = internalCallContextFactory.createCallContext(context); eventSent = onBasePlanEvent(subscription, event, fullCatalog, callContext); } else if (event.getType() == EventType.BCD_UPDATE) {
@Override public DateTime getDryRunChangePlanEffectiveDate(final SubscriptionBase subscription, final EntitlementSpecifier spec, final DateTime requestedDateWithMs, final BillingActionPolicy requestedPolicy, final InternalCallContext context) throws SubscriptionBaseApiException, CatalogApiException { final TenantContext tenantContext = internalCallContextFactory.createTenantContext(context); final CallContext callContext = internalCallContextFactory.createCallContext(context); // verify the number of subscriptions (of the same kind) allowed per bundle final Catalog catalog = catalogInternalApi.getFullCatalog(true, true, context); final DateTime effectiveDate = (requestedDateWithMs != null) ? DefaultClock.truncateMs(requestedDateWithMs) : null; final DateTime effectiveCatalogDate = effectiveDate != null ? effectiveDate : context.getCreatedDate(); final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(spec.getOverrides(), callContext); final Plan plan = catalog.createOrFindPlan(spec.getPlanPhaseSpecifier(), overridesWithContext, effectiveCatalogDate, subscription.getStartDate()); if (ProductCategory.ADD_ON.toString().equalsIgnoreCase(plan.getProduct().getCategory().toString())) { if (plan.getPlansAllowedInBundle() != -1 && plan.getPlansAllowedInBundle() > 0 && addonUtils.countExistingAddOnsWithSamePlanName(getSubscriptionsForBundle(subscription.getBundleId(), null, catalog, addonUtils, callContext, context), plan.getName()) >= plan.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, plan.getName()); } } return apiService.dryRunChangePlan((DefaultSubscriptionBase) subscription, spec, effectiveDate, requestedPolicy, tenantContext); }