Search in sources :

Example 21 with Subscription

use of org.killbill.billing.client.model.gen.Subscription in project killbill by killbill.

the class TestEntitlement method testOverwriteEntitlementBCDOnChange.

@Test(groups = "slow", description = "Verify we can move the BCD associated with the subscription when changing plan")
public void testOverwriteEntitlementBCDOnChange() 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 Subscription subscription = createSubscription(accountJson.getAccountId(), "99999", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY);
    // 2012-05-25
    // Move outside of trial
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
    clock.addDays(30);
    callbackServlet.assertListenerStatus();
    // 2012-05-28
    clock.addDays(3);
    final Subscription newInput = new Subscription();
    newInput.setAccountId(subscription.getAccountId());
    newInput.setSubscriptionId(subscription.getSubscriptionId());
    newInput.setPlanName("pistol-monthly");
    // Update BCD
    newInput.setBillCycleDayLocal(28);
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_CHANGE, ExtBusEventType.SUBSCRIPTION_CHANGE, ExtBusEventType.SUBSCRIPTION_BCD_CHANGE, ExtBusEventType.INVOICE_CREATION);
    subscriptionApi.changeSubscriptionPlan(subscription.getSubscriptionId(), newInput, new LocalDate(2012, 5, 28), null, NULL_PLUGIN_PROPERTIES, requestOptions);
    callbackServlet.assertListenerStatus();
    Subscription refreshedSubscription = subscriptionApi.getSubscription(subscription.getSubscriptionId(), requestOptions);
    Assert.assertNotNull(refreshedSubscription);
    // We charged a full period 2012-05-28 - 2012-06-28 based on the new BCD
    verifyChargedThroughDate(refreshedSubscription.getSubscriptionId(), new LocalDate(2012, 6, 28));
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Subscription(org.killbill.billing.client.model.gen.Subscription) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 22 with Subscription

use of org.killbill.billing.client.model.gen.Subscription in project killbill by killbill.

the class TestEntitlement method testCreateChangeAndCancelSubscriptionWithAutoInvoicingDraft.

@Test(groups = "slow", description = "Can create an entitlement with an account with autoInvoicingDraft")
public void testCreateChangeAndCancelSubscriptionWithAutoInvoicingDraft() throws Exception {
    final Account accountJson = createAccount();
    assertNotNull(accountJson);
    // assign AUTO_INVOICING_DRAFT tag to account
    callbackServlet.pushExpectedEvents(ExtBusEventType.TAG_CREATION);
    final Tags tags = accountApi.createAccountTags(accountJson.getAccountId(), ImmutableList.<UUID>of(new UUID(0L, 8L)), requestOptions);
    assertEquals(tags.get(0).getTagDefinitionName(), "AUTO_INVOICING_DRAFT");
    callbackServlet.assertListenerStatus();
    // verify that number of invoices and payments for account is still 0
    assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions).size(), 0);
    assertEquals(accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions).size(), 0);
    // create a subscription with no trial plan
    final Subscription input = new Subscription();
    input.setAccountId(accountJson.getAccountId());
    input.setProductName("Blowdart");
    input.setProductCategory(ProductCategory.BASE);
    input.setBillingPeriod(BillingPeriod.MONTHLY);
    input.setPriceList("notrial");
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, // The BCD is updated in that case
    ExtBusEventType.ACCOUNT_CHANGE);
    final Subscription subscriptionJson = subscriptionApi.createSubscription(input, null, null, false, false, false, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, NULL_PLUGIN_PROPERTIES, requestOptions);
    assertNotNull(subscriptionJson);
    callbackServlet.assertListenerStatus();
    // verify that number of invoices is 1 (DRAFT). Note that we have to poll because callCompletion will return
    // before the DRAFT invoice is generated (there is no event to synchronize on)
    Awaitility.await().atMost(10, TimeUnit.SECONDS).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
            return invoicesForAccount.size() == 1 && invoicesForAccount.get(0).getStatus() == InvoiceStatus.DRAFT;
        }
    });
    // verify that number of payments is still 0 (no attempts)
    assertEquals(accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions).size(), 0);
    // Avoid test timing issues
    clock.addDays(1);
    callbackServlet.assertListenerStatus();
    // Change Plan
    final Subscription newInput = new Subscription();
    newInput.setSubscriptionId(subscriptionJson.getSubscriptionId());
    newInput.setPlanName("pistol-monthly");
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_CHANGE, ExtBusEventType.SUBSCRIPTION_CHANGE);
    subscriptionApi.changeSubscriptionPlan(subscriptionJson.getSubscriptionId(), newInput, null, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, BillingActionPolicy.IMMEDIATE, NULL_PLUGIN_PROPERTIES, requestOptions);
    callbackServlet.assertListenerStatus();
    // Avoid test timing issues
    clock.addDays(1);
    callbackServlet.assertListenerStatus();
    // Cancel subscription (entitlement IMM, billing now() since the CTD is NULL)
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.ENTITLEMENT_CANCEL);
    subscriptionApi.cancelSubscriptionPlan(newInput.getSubscriptionId(), null, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, null, null, null, NULL_PLUGIN_PROPERTIES, requestOptions);
    callbackServlet.assertListenerStatus();
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoices(org.killbill.billing.client.model.Invoices) UUID(java.util.UUID) Subscription(org.killbill.billing.client.model.gen.Subscription) Tags(org.killbill.billing.client.model.Tags) KillBillClientException(org.killbill.billing.client.KillBillClientException) Test(org.testng.annotations.Test)

