Search in sources :

Example 1 with Invoice

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

the class TestAdmin method testAdminInvoiceEndpoint.

@Test(groups = "slow")
public void testAdminInvoiceEndpoint() throws Exception {
    final DateTime initialDate = new DateTime(2012, 4, 25, 0, 3, 42, 0);
    clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
    final List<UUID> accounts = new LinkedList<UUID>();
    for (int i = 0; i < 5; i++) {
        final Account accountJson = createAccountWithDefaultPaymentMethod();
        assertNotNull(accountJson);
        accounts.add(accountJson.getAccountId());
        createSubscription(accountJson.getAccountId(), UUID.randomUUID().toString(), "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY);
        clock.addDays(2);
        Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), i + 1);
        final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
        assertEquals(invoices.size(), 1);
    }
    // Trigger first non-trial invoice
    for (int i = 0; i < 5; i++) {
        callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
    }
    clock.addDays(32);
    callbackServlet.assertListenerStatus();
    Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), 10);
    for (final UUID accountId : accounts) {
        final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountId, null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
        assertEquals(invoices.size(), 2);
    }
    // Upload the config
    final ObjectMapper mapper = new ObjectMapper();
    final Map<String, String> perTenantProperties = new HashMap<String, String>();
    perTenantProperties.put("org.killbill.invoice.enabled", "false");
    final String perTenantConfig = mapper.writeValueAsString(perTenantProperties);
    callbackServlet.pushExpectedEvent(ExtBusEventType.TENANT_CONFIG_CHANGE);
    tenantApi.uploadPerTenantConfiguration(perTenantConfig, requestOptions);
    callbackServlet.assertListenerStatus();
    // Verify the second invoice isn't generated
    for (int i = 0; i < 5; i++) {
        callbackServlet.pushExpectedEvents(ExtBusEventType.TAG_CREATION);
    }
    clock.addDays(32);
    callbackServlet.assertListenerStatus();
    Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), 10);
    for (final UUID accountId : accounts) {
        final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountId, null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
        assertEquals(invoices.size(), 2);
    }
    // Fix one account
    final Response response = triggerInvoiceGenerationForParkedAccounts(1);
    Assert.assertEquals(response.getResponseBody(), "{\"" + accounts.get(0) + "\":\"OK\"}");
    Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), 11);
    // Fix all accounts
    final Response response2 = triggerInvoiceGenerationForParkedAccounts(4);
    final Map<String, String> fixedAccounts = mapper.readValue(response2.getResponseBody(), new TypeReference<Map<String, String>>() {
    });
    Assert.assertEquals(fixedAccounts.size(), 4);
    Assert.assertEquals(fixedAccounts.get(accounts.get(1).toString()), "OK");
    Assert.assertEquals(fixedAccounts.get(accounts.get(2).toString()), "OK");
    Assert.assertEquals(fixedAccounts.get(accounts.get(3).toString()), "OK");
    Assert.assertEquals(fixedAccounts.get(accounts.get(4).toString()), "OK");
    Assert.assertEquals(invoiceApi.getInvoices(requestOptions).getPaginationMaxNbRecords(), 15);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) HashMap(java.util.HashMap) DateTime(org.joda.time.DateTime) LinkedList(java.util.LinkedList) Response(org.asynchttpclient.Response) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(org.killbill.billing.util.jackson.ObjectMapper) Test(org.testng.annotations.Test)

Example 2 with Invoice

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

the class TestAccountTimeline method testAccountTimelineWithAudits.

