Search in sources :

Example 61 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultEntitlement method testCancelWithEntitlementDateInFuture.

@Test(groups = "slow")
public void testCancelWithEntitlementDateInFuture() throws AccountApiException, EntitlementApiException {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    final Account account = createAccount(getAccountData(7));
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    // Create entitlement and check each field
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
    assertEquals(entitlement.getState(), EntitlementState.ACTIVE);
    clock.addDays(5);
    final LocalDate cancelDate = new LocalDate(clock.getUTCToday().plusDays(1));
    entitlement.cancelEntitlementWithDate(cancelDate, true, ImmutableList.<PluginProperty>of(), callContext);
    final Entitlement entitlement2 = entitlementApi.getEntitlementForId(entitlement.getId(), callContext);
    assertEquals(entitlement2.getState(), EntitlementState.ACTIVE);
    assertEquals(entitlement2.getEffectiveEndDate(), cancelDate);
    clock.addDays(1);
    testListener.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK);
    assertListenerStatus();
    final Entitlement entitlement3 = entitlementApi.getEntitlementForId(entitlement.getId(), callContext);
    assertEquals(entitlement3.getState(), EntitlementState.CANCELLED);
    assertEquals(entitlement3.getEffectiveEndDate(), cancelDate);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 62 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultEntitlement method testUncancel.