Example 23 with Subscription

use of org.killbill.billing.client.model.gen.Subscription 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 = createSubscription(accountJson.getAccountId(), "99999", productName, ProductCategory.BASE, term);
    // Retrieves with GET
    Subscription objFromJson = subscriptionApi.getSubscription(subscriptionJson.getSubscriptionId(), requestOptions);
    // 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());
    subscriptionApi.changeSubscriptionPlan(subscriptionJson.getSubscriptionId(), newInput, null, BillingActionPolicy.IMMEDIATE, NULL_PLUGIN_PROPERTIES, requestOptions);
    objFromJson = subscriptionApi.getSubscription(subscriptionJson.getSubscriptionId(), requestOptions);
    assertEquals(objFromJson.getBillingPeriod(), BillingPeriod.MONTHLY);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) BillingPeriod(org.killbill.billing.catalog.api.BillingPeriod) Subscription(org.killbill.billing.client.model.gen.Subscription) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 24 with Subscription

use of org.killbill.billing.client.model.gen.Subscription in project killbill by killbill.

the class TestEntitlement method testCreateEntitlementsWithAddOnsThenCloseAccountWithItemAdjustment.

@Test(groups = "slow", description = "Create a bulk of base entitlement and addOns under the same transaction")
public void testCreateEntitlementsWithAddOnsThenCloseAccountWithItemAdjustment() throws Exception {
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account accountJson = createAccount();
    final Subscription base = new Subscription();
    base.setAccountId(accountJson.getAccountId());
    base.setProductName("Shotgun");
    base.setProductCategory(ProductCategory.BASE);
    base.setBillingPeriod(BillingPeriod.MONTHLY);
    base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Subscription addOn1 = new Subscription();
    addOn1.setAccountId(accountJson.getAccountId());
    addOn1.setProductName("Telescopic-Scope");
    addOn1.setProductCategory(ProductCategory.ADD_ON);
    addOn1.setBillingPeriod(BillingPeriod.MONTHLY);
    addOn1.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Subscription addOn2 = new Subscription();
    addOn2.setAccountId(accountJson.getAccountId());
    addOn2.setProductName("Laser-Scope");
    addOn2.setProductCategory(ProductCategory.ADD_ON);
    addOn2.setBillingPeriod(BillingPeriod.MONTHLY);
    addOn2.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final List<Subscription> subscriptions = new ArrayList<Subscription>();
    subscriptions.add(base);
    subscriptions.add(addOn1);
    subscriptions.add(addOn2);
    final BulkSubscriptionsBundle bulkList = new BulkSubscriptionsBundle();
    bulkList.setBaseEntitlementAndAddOns(subscriptions);
    final BulkSubscriptionsBundles input = new BulkSubscriptionsBundles();
    input.add(bulkList);
    input.add(bulkList);
    callbackServlet.pushExpectedEvents(ExtBusEventType.ACCOUNT_CHANGE, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED);
    final Bundles bundles = subscriptionApi.createSubscriptionsWithAddOns(input, null, null, false, false, null, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, NULL_PLUGIN_PROPERTIES, requestOptions);
    callbackServlet.assertListenerStatus();
    assertNotNull(bundles);
    assertEquals(bundles.size(), 2);
    assertFalse(bundles.get(0).getExternalKey().equals(bundles.get(1).getExternalKey()));
    final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    assertEquals(invoices.size(), 1);
    assertEquals(invoices.get(0).getBalance().compareTo(BigDecimal.ZERO), 1);
    assertEquals(invoiceApi.getInvoiceTags(invoices.get(0).getInvoiceId(), requestOptions).size(), 0);
    final Bundles accountBundles = accountApi.getAccountBundles(accountJson.getAccountId(), null, null, requestOptions);
    assertEquals(accountBundles.size(), 2);
    for (final Bundle bundle : accountBundles) {
        for (final Subscription subscription : bundle.getSubscriptions()) {
            assertEquals(subscription.getState(), EntitlementState.ACTIVE);
        }
    }
    clock.addDays(1);
    callbackServlet.assertListenerStatus();
    callbackServlet.pushExpectedEvents(ExtBusEventType.TAG_CREATION, ExtBusEventType.ENTITLEMENT_CANCEL, ExtBusEventType.ENTITLEMENT_CANCEL, ExtBusEventType.ENTITLEMENT_CANCEL, ExtBusEventType.ENTITLEMENT_CANCEL, ExtBusEventType.ENTITLEMENT_CANCEL, ExtBusEventType.ENTITLEMENT_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.SUBSCRIPTION_CANCEL, ExtBusEventType.INVOICE_ADJUSTMENT, ExtBusEventType.INVOICE_ADJUSTMENT, ExtBusEventType.INVOICE_ADJUSTMENT, ExtBusEventType.INVOICE_ADJUSTMENT, ExtBusEventType.BLOCKING_STATE);
    accountApi.closeAccount(accountJson.getAccountId(), true, false, true, true, requestOptions);
    callbackServlet.assertListenerStatus();
    final Bundles accountBundlesAfterClose = accountApi.getAccountBundles(accountJson.getAccountId(), null, null, requestOptions);
    assertEquals(accountBundlesAfterClose.size(), 2);
    for (final Bundle bundle : accountBundlesAfterClose) {
        for (final Subscription subscription : bundle.getSubscriptions()) {
            assertEquals(subscription.getState(), EntitlementState.CANCELLED);
        }
    }
    final List<Invoice> invoicesAfterClose = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
    assertEquals(invoicesAfterClose.size(), 1);
    assertEquals(invoicesAfterClose.get(0).getBalance().compareTo(BigDecimal.ZERO), 0);
    assertEquals(invoiceApi.getInvoiceTags(invoicesAfterClose.get(0).getInvoiceId(), requestOptions).size(), 0);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) BulkSubscriptionsBundle(org.killbill.billing.client.model.gen.BulkSubscriptionsBundle) Bundle(org.killbill.billing.client.model.gen.Bundle) ArrayList(java.util.ArrayList) BulkSubscriptionsBundle(org.killbill.billing.client.model.gen.BulkSubscriptionsBundle) BulkSubscriptionsBundles(org.killbill.billing.client.model.BulkSubscriptionsBundles) Subscription(org.killbill.billing.client.model.gen.Subscription) DateTime(org.joda.time.DateTime) BulkSubscriptionsBundles(org.killbill.billing.client.model.BulkSubscriptionsBundles) Bundles(org.killbill.billing.client.model.Bundles) Test(org.testng.annotations.Test)

