Search in sources :

Example 21 with DefaultSubscriptionBase

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

the class TestSubscriptionDao method testWithAuditAndHistory.

@Test(groups = "slow")
public void testWithAuditAndHistory() throws SubscriptionBaseApiException {
    final String bundleExternalKey = "54341455sttfs1";
    final DateTime startDate = clock.getUTCNow();
    final DefaultSubscriptionBaseBundle bundleDef = new DefaultSubscriptionBaseBundle(bundleExternalKey, accountId, startDate, startDate, startDate, startDate);
    final SubscriptionBaseBundle bundle = dao.createSubscriptionBundle(bundleDef, catalog, true, internalCallContext);
    final List<AuditLogWithHistory> bundleHistory1 = dao.getSubscriptionBundleAuditLogsWithHistoryForId(bundle.getId(), AuditLevel.FULL, internalCallContext);
    assertEquals(bundleHistory1.size(), 1);
    final AuditLogWithHistory bundleHistoryRow1 = bundleHistory1.get(0);
    assertEquals(bundleHistoryRow1.getChangeType(), ChangeType.INSERT);
    final SubscriptionBundleModelDao historyRow1 = (SubscriptionBundleModelDao) bundleHistoryRow1.getEntity();
    assertEquals(historyRow1.getExternalKey(), bundle.getExternalKey());
    assertEquals(historyRow1.getAccountId(), bundle.getAccountId());
    dao.updateBundleExternalKey(bundle.getId(), "you changed me!", internalCallContext);
    final List<AuditLogWithHistory> bundleHistory2 = dao.getSubscriptionBundleAuditLogsWithHistoryForId(bundle.getId(), AuditLevel.FULL, internalCallContext);
    assertEquals(bundleHistory2.size(), 2);
    final AuditLogWithHistory bundleHistoryRow2 = bundleHistory2.get(1);
    assertEquals(bundleHistoryRow2.getChangeType(), ChangeType.UPDATE);
    final SubscriptionBundleModelDao historyRow2 = (SubscriptionBundleModelDao) bundleHistoryRow2.getEntity();
    assertEquals(historyRow2.getExternalKey(), "you changed me!");
    assertEquals(historyRow2.getAccountId(), bundle.getAccountId());
    final SubscriptionBuilder builder = new SubscriptionBuilder().setId(UUIDs.randomUUID()).setBundleId(bundle.getId()).setBundleExternalKey(bundle.getExternalKey()).setCategory(ProductCategory.BASE).setBundleStartDate(startDate).setAlignStartDate(startDate).setMigrated(false);
    final ApiEventBuilder createBuilder = new ApiEventBuilder().setSubscriptionId(builder.getId()).setEventPlan("shotgun-monthly").setEventPlanPhase("shotgun-monthly-trial").setEventPriceList(DefaultPriceListSet.DEFAULT_PRICELIST_NAME).setEffectiveDate(startDate).setFromDisk(true);
    final SubscriptionBaseEvent creationEvent = new ApiEventCreate(createBuilder);
    final DefaultSubscriptionBase subscription = new DefaultSubscriptionBase(builder);
    testListener.pushExpectedEvents(NextEvent.CREATE);
    final SubscriptionBaseWithAddOns subscriptionBaseWithAddOns = new DefaultSubscriptionBaseWithAddOns(bundle, ImmutableList.<SubscriptionBase>of(subscription));
    final List<SubscriptionBaseEvent> resultSubscriptions = dao.createSubscriptionsWithAddOns(ImmutableList.<SubscriptionBaseWithAddOns>of(subscriptionBaseWithAddOns), ImmutableMap.<UUID, List<SubscriptionBaseEvent>>of(subscription.getId(), ImmutableList.<SubscriptionBaseEvent>of(creationEvent)), catalog, internalCallContext);
    assertListenerStatus();
    assertEquals(resultSubscriptions.size(), 1);
    final SubscriptionBaseEvent subscriptionBaseEvent = resultSubscriptions.get(0);
    final List<AuditLogWithHistory> subscriptionHistory = dao.getSubscriptionAuditLogsWithHistoryForId(subscriptionBaseEvent.getSubscriptionId(), AuditLevel.FULL, internalCallContext);
    assertEquals(subscriptionHistory.size(), 1);
    final AuditLogWithHistory subscriptionHistoryRow1 = subscriptionHistory.get(0);
    assertEquals(subscriptionHistoryRow1.getChangeType(), ChangeType.INSERT);
    final SubscriptionModelDao subHistoryRow1 = (SubscriptionModelDao) subscriptionHistoryRow1.getEntity();
    assertEquals(subHistoryRow1.getBundleId(), bundle.getId());
    assertEquals(subHistoryRow1.getCategory(), ProductCategory.BASE);
    final List<AuditLogWithHistory> subscriptionEventHistory = dao.getSubscriptionEventAuditLogsWithHistoryForId(subscriptionBaseEvent.getId(), AuditLevel.FULL, internalCallContext);
    final AuditLogWithHistory subscriptionEventHistoryRow1 = subscriptionEventHistory.get(0);
    assertEquals(subscriptionEventHistoryRow1.getChangeType(), ChangeType.INSERT);
    final SubscriptionEventModelDao subEventHistoryRow1 = (SubscriptionEventModelDao) subscriptionEventHistoryRow1.getEntity();
    assertEquals(subEventHistoryRow1.getSubscriptionId(), subscriptionBaseEvent.getSubscriptionId());
    assertEquals(subEventHistoryRow1.getEventType(), EventType.API_USER);
    assertEquals(subEventHistoryRow1.getUserType(), ApiEventType.CREATE);
    assertEquals(subEventHistoryRow1.getPlanName(), "shotgun-monthly");
    assertEquals(subEventHistoryRow1.getIsActive(), true);
}
Also used : ApiEventCreate(org.killbill.billing.subscription.events.user.ApiEventCreate) SubscriptionModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionModelDao) DefaultSubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseWithAddOns) SubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.SubscriptionBaseWithAddOns) DefaultSubscriptionBaseBundle(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle) SubscriptionBundleModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionBundleModelDao) SubscriptionBuilder(org.killbill.billing.subscription.api.user.SubscriptionBuilder) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao) DateTime(org.joda.time.DateTime) ApiEventBuilder(org.killbill.billing.subscription.events.user.ApiEventBuilder) SubscriptionBaseBundle(org.killbill.billing.subscription.api.user.SubscriptionBaseBundle) DefaultSubscriptionBaseBundle(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) AuditLogWithHistory(org.killbill.billing.util.audit.AuditLogWithHistory) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) DefaultSubscriptionBaseWithAddOns(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseWithAddOns) Test(org.testng.annotations.Test)

