Search in sources :

Example 46 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class TestIntegrationInvoice method testDraftInvoice.

@Test(groups = "slow")
public void testDraftInvoice() throws Exception {
    final int billingDay = 14;
    final DateTime initialCreationDate = new DateTime(2015, 5, 15, 0, 0, 0, 0, testTimeZone);
    // set clock to the initial start date
    clock.setTime(initialCreationDate);
    log.info("Beginning test with BCD of " + billingDay);
    final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
    int invoiceItemCount = 1;
    DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    DefaultSubscriptionBase subscription = subscriptionDataFromSubscription(baseEntitlement.getSubscriptionBase());
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 6, 14), new LocalDate(2015, 7, 14), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    // Move through time and verify we get the same invoice
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(30);
    assertListenerStatus();
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, expectedInvoices);
    expectedInvoices.clear();
    // This will verify that the upcoming invoice notification is found and the invoice is generated at the right date, with correct items
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 7, 14), new LocalDate(2015, 8, 14), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    // add create external charge
    final LocalDate date = clock.getToday(account.getTimeZone());
    final List<InvoiceItem> invoiceItemList = new ArrayList<InvoiceItem>();
    ExternalChargeInvoiceItem item = new ExternalChargeInvoiceItem(null, account.getId(), subscription.getBundleId(), "", date, date, BigDecimal.TEN, account.getCurrency(), null);
    invoiceItemList.add(item);
    final List<InvoiceItem> draftInvoiceItems = invoiceUserApi.insertExternalCharges(account.getId(), date, invoiceItemList, false, null, callContext);
    // add expected invoice
    final List<ExpectedInvoiceItemCheck> expectedDraftInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    expectedDraftInvoices.add(new ExpectedInvoiceItemCheck(InvoiceItemType.EXTERNAL_CHARGE, BigDecimal.TEN));
    // Move through time and verify invoices
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addMonths(1);
    assertListenerStatus();
    invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    invoiceChecker.checkInvoice(invoices.get(2).getId(), callContext, expectedDraftInvoices);
    invoiceChecker.checkInvoice(invoices.get(3).getId(), callContext, expectedInvoices);
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    invoiceUserApi.commitInvoice(draftInvoiceItems.get(0).getInvoiceId(), callContext);
    assertListenerStatus();
    final List<Payment> accountPayments = paymentApi.getAccountPayments(account.getId(), false, false, null, callContext);
    assertEquals(accountPayments.size(), 3);
    assertEquals(accountPayments.get(2).getPurchasedAmount(), new BigDecimal("10.00"));
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) ArrayList(java.util.ArrayList) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Payment(org.killbill.billing.payment.api.Payment) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Test(org.testng.annotations.Test)

Example 47 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionInternalApi method cancelBaseSubscriptions.

