use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestIntegration method testCancelBPWithAOTheSameDay.
@Test(groups = "slow")
public void testCancelBPWithAOTheSameDay() throws Exception {
// We take april as it has 30 days (easier to play with BCD)
// Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
clock.setDay(new LocalDate(2012, 4, 1));
final AccountData accountData = getAccountData(1);
final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
accountChecker.checkAccount(account.getId(), accountData, callContext);
final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
//
TestDryRunArguments dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, null, null, SubscriptionEventType.START_BILLING, null, null, null, null);
Invoice dryRunInvoice = invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, callContext, expectedInvoices);
final DefaultEntitlement bpSubscription = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
// Check bundle after BP got created otherwise we get an error from auditApi.
subscriptionChecker.checkSubscriptionCreated(bpSubscription.getId(), internalCallContext);
invoiceChecker.checkInvoice(account.getId(), 1, callContext, expectedInvoices);
expectedInvoices.clear();
//
// ADD ADD_ON ON THE SAME DAY
//
dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, "Telescopic-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, null, null, SubscriptionEventType.START_BILLING, null, bpSubscription.getBundleId(), null, null);
dryRunInvoice = invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.RECURRING, new BigDecimal("399.95")));
invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, callContext, expectedInvoices);
addAOEntitlementAndCheckForCompletion(bpSubscription.getBundleId(), "Telescopic-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
final Invoice invoice = invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
paymentChecker.checkPayment(account.getId(), 1, callContext, new ExpectedPaymentCheck(new LocalDate(2012, 4, 1), new BigDecimal("399.95"), TransactionStatus.SUCCESS, invoice.getId(), Currency.USD));
expectedInvoices.clear();
//
// CANCEL BP ON THE SAME DAY (we should have two cancellations, BP and AO)
// There is no invoice created as we only adjust the previous invoice.
//
dryRun = new TestDryRunArguments(DryRunType.SUBSCRIPTION_ACTION, null, null, null, null, null, SubscriptionEventType.STOP_BILLING, bpSubscription.getId(), bpSubscription.getBundleId(), null, null);
dryRunInvoice = invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), dryRun, callContext);
expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 5, 1), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-399.95")));
// The second invoice should be adjusted for the AO (we paid for the full period) and since we paid we should also see a CBA
expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2012, 4, 1), new LocalDate(2012, 4, 1), InvoiceItemType.CBA_ADJ, new BigDecimal("399.95"), false));
invoiceChecker.checkInvoiceNoAudits(dryRunInvoice, callContext, expectedInvoices);
cancelEntitlementAndCheckForCompletion(bpSubscription, NextEvent.BLOCK, NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.CANCEL, NextEvent.INVOICE);
invoiceChecker.checkInvoice(account.getId(), 3, callContext, expectedInvoices);
checkNoMoreInvoiceToGenerate(account);
}
use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestIntegration method testWithPauseResumeAfterENT_CANCELLEDBlockingState.
@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/715")
public void testWithPauseResumeAfterENT_CANCELLEDBlockingState() throws Exception {
final DateTime initialDate = new DateTime(2012, 2, 1, 0, 3, 42, 0, testTimeZone);
final int billingDay = 2;
// set clock to the initial start date
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
final UUID accountId = account.getId();
assertNotNull(account);
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.MONTHLY;
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
//
final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(baseEntitlement);
//
// VERIFY CTD HAS BEEN SET
//
final DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) baseEntitlement.getSubscriptionBase();
final DateTime startDate = subscription.getCurrentPhaseStart();
final BigDecimal rate = subscription.getCurrentPhase().getFixed().getPrice().getPrice(Currency.USD);
verifyTestResult(accountId, subscription.getId(), startDate, null, rate, clock.getUTCNow(), 1);
//
// MOVE TIME TO AFTER TRIAL (2012-03-04)
//
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
clock.addDeltaFromReality(AT_LEAST_ONE_MONTH_MS);
assertListenerStatus();
invoiceChecker.checkInvoice(accountId, 1, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 2, 1), null, InvoiceItemType.FIXED, BigDecimal.ZERO)));
invoiceChecker.checkInvoice(accountId, 2, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("249.95"))));
// Pause the entitlement between 2012-03-05 and 2012-03-15
DefaultEntitlement entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
entitlementApi.pause(entitlement.getBundleId(), new LocalDate(2012, 3, 5), ImmutableList.<PluginProperty>of(), callContext);
entitlementApi.resume(entitlement.getBundleId(), new LocalDate(2012, 3, 15), ImmutableList.<PluginProperty>of(), callContext);
// Advance clock to 2012-03-07
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.INVOICE);
clock.addDays(3);
assertListenerStatus();
invoiceChecker.checkInvoice(accountId, 2, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 2), new LocalDate(2012, 4, 2), InvoiceItemType.RECURRING, new BigDecimal("249.95"))));
invoiceChecker.checkInvoice(accountId, 3, callContext, ImmutableList.<ExpectedInvoiceItemCheck>of(new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 5), new LocalDate(2012, 4, 2), InvoiceItemType.REPAIR_ADJ, new BigDecimal("-225.76")), new ExpectedInvoiceItemCheck(new LocalDate(2012, 3, 7), new LocalDate(2012, 3, 7), InvoiceItemType.CBA_ADJ, new BigDecimal("225.76"))));
// Entitlement should be blocked
entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
Assert.assertEquals(entitlement.getState(), EntitlementState.BLOCKED);
// Advance clock to 2012-03-12, nothing should happen
clock.addDays(5);
assertListenerStatus();
// Entitlement is still blocked
entitlement = (DefaultEntitlement) entitlementApi.getEntitlementForId(baseEntitlement.getId(), callContext);
Assert.assertEquals(entitlement.getState(), EntitlementState.BLOCKED);
List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, callContext);
assertEquals(invoices.size(), 3);
// Cancel entitlement start of term but with billing policy immediate (ENT_BLOCKED must be after ENT_CANCELLED to trigger the bug)
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.CANCEL, NextEvent.NULL_INVOICE);
baseEntitlement.cancelEntitlementWithDateOverrideBillingPolicy(new LocalDate(2012, 3, 2), BillingActionPolicy.IMMEDIATE, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
// 2012-03-16
busHandler.pushExpectedEvents(NextEvent.BLOCK, NextEvent.NULL_INVOICE);
clock.addDays(4);
assertListenerStatus();
busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE);
clock.addMonths(3);
assertListenerStatus();
// No new invoices
invoices = invoiceUserApi.getInvoicesByAccount(accountId, false, callContext);
assertEquals(invoices.size(), 3);
}
use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestIntegrationParentInvoice method testParentingWithFuturePhaseEvent.
@Test(groups = "slow")
public void testParentingWithFuturePhaseEvent() 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);
final Account parentAccount = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
Account childAccount = createAccountWithNonOsgiPaymentMethod(getAccountData(billingDay));
// Verify mapping
childAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
assertNull(childAccount.getParentAccountId());
assertFalse(childAccount.isPaymentDelegatedToParent());
List<Account> childrenAccounts = accountUserApi.getChildrenAccounts(parentAccount.getId(), callContext);
assertEquals(childrenAccounts.size(), 0);
// Create subscription
createBaseEntitlementAndCheckForCompletion(childAccount.getId(), "bundleKey1", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
// First child invoice over TRIAL period
List<Invoice> childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, callContext);
assertEquals(childInvoices.size(), 1);
assertEquals(childInvoices.get(0).getBalance().compareTo(BigDecimal.ZERO), 0);
// Add parent to the child -- the child still pays its invoices though
AccountModelDao childAccountModelDao = new AccountModelDao(childAccount.getId(), childAccount);
childAccountModelDao.setParentAccountId(parentAccount.getId());
childAccountModelDao.setIsPaymentDelegatedToParent(false);
accountUserApi.updateAccount(new DefaultAccount(childAccountModelDao), callContext);
// Verify mapping
childAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
assertEquals(childAccount.getParentAccountId(), parentAccount.getId());
assertFalse(childAccount.isPaymentDelegatedToParent());
childrenAccounts = accountUserApi.getChildrenAccounts(parentAccount.getId(), callContext);
assertEquals(childrenAccounts.size(), 1);
assertEquals(childrenAccounts.get(0).getId(), childAccount.getId());
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
clock.addDays(30);
assertListenerStatus();
// The parent still has no invoice
List<Invoice> parentInvoices = invoiceUserApi.getInvoicesByAccount(parentAccount.getId(), false, callContext);
assertEquals(parentInvoices.size(), 0);
// Second child invoice over Recurring period
childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, callContext);
assertEquals(childInvoices.size(), 2);
assertEquals(childInvoices.get(1).getBalance().compareTo(BigDecimal.ZERO), 0);
assertEquals(childInvoices.get(1).getPayments().size(), 1);
assertEquals(paymentApi.getPayment(childInvoices.get(1).getPayments().get(0).getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext).getPaymentMethodId(), childAccount.getPaymentMethodId());
// The child now delegates its payments
childAccountModelDao = new AccountModelDao(childAccount.getId(), childAccount);
childAccountModelDao.setIsPaymentDelegatedToParent(true);
accountUserApi.updateAccount(new DefaultAccount(childAccountModelDao), callContext);
// Verify mapping
childAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
assertEquals(childAccount.getParentAccountId(), parentAccount.getId());
assertTrue(childAccount.isPaymentDelegatedToParent());
childrenAccounts = accountUserApi.getChildrenAccounts(parentAccount.getId(), callContext);
assertEquals(childrenAccounts.size(), 1);
assertEquals(childrenAccounts.get(0).getId(), childAccount.getId());
busHandler.pushExpectedEvents(NextEvent.INVOICE);
clock.addDays(30);
assertListenerStatus();
// Moving a day the NotificationQ calls the commitInvoice. No payment is expected
busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
clock.addDays(1);
assertListenerStatus();
// The parent now owns the invoice
parentInvoices = invoiceUserApi.getInvoicesByAccount(parentAccount.getId(), false, callContext);
assertEquals(parentInvoices.size(), 1);
final Invoice parentInvoice = parentInvoices.get(0);
assertEquals(parentInvoice.getNumberOfItems(), 1);
assertEquals(parentInvoice.getStatus(), InvoiceStatus.COMMITTED);
assertTrue(parentInvoice.isParentInvoice());
assertEquals(parentInvoice.getBalance().compareTo(BigDecimal.ZERO), 0);
assertEquals(parentInvoice.getPayments().size(), 1);
assertEquals(paymentApi.getPayment(parentInvoice.getPayments().get(0).getPaymentId(), false, false, ImmutableList.<PluginProperty>of(), callContext).getPaymentMethodId(), parentAccount.getPaymentMethodId());
// Third child invoice over Recurring period
childInvoices = invoiceUserApi.getInvoicesByAccount(childAccount.getId(), false, callContext);
assertEquals(childInvoices.size(), 3);
assertEquals(childInvoices.get(2).getBalance().compareTo(BigDecimal.ZERO), 0);
}
use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestIntegrationWithAutoInvoiceOffTag method testAutoInvoiceOffMultipleSubscriptions.
@Test(groups = "slow")
public void testAutoInvoiceOffMultipleSubscriptions() throws Exception {
clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
// set next invoice to fail and create network
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement);
final DefaultEntitlement bpEntitlement2 = createBaseEntitlementAndCheckForCompletion(account.getId(), "whatever", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement2);
Collection<Invoice> invoices = invoiceApi.getInvoicesByAccount(account.getId(), false, callContext);
// first invoice is generated immediately after creation can't reliably stop it
assertEquals(invoices.size(), 2);
add_AUTO_INVOICING_OFF_Tag(bpEntitlement.getSubscriptionBase().getBundleId(), ObjectType.BUNDLE);
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.PHASE, NextEvent.NULL_INVOICE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
// DAY 40 out of trial
clock.addDays(40);
assertListenerStatus();
invoices = invoiceApi.getInvoicesByAccount(account.getId(), false, callContext);
// Only one additional invoice generated
assertEquals(invoices.size(), 3);
}
use of org.killbill.billing.invoice.api.Invoice in project killbill by killbill.
the class TestIntegrationWithAutoPayOff method testAutoPayOffWithPaymentFailure.
@Test(groups = "slow")
public void testAutoPayOffWithPaymentFailure() throws Exception {
clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
add_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement);
Collection<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
assertEquals(invoices.size(), 1);
busHandler.pushExpectedEvents(NextEvent.PHASE);
busHandler.pushExpectedEvents(NextEvent.INVOICE);
// After trial
clock.addDays(40);
assertListenerStatus();
invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
assertEquals(invoices.size(), 2);
for (Invoice cur : invoices) {
if (cur.getChargedAmount().compareTo(BigDecimal.ZERO) == 0) {
continue;
}
assertEquals(cur.getBalance(), cur.getChargedAmount());
}
paymentPlugin.makeNextPaymentFailWithError();
remove_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
addDelayBceauseOfLackOfCorrectSynchro();
invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
assertEquals(invoices.size(), 2);
for (Invoice cur : invoices) {
if (cur.getChargedAmount().compareTo(BigDecimal.ZERO) == 0) {
continue;
}
assertEquals(cur.getBalance(), cur.getChargedAmount());
}
assertListenerStatus();
int nbDaysBeforeRetry = paymentConfig.getPaymentFailureRetryDays(internalCallContext).get(0);
// MOVE TIME FOR RETRY TO HAPPEN
busHandler.pushExpectedEvents(NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
clock.addDays(nbDaysBeforeRetry + 1);
assertListenerStatus();
invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
for (Invoice cur : invoices) {
if (cur.getChargedAmount().compareTo(BigDecimal.ZERO) == 0) {
continue;
}
assertTrue(cur.getBalance().compareTo(BigDecimal.ZERO) == 0);
assertTrue(cur.getPaidAmount().compareTo(cur.getChargedAmount()) == 0);
}
assertListenerStatus();
}
Aggregations