Example 22 with DefaultSubscriptionBase

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

the class TestIntegrationVoidInvoice method testVoidInvoice.

@Test(groups = "slow")
public void testVoidInvoice() 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));
    add_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    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);
    clock.addDays(30);
    assertListenerStatus();
    List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, false, callContext);
    invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, expectedInvoices);
    // Void the invoice
    busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
    invoiceUserApi.voidInvoice(invoices.get(1).getId(), callContext);
    assertListenerStatus();
    remove_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    // Move through time
    busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(31);
    assertListenerStatus();
    // get all invoices including the VOIDED; includeVoidedInvoices = true;
    invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, true, callContext);
    assertEquals(invoices.size(), 3);
    // verify integrity of the voided
    invoiceChecker.checkInvoice(invoices.get(1).getId(), callContext, expectedInvoices);
    assertEquals(invoices.get(1).getStatus(), InvoiceStatus.VOID);
    // verify that the new invoice contains current and VOIDED charge
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 7, 14), new LocalDate(2015, 8, 14), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    invoiceChecker.checkInvoice(invoices.get(2).getId(), callContext, expectedInvoices);
    // verify that the account balance is fully paid and a payment exists
    final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance.compareTo(BigDecimal.ZERO) == 0);
    final List<Payment> payments = paymentApi.getAccountPayments(account.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments.size(), 1);
    final Payment payment = payments.get(0);
    assertTrue(payment.getPurchasedAmount().compareTo(invoices.get(2).getChargedAmount()) == 0);
    // try to void an invoice that is already paid, it should fail.
    try {
        invoiceUserApi.voidInvoice(invoices.get(2).getId(), callContext);
        Assert.fail("Should fail to void invoice that is already paid");
    } catch (final InvoiceApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.CAN_NOT_VOID_INVOICE_THAT_IS_PAID.getCode());
    }
    // Refund the payment
    busHandler.pushExpectedEvents(NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    paymentApi.createRefundWithPaymentControl(account, payment.getId(), payment.getPurchasedAmount(), payment.getCurrency(), clock.getUTCNow(), null, PLUGIN_PROPERTIES, PAYMENT_OPTIONS, callContext);
    assertListenerStatus();
    busHandler.pushExpectedEvents(NextEvent.INVOICE_ADJUSTMENT);
    invoiceUserApi.voidInvoice(invoices.get(2).getId(), callContext);
    assertListenerStatus();
    invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, true, callContext);
    assertEquals(invoices.size(), 3);
    assertEquals(invoices.get(1).getStatus(), InvoiceStatus.VOID);
    assertEquals(invoices.get(2).getStatus(), InvoiceStatus.VOID);
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) ArrayList(java.util.ArrayList) 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) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) Test(org.testng.annotations.Test)

Example 23 with DefaultSubscriptionBase

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

the class DefaultSubscriptionDao method transferBundleDataFromTransaction.