@Test(groups = "slow", description = "Can retrieve the timeline with audits")
public void testAccountTimelineWithAudits() throws Exception {
    final DateTime startTime = clock.getUTCNow();
    final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();
    final DateTime endTime = clock.getUTCNow();
    // Add credit
    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.setAmount(creditAmount);
    final InvoiceItems credits = new InvoiceItems();
    credits.add(credit);
    creditApi.createCredits(credits, true, NULL_PLUGIN_PROPERTIES, requestOptions);
    // Add refund
    final Payment postedPayment = accountApi.getPaymentsForAccount(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions).get(0);
    final BigDecimal refundAmount = BigDecimal.ONE;
    final InvoicePaymentTransaction refund = new InvoicePaymentTransaction();
    refund.setPaymentId(postedPayment.getPaymentId());
    refund.setAmount(refundAmount);
    invoicePaymentApi.createRefundWithAdjustments(postedPayment.getPaymentId(), refund, accountJson.getPaymentMethodId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    // Add chargeback
    final BigDecimal chargebackAmount = BigDecimal.ONE;
    final InvoicePaymentTransaction chargeback = new InvoicePaymentTransaction();
    chargeback.setPaymentId(postedPayment.getPaymentId());
    chargeback.setAmount(chargebackAmount);
    invoicePaymentApi.createChargeback(postedPayment.getPaymentId(), chargeback, NULL_PLUGIN_PROPERTIES, requestOptions);
    // Verify payments
    verifyPayments(accountJson.getAccountId(), startTime, endTime, refundAmount, chargebackAmount);
    // Verify invoices
    verifyInvoices(accountJson.getAccountId(), startTime, endTime);
    // Verify credits
    verifyCredits(accountJson.getAccountId(), startTime, endTime, creditAmount);
    // Verify bundles
    verifyBundles(accountJson.getAccountId(), startTime, endTime);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Payment(org.killbill.billing.client.model.gen.Payment) Invoice(org.killbill.billing.client.model.gen.Invoice) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) InvoiceItems(org.killbill.billing.client.model.InvoiceItems) InvoicePaymentTransaction(org.killbill.billing.client.model.gen.InvoicePaymentTransaction) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 3 with Invoice

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

the class TestInvoice method testExternalChargeOnNewInvoice.

@Test(groups = "slow", description = "Can create an external charge")
public void testExternalChargeOnNewInvoice() throws Exception {
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    // Get the invoices
    final Invoices originalInvoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
    assertEquals(originalInvoices.size(), 2);
    final UUID firstInvoiceItemId = originalInvoices.get(0).getItems().get(0).getInvoiceItemId();
    // Post an external charge
    final BigDecimal chargeAmount = BigDecimal.TEN;
    final InvoiceItem externalCharge = new InvoiceItem();
    externalCharge.setAccountId(accountJson.getAccountId());
    externalCharge.setAmount(chargeAmount);
    externalCharge.setCurrency(accountJson.getCurrency());
    externalCharge.setPlanName("SomePlan");
    externalCharge.setProductName("SomeProduct");
    externalCharge.setDescription(UUID.randomUUID().toString());
    externalCharge.setItemDetails("Item Details");
    externalCharge.setLinkedInvoiceItemId(firstInvoiceItemId);
    final LocalDate startDate = clock.getUTCToday();
    externalCharge.setStartDate(startDate);
    final LocalDate endDate = startDate.plusDays(10);
    externalCharge.setEndDate(endDate);
    final InvoiceItems itemsForCharge = new InvoiceItems();
    itemsForCharge.add(externalCharge);
    final List<InvoiceItem> createdExternalCharges = invoiceApi.createExternalCharges(accountJson.getAccountId(), itemsForCharge, clock.getUTCToday(), true, NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(createdExternalCharges.size(), 1);
    final Invoice invoiceWithItems = invoiceApi.getInvoice(createdExternalCharges.get(0).getInvoiceId(), false, AuditLevel.NONE, requestOptions);
    assertEquals(invoiceWithItems.getBalance().compareTo(chargeAmount), 0);
    assertEquals(invoiceWithItems.getItems().size(), 1);
    assertEquals(invoiceWithItems.getItems().get(0).getDescription(), externalCharge.getDescription());
    assertNull(invoiceWithItems.getItems().get(0).getBundleId());
    assertEquals(invoiceWithItems.getItems().get(0).getStartDate().compareTo(startDate), 0);
    assertEquals(invoiceWithItems.getItems().get(0).getEndDate().compareTo(endDate), 0);
    assertEquals(invoiceWithItems.getItems().get(0).getItemDetails(), "Item Details");
    assertEquals(invoiceWithItems.getItems().get(0).getLinkedInvoiceItemId(), firstInvoiceItemId);
    assertEquals(invoiceWithItems.getItems().get(0).getPlanName().compareTo("SomePlan"), 0);
    assertEquals(invoiceWithItems.getItems().get(0).getProductName().compareTo("SomeProduct"), 0);
    // Verify the total number of invoices
    assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions).size(), 3);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) Invoice(org.killbill.billing.client.model.gen.Invoice) InvoiceItems(org.killbill.billing.client.model.InvoiceItems) Invoices(org.killbill.billing.client.model.Invoices) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with Invoice

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

