Search in sources :

Example 11 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class DefaultInvoiceInternalApi method recordRefund.

@Override
public InvoicePayment recordRefund(final UUID paymentId, final BigDecimal amount, final boolean isInvoiceAdjusted, final Map<UUID, BigDecimal> invoiceItemIdsWithAmounts, final String transactionExternalKey, final InternalCallContext context) throws InvoiceApiException {
    if (amount.compareTo(BigDecimal.ZERO) <= 0) {
        throw new InvoiceApiException(ErrorCode.PAYMENT_REFUND_AMOUNT_NEGATIVE_OR_NULL, paymentId, amount);
    }
    final InvoicePaymentModelDao refund = dao.createRefund(paymentId, amount, isInvoiceAdjusted, invoiceItemIdsWithAmounts, transactionExternalKey, context);
    // See https://github.com/killbill/killbill/issues/265
    final CallContext callContext = internalCallContextFactory.createCallContext(context);
    final Invoice invoice = getInvoiceById(refund.getInvoiceId(), context);
    final UUID accountId = invoice.getAccountId();
    final WithAccountLock withAccountLock = new WithAccountLock() {

        @Override
        public Iterable<Invoice> prepareInvoices() throws InvoiceApiException {
            return ImmutableList.<Invoice>of(invoice);
        }
    };
    final List<InvoiceItem> createdInvoiceItems = invoiceApiHelper.dispatchToInvoicePluginsAndInsertItems(accountId, false, withAccountLock, callContext);
    return new DefaultInvoicePayment(refund);
}
Also used : InvoicePaymentModelDao(org.killbill.billing.invoice.dao.InvoicePaymentModelDao) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) WithAccountLock(org.killbill.billing.invoice.api.WithAccountLock) UUID(java.util.UUID) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) CallContext(org.killbill.billing.util.callcontext.CallContext) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment)

Example 12 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class DefaultInvoiceInternalApi method getInvoicePayment.

private InvoicePayment getInvoicePayment(final UUID paymentId, final InvoicePaymentType type, final InternalTenantContext context) throws InvoiceApiException {
    final List<InvoicePaymentModelDao> invoicePayments = dao.getInvoicePaymentsByPaymentId(paymentId, context);
    final InvoicePaymentModelDao resultOrNull = Iterables.tryFind(invoicePayments, new Predicate<InvoicePaymentModelDao>() {

        @Override
        public boolean apply(final InvoicePaymentModelDao input) {
            return input.getType() == type && input.getSuccess();
        }
    }).orNull();
    return resultOrNull != null ? new DefaultInvoicePayment(resultOrNull) : null;
}
Also used : InvoicePaymentModelDao(org.killbill.billing.invoice.dao.InvoicePaymentModelDao) Predicate(com.google.common.base.Predicate) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment)

Example 13 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testAccountBalance.

@Test(groups = "slow")
public void testAccountBalance() throws EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal rate1 = new BigDecimal("17.0");
    final BigDecimal rate2 = new BigDecimal("42.0");
    final RecurringInvoiceItem item1 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase A", startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    final BigDecimal payment1 = new BigDecimal("48.0");
    final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
    invoiceUtil.createPayment(payment, context);
    final BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(rate1.add(rate2).subtract(payment1)), 0);
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 14 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testInvoicePayment.

@Test(groups = "slow")
public void testInvoicePayment() throws InvoiceApiException, EntityPersistenceException {
    final UUID accountId = account.getId();
    final Invoice invoice = new DefaultInvoice(accountId, clock.getUTCToday(), clock.getUTCToday(), Currency.USD);
    final UUID invoiceId = invoice.getId();
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = new LocalDate(2010, 1, 1);
    final LocalDate endDate = new LocalDate(2010, 4, 1);
    final InvoiceItem invoiceItem = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, "test plan", "test phase", startDate, endDate, new BigDecimal("21.00"), new BigDecimal("7.00"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoiceUtil.createInvoice(invoice, context);
    final InvoiceModelDao savedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(savedInvoice);
    assertEquals(InvoiceModelDaoHelper.getBalance(savedInvoice).compareTo(new BigDecimal("21.00")), 0);
    assertEquals(savedInvoice.getInvoiceItems().size(), 1);
    final BigDecimal paymentAmount = new BigDecimal("11.00");
    final UUID paymentId = UUID.randomUUID();
    final DefaultInvoicePayment defaultInvoicePayment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, paymentId, invoiceId, clock.getUTCNow().plusDays(12), paymentAmount, Currency.USD, Currency.USD, "cookie", true);
    invoiceDao.notifyOfPaymentCompletion(new InvoicePaymentModelDao(defaultInvoicePayment), context);
    final InvoiceModelDao retrievedInvoice = invoiceDao.getById(invoiceId, context);
    assertNotNull(retrievedInvoice);
    assertEquals(retrievedInvoice.getInvoiceItems().size(), 1);
    assertEquals(InvoiceModelDaoHelper.getBalance(retrievedInvoice).compareTo(new BigDecimal("10.00")), 0);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) ExternalChargeInvoiceItem(org.killbill.billing.invoice.model.ExternalChargeInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) ParentInvoiceItem(org.killbill.billing.invoice.model.ParentInvoiceItem) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Test(org.testng.annotations.Test)

Example 15 with DefaultInvoicePayment

use of org.killbill.billing.invoice.model.DefaultInvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testAccountBalanceWithAllSortsOfThings.

