Search in sources :

Example 6 with CatalogApiException

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);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) HashMap(java.util.HashMap) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) DateTime(org.joda.time.DateTime) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 7 with CatalogApiException

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);
    }
}
Also used : SubscriptionBaseError(org.killbill.billing.subscription.exceptions.SubscriptionBaseError) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Product(org.killbill.billing.catalog.api.Product) Plan(org.killbill.billing.catalog.api.Plan)

Example 8 with CatalogApiException

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);
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) ValidationException(org.killbill.xmlloader.ValidationException) JAXBException(javax.xml.bind.JAXBException) URI(java.net.URI) URL(java.net.URL) TransformerException(javax.xml.transform.TransformerException) InvalidConfigException(org.killbill.billing.catalog.api.InvalidConfigException) URISyntaxException(java.net.URISyntaxException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) ValidationException(org.killbill.xmlloader.ValidationException) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PriceOverride(org.killbill.billing.catalog.override.PriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)

Example 9 with CatalogApiException

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);
    }
}
Also used : CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) CatalogService(org.killbill.billing.catalog.api.CatalogService) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) Catalog(org.killbill.billing.catalog.api.Catalog)

Example 10 with CatalogApiException

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);
}
Also used : CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) Product(org.killbill.billing.catalog.api.Product)

Aggregations

CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)61 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)22 DateTime (org.joda.time.DateTime)20 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)14 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)12 Plan (org.killbill.billing.catalog.api.Plan)12 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)11 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)9 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)8 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)8 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)8 LinkedList (java.util.LinkedList)7 Catalog (org.killbill.billing.catalog.api.Catalog)7 Test (org.testng.annotations.Test)7 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)6 PlanSpecifier (org.killbill.billing.catalog.api.PlanSpecifier)6 URI (java.net.URI)5 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)5 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)5