use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.
the class TestInvoiceDao method testRefundCBAFullyConsumedTwice.
@Test(groups = "slow")
public void testRefundCBAFullyConsumedTwice() throws Exception {
final UUID accountId = account.getId();
// Create invoice 1
// Scenario: single item with payment
// * $10 item
// Then, a repair occur:
// * $-10 repair
// * $10 generated CBA due to the repair (assume previous payment)
final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final InvoiceItem fixedItem1 = new FixedPriceInvoiceItem(invoice1.getId(), invoice1.getAccountId(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), clock.getUTCToday(), BigDecimal.TEN, Currency.USD);
final RepairAdjInvoiceItem repairAdjInvoiceItem = new RepairAdjInvoiceItem(fixedItem1.getInvoiceId(), fixedItem1.getAccountId(), fixedItem1.getStartDate(), fixedItem1.getEndDate(), fixedItem1.getAmount().negate(), fixedItem1.getCurrency(), fixedItem1.getId());
final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem1 = new CreditBalanceAdjInvoiceItem(fixedItem1.getInvoiceId(), fixedItem1.getAccountId(), fixedItem1.getStartDate(), fixedItem1.getAmount(), fixedItem1.getCurrency());
invoiceUtil.createInvoice(invoice1, context);
invoiceUtil.createInvoiceItem(fixedItem1, context);
invoiceUtil.createInvoiceItem(repairAdjInvoiceItem, context);
invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem1, context);
final BigDecimal paymentAmount = new BigDecimal("10.00");
final UUID paymentId = UUID.randomUUID();
final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoice1.getId(), clock.getUTCNow().plusDays(12), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), context);
// Create invoice 2
// Scenario: single item
// * $5 item
// * $-5 CBA used
final DefaultInvoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final InvoiceItem fixedItem2 = new FixedPriceInvoiceItem(invoice2.getId(), invoice1.getAccountId(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), clock.getUTCToday(), new BigDecimal("5"), Currency.USD);
final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem2 = new CreditBalanceAdjInvoiceItem(fixedItem2.getInvoiceId(), fixedItem2.getAccountId(), fixedItem2.getStartDate(), fixedItem2.getAmount().negate(), fixedItem2.getCurrency());
invoiceUtil.createInvoice(invoice2, context);
invoiceUtil.createInvoiceItem(fixedItem2, context);
invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem2, context);
// Create invoice 3
// Scenario: single item
// * $5 item
// * $-5 CBA used
final DefaultInvoice invoice3 = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
final InvoiceItem fixedItem3 = new FixedPriceInvoiceItem(invoice3.getId(), invoice1.getAccountId(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), clock.getUTCToday(), new BigDecimal("5"), Currency.USD);
final CreditBalanceAdjInvoiceItem creditBalanceAdjInvoiceItem3 = new CreditBalanceAdjInvoiceItem(fixedItem3.getInvoiceId(), fixedItem3.getAccountId(), fixedItem3.getStartDate(), fixedItem3.getAmount().negate(), fixedItem3.getCurrency());
invoiceUtil.createInvoice(invoice3, context);
invoiceUtil.createInvoiceItem(fixedItem3, context);
invoiceUtil.createInvoiceItem(creditBalanceAdjInvoiceItem3, context);
// Verify scenario - all CBA should have been used
Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 0.00);
invoiceUtil.verifyInvoice(invoice1.getId(), 0.00, 10.00, context);
invoiceUtil.verifyInvoice(invoice2.getId(), 0.00, -5.00, context);
invoiceUtil.verifyInvoice(invoice3.getId(), 0.00, -5.00, context);
invoiceDao.createRefund(paymentId, paymentAmount, false, ImmutableMap.<UUID, BigDecimal>of(), UUID.randomUUID().toString(), context);
// Verify all three invoices were affected
Assert.assertEquals(invoiceDao.getAccountCBA(accountId, context).doubleValue(), 0.00);
invoiceUtil.verifyInvoice(invoice1.getId(), 10.00, 10.00, context);
invoiceUtil.verifyInvoice(invoice2.getId(), 0.00, -5.00, context);
invoiceUtil.verifyInvoice(invoice3.getId(), 0.00, -5.00, context);
}
use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.
the class TestInvoiceWithMetadata method testWith$0RecurringItem.
@Test(groups = "fast")
public void testWith$0RecurringItem() {
final LocalDate invoiceDate = new LocalDate(2016, 11, 15);
final DefaultInvoice originalInvoice = new DefaultInvoice(account.getId(), invoiceDate, account.getCurrency());
final Plan plan = new MockPlan("my-plan");
final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(BigDecimal.TEN, account.getCurrency()));
final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, invoiceDate.toDateTimeAtStartOfDay(), plan, planPhase, null, BigDecimal.ZERO, account.getCurrency(), planPhase.getRecurring().getBillingPeriod(), 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
final InvoiceItem invoiceItem = new RecurringInvoiceItem(UUID.randomUUID(), invoiceDate.toDateTimeAtStartOfDay(), originalInvoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), invoiceDate, invoiceDate.plusMonths(1), BigDecimal.ZERO, BigDecimal.ZERO, account.getCurrency());
originalInvoice.addInvoiceItem(invoiceItem);
final Map<UUID, SubscriptionFutureNotificationDates> perSubscriptionFutureNotificationDates = new HashMap<UUID, SubscriptionFutureNotificationDates>();
final SubscriptionFutureNotificationDates subscriptionFutureNotificationDates = new SubscriptionFutureNotificationDates(BillingMode.IN_ADVANCE);
subscriptionFutureNotificationDates.updateNextRecurringDateIfRequired(invoiceDate.plusMonths(1));
perSubscriptionFutureNotificationDates.put(subscription.getId(), subscriptionFutureNotificationDates);
final InvoiceWithMetadata invoiceWithMetadata = new InvoiceWithMetadata(originalInvoice, perSubscriptionFutureNotificationDates);
// We generate an invoice with one item, invoicing for $0
final Invoice resultingInvoice = invoiceWithMetadata.getInvoice();
Assert.assertNotNull(resultingInvoice);
Assert.assertEquals(resultingInvoice.getInvoiceItems().size(), 1);
Assert.assertEquals(resultingInvoice.getInvoiceItems().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
final Map<UUID, InvoiceWithMetadata.SubscriptionFutureNotificationDates> dateMap = invoiceWithMetadata.getPerSubscriptionFutureNotificationDates();
final InvoiceWithMetadata.SubscriptionFutureNotificationDates futureNotificationDates = dateMap.get(subscription.getId());
// We verify that we generated the future notification for a month ahead
Assert.assertNotNull(futureNotificationDates.getNextRecurringDate());
Assert.assertEquals(futureNotificationDates.getNextRecurringDate().compareTo(invoiceDate.plusMonths(1)), 0);
}
use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.
the class TestPaymentApi method testCreateSuccessRefundWithPaymentControl.
@Test(groups = "slow")
public void testCreateSuccessRefundWithPaymentControl() throws PaymentApiException, InvoiceApiException, EventBusException {
final BigDecimal requestedAmount = BigDecimal.TEN;
final UUID subscriptionId = UUID.randomUUID();
final UUID bundleId = UUID.randomUUID();
final LocalDate now = clock.getUTCToday();
final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
final String paymentExternalKey = invoice.getId().toString();
final String transactionExternalKey = "sacrebleu";
final String transactionExternalKey2 = "maisenfin";
final InvoiceItem invoiceItem = new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, now, now.plusMonths(1), requestedAmount, new BigDecimal("1.0"), Currency.USD);
invoice.addInvoiceItem(invoiceItem);
final Payment payment = paymentApi.createPurchaseWithPaymentControl(account, account.getPaymentMethodId(), null, requestedAmount, Currency.USD, paymentExternalKey, transactionExternalKey, createPropertiesForInvoice(invoice), INVOICE_PAYMENT, callContext);
final List<PluginProperty> refundProperties = ImmutableList.<PluginProperty>of();
final Payment payment2 = paymentApi.createRefundWithPaymentControl(account, payment.getId(), requestedAmount, Currency.USD, transactionExternalKey2, refundProperties, INVOICE_PAYMENT, callContext);
assertEquals(payment2.getTransactions().size(), 2);
assertEquals(payment2.getExternalKey(), paymentExternalKey);
assertEquals(payment2.getPaymentMethodId(), account.getPaymentMethodId());
assertEquals(payment2.getAccountId(), account.getId());
assertEquals(payment2.getAuthAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(payment2.getCapturedAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(payment2.getPurchasedAmount().compareTo(requestedAmount), 0);
assertEquals(payment2.getRefundedAmount().compareTo(requestedAmount), 0);
assertEquals(payment2.getCurrency(), Currency.USD);
}
use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.
the class TestPaymentHelper method createTestInvoice.
public Invoice createTestInvoice(final Account account, final LocalDate targetDate, final Currency currency, final InvoiceItem... items) throws EventBusException, InvoiceApiException {
final Invoice invoice = new MockInvoice(account.getId(), clock.getUTCToday(), targetDate, currency);
for (final InvoiceItem item : items) {
if (item instanceof MockRecurringInvoiceItem) {
final MockRecurringInvoiceItem recurringInvoiceItem = (MockRecurringInvoiceItem) item;
invoice.addInvoiceItem(new MockRecurringInvoiceItem(invoice.getId(), account.getId(), recurringInvoiceItem.getBundleId(), recurringInvoiceItem.getSubscriptionId(), recurringInvoiceItem.getPlanName(), recurringInvoiceItem.getPhaseName(), null, recurringInvoiceItem.getStartDate(), recurringInvoiceItem.getEndDate(), recurringInvoiceItem.getAmount(), recurringInvoiceItem.getRate(), recurringInvoiceItem.getCurrency()));
}
}
Mockito.when(invoiceApi.getInvoiceById(Mockito.eq(invoice.getId()), Mockito.<InternalTenantContext>any())).thenReturn(invoice);
Mockito.when(invoiceApi.getInvoiceForPaymentId(Mockito.<UUID>any(), Mockito.<InternalCallContext>any())).thenReturn(invoice);
final InvoiceCreationInternalEvent event = new MockInvoiceCreationEvent(invoice.getId(), invoice.getAccountId(), invoice.getBalance(), invoice.getCurrency(), invoice.getInvoiceDate(), internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId(), null);
eventBus.post(event);
return invoice;
}
use of org.killbill.billing.invoice.api.InvoiceItem in project killbill by killbill.
the class TestBundleTransfer method testBundleTransferWithBPAnnualOnly.
@Test(groups = "slow")
public void testBundleTransferWithBPAnnualOnly() throws Exception {
// Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
final DateTime initialDate = new DateTime(2012, 4, 1, 0, 15, 42, 0, testTimeZone);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(3));
final String productName = "Shotgun";
final BillingPeriod term = BillingPeriod.ANNUAL;
final String planSetName = PriceListSet.DEFAULT_PRICELIST_NAME;
//
// CREATE SUBSCRIPTION AND EXPECT BOTH EVENTS: NextEvent.CREATE, NextEvent.BLOCK NextEvent.INVOICE
//
final DefaultEntitlement bpEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
assertNotNull(bpEntitlement);
assertEquals(invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext).size(), 1);
assertEquals(bpEntitlement.getSubscriptionBase().getCurrentPlan().getRecurringBillingPeriod(), BillingPeriod.ANNUAL);
// Move out of trials for interesting invoices adjustments
busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
clock.addDays(40);
assertListenerStatus();
// BUNDLE TRANSFER
final Account newAccount = createAccountWithNonOsgiPaymentMethod(getAccountData(17));
busHandler.pushExpectedEvents(NextEvent.TRANSFER, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
transferApi.transferBundle(account.getId(), newAccount.getId(), "externalKey", clock.getUTCNow(), false, false, callContext);
assertListenerStatus();
final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(newAccount.getId(), false, callContext);
assertEquals(invoices.size(), 1);
final List<InvoiceItem> invoiceItems = invoices.get(0).getInvoiceItems();
assertEquals(invoiceItems.size(), 1);
final InvoiceItem theItem = invoiceItems.get(0);
assertTrue(theItem.getStartDate().compareTo(new LocalDate(2012, 5, 11)) == 0);
assertTrue(theItem.getEndDate().compareTo(new LocalDate(2013, 5, 11)) == 0);
assertTrue(theItem.getAmount().compareTo(new BigDecimal("2399.9500")) == 0);
checkNoMoreInvoiceToGenerate(account);
}
Aggregations