Search in sources :

Example 61 with Account

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

the class TestInvoicePayment method testPayAllInvoices.

@Test(groups = "slow", description = "Can pay invoices")
public void testPayAllInvoices() throws Exception {
    clock.setTime(new DateTime(2012, 4, 25, 0, 3, 42, 0));
    // No payment method
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    // Check there was no payment made
    assertEquals(accountApi.getPaymentsForAccount(accountJson.getAccountId(), null, requestOptions).size(), 0);
    // Get the invoices
    final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    assertEquals(invoices.size(), 2);
    final Invoice invoiceToPay = invoices.get(1);
    assertEquals(invoiceToPay.getBalance().compareTo(BigDecimal.ZERO), 1);
    // Pay all invoices
    final Invoices paidInvoices = accountApi.payAllInvoices(accountJson.getAccountId(), null, true, null, null, NULL_PLUGIN_PROPERTIES, requestOptions);
    assertEquals(paidInvoices.size(), 1);
    for (final Invoice invoice : accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions)) {
        assertEquals(invoice.getBalance().compareTo(BigDecimal.ZERO), 0);
    }
    assertEquals(accountApi.getPaymentsForAccount(accountJson.getAccountId(), null, requestOptions).size(), 1);
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) Invoices(org.killbill.billing.client.model.Invoices) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 62 with Account

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

the class TestInvoicePayment method setupScenarioWithPayment.

private InvoicePayment setupScenarioWithPayment(final boolean invoicePaymentSuccess) throws Exception {
    final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice("Shotgun", invoicePaymentSuccess, true);
    final List<InvoicePayment> paymentsForAccount = accountApi.getInvoicePayments(accountJson.getAccountId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(paymentsForAccount.size(), 1);
    final InvoicePayment paymentJson = paymentsForAccount.get(0);
    // Check the PaymentMethod from paymentMethodId returned in the Payment object
    final UUID paymentMethodId = paymentJson.getPaymentMethodId();
    final PaymentMethod paymentMethodJson = paymentMethodApi.getPaymentMethod(paymentMethodId, NULL_PLUGIN_PROPERTIES, requestOptions);
    Assert.assertEquals(paymentMethodJson.getPaymentMethodId(), paymentMethodId);
    Assert.assertEquals(paymentMethodJson.getAccountId(), accountJson.getAccountId());
    // Verify the refunds
    final List<PaymentTransaction> objRefundFromJson = getInvoicePaymentTransactions(paymentsForAccount, TransactionType.REFUND);
    Assert.assertEquals(objRefundFromJson.size(), 0);
    return paymentJson;
}
Also used : InvoicePaymentTransaction(org.killbill.billing.client.model.gen.InvoicePaymentTransaction) PaymentTransaction(org.killbill.billing.client.model.gen.PaymentTransaction) Account(org.killbill.billing.client.model.gen.Account) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) PaymentMethod(org.killbill.billing.client.model.gen.PaymentMethod) UUID(java.util.UUID)

Example 63 with Account

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

the class TestOverdue method testControlTagOverdueConfig.

@Test(groups = "slow", description = "Allow overdue condition by control tag defined in overdue config xml file")
public void testControlTagOverdueConfig() throws Exception {
    uploadTenantOverdueConfig("org/killbill/billing/server/overdueWithControlTag.xml");
    // Create an account without a payment method and assign a TEST tag
    final Account accountJson = createAccountNoPMBundleAndSubscription();
    callbackServlet.pushExpectedEvent(ExtBusEventType.TAG_CREATION);
    final Tags accountTag = accountApi.createAccountTags(accountJson.getAccountId(), ImmutableList.<UUID>of(ControlTagType.TEST.getId()), requestOptions);
    callbackServlet.assertListenerStatus();
    assertEquals(accountTag.get(0).getTagDefinitionId(), ControlTagType.TEST.getId());
    // Create an account without a TEST tag
    final Account accountJsonNoTag = createAccountNoPMBundleAndSubscription();
    // No payment will be triggered as the account doesn't have a payment method
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.INVOICE_PAYMENT_FAILED);
    clock.addMonths(1);
    callbackServlet.assertListenerStatus();
    // 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 List<Invoice> invoicesNoTag = accountApi.getInvoicesForAccount(accountJsonNoTag.getAccountId(), null, null, null, requestOptions);
    // 2 invoices but look for the non zero dollar one
    assertEquals(invoicesNoTag.size(), 2);
    // We're still clear - see the configuration
    Assert.assertTrue(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).isClearState());
    Assert.assertTrue(accountApi.getOverdueAccount(accountJsonNoTag.getAccountId(), requestOptions).isClearState());
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.BLOCKING_STATE, ExtBusEventType.OVERDUE_CHANGE);
    clock.addDays(30);
    callbackServlet.assertListenerStatus();
    // This account is expected to move to OD1 state because it matches with controlTag defined
    Assert.assertEquals(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).getName(), "OD1");
    // This account is not expected to move to OD1 state because it does not match with controlTag defined
    Assert.assertTrue(accountApi.getOverdueAccount(accountJsonNoTag.getAccountId(), requestOptions).isClearState());
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) Tags(org.killbill.billing.client.model.Tags) Test(org.testng.annotations.Test)

