Search in sources :

Example 6 with EntitlementSpecifier

use of org.killbill.billing.entitlement.api.EntitlementSpecifier in project killbill by killbill.

the class TestSubscription method testCreateMultipleSubscriptionsWithAddOns.

@Test(groups = "slow")
public void testCreateMultipleSubscriptionsWithAddOns() throws Exception {
    final LocalDate initialDate = new LocalDate(2015, 10, 1);
    clock.setDay(initialDate);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(1));
    final PlanPhaseSpecifier baseSpec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    final PlanPhaseSpecifier addOnSpec1 = new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    final PlanPhaseSpecifier addOnSpec2 = new PlanPhaseSpecifier("Laser-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    final String externalKeyA = "baseExternalKeyAAA";
    final EntitlementSpecifier baseEntitlementSpecifier = new DefaultEntitlementSpecifier(baseSpec, null);
    final EntitlementSpecifier addOnEntitlementSpecifier1 = new DefaultEntitlementSpecifier(addOnSpec1, null);
    final EntitlementSpecifier addOnEntitlementSpecifier2 = new DefaultEntitlementSpecifier(addOnSpec2, null);
    final List<EntitlementSpecifier> specifierListA = new ArrayList<EntitlementSpecifier>();
    specifierListA.add(baseEntitlementSpecifier);
    specifierListA.add(addOnEntitlementSpecifier1);
    specifierListA.add(addOnEntitlementSpecifier2);
    final String externalKeyB = "baseExternalKeyBBB";
    final List<EntitlementSpecifier> specifierListB = new ArrayList<EntitlementSpecifier>();
    specifierListB.add(baseEntitlementSpecifier);
    specifierListB.add(addOnEntitlementSpecifier1);
    specifierListB.add(addOnEntitlementSpecifier2);
    final List<BaseEntitlementWithAddOnsSpecifier> entitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    final BaseEntitlementWithAddOnsSpecifier cartSpecifierA = new DefaultBaseEntitlementWithAddOnsSpecifier(null, externalKeyA, specifierListA, null, null, false);
    final BaseEntitlementWithAddOnsSpecifier cartSpecifierB = new DefaultBaseEntitlementWithAddOnsSpecifier(null, externalKeyB, specifierListB, null, null, false);
    entitlementWithAddOnsSpecifierList.add(cartSpecifierA);
    entitlementWithAddOnsSpecifierList.add(cartSpecifierB);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final List<Entitlement> allEntitlements = entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), entitlementWithAddOnsSpecifierList, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    checkNoMoreInvoiceToGenerate(account);
    assertNotNull(allEntitlements);
    assertEquals(allEntitlements.size(), 6);
    final Entitlement baseEntitlement = allEntitlements.get(0);
    final Entitlement addOnEntitlement1 = allEntitlements.get(1);
    final Entitlement addOnEntitlement2 = allEntitlements.get(2);
    assertEquals(baseEntitlement.getLastActiveProduct().getName(), "Shotgun");
    assertEquals(baseEntitlement.getLastActiveProductCategory(), ProductCategory.BASE);
    assertEquals(addOnEntitlement1.getLastActiveProduct().getName(), "Telescopic-Scope");
    assertEquals(addOnEntitlement1.getLastActiveProductCategory(), ProductCategory.ADD_ON);
    assertEquals(addOnEntitlement2.getLastActiveProduct().getName(), "Laser-Scope");
    assertEquals(addOnEntitlement2.getLastActiveProductCategory(), ProductCategory.ADD_ON);
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    // ONLY ONE INVOICE
    assertEquals(invoices.size(), 1);
    assertEquals(invoices.get(0).getInvoiceItems().size(), 6);
    final ImmutableList<ExpectedInvoiceItemCheck> toBeChecked = ImmutableList.<ExpectedInvoiceItemCheck>of(// amount=387.05, rate=399.95 -> Telescopic-Scope
    new ExpectedInvoiceItemCheck(initialDate, new LocalDate(2015, 10, 31), InvoiceItemType.RECURRING, new BigDecimal("387.05")), // amount=967.69, rate=999.95 -> Laser-Scope
    new ExpectedInvoiceItemCheck(initialDate, new LocalDate(2015, 10, 31), InvoiceItemType.RECURRING, new BigDecimal("967.69")), // Shotgun
    new ExpectedInvoiceItemCheck(initialDate, null, InvoiceItemType.FIXED, new BigDecimal("0")), // amount=387.05, rate=399.95 -> Telescopic-Scope
    new ExpectedInvoiceItemCheck(initialDate, new LocalDate(2015, 10, 31), InvoiceItemType.RECURRING, new BigDecimal("387.05")), // amount=967.69, rate=999.95 -> Laser-Scope
    new ExpectedInvoiceItemCheck(initialDate, new LocalDate(2015, 10, 31), InvoiceItemType.RECURRING, new BigDecimal("967.69")), // Shotgun
    new ExpectedInvoiceItemCheck(initialDate, null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkInvoice(invoices.get(0).getId(), callContext, toBeChecked);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) Test(org.testng.annotations.Test)

Example 7 with EntitlementSpecifier

use of org.killbill.billing.entitlement.api.EntitlementSpecifier in project killbill by killbill.

the class DefaultSubscriptionInternalApi method countCurrentAddOnsWithSamePlanName.

private int countCurrentAddOnsWithSamePlanName(final Iterable<EntitlementSpecifier> entitlements, final Catalog catalog, final String planName, final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException {
    int countCurrentAddOns = 0;
    for (final EntitlementSpecifier entitlement : entitlements) {
        final PlanPhaseSpecifier spec = entitlement.getPlanPhaseSpecifier();
        final PlanPhasePriceOverridesWithCallContext overridesWithContext = new DefaultPlanPhasePriceOverridesWithCallContext(entitlement.getOverrides(), callContext);
        final Plan plan = catalog.createOrFindPlan(spec, overridesWithContext, effectiveDate);
        if (plan.getName().equalsIgnoreCase(planName) && plan.getProduct().getCategory() != null && ProductCategory.ADD_ON.equals(plan.getProduct().getCategory())) {
            countCurrentAddOns++;
        }
    }
    return countCurrentAddOns;
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) PlanPhasePriceOverridesWithCallContext(org.killbill.billing.catalog.api.PlanPhasePriceOverridesWithCallContext) Plan(org.killbill.billing.catalog.api.Plan)

Example 8 with EntitlementSpecifier

use of org.killbill.billing.entitlement.api.EntitlementSpecifier in project killbill by killbill.

the class SubscriptionResource method buildEntitlementSpecifierList.

private List<EntitlementSpecifier> buildEntitlementSpecifierList(final List<SubscriptionJson> entitlements, final Currency currency) {
    final List<EntitlementSpecifier> entitlementSpecifierList = new ArrayList<EntitlementSpecifier>();
    for (final SubscriptionJson entitlement : entitlements) {
        // verifications
        verifyNonNullOrEmpty(entitlement, "SubscriptionJson body should be specified for each element");
        if (entitlement.getPlanName() == null) {
            verifyNonNullOrEmpty(entitlement.getProductName(), "SubscriptionJson productName needs to be set for each element", entitlement.getProductCategory(), "SubscriptionJson productCategory needs to be set for each element", entitlement.getBillingPeriod(), "SubscriptionJson billingPeriod needs to be set for each element", entitlement.getPriceList(), "SubscriptionJson priceList needs to be set for each element");
        }
        // create the entitlementSpecifier
        final PlanPhaseSpecifier planPhaseSpecifier = entitlement.getPlanName() != null ? new PlanPhaseSpecifier(entitlement.getPlanName(), null) : new PlanPhaseSpecifier(entitlement.getProductName(), BillingPeriod.valueOf(entitlement.getBillingPeriod()), entitlement.getPriceList(), null);
        final List<PlanPhasePriceOverride> overrides = PhasePriceOverrideJson.toPlanPhasePriceOverrides(entitlement.getPriceOverrides(), planPhaseSpecifier, currency);
        EntitlementSpecifier specifier = new EntitlementSpecifier() {

            @Override
            public PlanPhaseSpecifier getPlanPhaseSpecifier() {
                return planPhaseSpecifier;
            }

            @Override
            public List<PlanPhasePriceOverride> getOverrides() {
                return overrides;
            }
        };
        entitlementSpecifierList.add(specifier);
    }
    return entitlementSpecifierList;
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) SubscriptionJson(org.killbill.billing.jaxrs.json.SubscriptionJson) ArrayList(java.util.ArrayList) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 9 with EntitlementSpecifier

use of org.killbill.billing.entitlement.api.EntitlementSpecifier in project killbill by killbill.

the class TestMigrationSubscriptions method testMigrationWithMultipleBundlesAndDifferentDates.

@Test(groups = "slow")
public void testMigrationWithMultipleBundlesAndDifferentDates() throws Exception {
    clock.setDay(new LocalDate(2016, 1, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDateBundle1 = new LocalDate(2015, 12, 20);
    final LocalDate billingMigrationDateBundle1 = new LocalDate(2016, 2, 1);
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDateBundle2 = new LocalDate(2015, 12, 20);
    final LocalDate billingMigrationDateBundle2 = new LocalDate(2016, 3, 1);
    final PlanPhaseSpecifier baseSpec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final PlanPhaseSpecifier addOnSpec1 = new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final String externalKey = "baseExternalKey";
    EntitlementSpecifier baseEntitlementSpecifier = new DefaultEntitlementSpecifier(baseSpec, null);
    EntitlementSpecifier addOnEntitlementSpecifier1 = new DefaultEntitlementSpecifier(addOnSpec1, null);
    final List<EntitlementSpecifier> specifierList = new ArrayList<EntitlementSpecifier>();
    specifierList.add(baseEntitlementSpecifier);
    specifierList.add(addOnEntitlementSpecifier1);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.BLOCK, NextEvent.BLOCK, NextEvent.BLOCK);
    BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifierBundle1 = buildBaseEntitlementWithAddOnsSpecifier(entitlementMigrationDateBundle1, billingMigrationDateBundle1, externalKey, specifierList);
    List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifierBundle2 = buildBaseEntitlementWithAddOnsSpecifier(entitlementMigrationDateBundle2, billingMigrationDateBundle2, externalKey, specifierList);
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifierBundle1);
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifierBundle2);
    final List<Entitlement> baseEntitlements = entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), baseEntitlementWithAddOnsSpecifierList, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Assert.assertEquals(baseEntitlements.get(0).getState(), EntitlementState.ACTIVE);
    Assert.assertEquals(baseEntitlements.get(1).getState(), EntitlementState.ACTIVE);
    // Billing starts straight on EVERGREEN for Bundle 1 after 1 month
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
    // Billing starts straight on EVERGREEN for Bundle 2 after 2 months
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
    // Next month we should still get one single invoice and payment / invoice payment
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) AccountData(org.killbill.billing.account.api.AccountData) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Test(org.testng.annotations.Test)

