Search in sources :

Example 71 with Account

use of org.killbill.billing.client.model.gen.Account 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 72 with Account

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

the class TestInvoiceItem method testCustomFields.

@Test(groups = "slow", description = "Add custom fields to invoice item")
public void testCustomFields() throws Exception {
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    final Invoices invoicesJson = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
    Assert.assertNotNull(invoicesJson);
    Assert.assertEquals(invoicesJson.size(), 2);
    final List<InvoiceItem> invoiceItems = invoicesJson.get(0).getItems();
    Assert.assertNotNull(invoiceItems);
    final CustomFields customFields = new CustomFields();
    customFields.add(new CustomField(null, invoiceItems.get(0).getInvoiceItemId(), ObjectType.INVOICE_ITEM, "1", "value1", null));
    customFields.add(new CustomField(null, invoiceItems.get(0).getInvoiceItemId(), ObjectType.INVOICE_ITEM, "2", "value2", null));
    customFields.add(new CustomField(null, invoiceItems.get(0).getInvoiceItemId(), ObjectType.INVOICE_ITEM, "3", "value3", null));
    invoiceItemApi.createInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), customFields, requestOptions);
    final List<CustomField> invoiceItemCustomFields = invoiceItemApi.getInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), requestOptions);
    Assert.assertEquals(invoiceItemCustomFields.size(), 3);
    // Delete all custom fields for account
    invoiceItemApi.deleteInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), null, requestOptions);
    final List<CustomField> remainingCustomFields = invoiceItemApi.getInvoiceItemCustomFields(invoiceItems.get(0).getInvoiceItemId(), requestOptions);
    Assert.assertEquals(remainingCustomFields.size(), 0);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) CustomFields(org.killbill.billing.client.model.CustomFields) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) CustomField(org.killbill.billing.client.model.gen.CustomField) Invoices(org.killbill.billing.client.model.Invoices) Test(org.testng.annotations.Test)

Example 73 with Account

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

the class TestInvoiceItem method testTags.

@Test(groups = "slow", description = "Add tags to invoice item")
public void testTags() throws Exception {
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    final Invoices invoicesJson = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.NONE, requestOptions);
    Assert.assertNotNull(invoicesJson);
    Assert.assertEquals(invoicesJson.size(), 2);
    final List<InvoiceItem> invoiceItems = invoicesJson.get(0).getItems();
    Assert.assertNotNull(invoiceItems);
    // Create tag definition
    final TagDefinition input = new TagDefinition(null, false, "tagtest", "invoice item tag test", ImmutableList.<ObjectType>of(ObjectType.INVOICE_ITEM), null);
    final TagDefinition objFromJson = tagDefinitionApi.createTagDefinition(input, requestOptions);
    Assert.assertNotNull(objFromJson);
    Assert.assertEquals(objFromJson.getName(), input.getName());
    Assert.assertEquals(objFromJson.getDescription(), input.getDescription());
    // Add a tag
    final Multimap<String, String> followQueryParams = HashMultimap.create();
    followQueryParams.put(JaxrsResource.QUERY_ACCOUNT_ID, accountJson.getAccountId().toString());
    final RequestOptions followRequestOptions = requestOptions.extend().withQueryParamsForFollow(followQueryParams).build();
    invoiceItemApi.createInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), ImmutableList.<UUID>of(objFromJson.getId()), followRequestOptions);
    // Retrieves all tags
    final List<Tag> tags1 = invoiceItemApi.getInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), accountJson.getAccountId(), null, AuditLevel.FULL, requestOptions);
    Assert.assertEquals(tags1.size(), 1);
    Assert.assertEquals(tags1.get(0).getTagDefinitionId(), objFromJson.getId());
    // Verify adding the same tag a second time doesn't do anything
    invoiceItemApi.createInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), ImmutableList.<UUID>of(objFromJson.getId()), followRequestOptions);
    // Retrieves all tags again
    final List<Tag> tags2 = invoiceItemApi.getInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), accountJson.getAccountId(), null, AuditLevel.FULL, requestOptions);
    Assert.assertEquals(tags2, tags1);
    // Verify audit logs
    Assert.assertEquals(tags2.get(0).getAuditLogs().size(), 1);
    final AuditLog auditLogJson = tags2.get(0).getAuditLogs().get(0);
    Assert.assertEquals(auditLogJson.getChangeType(), "INSERT");
    Assert.assertEquals(auditLogJson.getChangedBy(), createdBy);
    Assert.assertEquals(auditLogJson.getReasonCode(), reason);
    Assert.assertEquals(auditLogJson.getComments(), comment);
    Assert.assertNotNull(auditLogJson.getChangeDate());
    Assert.assertNotNull(auditLogJson.getUserToken());
    // remove it
    invoiceItemApi.deleteInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), ImmutableList.<UUID>of(objFromJson.getId()), requestOptions);
    final List<Tag> tags3 = invoiceItemApi.getInvoiceItemTags(invoiceItems.get(0).getInvoiceItemId(), accountJson.getAccountId(), null, AuditLevel.FULL, requestOptions);
    Assert.assertEquals(tags3.size(), 0);
    tagDefinitionApi.deleteTagDefinition(objFromJson.getId(), requestOptions);
    List<TagDefinition> objsFromJson = tagDefinitionApi.getTagDefinitions(requestOptions);
    Assert.assertNotNull(objsFromJson);
    Boolean isFound = false;
    for (TagDefinition tagDefinition : objsFromJson) {
        isFound |= tagDefinition.getId().equals(objFromJson.getId());
    }
    Assert.assertFalse(isFound);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) TagDefinition(org.killbill.billing.client.model.gen.TagDefinition) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) RequestOptions(org.killbill.billing.client.RequestOptions) Invoices(org.killbill.billing.client.model.Invoices) AuditLog(org.killbill.billing.client.model.gen.AuditLog) Tag(org.killbill.billing.client.model.gen.Tag) Test(org.testng.annotations.Test)

Example 74 with Account

use of org.killbill.billing.client.model.gen.Account 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 75 with Account

use of org.killbill.billing.client.model.gen.Account 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)

Aggregations

Account (org.killbill.billing.client.model.gen.Account)153 Test (org.testng.annotations.Test)139 DateTime (org.joda.time.DateTime)43 UUID (java.util.UUID)38 InvoicePayment (org.killbill.billing.client.model.gen.InvoicePayment)37 Subscription (org.killbill.billing.client.model.gen.Subscription)34 Invoice (org.killbill.billing.client.model.gen.Invoice)33 BigDecimal (java.math.BigDecimal)29 Payment (org.killbill.billing.client.model.gen.Payment)28 PaymentTransaction (org.killbill.billing.client.model.gen.PaymentTransaction)21 KillBillClientException (org.killbill.billing.client.KillBillClientException)19 Invoices (org.killbill.billing.client.model.Invoices)19 ComboPaymentTransaction (org.killbill.billing.client.model.gen.ComboPaymentTransaction)19 InvoiceItem (org.killbill.billing.client.model.gen.InvoiceItem)19 LocalDate (org.joda.time.LocalDate)13 InvoicePayments (org.killbill.billing.client.model.InvoicePayments)13 Tags (org.killbill.billing.client.model.Tags)13 PaymentMethod (org.killbill.billing.client.model.gen.PaymentMethod)12 InvoicePaymentTransaction (org.killbill.billing.client.model.gen.InvoicePaymentTransaction)11 Payments (org.killbill.billing.client.model.Payments)10