Example 64 with Account

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

the class TestOverdue method testOverdueStatus.

@Test(groups = "slow", description = "Can retrieve the account overdue status")
public void testOverdueStatus() throws Exception {
    // Create an account without a payment method
    final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
    // 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);
    // We're still clear - see the configuration
    Assert.assertTrue(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).isClearState());
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.BLOCKING_STATE, ExtBusEventType.OVERDUE_CHANGE);
    clock.addDays(30);
    callbackServlet.assertListenerStatus();
    Assert.assertEquals(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).getName(), "OD1");
    callbackServlet.pushExpectedEvents(ExtBusEventType.TAG_CREATION, ExtBusEventType.BLOCKING_STATE, ExtBusEventType.OVERDUE_CHANGE);
    clock.addDays(10);
    callbackServlet.assertListenerStatus();
    Assert.assertEquals(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).getName(), "OD2");
    callbackServlet.pushExpectedEvents(ExtBusEventType.BLOCKING_STATE, ExtBusEventType.OVERDUE_CHANGE);
    clock.addDays(10);
    callbackServlet.assertListenerStatus();
    Assert.assertEquals(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).getName(), "OD3");
    // Post external payments, paying the most recent invoice first: this is to avoid a race condition where
    // a refresh overdue notification kicks in after the first payment, which makes the account goes CLEAR and
    // triggers an AUTO_INVOICE_OFF tag removal (hence adjustment of the other invoices balance).
    final Invoices invoicesForAccount = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    final List<Invoice> mostRecentInvoiceFirst = Ordering.<Invoice>from(new Comparator<Invoice>() {

        @Override
        public int compare(final Invoice invoice1, final Invoice invoice2) {
            return invoice1.getInvoiceDate().compareTo(invoice2.getInvoiceDate());
        }
    }).reverse().sortedCopy(invoicesForAccount);
    for (final Invoice invoice : mostRecentInvoiceFirst) {
        if (invoice.getBalance().compareTo(BigDecimal.ZERO) > 0) {
            final InvoicePayment invoicePayment = new InvoicePayment();
            invoicePayment.setPurchasedAmount(invoice.getAmount());
            invoicePayment.setAccountId(accountJson.getAccountId());
            invoicePayment.setTargetInvoiceId(invoice.getInvoiceId());
            callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
            invoiceApi.createInstantPayment(invoice.getInvoiceId(), invoicePayment, true, ImmutableList.of(), NULL_PLUGIN_PROPERTIES, requestOptions);
            callbackServlet.assertListenerStatus();
        }
    }
    // Wait a bit for overdue to pick up the payment events...
    callbackServlet.pushExpectedEvents(ExtBusEventType.TAG_DELETION, ExtBusEventType.BLOCKING_STATE, ExtBusEventType.OVERDUE_CHANGE);
    callbackServlet.assertListenerStatus();
    // Verify we're in clear state
    Assert.assertTrue(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).isClearState());
}
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) Invoices(org.killbill.billing.client.model.Invoices) Test(org.testng.annotations.Test)

Example 65 with Account

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

the class TestUsage method testRecordUsage.

