Search in sources :

Example 16 with InvoicePayment

use of org.killbill.billing.invoice.api.InvoicePayment in project killbill by killbill.

the class AccountResource method getAccountTimeline.

@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + TIMELINE)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account timeline", response = AccountTimelineJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getAccountTimeline(@PathParam("accountId") final String accountIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_PARALLEL) @DefaultValue("false") final Boolean parallel, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException, SubscriptionApiException, InvoiceApiException, CatalogApiException {
    final TenantContext tenantContext = context.createContext(request);
    final UUID accountId = UUID.fromString(accountIdString);
    final Account account = accountUserApi.getAccountById(accountId, tenantContext);
    final Callable<List<SubscriptionBundle>> bundlesCallable = new Callable<List<SubscriptionBundle>>() {

        @Override
        public List<SubscriptionBundle> call() throws Exception {
            return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, tenantContext);
        }
    };
    final Callable<List<Invoice>> invoicesCallable = new Callable<List<Invoice>>() {

        @Override
        public List<Invoice> call() throws Exception {
            return invoiceApi.getInvoicesByAccount(accountId, false, tenantContext);
        }
    };
    final Callable<List<InvoicePayment>> invoicePaymentsCallable = new Callable<List<InvoicePayment>>() {

        @Override
        public List<InvoicePayment> call() throws Exception {
            return invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext);
        }
    };
    final Callable<List<Payment>> paymentsCallable = new Callable<List<Payment>>() {

        @Override
        public List<Payment> call() throws Exception {
            return paymentApi.getAccountPayments(accountId, false, false, ImmutableList.<PluginProperty>of(), tenantContext);
        }
    };
    final Callable<AccountAuditLogs> auditsCallable = new Callable<AccountAuditLogs>() {

        @Override
        public AccountAuditLogs call() throws Exception {
            return auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
        }
    };
    final AccountTimelineJson json;
    List<Invoice> invoices = null;
    List<SubscriptionBundle> bundles = null;
    List<InvoicePayment> invoicePayments = null;
    List<Payment> payments = null;
    AccountAuditLogs accountAuditLogs = null;
    if (parallel) {
        final ExecutorService executor = jaxrsExecutors.getJaxrsExecutorService();
        final Future<List<SubscriptionBundle>> futureBundlesCallable = executor.submit(bundlesCallable);
        final Future<List<Invoice>> futureInvoicesCallable = executor.submit(invoicesCallable);
        final Future<List<InvoicePayment>> futureInvoicePaymentsCallable = executor.submit(invoicePaymentsCallable);
        final Future<List<Payment>> futurePaymentsCallable = executor.submit(paymentsCallable);
        final Future<AccountAuditLogs> futureAuditsCallable = executor.submit(auditsCallable);
        final ImmutableList<Future> toBeCancelled = ImmutableList.<Future>of(futureBundlesCallable, futureInvoicesCallable, futureInvoicePaymentsCallable, futurePaymentsCallable, futureAuditsCallable);
        final int timeoutMsec = 100;
        final long ini = System.currentTimeMillis();
        do {
            bundles = (bundles == null) ? waitOnFutureAndHandleTimeout("bundles", futureBundlesCallable, timeoutMsec, toBeCancelled) : bundles;
            invoices = (invoices == null) ? waitOnFutureAndHandleTimeout("invoices", futureInvoicesCallable, timeoutMsec, toBeCancelled) : invoices;
            invoicePayments = (invoicePayments == null) ? waitOnFutureAndHandleTimeout("invoicePayments", futureInvoicePaymentsCallable, timeoutMsec, toBeCancelled) : invoicePayments;
            payments = (payments == null) ? waitOnFutureAndHandleTimeout("payments", futurePaymentsCallable, timeoutMsec, toBeCancelled) : payments;
            accountAuditLogs = (accountAuditLogs == null) ? waitOnFutureAndHandleTimeout("accountAuditLogs", futureAuditsCallable, timeoutMsec, toBeCancelled) : accountAuditLogs;
        } while ((System.currentTimeMillis() - ini < jaxrsConfig.getJaxrsTimeout().getMillis()) && (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null));
        if (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null) {
            Response.status(Status.SERVICE_UNAVAILABLE).build();
        }
    } else {
        invoices = runCallable("invoices", invoicesCallable);
        payments = runCallable("payments", paymentsCallable);
        bundles = runCallable("bundles", bundlesCallable);
        accountAuditLogs = runCallable("accountAuditLogs", auditsCallable);
        invoicePayments = runCallable("invoicePayments", invoicePaymentsCallable);
    }
    json = new AccountTimelineJson(account, invoices, payments, invoicePayments, bundles, accountAuditLogs);
    return Response.status(Status.OK).entity(json).build();
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) TenantContext(org.killbill.billing.util.callcontext.TenantContext) Callable(java.util.concurrent.Callable) SubscriptionBundle(org.killbill.billing.entitlement.api.SubscriptionBundle) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) UUID(java.util.UUID) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) AccountTimelineJson(org.killbill.billing.jaxrs.json.AccountTimelineJson) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) AccountAuditLogs(org.killbill.billing.util.audit.AccountAuditLogs) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 17 with InvoicePayment