the class TestInvoice method testAddTaxItemsOnNewInvoice.

@Test(groups = "slow", description = "Can create tax items for a bundle")
public void testAddTaxItemsOnNewInvoice() throws Exception {
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    // Get the invoices
    assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions).size(), 2);
    // Post an external charge
    final BigDecimal taxAmount = BigDecimal.TEN;
    final UUID bundleId = UUID.randomUUID();
    final InvoiceItem taxItem = new InvoiceItem();
    taxItem.setAccountId(accountJson.getAccountId());
    taxItem.setAmount(taxAmount);
    taxItem.setCurrency(accountJson.getCurrency());
    taxItem.setBundleId(bundleId);
    final InvoiceItems input = new InvoiceItems();
    input.add(taxItem);
    final List<InvoiceItem> createdTaxItems = invoiceApi.createTaxItems(accountJson.getAccountId(), input, true, clock.getUTCToday(), NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(createdTaxItems.size(), 1);
    final Invoice invoiceWithItems = invoiceApi.getInvoice(createdTaxItems.get(0).getInvoiceId(), null, AuditLevel.NONE, requestOptions);
    assertEquals(invoiceWithItems.getBalance().compareTo(taxAmount), 0);
    assertEquals(invoiceWithItems.getItems().size(), 1);
    assertEquals(invoiceWithItems.getItems().get(0).getBundleId(), bundleId);
    assertEquals(invoiceWithItems.getItems().get(0).getItemType(), InvoiceItemType.TAX);
    // Verify the total number of invoices
    assertEquals(accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions).size(), 3);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) Invoice(org.killbill.billing.client.model.gen.Invoice) InvoiceItems(org.killbill.billing.client.model.InvoiceItems) UUID(java.util.UUID) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 5 with Invoice

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

the class TestInvoice method testExternalPayment.

@Test(groups = "slow", description = "Can create an external payment")
public void testExternalPayment() throws Exception {
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    // Verify we didn't get any invoicePayment
    final List<InvoicePayment> noPaymentsFromJson = accountApi.getInvoicePayments(accountJson.getAccountId(), null, 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);
    final UUID invoiceId = invoices.get(1).getInvoiceId();
    // Post an external invoicePayment
    final InvoicePayment invoicePayment = new InvoicePayment();
    invoicePayment.setPurchasedAmount(BigDecimal.TEN);
    invoicePayment.setAccountId(accountJson.getAccountId());
    invoicePayment.setTargetInvoiceId(invoiceId);
    invoiceApi.createInstantPayment(invoiceId, invoicePayment, true, ImmutableList.of(), null, requestOptions);
    // Verify we indeed got the invoicePayment
    final List<InvoicePayment> paymentsFromJson = accountApi.getInvoicePayments(accountJson.getAccountId(), null, requestOptions);
    assertEquals(paymentsFromJson.size(), 1);
    assertEquals(paymentsFromJson.get(0).getPurchasedAmount().compareTo(BigDecimal.TEN), 0);
    assertEquals(paymentsFromJson.get(0).getTargetInvoiceId(), invoiceId);
    // Check the PaymentMethod from paymentMethodId returned in the Payment object
    final UUID paymentMethodId = paymentsFromJson.get(0).getPaymentMethodId();
    final PaymentMethod paymentMethodJson = paymentMethodApi.getPaymentMethod(paymentMethodId, null, requestOptions);
    assertEquals(paymentMethodJson.getPaymentMethodId(), paymentMethodId);
    assertEquals(paymentMethodJson.getAccountId(), accountJson.getAccountId());
    assertEquals(paymentMethodJson.getPluginName(), ExternalPaymentProviderPlugin.PLUGIN_NAME);
    assertNull(paymentMethodJson.getPluginInfo());
}
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) PaymentMethod(org.killbill.billing.client.model.gen.PaymentMethod) UUID(java.util.UUID) 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