@Test(groups = "slow")
public void testUncancel() throws AccountApiException, EntitlementApiException {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    final Account account = createAccount(getAccountData(7));
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
    // Create entitlement and check each field
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    final UUID entitlementId = entitlementApi.createBaseEntitlement(account.getId(), new DefaultEntitlementSpecifier(spec), account.getExternalKey(), null, null, false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final Entitlement entitlement = entitlementApi.getEntitlementForId(entitlementId, callContext);
    assertEquals(entitlement.getState(), EntitlementState.ACTIVE);
    clock.addDays(5);
    final LocalDate cancelDate = new LocalDate(clock.getUTCToday().plusDays(1));
    entitlement.cancelEntitlementWithDate(cancelDate, true, ImmutableList.<PluginProperty>of(), callContext);
    final Entitlement entitlement2 = entitlementApi.getEntitlementForId(entitlement.getId(), callContext);
    assertEquals(entitlement2.getState(), EntitlementState.ACTIVE);
    assertEquals(entitlement2.getEffectiveEndDate(), cancelDate);
    testListener.pushExpectedEvents(NextEvent.UNCANCEL);
    entitlement2.uncancelEntitlement(ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    clock.addDays(1);
    final Entitlement entitlement3 = entitlementApi.getEntitlementForId(entitlement.getId(), callContext);
    assertEquals(entitlement3.getState(), EntitlementState.ACTIVE);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 63 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class DefaultInternalBillingApi method addBillingEventsForSubscription.

private void addBillingEventsForSubscription(final ImmutableAccountData account, @Nullable final List<SubscriptionBase> subscriptions, final SubscriptionBase baseSubscription, final int currentAccountBCD, final InternalCallContext context, final DefaultBillingEventSet result, final Set<UUID> skipSubscriptionsSet, final VersionedCatalog catalog) throws SubscriptionBaseApiException, CatalogApiException {
    if (subscriptions == null) {
        return;
    }
    final Map<UUID, Integer> bcdCache = new HashMap<UUID, Integer>();
    for (final SubscriptionBase subscription : subscriptions) {
        // TODO Can we batch those ?
        final List<SubscriptionBillingEvent> billingTransitions = subscriptionApi.getSubscriptionBillingEvents(catalog, subscription, context);
        if (billingTransitions.isEmpty() || (billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.CREATE && billingTransitions.get(0).getType() != SubscriptionBaseTransitionType.TRANSFER)) {
            log.warn("Skipping billing events for subscription " + subscription.getId() + ": Does not start with a valid CREATE transition");
            skipSubscriptionsSet.add(subscription.getId());
            return;
        }
        Integer overridenBCD = null;
        int bcdLocal = 0;
        BillingAlignment alignment = null;
        for (final SubscriptionBillingEvent transition : billingTransitions) {
            if (transition.getType() != SubscriptionBaseTransitionType.CANCEL) {
                final PlanPhaseSpecifier spec = new PlanPhaseSpecifier(transition.getPlan().getName(), transition.getPlanPhase().getPhaseType());
                alignment = subscription.getBillingAlignment(spec, transition.getEffectiveDate(), catalog);
                // 
                // A BCD_CHANGE transition defines a new billCycleDayLocal for the subscription and this overrides whatever computation
                // occurs below (which is based on billing alignment policy). Also multiple of those BCD_CHANGE transitions could occur,
                // to define different intervals with different billing cycle days.
                // 
                overridenBCD = transition.getBcdLocal() != null ? transition.getBcdLocal() : overridenBCD;
                bcdLocal = overridenBCD != null ? overridenBCD : calculateBcdForTransition(alignment, bcdCache, baseSubscription, subscription, currentAccountBCD, context);
            }
            final BillingEvent event = new DefaultBillingEvent(transition, subscription, bcdLocal, alignment, account.getCurrency());
            result.add(event);
        }
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) HashMap(java.util.HashMap) BillingAlignment(org.killbill.billing.catalog.api.BillingAlignment) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent) BillingEvent(org.killbill.billing.junction.BillingEvent) UUID(java.util.UUID) SubscriptionBillingEvent(org.killbill.billing.subscription.api.user.SubscriptionBillingEvent)

Example 64 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultInternalBillingApi method testRecurringInArrear.

@Test(groups = "slow")
public void testRecurringInArrear() throws Exception {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    // Account with no BCD
    final Account account = createAccount(getAccountData(0));
    Assert.assertEquals(account.getBillCycleDayLocal(), (Integer) 0);
    // Create base entitlement
    final String bundleKey = UUID.randomUUID().toString();
    final EntitlementSpecifier entitlementSpecifierBase = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Cannon", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null));
    final BaseEntitlementWithAddOnsSpecifier specifier = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey, ImmutableList.of(entitlementSpecifierBase), null, null, false);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK);
    entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), ImmutableList.of(specifier), false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final List<Entitlement> entitlements = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlements.size(), 1);
    Assert.assertEquals(entitlements.get(0).getLastActiveProduct().getName(), "Cannon");
    Assert.assertNull(entitlements.get(0).getBillCycleDayLocal());
    // Account still has no BCD
    final Account accountNoBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountNoBCD.getBillCycleDayLocal(), (Integer) 0);
    List<BillingEvent> events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 1);
    Assert.assertEquals(events.get(0).getBillCycleDayLocal(), 7);
    // Verify BCD
    final Account accountWithBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountWithBCD.getBillCycleDayLocal(), (Integer) 7);
    // Verify GET
    final List<Entitlement> entitlementsUpdated = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlementsUpdated.size(), 1);
    Assert.assertEquals(entitlementsUpdated.get(0).getLastActiveProduct().getName(), "Cannon");
    Assert.assertEquals(entitlementsUpdated.get(0).getBillCycleDayLocal(), (Integer) 7);
    events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 1);
    Assert.assertEquals(events.get(0).getBillCycleDayLocal(), 7);
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) BillingEvent(org.killbill.billing.junction.BillingEvent) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) Test(org.testng.annotations.Test)

Example 65 with PlanPhaseSpecifier

use of org.killbill.billing.catalog.api.PlanPhaseSpecifier in project killbill by killbill.

the class TestDefaultInternalBillingApi method testBCDUpdateMultipleSubscriptionsAccountAndSubscriptionAligned.