Example 10 with EntitlementSpecifier

use of org.killbill.billing.entitlement.api.EntitlementSpecifier in project killbill by killbill.

the class TestMigrationSubscriptions method testSimpleMigrationBundle.

//
// Scenario: On 2016-1-1, we decide to migrate a bundle (one BP and one AO). We migrate straight to EVERGREEN phase. The scenario is very similar to previous one
//           but with an additional AO (by using the createBaseEntitlementWithAddOns api).
//
//  Note that while convenient to migrate a bundle at once (BP + AOS), one could do several calls for each subscription. The advantage of the later approach is that
//  the granularity in terms of alignments for when things start and which phase are skipped can be better controlled.
//
@Test(groups = "slow")
public void testSimpleMigrationBundle() throws Exception {
    clock.setDay(new LocalDate(2016, 1, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    // We set both entitlement and billing date with desired value
    final LocalDate entitlementMigrationDate = new LocalDate(2015, 12, 20);
    final LocalDate billingMigrationDate = new LocalDate(2016, 2, 1);
    final PlanPhaseSpecifier baseSpec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final PlanPhaseSpecifier addOnSpec1 = new PlanPhaseSpecifier("Telescopic-Scope", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, PhaseType.EVERGREEN);
    final String externalKey = "baseExternalKey";
    EntitlementSpecifier baseEntitlementSpecifier = new DefaultEntitlementSpecifier(baseSpec, null);
    EntitlementSpecifier addOnEntitlementSpecifier1 = new DefaultEntitlementSpecifier(addOnSpec1, null);
    final List<EntitlementSpecifier> specifierList = new ArrayList<EntitlementSpecifier>();
    specifierList.add(baseEntitlementSpecifier);
    specifierList.add(addOnEntitlementSpecifier1);
    busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.BLOCK);
    BaseEntitlementWithAddOnsSpecifier baseEntitlementWithAddOnsSpecifier = buildBaseEntitlementWithAddOnsSpecifier(entitlementMigrationDate, billingMigrationDate, externalKey, specifierList);
    List<BaseEntitlementWithAddOnsSpecifier> baseEntitlementWithAddOnsSpecifierList = new ArrayList<BaseEntitlementWithAddOnsSpecifier>();
    baseEntitlementWithAddOnsSpecifierList.add(baseEntitlementWithAddOnsSpecifier);
    final List<Entitlement> baseEntitlements = entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), baseEntitlementWithAddOnsSpecifierList, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    Assert.assertEquals(baseEntitlements.get(0).getState(), EntitlementState.ACTIVE);
    // Billing starts straight on EVERGREEN
    clock.addMonths(1);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.CREATE, NextEvent.INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    assertListenerStatus();
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) AccountData(org.killbill.billing.account.api.AccountData) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) Entitlement(org.killbill.billing.entitlement.api.Entitlement) Test(org.testng.annotations.Test)

Aggregations

EntitlementSpecifier (org.killbill.billing.entitlement.api.EntitlementSpecifier)11 ArrayList (java.util.ArrayList)9 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)8 LocalDate (org.joda.time.LocalDate)7 Account (org.killbill.billing.account.api.Account)7 BaseEntitlementWithAddOnsSpecifier (org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier)7 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)5 Entitlement (org.killbill.billing.entitlement.api.Entitlement)5 Test (org.testng.annotations.Test)5 DefaultBaseEntitlementWithAddOnsSpecifier (org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier)3 SubscriptionJson (org.killbill.billing.jaxrs.json.SubscriptionJson)3 Predicate (com.google.common.base.Predicate)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 List (java.util.List)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 AccountData (org.killbill.billing.account.api.AccountData)2