Example 25 with Subscription

use of org.killbill.billing.client.model.gen.Subscription in project killbill by killbill.

the class TestEntitlement method testCreateSubscriptionBillingInTheFutureV2.

@Test(groups = "slow", description = "Can create an entitlement with a future billing date v2 -- see https://github.com/killbill/killbill/pull/1234#discussion_r332148759")
public void testCreateSubscriptionBillingInTheFutureV2() throws Exception {
    // 2012-04-25T00:03:42.000Z
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account accountJson = createAccountWithDefaultPaymentMethod();
    // Move clock back to 2012-04-25T00:01:42.000Z (we want a reference time in the future)
    clock.setTime(new DateTime(2012, 4, 25, 0, 1, 42, 0));
    final Subscription input = new Subscription();
    input.setAccountId(accountJson.getAccountId());
    input.setProductName("Shotgun");
    input.setProductCategory(ProductCategory.BASE);
    input.setBillingPeriod(BillingPeriod.MONTHLY);
    input.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    // Verify callCompletion works (related to https://github.com/killbill/killbill/issues/1193)
    final Subscription entitlementJson = subscriptionApi.createSubscription(input, null, new LocalDate(2012, 4, 25), false, false, false, true, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(entitlementJson.getState(), EntitlementState.ACTIVE);
    Assert.assertNull(entitlementJson.getChargedThroughDate());
    Assert.assertEquals(entitlementJson.getBillingStartDate(), initialDate.toLocalDate());
    Assert.assertEquals(entitlementJson.getStartDate(), initialDate.toLocalDate());
    Assert.assertEquals(entitlementJson.getProductName(), input.getProductName());
    Assert.assertEquals(entitlementJson.getProductCategory(), input.getProductCategory());
    Assert.assertEquals(entitlementJson.getBillingPeriod(), input.getBillingPeriod());
    Assert.assertEquals(entitlementJson.getPriceList(), input.getPriceList());
    // Retrieves with GET
    final Subscription subscription = subscriptionApi.getSubscription(entitlementJson.getSubscriptionId(), requestOptions);
    Assert.assertEquals(subscription, entitlementJson);
    Assert.assertEquals(subscription.getPrices().size(), 2);
    Assert.assertEquals(subscription.getPrices().get(0).getPhaseName(), "shotgun-monthly-trial");
    Assert.assertEquals(subscription.getPrices().get(0).getFixedPrice(), BigDecimal.ZERO);
    Assert.assertNull(subscription.getPrices().get(0).getRecurringPrice());
    Assert.assertEquals(subscription.getPrices().get(1).getPhaseName(), "shotgun-monthly-evergreen");
    Assert.assertNull(subscription.getPrices().get(1).getFixedPrice());
    Assert.assertEquals(subscription.getPrices().get(1).getRecurringPrice(), new BigDecimal("249.95"));
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Subscription(org.killbill.billing.client.model.gen.Subscription) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

Subscription (org.killbill.billing.client.model.gen.Subscription)35 Account (org.killbill.billing.client.model.gen.Account)34 Test (org.testng.annotations.Test)29 DateTime (org.joda.time.DateTime)21 BigDecimal (java.math.BigDecimal)8 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)8 Bundles (org.killbill.billing.client.model.Bundles)7 UUID (java.util.UUID)6 Bundle (org.killbill.billing.client.model.gen.Bundle)6 Invoice (org.killbill.billing.client.model.gen.Invoice)6 LocalDate (org.joda.time.LocalDate)5 BulkSubscriptionsBundles (org.killbill.billing.client.model.BulkSubscriptionsBundles)5 Tags (org.killbill.billing.client.model.Tags)5 ArrayList (java.util.ArrayList)4 BulkSubscriptionsBundle (org.killbill.billing.client.model.gen.BulkSubscriptionsBundle)4 KillBillClientException (org.killbill.billing.client.KillBillClientException)3 Subscriptions (org.killbill.billing.client.model.Subscriptions)3 Predicate (com.google.common.base.Predicate)2 Invoices (org.killbill.billing.client.model.Invoices)2 SubscriptionUsageRecord (org.killbill.billing.client.model.gen.SubscriptionUsageRecord)2