Search in sources :

Example 16 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestExternalRefund method testManualPaymentAndExternalRefund.

@Test(groups = "slow", description = "#255 - Scenario 1 - Can refund a manual payment though an external refund")
public void testManualPaymentAndExternalRefund() throws Exception {
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account accountJson = createAccountWithExternalPMBundleAndSubscriptionAndManualPayTagAndWaitForFirstInvoice();
    final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    final Invoice unpaidInvoice = invoicesForAccount.get(1);
    assertEquals(unpaidInvoice.getBalance().compareTo(BigDecimal.valueOf(249.95)), 0);
    final Payments paymentsForAccount = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(paymentsForAccount.size(), 0);
    final InvoicePayment invoicePaymentRequest = new InvoicePayment();
    invoicePaymentRequest.setTargetInvoiceId(unpaidInvoice.getInvoiceId());
    invoicePaymentRequest.setAccountId(accountJson.getAccountId());
    invoicePaymentRequest.setCurrency(unpaidInvoice.getCurrency());
    invoicePaymentRequest.setPurchasedAmount(unpaidInvoice.getAmount());
    final InvoicePayment invoicePayment = invoiceApi.createInstantPayment(unpaidInvoice.getInvoiceId(), invoicePaymentRequest, true, ImmutableList.of(), NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(invoicePayment.getPurchasedAmount().compareTo(BigDecimal.valueOf(249.95)), 0);
    assertEquals(invoicePayment.getRefundedAmount().compareTo(BigDecimal.ZERO), 0);
    final InvoicePaymentTransaction invoicePaymentTransactionRequest = new InvoicePaymentTransaction();
    invoicePaymentTransactionRequest.setAmount(BigDecimal.valueOf(249.95));
    invoicePaymentTransactionRequest.setPaymentId(invoicePayment.getPaymentId());
    final InvoicePayment invoicePaymentRefund = invoicePaymentApi.createRefundWithAdjustments(invoicePayment.getPaymentId(), invoicePaymentTransactionRequest, invoicePayment.getPaymentMethodId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    assertNotNull(invoicePaymentRefund);
    assertSingleInvoicePaymentRefund(invoicePaymentRefund);
    assertRefundInvoiceNoAdjustments(accountJson.getAccountId());
    assertRefundAccountBalance(accountJson.getAccountId(), BigDecimal.valueOf(249.95), BigDecimal.ZERO);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Invoice(org.killbill.billing.client.model.gen.Invoice) InvoicePaymentTransaction(org.killbill.billing.client.model.gen.InvoicePaymentTransaction) Payments(org.killbill.billing.client.model.Payments) InvoicePayments(org.killbill.billing.client.model.InvoicePayments) Invoices(org.killbill.billing.client.model.Invoices) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 17 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestInvoiceVoid method testParentVoidInvoice.

@Test(groups = "slow", description = "Void a parent invoice")
public void testParentVoidInvoice() throws Exception {
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
    final LocalDate triggeredDate = new LocalDate(2012, 5, 26);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final Account parentAccount = createAccount();
    final Account childAccount1 = createAccount(parentAccount.getAccountId());
    // Add a bundle and subscription
    createSubscription(childAccount1.getAccountId(), UUID.randomUUID().toString(), "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY);
    // trigger an invoice generation
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_CREATION);
    invoiceApi.createFutureInvoice(childAccount1.getAccountId(), triggeredDate, requestOptions);
    callbackServlet.assertListenerStatus();
    List<Invoice> child1Invoices = accountApi.getInvoicesForAccount(childAccount1.getAccountId(), null, null, false, false, true, null, AuditLevel.NONE, requestOptions);
    assertEquals(child1Invoices.size(), 2);
    // move one day so that the parent invoice is committed
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED);
    clock.addDays(1);
    callbackServlet.assertListenerStatus();
    List<Invoice> parentInvoices = accountApi.getInvoicesForAccount(parentAccount.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
    assertEquals(parentInvoices.size(), 1);
    // try to void parent invoice
    callbackServlet.pushExpectedEvent(ExtBusEventType.INVOICE_ADJUSTMENT);
    invoiceApi.voidInvoice(parentInvoices.get(0).getInvoiceId(), requestOptions);
    callbackServlet.assertListenerStatus();
    // move the clock 1 month to check if invoices change
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE);
    clock.addDays(31);
    callbackServlet.assertListenerStatus();
    // since the child did not have any change, the parent does not have an invoice
    // after the void.
    parentInvoices = accountApi.getInvoicesForAccount(parentAccount.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
    assertEquals(parentInvoices.size(), 0);
    // the child does not have any change
    child1Invoices = accountApi.getInvoicesForAccount(childAccount1.getAccountId(), null, null, false, false, true, null, AuditLevel.NONE, requestOptions);
    assertEquals(child1Invoices.size(), 2);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 18 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestInvoiceVoid method testInvoiceVoid.

@Test(groups = "slow", description = "Can void an invoice")
public void testInvoiceVoid() throws Exception {
    final Account accountJson = createAccountWithExternalPMBundleAndSubscriptionAndManualPayTagAndWaitForFirstInvoice();
    assertNotNull(accountJson);
    // Verify we didn't get any invoicePayment
    final List<InvoicePayment> noPaymentsFromJson = accountApi.getInvoicePayments(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(noPaymentsFromJson.size(), 0);
    // Get the invoices
    List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    // 2 invoices but look for the non zero dollar one
    assertEquals(invoices.size(), 2);
    // verify account balance
    Account account = accountApi.getAccount(accountJson.getAccountId(), true, true, AuditLevel.NONE, requestOptions);
    assertEquals(account.getAccountBalance().compareTo(invoices.get(1).getBalance()), 0);
    // void the invoice
    callbackServlet.pushExpectedEvent(ExtBusEventType.INVOICE_ADJUSTMENT);
    invoiceApi.voidInvoice(invoices.get(1).getInvoiceId(), requestOptions);
    callbackServlet.assertListenerStatus();
    // Get the invoices excluding voided
    invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    // the voided invoice should not be returned
    assertEquals(invoices.size(), 1);
    // Get the invoices including voided
    invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, true, null, AuditLevel.NONE, requestOptions);
    assertEquals(invoices.size(), 2);
    assertEquals(invoices.get(1).getStatus(), InvoiceStatus.VOID);
    assertEquals(invoices.get(1).getBalance().compareTo(BigDecimal.ZERO), 0);
    // check that account balance is zero
    account = accountApi.getAccount(accountJson.getAccountId(), true, true, AuditLevel.NONE, requestOptions);
    assertEquals(account.getAccountBalance().compareTo(BigDecimal.ZERO), 0);
    // After invoice was voided verify the subscription is re-invoiced on a new invoice
    // trigger an invoice generation
    callbackServlet.pushExpectedEvent(ExtBusEventType.INVOICE_CREATION);
    invoiceApi.createFutureInvoice(accountJson.getAccountId(), clock.getToday(DateTimeZone.forID(accountJson.getTimeZone())), requestOptions);
    callbackServlet.assertListenerStatus();
    // Get the invoices excluding voided
    invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    // the voided invoice should not be returned
    assertEquals(invoices.size(), 2);
    // process payment
    final InvoicePayment invoicePayment = processPayment(accountJson, invoices.get(1), false);
    // try to void invoice
    try {
        invoiceApi.voidInvoice(invoices.get(1).getInvoiceId(), requestOptions);
        Assert.fail("VoidInvoice call should fail with 400");
    } catch (final KillBillClientException e) {
        assertTrue(true);
    }
    // refund payment
    refundPayment(invoicePayment);
    // Void invoice
    callbackServlet.pushExpectedEvent(ExtBusEventType.INVOICE_ADJUSTMENT);
    invoiceApi.voidInvoice(invoices.get(1).getInvoiceId(), requestOptions);
    callbackServlet.assertListenerStatus();
    // check that account balance is zero
    account = accountApi.getAccount(accountJson.getAccountId(), true, true, AuditLevel.NONE, requestOptions);
    assertEquals(account.getAccountBalance().compareTo(BigDecimal.ZERO), 0);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Invoice(org.killbill.billing.client.model.gen.Invoice) KillBillClientException(org.killbill.billing.client.KillBillClientException) Test(org.testng.annotations.Test)

Example 19 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestInvoiceVoid method testInvoiceVoidWithPartialPay.

@Test(groups = "slow", description = "Can not void an invoice with partial payment")
public void testInvoiceVoidWithPartialPay() throws Exception {
    final Account accountJson = createAccountWithExternalPMBundleAndSubscriptionAndManualPayTagAndWaitForFirstInvoice();
    assertNotNull(accountJson);
    // Verify we didn't get any invoicePayment
    final List<InvoicePayment> noPaymentsFromJson = accountApi.getInvoicePayments(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(noPaymentsFromJson.size(), 0);
    // Get the invoices
    final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    // 2 invoices but look for the non zero dollar one
    assertEquals(invoices.size(), 2);
    // verify account balance
    final Account account = accountApi.getAccount(accountJson.getAccountId(), true, true, AuditLevel.NONE, requestOptions);
    assertEquals(account.getAccountBalance().compareTo(invoices.get(1).getBalance()), 0);
    // process payment
    final InvoicePayment invoicePayment = processPayment(accountJson, invoices.get(1), true);
    // try to void invoice
    try {
        invoiceApi.voidInvoice(invoices.get(1).getInvoiceId(), requestOptions);
        Assert.fail("VoidInvoice call should fail with 400");
    } catch (final KillBillClientException e) {
        assertTrue(true);
    }
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Invoice(org.killbill.billing.client.model.gen.Invoice) KillBillClientException(org.killbill.billing.client.KillBillClientException) Test(org.testng.annotations.Test)

Example 20 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestCredit method testAddCreditToCommittedInvoice.

@Test(groups = "slow", description = "Can add a credit to an existing account", expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = ".*it is already in COMMITTED status")
public void testAddCreditToCommittedInvoice() throws Exception {
    final Invoice invoice = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions).get(1);
    final BigDecimal creditAmount = BigDecimal.ONE;
    final InvoiceItem credit = new InvoiceItem();
    credit.setAccountId(accountJson.getAccountId());
    credit.setInvoiceId(invoice.getInvoiceId());
    credit.setAmount(creditAmount);
    final InvoiceItems credits = new InvoiceItems();
    credits.add(credit);
    final InvoiceItems objFromJson = creditApi.createCredits(credits, true, NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertTrue(objFromJson.get(0).getAmount().compareTo(creditAmount) == 0);
}
Also used : Invoice(org.killbill.billing.client.model.gen.Invoice) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) InvoiceItems(org.killbill.billing.client.model.InvoiceItems) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

Invoice (org.killbill.billing.client.model.gen.Invoice)42 Test (org.testng.annotations.Test)37 Account (org.killbill.billing.client.model.gen.Account)33 DateTime (org.joda.time.DateTime)17 InvoicePayment (org.killbill.billing.client.model.gen.InvoicePayment)15 InvoiceItem (org.killbill.billing.client.model.gen.InvoiceItem)14 BigDecimal (java.math.BigDecimal)12 Invoices (org.killbill.billing.client.model.Invoices)10 InvoiceItems (org.killbill.billing.client.model.InvoiceItems)7 InvoicePaymentTransaction (org.killbill.billing.client.model.gen.InvoicePaymentTransaction)7 LocalDate (org.joda.time.LocalDate)6 Subscription (org.killbill.billing.client.model.gen.Subscription)6 UUID (java.util.UUID)5 InvoicePayments (org.killbill.billing.client.model.InvoicePayments)5 Payment (org.killbill.billing.client.model.gen.Payment)4 ArrayList (java.util.ArrayList)3 KillBillClientException (org.killbill.billing.client.KillBillClientException)3 BulkSubscriptionsBundles (org.killbill.billing.client.model.BulkSubscriptionsBundles)3 Bundles (org.killbill.billing.client.model.Bundles)3 Payments (org.killbill.billing.client.model.Payments)3