use of org.killbill.billing.invoice.api.InvoicePayment 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 18 with InvoicePayment

use of org.killbill.billing.invoice.api.InvoicePayment in project killbill by killbill.

the class TestDefaultInvoicePaymentApi method verifyRefund.

private void verifyRefund(final Invoice invoice, final BigDecimal invoiceAmount, final BigDecimal refundAmount, final BigDecimal finalInvoiceAmount, final boolean adjusted, final Map<UUID, BigDecimal> invoiceItemIdsWithAmounts) throws InvoiceApiException {
    final InvoicePayment payment = createAndPersistPayment(invoiceInternalApi, clock, invoice.getId(), invoiceAmount, CURRENCY, internalCallContext);
    // Verify the initial invoice balance
    final BigDecimal initialInvoiceBalance = invoiceInternalApi.getInvoiceById(invoice.getId(), internalCallContext).getBalance();
    Assert.assertEquals(initialInvoiceBalance.compareTo(BigDecimal.ZERO), 0);
    // Create a full refund with no adjustment
    final InvoicePayment refund = invoiceInternalApi.recordRefund(payment.getPaymentId(), refundAmount, adjusted, invoiceItemIdsWithAmounts, UUID.randomUUID().toString(), internalCallContext);
    Assert.assertEquals(refund.getAmount().compareTo(refundAmount.negate()), 0);
    Assert.assertEquals(refund.getCurrency(), CURRENCY);
    Assert.assertEquals(refund.getInvoiceId(), invoice.getId());
    Assert.assertEquals(refund.getPaymentId(), payment.getPaymentId());
    Assert.assertEquals(refund.getType(), InvoicePaymentType.REFUND);
    // Verify the current invoice balance
    final BigDecimal newInvoiceBalance = invoiceInternalApi.getInvoiceById(invoice.getId(), internalCallContext).getBalance();
    Assert.assertEquals(newInvoiceBalance.compareTo(finalInvoiceAmount), 0);
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) BigDecimal(java.math.BigDecimal)

Example 19 with InvoicePayment

use of org.killbill.billing.invoice.api.InvoicePayment 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)

Example 20 with InvoicePayment

use of org.killbill.billing.invoice.api.InvoicePayment in project killbill by killbill.

the class TestInvoiceDao method testAccountBalanceWithNoInvoiceItems.

@Test(groups = "slow")
public void testAccountBalanceWithNoInvoiceItems() throws EntityPersistenceException {
    final UUID accountId = account.getId();
    final LocalDate targetDate1 = new LocalDate(2011, 10, 6);
    final Invoice invoice1 = new DefaultInvoice(accountId, clock.getUTCToday(), targetDate1, Currency.USD);
    invoiceUtil.createInvoice(invoice1, 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(BigDecimal.ZERO.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) 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)

Aggregations

InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)20 UUID (java.util.UUID)11 BigDecimal (java.math.BigDecimal)9 DateTime (org.joda.time.DateTime)8 Invoice (org.killbill.billing.invoice.api.Invoice)8 DefaultInvoicePayment (org.killbill.billing.invoice.model.DefaultInvoicePayment)8 LocalDate (org.joda.time.LocalDate)7 DefaultInvoice (org.killbill.billing.invoice.model.DefaultInvoice)6 Payment (org.killbill.billing.payment.api.Payment)6 RecurringInvoiceItem (org.killbill.billing.invoice.model.RecurringInvoiceItem)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 TenantContext (org.killbill.billing.util.callcontext.TenantContext)4 TimedResource (org.killbill.commons.metrics.TimedResource)4 Predicate (com.google.common.base.Predicate)3 ArrayList (java.util.ArrayList)3 CreditBalanceAdjInvoiceItem (org.killbill.billing.invoice.model.CreditBalanceAdjInvoiceItem)3