@Test(groups = "slow")
public void testBCDUpdateMultipleSubscriptionsAccountAndSubscriptionAligned() throws Exception {
    final LocalDate initialDate = new LocalDate(2013, 8, 7);
    clock.setDay(initialDate);
    // Account with no BCD
    final Account account = createAccount(getAccountData(0));
    Assert.assertEquals(account.getBillCycleDayLocal(), (Integer) 0);
    // Create 2 BASE entitlements
    final String bundleKey1 = UUID.randomUUID().toString();
    final String bundleKey2 = UUID.randomUUID().toString();
    final EntitlementSpecifier entitlementSpecifierBase1 = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Pistol", BillingPeriod.ANNUAL, "gunclubDiscountNoTrial", null));
    final EntitlementSpecifier entitlementSpecifierBase2 = new DefaultEntitlementSpecifier(new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null));
    final BaseEntitlementWithAddOnsSpecifier specifier1 = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey1, ImmutableList.of(entitlementSpecifierBase1), null, null, false);
    final BaseEntitlementWithAddOnsSpecifier specifier2 = new DefaultBaseEntitlementWithAddOnsSpecifier(null, bundleKey2, ImmutableList.of(entitlementSpecifierBase2), null, null, false);
    testListener.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.CREATE, NextEvent.BLOCK);
    entitlementApi.createBaseEntitlementsWithAddOns(account.getId(), ImmutableList.of(specifier1, specifier2), false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    final List<Entitlement> entitlements = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlements.size(), 2);
    for (final Entitlement entitlement : entitlements) {
        if ("pistol-annual-gunclub-discount-notrial".equals(entitlement.getLastActivePlan().getName())) {
            // SUBSCRIPTION aligned
            Assert.assertEquals(entitlement.getBillCycleDayLocal(), (Integer) 7);
        } else {
            // ACCOUNT aligned
            Assert.assertNull(entitlement.getBillCycleDayLocal());
        }
    }
    // Account still has no BCD
    final Account accountNoBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountNoBCD.getBillCycleDayLocal(), (Integer) 0);
    List<BillingEvent> events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 4);
    for (final BillingEvent billingEvent : events) {
        if ("pistol-annual-gunclub-discount-notrial".equals(billingEvent.getPlan().getName())) {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 7);
        } else {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 6);
        }
    }
    // Verify BCD
    final Account accountWithBCD = accountApi.getAccountById(account.getId(), callContext);
    Assert.assertEquals(accountWithBCD.getBillCycleDayLocal(), (Integer) 6);
    // Verify GET
    final List<Entitlement> entitlementsUpdated = entitlementApi.getAllEntitlementsForAccountId(account.getId(), callContext);
    Assert.assertEquals(entitlementsUpdated.size(), 2);
    for (final Entitlement entitlement : entitlementsUpdated) {
        if ("pistol-annual-gunclub-discount-notrial".equals(entitlement.getLastActivePlan().getName())) {
            Assert.assertEquals(entitlement.getBillCycleDayLocal(), (Integer) 7);
        } else {
            Assert.assertEquals(entitlement.getBillCycleDayLocal(), (Integer) 6);
        }
    }
    events = ImmutableList.<BillingEvent>copyOf(billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), null, null, internalCallContext));
    Assert.assertEquals(events.size(), 4);
    for (final BillingEvent billingEvent : events) {
        if ("pistol-annual-gunclub-discount-notrial".equals(billingEvent.getPlan().getName())) {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 7);
        } else {
            Assert.assertEquals(billingEvent.getBillCycleDayLocal(), 6);
        }
    }
}
Also used : EntitlementSpecifier(org.killbill.billing.entitlement.api.EntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) DefaultEntitlementSpecifier(org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier) PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) BaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.BaseEntitlementWithAddOnsSpecifier) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) BillingEvent(org.killbill.billing.junction.BillingEvent) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DefaultBaseEntitlementWithAddOnsSpecifier(org.killbill.billing.entitlement.api.DefaultBaseEntitlementWithAddOnsSpecifier) Test(org.testng.annotations.Test)

Aggregations

PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)220 Test (org.testng.annotations.Test)181 LocalDate (org.joda.time.LocalDate)153 Account (org.killbill.billing.account.api.Account)144 DefaultEntitlementSpecifier (org.killbill.billing.entitlement.api.DefaultEntitlementSpecifier)140 UUID (java.util.UUID)129 DateTime (org.joda.time.DateTime)79 Entitlement (org.killbill.billing.entitlement.api.Entitlement)68 BigDecimal (java.math.BigDecimal)63 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)61 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)59 Invoice (org.killbill.billing.invoice.api.Invoice)42 ArrayList (java.util.ArrayList)30 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)29 AccountData (org.killbill.billing.account.api.AccountData)23 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)22 DefaultBlockingState (org.killbill.billing.junction.DefaultBlockingState)19 EntitlementSpecifier (org.killbill.billing.entitlement.api.EntitlementSpecifier)18 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)17 Plan (org.killbill.billing.catalog.api.Plan)16