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);
}
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);
}
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);
}
}
}
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);
}
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);
}
}
}
Aggregations