use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class DefaultSubscriptionBaseApiService method cancelWithPolicyNoValidation.
@Override
public boolean cancelWithPolicyNoValidation(final Iterable<DefaultSubscriptionBase> subscriptions, final BillingActionPolicy policy, final DateTimeZone accountTimeZone, final int accountBillCycleDayLocal, final InternalCallContext context) throws SubscriptionBaseApiException {
final Map<DefaultSubscriptionBase, DateTime> subscriptionsWithEffectiveDate = new HashMap<DefaultSubscriptionBase, DateTime>();
final DateTime now = clock.getUTCNow();
try {
for (final DefaultSubscriptionBase subscription : subscriptions) {
final BillingAlignment billingAlignment = (subscription.getState() == EntitlementState.PENDING ? null : catalogService.getFullCatalog(true, true, context).billingAlignment(new PlanPhaseSpecifier(subscription.getLastActivePlan().getName(), subscription.getLastActivePhase().getPhaseType()), clock.getUTCNow()));
final DateTime effectiveDate = subscription.getPlanChangeEffectiveDate(policy, billingAlignment, accountTimeZone, accountBillCycleDayLocal, context);
subscriptionsWithEffectiveDate.put(subscription, effectiveDate);
}
} catch (final CatalogApiException e) {
throw new SubscriptionBaseApiException(e);
}
return doCancelPlan(subscriptionsWithEffectiveDate, now, context);
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class AddonUtils method isAddonIncludedFromPlanName.
public boolean isAddonIncludedFromPlanName(final String basePlanName, final Plan targetAddOnPlan, final DateTime requestedDate, final InternalTenantContext context) {
try {
final Plan plan = catalogService.getFullCatalog(true, true, context).findPlan(basePlanName, requestedDate);
final Product product = plan.getProduct();
return isAddonIncluded(product, targetAddOnPlan);
} catch (CatalogApiException e) {
throw new SubscriptionBaseError(e);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class VersionedCatalogLoader method loadDefaultCatalog.
@Override
public VersionedCatalog loadDefaultCatalog(final String uriString) throws CatalogApiException {
try {
final List<URI> xmlURIs;
if (uriString.endsWith(XML_EXTENSION)) {
// Assume its an xml file
xmlURIs = new ArrayList<URI>();
xmlURIs.add(new URI(uriString));
} else {
// Assume its a directory
final URL url = getURLFromString(uriString);
final String directoryContents = UriAccessor.accessUriAsString(uriString);
xmlURIs = findXmlReferences(directoryContents, url);
}
final VersionedCatalog result = new VersionedCatalog(clock);
for (final URI u : xmlURIs) {
final StandaloneCatalog catalog = XMLLoader.getObjectFromUri(u, StandaloneCatalog.class);
result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID, internalCallContextFactory));
}
// Perform initialization and validation for VersionedCatalog
XMLLoader.initializeAndValidate(new URI(uriString), result);
return result;
} catch (final ValidationException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (final JAXBException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (IllegalArgumentException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (Exception e) {
logger.warn("Failed to load default catalog", e);
throw new IllegalStateException(e);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class MockCatalogModule method configure.
@Override
protected void configure() {
final Catalog catalog = Mockito.mock(Catalog.class);
final CatalogService catalogService = Mockito.mock(CatalogService.class);
try {
Mockito.when(catalogService.getCurrentCatalog(Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(InternalCallContext.class))).thenReturn(new MockCatalog());
Mockito.when(catalogService.getFullCatalog(Mockito.any(Boolean.class), Mockito.any(Boolean.class), Mockito.any(InternalCallContext.class))).thenReturn(catalog);
bind(CatalogService.class).toInstance(catalogService);
} catch (CatalogApiException e) {
throw new RuntimeException(e);
}
}
use of org.killbill.billing.catalog.api.CatalogApiException in project killbill by killbill.
the class CatalogUpdater method addSimplePlanDescriptor.
public void addSimplePlanDescriptor(final SimplePlanDescriptor desc) throws CatalogApiException {
// We need at least a planId
if (desc == null || desc.getPlanId() == null) {
throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc);
}
DefaultPlan plan = (DefaultPlan) getExistingPlan(desc.getPlanId());
if (plan == null && desc.getProductName() == null) {
throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc);
}
validateNewPlanDescriptor(desc);
DefaultProduct product = plan != null ? (DefaultProduct) plan.getProduct() : (DefaultProduct) getExistingProduct(desc.getProductName());
if (product == null) {
product = new DefaultProduct();
product.setName(desc.getProductName());
product.setCatagory(desc.getProductCategory());
product.initialize(catalog, DEFAULT_URI);
catalog.addProduct(product);
}
if (plan == null) {
plan = new DefaultPlan();
plan.setName(desc.getPlanId());
plan.setPriceListName(PriceListSet.DEFAULT_PRICELIST_NAME);
plan.setProduct(product);
if (desc.getTrialLength() > 0 && desc.getTrialTimeUnit() != TimeUnit.UNLIMITED) {
final DefaultPlanPhase trialPhase = new DefaultPlanPhase();
trialPhase.setPhaseType(PhaseType.TRIAL);
trialPhase.setDuration(new DefaultDuration().setUnit(desc.getTrialTimeUnit()).setNumber(desc.getTrialLength()));
trialPhase.setFixed(new DefaultFixed().setFixedPrice(new DefaultInternationalPrice().setPrices(new DefaultPrice[] { new DefaultPrice().setCurrency(desc.getCurrency()).setValue(BigDecimal.ZERO) })));
plan.setInitialPhases(new DefaultPlanPhase[] { trialPhase });
}
catalog.addPlan(plan);
} else {
validateExistingPlan(plan, desc);
}
//
if (!isCurrencySupported(desc.getCurrency())) {
catalog.addCurrency(desc.getCurrency());
// Reset the fixed price to null so the isZero() logic goes through new currencies and set the zero price for all
if (plan.getInitialPhases().length == 1) {
((DefaultInternationalPrice) plan.getInitialPhases()[0].getFixed().getPrice()).setPrices(null);
}
}
DefaultPlanPhase evergreenPhase = plan.getFinalPhase();
if (evergreenPhase == null) {
evergreenPhase = new DefaultPlanPhase();
evergreenPhase.setPhaseType(PhaseType.EVERGREEN);
evergreenPhase.setDuration(new DefaultDuration().setUnit(TimeUnit.UNLIMITED));
plan.setFinalPhase(evergreenPhase);
}
DefaultRecurring recurring = (DefaultRecurring) evergreenPhase.getRecurring();
if (recurring == null) {
recurring = new DefaultRecurring();
recurring.setBillingPeriod(desc.getBillingPeriod());
recurring.setRecurringPrice(new DefaultInternationalPrice().setPrices(new DefaultPrice[0]));
evergreenPhase.setRecurring(recurring);
}
if (!isPriceForCurrencyExists(recurring.getRecurringPrice(), desc.getCurrency())) {
catalog.addRecurringPriceToPlan(recurring.getRecurringPrice(), new DefaultPrice().setCurrency(desc.getCurrency()).setValue(desc.getAmount()));
}
if (desc.getProductCategory() == ProductCategory.ADD_ON) {
for (final String bp : desc.getAvailableBaseProducts()) {
final Product targetBasePlan = getExistingProduct(bp);
boolean found = false;
for (Product cur : targetBasePlan.getAvailable()) {
if (cur.getName().equals(product.getName())) {
found = true;
break;
}
}
if (!found) {
catalog.addProductAvailableAO(getExistingProduct(bp), product);
}
}
}
// Reinit catalog
catalog.initialize(catalog, DEFAULT_URI);
}
Aggregations