Search in sources :

Example 56 with BillingPeriod

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);
}
Also used : BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 57 with BillingPeriod

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

Example 58 with BillingPeriod

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);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) ProductCategory(org.killbill.billing.catalog.api.ProductCategory) PhaseType(org.killbill.billing.catalog.api.PhaseType) Product(org.killbill.billing.catalog.api.Product) PlanPhase(org.killbill.billing.catalog.api.PlanPhase) SubscriptionBaseTransition(org.killbill.billing.subscription.api.user.SubscriptionBaseTransition)

Example 59 with BillingPeriod

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"));
}
Also used : Account(org.killbill.billing.client.model.Account) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Subscription(org.killbill.billing.client.model.Subscription) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Interval(org.joda.time.Interval) Test(org.testng.annotations.Test)

Example 60 with BillingPeriod

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);
}
Also used : Account(org.killbill.billing.client.model.Account) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Subscription(org.killbill.billing.client.model.Subscription) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)80 Test (org.testng.annotations.Test)69 DateTime (org.joda.time.DateTime)53 LocalDate (org.joda.time.LocalDate)30 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)29 BigDecimal (java.math.BigDecimal)28 Account (org.killbill.billing.account.api.Account)27 Invoice (org.killbill.billing.invoice.api.Invoice)26 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)20 Interval (org.joda.time.Interval)18 Plan (org.killbill.billing.catalog.api.Plan)16 PlanPhase (org.killbill.billing.catalog.api.PlanPhase)16 ArrayList (java.util.ArrayList)11 Duration (org.killbill.billing.catalog.api.Duration)9 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)9 DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)9 Account (org.killbill.billing.client.model.Account)7 Subscription (org.killbill.billing.client.model.Subscription)7 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)7 UUID (java.util.UUID)6