@Override
public void cancelBaseSubscriptions(final Iterable<SubscriptionBase> subscriptions, final BillingActionPolicy policy, final InternalCallContext context) throws SubscriptionBaseApiException {
    try {
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(context);
        apiService.cancelWithPolicyNoValidationAndCatalog(Iterables.<SubscriptionBase, DefaultSubscriptionBase>transform(subscriptions, new Function<SubscriptionBase, DefaultSubscriptionBase>() {

            @Override
            public DefaultSubscriptionBase apply(final SubscriptionBase subscriptionBase) {
                try {
                    return getDefaultSubscriptionBase(subscriptionBase, catalog, context);
                } catch (final CatalogApiException e) {
                    throw new RuntimeException(e);
                }
            }
        }), policy, catalog, context);
    } catch (CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Function(com.google.common.base.Function) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 48 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionInternalApi method getSubscriptionsForAccount.

@Override
public Map<UUID, List<SubscriptionBase>> getSubscriptionsForAccount(final VersionedCatalog publicCatalog, @Nullable final LocalDate cutoffDt, final InternalTenantContext context) throws SubscriptionBaseApiException {
    try {
        final SubscriptionCatalog catalog = DefaultSubscriptionCatalogApi.wrapCatalog(publicCatalog, clock);
        final Map<UUID, List<DefaultSubscriptionBase>> internalSubscriptions = dao.getSubscriptionsForAccount(catalog, cutoffDt, context);
        final Map<UUID, List<SubscriptionBase>> result = new HashMap<UUID, List<SubscriptionBase>>();
        for (final UUID bundleId : internalSubscriptions.keySet()) {
            final List<DefaultSubscriptionBase> subscriptionsForApiUse = createSubscriptionsForApiUse(internalSubscriptions.get(bundleId));
            result.put(bundleId, new ArrayList<SubscriptionBase>(subscriptionsForApiUse));
        }
        return result;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) HashMap(java.util.HashMap) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) UUID(java.util.UUID) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 49 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionInternalApi method getDryRunChangePlanStatus.

@Override
public List<EntitlementAOStatusDryRun> getDryRunChangePlanStatus(final UUID subscriptionId, @Nullable final String baseProductName, final DateTime requestedDate, final InternalTenantContext context) throws SubscriptionBaseApiException {
    try {
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(context);
        final SubscriptionBase subscription = dao.getSubscriptionFromId(subscriptionId, catalog, context);
        if (subscription == null) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_INVALID_SUBSCRIPTION_ID, subscriptionId);
        }
        if (subscription.getCategory() != ProductCategory.BASE) {
            throw new SubscriptionBaseApiException(ErrorCode.SUB_CHANGE_DRY_RUN_NOT_BP);
        }
        final List<EntitlementAOStatusDryRun> result = new LinkedList<EntitlementAOStatusDryRun>();
        final List<DefaultSubscriptionBase> bundleSubscriptions = dao.getSubscriptions(subscription.getBundleId(), ImmutableList.<SubscriptionBaseEvent>of(), catalog, context);
        for (final SubscriptionBase cur : bundleSubscriptions) {
            if (cur.getId().equals(subscriptionId)) {
                continue;
            }
            // If ADDON is cancelled, skip
            if (cur.getState() == EntitlementState.CANCELLED) {
                continue;
            }
            final StaticCatalog catalogVersion = catalog.versionForDate(requestedDate);
            final Product baseProduct = baseProductName != null ? catalogVersion.findProduct(baseProductName) : null;
            final DryRunChangeReason reason;
            // If baseProductName is null, it's a cancellation dry-run. In this case, return all addons, so they are cancelled
            if (baseProduct != null && addonUtils.isAddonIncluded(baseProduct, cur.getCurrentPlan())) {
                reason = DryRunChangeReason.AO_INCLUDED_IN_NEW_PLAN;
            } else if (baseProduct != null && addonUtils.isAddonAvailable(baseProduct, cur.getCurrentPlan())) {
                reason = DryRunChangeReason.AO_AVAILABLE_IN_NEW_PLAN;
            } else {
                reason = DryRunChangeReason.AO_NOT_AVAILABLE_IN_NEW_PLAN;
            }
            final EntitlementAOStatusDryRun status = new DefaultSubscriptionStatusDryRun(cur.getId(), cur.getCurrentPlan().getProduct().getName(), cur.getCurrentPhase().getPhaseType(), cur.getCurrentPlan().getRecurringBillingPeriod(), cur.getCurrentPriceList().getName(), reason);
            result.add(status);
        }
        return result;
    } catch (final CatalogApiException e) {
        throw new SubscriptionBaseApiException(e);
    }
}
Also used : Product(org.killbill.billing.catalog.api.Product) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) LinkedList(java.util.LinkedList) SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) DryRunChangeReason(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun.DryRunChangeReason) EntitlementAOStatusDryRun(org.killbill.billing.entitlement.api.EntitlementAOStatusDryRun) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) DefaultSubscriptionStatusDryRun(org.killbill.billing.subscription.api.user.DefaultSubscriptionStatusDryRun) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseApiException(org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)

Example 50 with DefaultSubscriptionBase

use of org.killbill.billing.subscription.api.user.DefaultSubscriptionBase in project killbill by killbill.

the class DefaultSubscriptionBaseTimelineApi method getBundleTimeline.

@Override
public BundleBaseTimeline getBundleTimeline(final SubscriptionBaseBundle bundle, final TenantContext context) throws SubscriptionBaseRepairException {
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(bundle.getAccountId(), context);
        final SubscriptionCatalog catalog = subscriptionCatalogApi.getFullCatalog(internalTenantContext);
        final List<DefaultSubscriptionBase> subscriptions = dao.getSubscriptions(bundle.getId(), ImmutableList.<SubscriptionBaseEvent>of(), catalog, internalTenantContext);
        if (subscriptions.size() == 0) {
            throw new SubscriptionBaseRepairException(ErrorCode.SUB_NO_ACTIVE_SUBSCRIPTIONS, bundle.getId());
        }
        final List<SubscriptionBaseTimeline> repairs = createGetSubscriptionRepairList(subscriptions, Collections.<SubscriptionBaseTimeline>emptyList(), catalog, internalTenantContext);
        return createGetBundleRepair(bundle.getId(), bundle.getExternalKey(), repairs);
    } catch (CatalogApiException e) {
        throw new SubscriptionBaseRepairException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionCatalog(org.killbill.billing.subscription.catalog.SubscriptionCatalog)

Aggregations

DefaultSubscriptionBase (org.killbill.billing.subscription.api.user.DefaultSubscriptionBase)63 DateTime (org.joda.time.DateTime)38 Test (org.testng.annotations.Test)30 SubscriptionBaseEvent (org.killbill.billing.subscription.events.SubscriptionBaseEvent)18 Plan (org.killbill.billing.catalog.api.Plan)17 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)17 ArrayList (java.util.ArrayList)16 LocalDate (org.joda.time.LocalDate)13 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)13 SubscriptionBuilder (org.killbill.billing.subscription.api.user.SubscriptionBuilder)13 BigDecimal (java.math.BigDecimal)12 Account (org.killbill.billing.account.api.Account)12 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)12 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)12 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)11 LinkedList (java.util.LinkedList)10 SubscriptionBaseBundle (org.killbill.billing.subscription.api.user.SubscriptionBaseBundle)10 UUID (java.util.UUID)9 Invoice (org.killbill.billing.invoice.api.Invoice)9 BillingPeriod (org.killbill.billing.catalog.api.BillingPeriod)8