Search in sources :

Example 41 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestIntegration method testWeeklyPlan.

@Test(groups = "slow")
public void testWeeklyPlan() throws Exception {
    // Set clock to the initial start date - we implicitly assume here that the account timezone is UTC
    clock.setDay(new LocalDate(2015, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final List<ExpectedInvoiceItemCheck> expectedInvoices = new ArrayList<ExpectedInvoiceItemCheck>();
    // First invoice
    createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Pistol", ProductCategory.BASE, BillingPeriod.WEEKLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 4, 1), null, InvoiceItemType.FIXED, BigDecimal.ZERO));
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, expectedInvoices);
    expectedInvoices.clear();
    // 2015-5-1
    expectedInvoices.add(new ExpectedInvoiceItemCheck(new LocalDate(2015, 5, 1), new LocalDate(2015, 5, 8), InvoiceItemType.RECURRING, new BigDecimal("29.95")));
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    // Also = 1 month because or initial date 2015, 4, 1
    clock.addDays(30);
    assertListenerStatus();
    invoiceChecker.checkInvoice(account.getId(), 2, callContext, expectedInvoices);
    expectedInvoices.clear();
    LocalDate startDateBase = new LocalDate(2015, 5, 8);
    for (int i = 0; i < 10; i++) {
        final LocalDate endDateBase = startDateBase.plusDays(7);
        expectedInvoices.add(new ExpectedInvoiceItemCheck(startDateBase, endDateBase, InvoiceItemType.RECURRING, new BigDecimal("29.95")));
        busHandler.pushExpectedEvents(NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
        clock.setDay(startDateBase);
        assertListenerStatus();
        invoiceChecker.checkInvoice(account.getId(), 3 + i, callContext, expectedInvoices);
        expectedInvoices.clear();
        startDateBase = endDateBase;
    }
}
Also used : Account(org.killbill.billing.account.api.Account) AccountData(org.killbill.billing.account.api.AccountData) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 42 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestInvoicePayment method testWithPaymentFailure.

@Test(groups = "slow")
public void testWithPaymentFailure() throws Exception {
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    paymentPlugin.makeNextPaymentFailWithError();
    createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    busHandler.pushExpectedEvents(NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    clock.addDays(30);
    assertListenerStatus();
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    assertEquals(invoices.size(), 2);
    final Invoice invoice1 = invoices.get(0).getInvoiceItems().get(0).getInvoiceItemType() == InvoiceItemType.RECURRING ? invoices.get(0) : invoices.get(1);
    assertTrue(invoice1.getBalance().compareTo(new BigDecimal("249.95")) == 0);
    assertTrue(invoice1.getPaidAmount().compareTo(BigDecimal.ZERO) == 0);
    assertTrue(invoice1.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertEquals(invoice1.getPayments().size(), 1);
    assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(invoice1.getPayments().get(0).getCurrency(), Currency.USD);
    assertFalse(invoice1.getPayments().get(0).isSuccess());
    assertNotNull(invoice1.getPayments().get(0).getPaymentId());
    final BigDecimal accountBalance1 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance1.compareTo(new BigDecimal("249.95")) == 0);
    final List<Payment> payments = paymentApi.getAccountPayments(account.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments.size(), 1);
    assertEquals(payments.get(0).getPurchasedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments.get(0).getTransactions().size(), 1);
    assertEquals(payments.get(0).getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments.get(0).getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payments.get(0).getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments.get(0).getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    // Verify fix for https://github.com/killbill/killbill/issues/349
    assertEquals(payments.get(0).getId().toString(), payments.get(0).getExternalKey());
    assertEquals(payments.get(0).getTransactions().get(0).getId().toString(), payments.get(0).getTransactions().get(0).getExternalKey());
    // Trigger the payment retry
    busHandler.pushExpectedEvents(NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    clock.addDays(8);
    assertListenerStatus();
    final Invoice invoice2 = invoiceUserApi.getInvoice(invoice1.getId(), callContext);
    assertTrue(invoice2.getBalance().compareTo(BigDecimal.ZERO) == 0);
    assertTrue(invoice2.getPaidAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertTrue(invoice2.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertEquals(invoice2.getPayments().size(), 1);
    assertTrue(invoice2.getPayments().get(0).isSuccess());
    final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance2.compareTo(BigDecimal.ZERO) == 0);
    final List<Payment> payments2 = paymentApi.getAccountPayments(account.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments2.size(), 1);
    assertEquals(payments2.get(0).getTransactions().size(), 2);
    assertEquals(payments2.get(0).getTransactions().get(1).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments2.get(0).getTransactions().get(1).getCurrency(), Currency.USD);
    assertEquals(payments2.get(0).getTransactions().get(1).getProcessedAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments2.get(0).getTransactions().get(1).getProcessedCurrency(), Currency.USD);
}
Also used : Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 43 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestInvoicePayment method testWithFailedPaymentFixedToSuccess.

@Test(groups = "slow")
public void testWithFailedPaymentFixedToSuccess() throws Exception {
    // Verify integration with Overdue in that particular test
    final String configXml = "<overdueConfig>" + "   <accountOverdueStates>" + "       <initialReevaluationInterval>" + "           <unit>DAYS</unit><number>1</number>" + "       </initialReevaluationInterval>" + "       <state name=\"OD1\">" + "           <condition>" + "               <timeSinceEarliestUnpaidInvoiceEqualsOrExceeds>" + "                   <unit>DAYS</unit><number>1</number>" + "               </timeSinceEarliestUnpaidInvoiceEqualsOrExceeds>" + "           </condition>" + "           <externalMessage>Reached OD1</externalMessage>" + "           <blockChanges>true</blockChanges>" + "           <disableEntitlementAndChangesBlocked>false</disableEntitlementAndChangesBlocked>" + "       </state>" + "   </accountOverdueStates>" + "</overdueConfig>";
    final InputStream is = new ByteArrayInputStream(configXml.getBytes());
    final DefaultOverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, DefaultOverdueConfig.class);
    overdueConfigCache.loadDefaultOverdueConfig(config);
    clock.setDay(new LocalDate(2012, 4, 1));
    final AccountData accountData = getAccountData(1);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    checkODState(OverdueWrapper.CLEAR_STATE_NAME, account.getId());
    paymentPlugin.makeNextPaymentFailWithError();
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "bundleKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    addDaysAndCheckForCompletion(30, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 6, 1), callContext);
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    assertEquals(invoices.size(), 2);
    final Invoice invoice1 = invoices.get(0).getInvoiceItems().get(0).getInvoiceItemType() == InvoiceItemType.RECURRING ? invoices.get(0) : invoices.get(1);
    assertTrue(invoice1.getBalance().compareTo(new BigDecimal("249.95")) == 0);
    assertTrue(invoice1.getPaidAmount().compareTo(BigDecimal.ZERO) == 0);
    assertTrue(invoice1.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertEquals(invoice1.getPayments().size(), 1);
    assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(invoice1.getPayments().get(0).getCurrency(), Currency.USD);
    assertFalse(invoice1.getPayments().get(0).isSuccess());
    assertNotNull(invoice1.getPayments().get(0).getPaymentId());
    final BigDecimal accountBalance1 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance1.compareTo(new BigDecimal("249.95")) == 0);
    final List<Payment> payments = paymentApi.getAccountPayments(account.getId(), false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments.size(), 1);
    assertEquals(payments.get(0).getPurchasedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments.get(0).getTransactions().size(), 1);
    assertEquals(payments.get(0).getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments.get(0).getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payments.get(0).getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payments.get(0).getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    assertEquals(payments.get(0).getTransactions().get(0).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
    assertEquals(payments.get(0).getPaymentAttempts().size(), 2);
    assertEquals(payments.get(0).getPaymentAttempts().get(0).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
    assertEquals(payments.get(0).getPaymentAttempts().get(0).getStateName(), "RETRIED");
    assertEquals(payments.get(0).getPaymentAttempts().get(1).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
    assertEquals(payments.get(0).getPaymentAttempts().get(1).getStateName(), "SCHEDULED");
    // Verify account transitions to OD1
    addDaysAndCheckForCompletion(2, NextEvent.BLOCK);
    checkODState("OD1", account.getId());
    // Transition the payment to success
    final PaymentTransaction existingPaymentTransaction = payments.get(0).getTransactions().get(0);
    final PaymentTransaction updatedPaymentTransaction = Mockito.mock(PaymentTransaction.class);
    Mockito.when(updatedPaymentTransaction.getId()).thenReturn(existingPaymentTransaction.getId());
    Mockito.when(updatedPaymentTransaction.getExternalKey()).thenReturn(existingPaymentTransaction.getExternalKey());
    Mockito.when(updatedPaymentTransaction.getTransactionType()).thenReturn(existingPaymentTransaction.getTransactionType());
    Mockito.when(updatedPaymentTransaction.getProcessedAmount()).thenReturn(new BigDecimal("249.95"));
    Mockito.when(updatedPaymentTransaction.getProcessedCurrency()).thenReturn(existingPaymentTransaction.getCurrency());
    busHandler.pushExpectedEvents(NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT, NextEvent.BLOCK);
    adminPaymentApi.fixPaymentTransactionState(payments.get(0), updatedPaymentTransaction, TransactionStatus.SUCCESS, null, null, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    checkODState(OverdueWrapper.CLEAR_STATE_NAME, account.getId());
    final Invoice invoice2 = invoiceUserApi.getInvoice(invoice1.getId(), callContext);
    assertTrue(invoice2.getBalance().compareTo(BigDecimal.ZERO) == 0);
    assertTrue(invoice2.getPaidAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertTrue(invoice2.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
    assertEquals(invoice2.getPayments().size(), 1);
    assertEquals(invoice2.getPayments().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(invoice2.getPayments().get(0).getCurrency(), Currency.USD);
    assertTrue(invoice2.getPayments().get(0).isSuccess());
    assertNotNull(invoice2.getPayments().get(0).getPaymentId());
    final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
    assertTrue(accountBalance2.compareTo(BigDecimal.ZERO) == 0);
    final List<Payment> payments2 = paymentApi.getAccountPayments(account.getId(), false, true, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payments2.size(), 1);
    assertEquals(payments2.get(0).getPurchasedAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments2.get(0).getTransactions().size(), 1);
    assertEquals(payments2.get(0).getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments2.get(0).getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payments2.get(0).getTransactions().get(0).getProcessedAmount().compareTo(new BigDecimal("249.95")), 0);
    assertEquals(payments2.get(0).getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    assertEquals(payments2.get(0).getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
    assertEquals(payments2.get(0).getPaymentAttempts().size(), 1);
    assertEquals(payments2.get(0).getPaymentAttempts().get(0).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
    assertEquals(payments2.get(0).getPaymentAttempts().get(0).getStateName(), "SUCCESS");
}
Also used : Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) Payment(org.killbill.billing.payment.api.Payment) ByteArrayInputStream(java.io.ByteArrayInputStream) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Test(org.testng.annotations.Test)

Example 44 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestInvoicePayment method testAUTO_PAY_OFFThenPartialPayment.

@Test(groups = "slow")
public void testAUTO_PAY_OFFThenPartialPayment() throws Exception {
    // 2012-05-01T00:03:42.000Z
    clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
    final AccountData accountData = getAccountData(0);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 5, 1), callContext);
    // Put the account in AUTO_PAY_OFF to make sure payment system does not try to pay the initial invoice
    add_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT);
    // 2012-05-31 => DAY 30 have to get out of trial {I0, P0}
    addDaysAndCheckForCompletion(30, NextEvent.PHASE, NextEvent.INVOICE);
    Invoice invoice2 = invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 31), new LocalDate(2012, 6, 30), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 6, 30), callContext);
    // Invoice is not paid
    Assert.assertEquals(paymentApi.getAccountPayments(account.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext).size(), 0);
    Assert.assertEquals(invoice2.getBalance().compareTo(new BigDecimal("249.95")), 0);
    Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
    // Trigger partial payment
    final Payment payment1 = createPaymentAndCheckForCompletion(account, invoice2, BigDecimal.TEN, account.getCurrency(), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    paymentChecker.checkPayment(account.getId(), 1, callContext, new ExpectedPaymentCheck(new LocalDate(2012, 5, 31), BigDecimal.TEN, TransactionStatus.SUCCESS, invoice2.getId(), Currency.USD));
    Assert.assertEquals(payment1.getTransactions().size(), 1);
    Assert.assertEquals(payment1.getPurchasedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    invoice2 = invoiceUserApi.getInvoice(invoice2.getId(), callContext);
    Assert.assertEquals(invoice2.getBalance().compareTo(new BigDecimal("239.95")), 0);
    Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
    // Remove AUTO_PAY_OFF and verify the invoice is fully paid
    remove_AUTO_PAY_OFF_Tag(account.getId(), ObjectType.ACCOUNT, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final Payment payment2 = paymentChecker.checkPayment(account.getId(), 2, callContext, new ExpectedPaymentCheck(new LocalDate(2012, 5, 31), new BigDecimal("239.95"), TransactionStatus.SUCCESS, invoice2.getId(), Currency.USD));
    Assert.assertEquals(payment2.getTransactions().size(), 1);
    Assert.assertEquals(payment2.getPurchasedAmount().compareTo(new BigDecimal("239.95")), 0);
    Assert.assertEquals(payment2.getTransactions().get(0).getProcessedAmount().compareTo(new BigDecimal("239.95")), 0);
    invoice2 = invoiceUserApi.getInvoice(invoice2.getId(), callContext);
    Assert.assertEquals(invoice2.getBalance().compareTo(BigDecimal.ZERO), 0);
    Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
}
Also used : Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) ExpectedPaymentCheck(org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck) Test(org.testng.annotations.Test)

Example 45 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestInvoicePayment method testPartialPaymentByPaymentPluginThenChargebackThenChargebackReversal.

@Test(groups = "slow")
public void testPartialPaymentByPaymentPluginThenChargebackThenChargebackReversal() throws Exception {
    // 2012-05-01T00:03:42.000Z
    clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
    final AccountData accountData = getAccountData(0);
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    final DefaultEntitlement baseEntitlement = createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", "Shotgun", ProductCategory.BASE, BillingPeriod.MONTHLY, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
    invoiceChecker.checkInvoice(account.getId(), 1, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 1), null, InvoiceItemType.FIXED, new BigDecimal("0")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 5, 1), callContext);
    // Trigger a partial payment on the next invoice
    paymentPlugin.overrideNextProcessedAmount(BigDecimal.TEN);
    // 2012-05-31 => DAY 30 have to get out of trial {I0, P0}
    addDaysAndCheckForCompletion(30, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    Invoice invoice2 = invoiceChecker.checkInvoice(account.getId(), 2, callContext, new ExpectedInvoiceItemCheck(new LocalDate(2012, 5, 31), new LocalDate(2012, 6, 30), InvoiceItemType.RECURRING, new BigDecimal("249.95")));
    invoiceChecker.checkChargedThroughDate(baseEntitlement.getId(), new LocalDate(2012, 6, 30), callContext);
    // Invoice is partially paid
    Payment payment1 = paymentChecker.checkPayment(account.getId(), 1, callContext, new ExpectedPaymentCheck(new LocalDate(2012, 5, 31), new BigDecimal("249.95"), TransactionStatus.SUCCESS, invoice2.getId(), Currency.USD));
    Assert.assertEquals(payment1.getPurchasedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().size(), 1);
    Assert.assertEquals(payment1.getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(invoice2.getBalance().compareTo(new BigDecimal("239.95")), 0);
    Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
    // Trigger chargeback
    payment1 = createChargeBackAndCheckForCompletion(account, payment1, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    Assert.assertEquals(payment1.getPurchasedAmount().compareTo(BigDecimal.ZERO), 0);
    Assert.assertEquals(payment1.getTransactions().size(), 2);
    Assert.assertEquals(payment1.getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    Assert.assertEquals(payment1.getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().get(1).getAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().get(1).getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    invoice2 = invoiceUserApi.getInvoice(invoice2.getId(), callContext);
    Assert.assertEquals(invoice2.getBalance().compareTo(new BigDecimal("249.95")), 0);
    Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
    // Trigger chargeback reversal
    payment1 = createChargeBackReversalAndCheckForCompletion(account, payment1, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
    Assert.assertEquals(payment1.getPurchasedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().size(), 3);
    Assert.assertEquals(payment1.getTransactions().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
    Assert.assertEquals(payment1.getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().get(1).getAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(payment1.getTransactions().get(1).getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertNull(payment1.getTransactions().get(2).getAmount());
    Assert.assertEquals(payment1.getTransactions().get(2).getProcessedAmount().compareTo(BigDecimal.ZERO), 0);
    invoice2 = invoiceUserApi.getInvoice(invoice2.getId(), callContext);
    Assert.assertEquals(invoice2.getBalance().compareTo(new BigDecimal("239.95")), 0);
    Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
}
Also used : Account(org.killbill.billing.account.api.Account) Payment(org.killbill.billing.payment.api.Payment) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) ExpectedInvoiceItemCheck(org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) ExpectedPaymentCheck(org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck) Test(org.testng.annotations.Test)

Aggregations

AccountData (org.killbill.billing.account.api.AccountData)54 Test (org.testng.annotations.Test)49 Account (org.killbill.billing.account.api.Account)47 LocalDate (org.joda.time.LocalDate)38 BigDecimal (java.math.BigDecimal)33 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)30 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)26 Invoice (org.killbill.billing.invoice.api.Invoice)19 ArrayList (java.util.ArrayList)17 Payment (org.killbill.billing.payment.api.Payment)14 DateTime (org.joda.time.DateTime)11 MutableAccountData (org.killbill.billing.account.api.MutableAccountData)9 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)8 DefaultMutableAccountData (org.killbill.billing.account.api.DefaultMutableAccountData)8 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)8 Entitlement (org.killbill.billing.entitlement.api.Entitlement)8 ExpectedPaymentCheck (org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck)7 MockAccountBuilder (org.killbill.billing.mock.MockAccountBuilder)7 DateTimeZone (org.joda.time.DateTimeZone)6 AccountTestUtils.createAccountData (org.killbill.billing.account.AccountTestUtils.createAccountData)6