use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class TestUserApiCreate method testCreateWithRequestedDate.
@Test(groups = "slow")
public void testCreateWithRequestedDate() throws SubscriptionBaseApiException {
final DateTime init = clock.getUTCNow();
final DateTime requestedDate = init.minusYears(1);
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.MONTHLY;
final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.CREATE);
final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) subscriptionInternalApi.createSubscription(bundle.getId(), testUtil.getProductSpecifier(productName, planSetName, term, null), null, requestedDate, false, internalCallContext);
assertNotNull(subscription);
//
// In addition to Alignment phase we also test SubscriptionBaseTransition eventIds and created dates.
// Keep tracks of row events to compare with ids and created dates returned by SubscriptionBaseTransition later.
//
final List<SubscriptionBaseEvent> events = subscription.getEvents();
Assert.assertEquals(events.size(), 2);
final SubscriptionBaseEvent trialEvent = events.get(0);
final SubscriptionBaseEvent phaseEvent = events.get(1);
assertEquals(subscription.getBundleId(), bundle.getId());
assertEquals(subscription.getStartDate(), requestedDate);
assertListenerStatus();
final SubscriptionBaseTransition transition = subscription.getPreviousTransition();
assertEquals(transition.getPreviousEventId(), trialEvent.getId());
assertEquals(transition.getNextEventId(), phaseEvent.getId());
assertEquals(transition.getPreviousEventCreatedDate().compareTo(trialEvent.getCreatedDate()), 0);
assertEquals(transition.getNextEventCreatedDate().compareTo(phaseEvent.getCreatedDate()), 0);
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class BlockingStateOrdering method toSubscriptionEvent.
private SubscriptionEvent toSubscriptionEvent(@Nullable final SubscriptionEvent prev, @Nullable final SubscriptionEvent next, final UUID entitlementId, final BlockingState in, final SubscriptionEventType eventType, final InternalTenantContext internalTenantContext) {
final Product prevProduct;
final Plan prevPlan;
final PlanPhase prevPlanPhase;
final PriceList prevPriceList;
final BillingPeriod prevBillingPeriod;
// Enforce prev = null for start events
if (prev == null || SubscriptionEventType.START_ENTITLEMENT.equals(eventType) || SubscriptionEventType.START_BILLING.equals(eventType)) {
prevProduct = null;
prevPlan = null;
prevPlanPhase = null;
prevPriceList = null;
prevBillingPeriod = null;
} else {
// We look for the next for the 'prev' meaning we we are headed to, but if this is null -- for example on cancellation we get the prev which gives the correct state.
prevProduct = (prev.getNextProduct() != null ? prev.getNextProduct() : prev.getPrevProduct());
prevPlan = (prev.getNextPlan() != null ? prev.getNextPlan() : prev.getPrevPlan());
prevPlanPhase = (prev.getNextPhase() != null ? prev.getNextPhase() : prev.getPrevPhase());
prevPriceList = (prev.getNextPriceList() != null ? prev.getNextPriceList() : prev.getPrevPriceList());
prevBillingPeriod = (prev.getNextBillingPeriod() != null ? prev.getNextBillingPeriod() : prev.getPrevBillingPeriod());
}
final Product nextProduct;
final Plan nextPlan;
final PlanPhase nextPlanPhase;
final PriceList nextPriceList;
final BillingPeriod nextBillingPeriod;
if (SubscriptionEventType.PAUSE_ENTITLEMENT.equals(eventType) || SubscriptionEventType.PAUSE_BILLING.equals(eventType) || SubscriptionEventType.RESUME_ENTITLEMENT.equals(eventType) || SubscriptionEventType.RESUME_BILLING.equals(eventType) || (SubscriptionEventType.SERVICE_STATE_CHANGE.equals(eventType) && (prev == null || (!SubscriptionEventType.STOP_ENTITLEMENT.equals(prev.getSubscriptionEventType()) && !SubscriptionEventType.STOP_BILLING.equals(prev.getSubscriptionEventType()))))) {
// Enforce next = prev for pause/resume events as well as service changes
nextProduct = prevProduct;
nextPlan = prevPlan;
nextPlanPhase = prevPlanPhase;
nextPriceList = prevPriceList;
nextBillingPeriod = prevBillingPeriod;
} else if (next == null) {
// Enforce next = null for stop events
if (prev == null || SubscriptionEventType.STOP_ENTITLEMENT.equals(eventType) || SubscriptionEventType.STOP_BILLING.equals(eventType)) {
nextProduct = null;
nextPlan = null;
nextPlanPhase = null;
nextPriceList = null;
nextBillingPeriod = null;
} else {
nextProduct = prev.getNextProduct();
nextPlan = prev.getNextPlan();
nextPlanPhase = prev.getNextPhase();
nextPriceList = prev.getNextPriceList();
nextBillingPeriod = prev.getNextBillingPeriod();
}
} else if (prev != null && (SubscriptionEventType.START_ENTITLEMENT.equals(eventType) || SubscriptionEventType.START_BILLING.equals(eventType))) {
// For start events, next is actually the prev (e.g. the trial, not the phase)
nextProduct = prev.getNextProduct();
nextPlan = prev.getNextPlan();
nextPlanPhase = prev.getNextPhase();
nextPriceList = prev.getNextPriceList();
nextBillingPeriod = prev.getNextBillingPeriod();
} else {
nextProduct = next.getNextProduct();
nextPlan = next.getNextPlan();
nextPlanPhase = next.getNextPhase();
nextPriceList = next.getNextPriceList();
nextBillingPeriod = next.getNextBillingPeriod();
}
// See https://github.com/killbill/killbill/issues/135
final String serviceName = getRealServiceNameForEntitlementOrExternalServiceName(in.getService(), eventType);
return new DefaultSubscriptionEvent(in.getId(), entitlementId, in.getEffectiveDate(), eventType, in.isBlockEntitlement(), in.isBlockBilling(), serviceName, in.getStateName(), prevProduct, prevPlan, prevPlanPhase, prevPriceList, prevBillingPeriod, nextProduct, nextPlan, nextPlanPhase, nextPriceList, nextBillingPeriod, in.getCreatedDate(), internalTenantContext);
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class EventsStreamBuilder method createPlanPhaseSpecifier.
private PlanPhaseSpecifier createPlanPhaseSpecifier(final SubscriptionBase subscription) {
final String lastActiveProductName;
final BillingPeriod billingPeriod;
final ProductCategory productCategory;
final String priceListName;
final PhaseType phaseType;
if (subscription.getState() == EntitlementState.PENDING) {
final SubscriptionBaseTransition transition = subscription.getPendingTransition();
final Product pendingProduct = transition.getNextPlan().getProduct();
lastActiveProductName = pendingProduct.getName();
productCategory = pendingProduct.getCategory();
final PlanPhase pendingPlanPhase = transition.getNextPhase();
billingPeriod = pendingPlanPhase.getRecurring() != null ? pendingPlanPhase.getRecurring().getBillingPeriod() : BillingPeriod.NO_BILLING_PERIOD;
priceListName = transition.getNextPriceList().getName();
phaseType = transition.getNextPhase().getPhaseType();
} else {
final Product lastActiveProduct = subscription.getLastActiveProduct();
lastActiveProductName = lastActiveProduct.getName();
productCategory = lastActiveProduct.getCategory();
final PlanPhase lastActivePlanPhase = subscription.getLastActivePhase();
billingPeriod = lastActivePlanPhase.getRecurring() != null ? lastActivePlanPhase.getRecurring().getBillingPeriod() : BillingPeriod.NO_BILLING_PERIOD;
priceListName = subscription.getLastActivePlan().getPriceListName();
phaseType = subscription.getLastActivePhase().getPhaseType();
}
return new PlanPhaseSpecifier(lastActiveProductName, billingPeriod, priceListName, phaseType);
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class TestEntitlement method testEntitlementUncancel.
@Test(groups = "slow", description = "Can cancel and uncancel a subscription")
public void testEntitlementUncancel() throws Exception {
final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account accountJson = createAccountWithDefaultPaymentMethod();
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.MONTHLY;
final Subscription entitlementJson = createEntitlement(accountJson.getAccountId(), "99999", productName, ProductCategory.BASE, term, true);
// Retrieves with GET
Subscription objFromJson = killBillClient.getSubscription(entitlementJson.getSubscriptionId(), requestOptions);
Assert.assertEquals(objFromJson.getPriceOverrides().size(), 2);
Assert.assertEquals(objFromJson.getPriceOverrides().get(0).getPhaseName(), "shotgun-monthly-trial");
Assert.assertEquals(objFromJson.getPriceOverrides().get(0).getFixedPrice(), BigDecimal.ZERO);
Assert.assertNull(objFromJson.getPriceOverrides().get(0).getRecurringPrice());
Assert.assertEquals(objFromJson.getPriceOverrides().get(1).getPhaseName(), "shotgun-monthly-evergreen");
Assert.assertNull(objFromJson.getPriceOverrides().get(1).getFixedPrice());
Assert.assertEquals(objFromJson.getPriceOverrides().get(1).getRecurringPrice(), new BigDecimal("249.95"));
// Equality in java client is not correctly implemented so manually check PriceOverrides section and then reset before equality
objFromJson.setPriceOverrides(null);
entitlementJson.setPriceOverrides(null);
Assert.assertTrue(objFromJson.equals(entitlementJson));
// MOVE AFTER TRIAL
final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31));
clock.addDeltaFromReality(it.toDurationMillis());
crappyWaitForLackOfProperSynchonization();
// Cancel EOT
killBillClient.cancelSubscription(entitlementJson.getSubscriptionId(), EntitlementActionPolicy.END_OF_TERM, BillingActionPolicy.END_OF_TERM, CALL_COMPLETION_TIMEOUT_SEC, requestOptions);
// Retrieves to check EndDate
objFromJson = killBillClient.getSubscription(entitlementJson.getSubscriptionId(), requestOptions);
assertNotNull(objFromJson.getCancelledDate());
Assert.assertEquals(objFromJson.getPriceOverrides().size(), 2);
Assert.assertEquals(objFromJson.getPriceOverrides().get(0).getPhaseName(), "shotgun-monthly-trial");
Assert.assertEquals(objFromJson.getPriceOverrides().get(0).getFixedPrice(), BigDecimal.ZERO);
Assert.assertNull(objFromJson.getPriceOverrides().get(0).getRecurringPrice());
Assert.assertEquals(objFromJson.getPriceOverrides().get(1).getPhaseName(), "shotgun-monthly-evergreen");
Assert.assertNull(objFromJson.getPriceOverrides().get(1).getFixedPrice());
Assert.assertEquals(objFromJson.getPriceOverrides().get(1).getRecurringPrice(), new BigDecimal("249.95"));
// Uncancel
killBillClient.uncancelSubscription(entitlementJson.getSubscriptionId(), requestOptions);
objFromJson = killBillClient.getSubscription(entitlementJson.getSubscriptionId());
assertNull(objFromJson.getCancelledDate());
Assert.assertEquals(objFromJson.getPriceOverrides().size(), 2);
Assert.assertEquals(objFromJson.getPriceOverrides().get(0).getPhaseName(), "shotgun-monthly-trial");
Assert.assertEquals(objFromJson.getPriceOverrides().get(0).getFixedPrice(), BigDecimal.ZERO);
Assert.assertNull(objFromJson.getPriceOverrides().get(0).getRecurringPrice());
Assert.assertEquals(objFromJson.getPriceOverrides().get(1).getPhaseName(), "shotgun-monthly-evergreen");
Assert.assertNull(objFromJson.getPriceOverrides().get(1).getFixedPrice());
Assert.assertEquals(objFromJson.getPriceOverrides().get(1).getRecurringPrice(), new BigDecimal("249.95"));
}
use of org.killbill.billing.catalog.api.BillingPeriod in project killbill by killbill.
the class TestEntitlement method testOverridePolicy.
@Test(groups = "slow", description = "Can override billing policy on change")
public void testOverridePolicy() throws Exception {
final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account accountJson = createAccountWithDefaultPaymentMethod();
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.ANNUAL;
final Subscription subscriptionJson = createEntitlement(accountJson.getAccountId(), "99999", productName, ProductCategory.BASE, term, true);
// Retrieves with GET
Subscription objFromJson = killBillClient.getSubscription(subscriptionJson.getSubscriptionId());
// Equality in java client is not correctly implemented so manually check PriceOverrides section and then reset before equality
objFromJson.setPriceOverrides(null);
subscriptionJson.setPriceOverrides(null);
Assert.assertTrue(objFromJson.equals(subscriptionJson));
assertEquals(objFromJson.getBillingPeriod(), BillingPeriod.ANNUAL);
// Change billing period immediately
final Subscription newInput = new Subscription();
newInput.setAccountId(accountJson.getAccountId());
newInput.setSubscriptionId(subscriptionJson.getSubscriptionId());
newInput.setProductName(subscriptionJson.getProductName());
newInput.setProductCategory(ProductCategory.BASE);
newInput.setBillingPeriod(BillingPeriod.MONTHLY);
newInput.setPriceList(subscriptionJson.getPriceList());
objFromJson = killBillClient.updateSubscription(newInput, BillingActionPolicy.IMMEDIATE, CALL_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
Assert.assertNotNull(objFromJson);
assertEquals(objFromJson.getBillingPeriod(), BillingPeriod.MONTHLY);
}
Aggregations