@Test(groups = "slow", description = "Can record and retrieve usage data")
public void testRecordUsage() throws Exception {
    final Account accountJson = createAccountWithDefaultPaymentMethod();
    final Subscription base = new Subscription();
    base.setAccountId(accountJson.getAccountId());
    base.setProductName("Pistol");
    base.setProductCategory(ProductCategory.BASE);
    base.setBillingPeriod(BillingPeriod.MONTHLY);
    base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    final Subscription addOn = new Subscription();
    addOn.setAccountId(accountJson.getAccountId());
    addOn.setProductName("Bullets");
    addOn.setProductCategory(ProductCategory.ADD_ON);
    addOn.setBillingPeriod(BillingPeriod.NO_BILLING_PERIOD);
    addOn.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
    callbackServlet.pushExpectedEvents(ExtBusEventType.ACCOUNT_CHANGE, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.ENTITLEMENT_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.SUBSCRIPTION_CREATION, ExtBusEventType.INVOICE_CREATION);
    final Subscriptions body = new Subscriptions();
    body.add(base);
    body.add(addOn);
    final Bundle bundle = subscriptionApi.createSubscriptionWithAddOns(body, null, null, NULL_PLUGIN_PROPERTIES, requestOptions);
    callbackServlet.assertListenerStatus();
    final UUID addOnSubscriptionId = Iterables.<Subscription>find(bundle.getSubscriptions(), new Predicate<Subscription>() {

        @Override
        public boolean apply(final Subscription input) {
            return ProductCategory.ADD_ON.equals(input.getProductCategory());
        }
    }).getSubscriptionId();
    clock.addDays(1);
    final UsageRecord usageRecord1 = new UsageRecord();
    usageRecord1.setAmount(10L);
    usageRecord1.setRecordDate(clock.getUTCToday().minusDays(1));
    final UsageRecord usageRecord2 = new UsageRecord();
    usageRecord2.setAmount(5L);
    usageRecord2.setRecordDate(clock.getUTCToday());
    final UnitUsageRecord unitUsageRecord = new UnitUsageRecord();
    unitUsageRecord.setUnitType("bullets");
    unitUsageRecord.setUsageRecords(ImmutableList.<UsageRecord>of(usageRecord1, usageRecord2));
    final SubscriptionUsageRecord usage = new SubscriptionUsageRecord();
    usage.setSubscriptionId(addOnSubscriptionId);
    usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
    usageApi.recordUsage(usage, requestOptions);
    callbackServlet.assertListenerStatus();
    final RolledUpUsage retrievedUsage1 = usageApi.getUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday(), requestOptions);
    Assert.assertEquals(retrievedUsage1.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage1.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage1.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
    // endDate is excluded
    Assert.assertEquals((long) retrievedUsage1.getRolledUpUnits().get(0).getAmount(), 10);
    final RolledUpUsage retrievedUsage2 = usageApi.getUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday().plusDays(1), requestOptions);
    Assert.assertEquals(retrievedUsage2.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage2.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage2.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
    Assert.assertEquals((long) retrievedUsage2.getRolledUpUnits().get(0).getAmount(), 15);
    final RolledUpUsage retrievedUsage3 = usageApi.getUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday(), clock.getUTCToday().plusDays(1), requestOptions);
    Assert.assertEquals(retrievedUsage3.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage3.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage3.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
    Assert.assertEquals((long) retrievedUsage3.getRolledUpUnits().get(0).getAmount(), 5);
    final RolledUpUsage retrievedUsage4 = usageApi.getAllUsage(addOnSubscriptionId, clock.getUTCToday(), clock.getUTCToday().plusDays(1), requestOptions);
    Assert.assertEquals(retrievedUsage4.getSubscriptionId(), usage.getSubscriptionId());
    Assert.assertEquals(retrievedUsage4.getRolledUpUnits().size(), 1);
    Assert.assertEquals(retrievedUsage4.getRolledUpUnits().get(0).getUnitType(), "bullets");
    Assert.assertEquals((long) retrievedUsage4.getRolledUpUnits().get(0).getAmount(), 5);
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_SUCCESS, ExtBusEventType.PAYMENT_SUCCESS);
    clock.addMonths(1);
    callbackServlet.assertListenerStatus();
    final Invoices invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, false, false, false, null, AuditLevel.MINIMAL, requestOptions);
    Assert.assertEquals(invoices.size(), 2);
    // Verify system assigned one tracking ID and this is correctly returned
    Assert.assertEquals(invoices.get(1).getTrackingIds().size(), 1);
    final InvoiceItem usageItem = Iterables.tryFind(invoices.get(1).getItems(), new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getItemType() == InvoiceItemType.USAGE;
        }
    }).orNull();
    Assert.assertNotNull(usageItem);
    Assert.assertEquals(usageItem.getPrettyPlanName(), "Bullet Monthly Plan");
    Assert.assertEquals(usageItem.getPrettyPhaseName(), "Bullet Monthly Plan Evergreen");
    Assert.assertEquals(usageItem.getPrettyUsageName(), "Bullet Usage In Arrear");
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) Subscriptions(org.killbill.billing.client.model.Subscriptions) RolledUpUsage(org.killbill.billing.client.model.gen.RolledUpUsage) Bundle(org.killbill.billing.client.model.gen.Bundle) UnitUsageRecord(org.killbill.billing.client.model.gen.UnitUsageRecord) UnitUsageRecord(org.killbill.billing.client.model.gen.UnitUsageRecord) SubscriptionUsageRecord(org.killbill.billing.client.model.gen.SubscriptionUsageRecord) UsageRecord(org.killbill.billing.client.model.gen.UsageRecord) Invoices(org.killbill.billing.client.model.Invoices) Subscription(org.killbill.billing.client.model.gen.Subscription) UUID(java.util.UUID) SubscriptionUsageRecord(org.killbill.billing.client.model.gen.SubscriptionUsageRecord) Predicate(com.google.common.base.Predicate) 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