@Test(groups = "slow")
public void testAccountBalanceWithAllSortsOfThings() throws EntityPersistenceException {
    final UUID accountId = account.getId();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, context);
    final LocalDate startDate = new LocalDate(2011, 3, 1);
    final LocalDate endDate = startDate.plusMonths(1);
    final BigDecimal amount1 = new BigDecimal("5.0");
    final BigDecimal rate1 = new BigDecimal("20.0");
    final BigDecimal rate2 = new BigDecimal("10.0");
    // Fixed Item
    final FixedPriceInvoiceItem item1 = new FixedPriceInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase A", startDate, amount1, Currency.USD);
    invoiceUtil.createInvoiceItem(item1, context);
    BigDecimal balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("5.00")), 0);
    // Recurring item
    final RecurringInvoiceItem item2 = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, rate1, rate1, Currency.USD);
    invoiceUtil.createInvoiceItem(item2, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("25.00")), 0);
    // Pay the whole thing
    final BigDecimal payment1 = amount1.add(rate1);
    final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), payment1, Currency.USD, Currency.USD, null, true);
    invoiceUtil.createPayment(payment, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("0.00")), 0);
    // Repair previous item with rate 2
    final RepairAdjInvoiceItem item2Repair = new RepairAdjInvoiceItem(invoice1.getId(), accountId, startDate, endDate, rate1.negate(), Currency.USD, item2.getId());
    final RecurringInvoiceItem item2Replace = new RecurringInvoiceItem(invoice1.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test phase B", startDate, endDate, rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(item2Repair, context);
    invoiceUtil.createInvoiceItem(item2Replace, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("-10.00")), 0);
    // CBA
    final CreditBalanceAdjInvoiceItem cbaItem = new CreditBalanceAdjInvoiceItem(invoice1.getId(), accountId, new LocalDate(), balance.negate(), Currency.USD);
    invoiceUtil.createInvoiceItem(cbaItem, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("-10.00")), 0);
    BigDecimal cba = invoiceDao.getAccountCBA(accountId, context);
    assertEquals(cba.compareTo(new BigDecimal("10.00")), 0);
    // partial REFUND on the payment (along with CBA generated by the system)
    final InvoicePayment refund = new DefaultInvoicePayment(UUID.randomUUID(), InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoice1.getId(), new DateTime(), rate2.negate(), Currency.USD, Currency.USD, null, payment.getId());
    invoiceUtil.createPayment(refund, context);
    final CreditBalanceAdjInvoiceItem cbaItem2 = new CreditBalanceAdjInvoiceItem(invoice1.getId(), accountId, new LocalDate(), rate2.negate(), Currency.USD);
    invoiceUtil.createInvoiceItem(cbaItem2, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(BigDecimal.ZERO), 0);
    cba = invoiceDao.getAccountCBA(accountId, context);
    assertEquals(cba.compareTo(BigDecimal.ZERO), 0);
    // NEXT RECURRING on invoice 2
    final Invoice invoice2 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1.plusMonths(1), Currency.USD);
    invoiceUtil.createInvoice(invoice2, context);
    final RecurringInvoiceItem nextItem = new RecurringInvoiceItem(invoice2.getId(), accountId, bundleId, UUID.randomUUID(), "test plan", "test bla", startDate.plusMonths(1), endDate.plusMonths(1), rate2, rate2, Currency.USD);
    invoiceUtil.createInvoiceItem(nextItem, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("10.00")), 0);
    cba = invoiceDao.getAccountCBA(accountId, context);
    assertEquals(cba.compareTo(new BigDecimal("0.00")), 0);
    // FINALLY ISSUE A CREDIT ADJ
    final CreditAdjInvoiceItem creditItem = new CreditAdjInvoiceItem(invoice2.getId(), accountId, new LocalDate(), null, rate2.negate(), Currency.USD);
    invoiceUtil.createInvoiceItem(creditItem, context);
    balance = invoiceDao.getAccountBalance(accountId, context);
    assertEquals(balance.compareTo(new BigDecimal("0.00")), 0);
    cba = invoiceDao.getAccountCBA(accountId, context);
    assertEquals(cba.compareTo(new BigDecimal("0.00")), 0);
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) Invoice(org.killbill.billing.invoice.api.Invoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) CreditAdjInvoiceItem(org.killbill.billing.invoice.model.CreditAdjInvoiceItem) FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) DefaultInvoicePayment(org.killbill.billing.invoice.model.DefaultInvoicePayment) CreditBalanceAdjInvoiceItem(org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem) UUID(java.util.UUID) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) RepairAdjInvoiceItem(org.killbill.billing.invoice.model.RepairAdjInvoiceItem) Test(org.testng.annotations.Test)

Aggregations

DefaultInvoicePayment (org.killbill.billing.invoice.model.DefaultInvoicePayment)17 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)14 Invoice (org.killbill.billing.invoice.api.Invoice)13 UUID (java.util.UUID)12 BigDecimal (java.math.BigDecimal)11 LocalDate (org.joda.time.LocalDate)10 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)10 Test (org.testng.annotations.Test)10 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)8 CreditBalanceAdjInvoiceItem (org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem)8 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)8 RepairAdjInvoiceItem (org.killbill.billing.invoice.model.RepairAdjInvoiceItem)8 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)7 CreditAdjInvoiceItem (org.killbill.billing.invoice.model.CreditAdjInvoiceItem)7 DateTime (org.joda.time.DateTime)6 ExternalChargeInvoiceItem (org.killbill.billing.invoice.model.ExternalChargeInvoiceItem)5 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)5 ParentInvoiceItem (org.killbill.billing.invoice.model.ParentInvoiceItem)5 InvoicePaymentModelDao (org.killbill.billing.invoice.dao.InvoicePaymentModelDao)4 HashMap (java.util.HashMap)3