private void transferBundleDataFromTransaction(final BundleTransferData bundleTransferData, final EntitySqlDao transactional, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws EntityPersistenceException {
    final SubscriptionSqlDao transSubDao = entitySqlDaoWrapperFactory.become(SubscriptionSqlDao.class);
    final BundleSqlDao transBundleDao = entitySqlDaoWrapperFactory.become(BundleSqlDao.class);
    final DefaultSubscriptionBaseBundle bundleData = bundleTransferData.getData();
    final SubscriptionBundleModelDao existingBundleForAccount = transBundleDao.getBundlesFromAccountAndKey(bundleData.getAccountId().toString(), bundleData.getExternalKey(), context);
    if (existingBundleForAccount != null) {
        log.warn("Bundle already exists for accountId='{}', bundleExternalKey='{}'", bundleData.getAccountId(), bundleData.getExternalKey());
        return;
    }
    for (final SubscriptionTransferData curSubscription : bundleTransferData.getSubscriptions()) {
        final DefaultSubscriptionBase subData = curSubscription.getData();
        for (final SubscriptionBaseEvent curEvent : curSubscription.getInitialEvents()) {
            createAndRefresh(transactional, new SubscriptionEventModelDao(curEvent), context);
            recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory, curEvent.getEffectiveDate(), new SubscriptionNotificationKey(curEvent.getId()), context);
        }
        createAndRefresh(transSubDao, new SubscriptionModelDao(subData), context);
        // Notify the Bus of the latest requested change
        final SubscriptionBaseEvent finalEvent = curSubscription.getInitialEvents().get(curSubscription.getInitialEvents().size() - 1);
        notifyBusOfRequestedChange(entitySqlDaoWrapperFactory, subData, finalEvent, SubscriptionBaseTransitionType.TRANSFER, 0, context);
    }
    createAndRefresh(transBundleDao, new SubscriptionBundleModelDao(bundleData), context);
}
Also used : SubscriptionNotificationKey(org.killbill.billing.subscription.engine.core.SubscriptionNotificationKey) SubscriptionModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionModelDao) DefaultSubscriptionBaseBundle(org.killbill.billing.subscription.api.user.DefaultSubscriptionBaseBundle) SubscriptionBundleModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionBundleModelDao) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionTransferData(org.killbill.billing.subscription.api.transfer.SubscriptionTransferData) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent) SubscriptionEventModelDao(org.killbill.billing.subscription.engine.dao.model.SubscriptionEventModelDao)

Example 24 with DefaultSubscriptionBase

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

the class DefaultSubscriptionDao method cancelSubscriptionsFromTransaction.

private void cancelSubscriptionsFromTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final List<DefaultSubscriptionBase> subscriptions, final List<SubscriptionBaseEvent> cancelEvents, final SubscriptionCatalog catalog, final InternalCallContext context) throws EntityPersistenceException {
    for (int i = 0; i < subscriptions.size(); i++) {
        final DefaultSubscriptionBase subscription = subscriptions.get(i);
        final SubscriptionBaseEvent cancelEvent = cancelEvents.get(i);
        cancelSubscriptionFromTransaction(subscription, cancelEvent, entitySqlDaoWrapperFactory, catalog, context, subscriptions.size() - i - 1);
    }
}
Also used : DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

Example 25 with DefaultSubscriptionBase

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

the class DefaultSubscriptionDao method getSubscriptionsForAccount.

@Override
public Map<UUID, List<DefaultSubscriptionBase>> getSubscriptionsForAccount(final SubscriptionCatalog catalog, @Nullable final LocalDate cutoffDt, final InternalTenantContext context) throws CatalogApiException {
    final Map<UUID, List<DefaultSubscriptionBase>> subscriptionsFromAccountId = getSubscriptionsFromAccountId(cutoffDt, context);
    final List<SubscriptionBaseEvent> eventsForAccount = getEventsForAccountId(cutoffDt, context);
    final Map<UUID, List<DefaultSubscriptionBase>> result = new HashMap<UUID, List<DefaultSubscriptionBase>>();
    final Multimap<UUID, SubscriptionBaseEvent> eventsForSubscriptions = ArrayListMultimap.create();
    for (final SubscriptionBaseEvent evt : eventsForAccount) {
        eventsForSubscriptions.put(evt.getSubscriptionId(), evt);
    }
    for (final UUID bundleId : subscriptionsFromAccountId.keySet()) {
        final List<DefaultSubscriptionBase> subscriptionsForBundle = subscriptionsFromAccountId.get(bundleId);
        result.put(bundleId, buildBundleSubscriptions(subscriptionsForBundle, eventsForSubscriptions, null, catalog, context));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) DefaultSubscriptionBase(org.killbill.billing.subscription.api.user.DefaultSubscriptionBase) UUID(java.util.UUID) SubscriptionBaseEvent(org.killbill.billing.subscription.events.SubscriptionBaseEvent)

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