use of org.killbill.billing.overdue.config.DefaultOverdueConfig in project killbill by killbill.
the class TestInvoicePayment method testWithSuccessfulPaymentFixedToFailure.
@Test(groups = "slow")
public void testWithSuccessfulPaymentFixedToFailure() 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);
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, NextEvent.INVOICE_PAYMENT);
checkODState(OverdueWrapper.CLEAR_STATE_NAME, account.getId());
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(BigDecimal.ZERO) == 0);
assertTrue(invoice1.getPaidAmount().compareTo(new BigDecimal("249.95")) == 0);
assertTrue(invoice1.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
assertEquals(invoice1.getPayments().size(), 1);
assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0);
assertEquals(invoice1.getPayments().get(0).getCurrency(), Currency.USD);
assertTrue(invoice1.getPayments().get(0).isSuccess());
assertNotNull(invoice1.getPayments().get(0).getPaymentId());
final BigDecimal accountBalance1 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
assertTrue(accountBalance1.compareTo(BigDecimal.ZERO) == 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(new BigDecimal("249.95")), 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(new BigDecimal("249.95")), 0);
assertEquals(payments.get(0).getTransactions().get(0).getProcessedCurrency(), Currency.USD);
assertEquals(payments.get(0).getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
assertEquals(payments.get(0).getPaymentAttempts().size(), 1);
assertEquals(payments.get(0).getPaymentAttempts().get(0).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
assertEquals(payments.get(0).getPaymentAttempts().get(0).getStateName(), "SUCCESS");
// Transition the payment to failure
busHandler.pushExpectedEvents(NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
adminPaymentApi.fixPaymentTransactionState(payments.get(0), payments.get(0).getTransactions().get(0), TransactionStatus.PAYMENT_FAILURE, null, null, ImmutableList.<PluginProperty>of(), callContext);
assertListenerStatus();
final Invoice invoice2 = invoiceUserApi.getInvoice(invoice1.getId(), callContext);
assertTrue(invoice2.getBalance().compareTo(new BigDecimal("249.95")) == 0);
assertTrue(invoice2.getPaidAmount().compareTo(BigDecimal.ZERO) == 0);
assertTrue(invoice2.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0);
assertEquals(invoice2.getPayments().size(), 1);
assertEquals(invoice2.getPayments().get(0).getAmount().compareTo(BigDecimal.ZERO), 0);
assertEquals(invoice2.getPayments().get(0).getCurrency(), Currency.USD);
assertFalse(invoice2.getPayments().get(0).isSuccess());
assertNotNull(invoice2.getPayments().get(0).getPaymentId());
final BigDecimal accountBalance2 = invoiceUserApi.getAccountBalance(account.getId(), callContext);
assertTrue(accountBalance2.compareTo(new BigDecimal("249.95")) == 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(BigDecimal.ZERO), 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.PAYMENT_FAILURE);
assertEquals(payments2.get(0).getPaymentAttempts().size(), 1);
assertEquals(payments2.get(0).getPaymentAttempts().get(0).getPluginName(), InvoicePaymentControlPluginApi.PLUGIN_NAME);
// Note that because fixPaymentTransactionState is considered an API call, no retry will be attempted
assertEquals(payments2.get(0).getPaymentAttempts().get(0).getStateName(), "ABORTED");
// Verify account transitions to OD1
addDaysAndCheckForCompletion(2, NextEvent.BLOCK);
checkODState("OD1", account.getId());
}
use of org.killbill.billing.overdue.config.DefaultOverdueConfig in project killbill by killbill.
the class TestOverdueIntegration method testOverdueStateWithTotalUnpaidInvoiceBalanceCondition.
@Test(groups = "slow", description = "Test overdue state with total unpaid invoice balance condition")
public void testOverdueStateWithTotalUnpaidInvoiceBalanceCondition() throws Exception {
// 2012-05-01T00:03:42.000Z
clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
final DefaultOverdueConfig config = XMLLoader.getObjectFromString(Resources.getResource("overdueWithTotalUnpaidInvoiceBalanceCondition.xml").toExternalForm(), DefaultOverdueConfig.class);
overdueConfigCache.loadDefaultOverdueConfig(config);
setupAccount();
paymentPlugin.makeAllInvoicesFailWithError(true);
createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
// 2012-05-31 => DAY 30 have to get out of trial before first payment
addMonthsAndCheckForCompletion(1, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Amount balance should be USD 249.95
assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(BigDecimal.valueOf(249.95)), 0);
// Should still be in clear state
checkODState(OverdueWrapper.CLEAR_STATE_NAME);
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.BLOCK, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Amount balance should be USD 499.90
assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(BigDecimal.valueOf(499.90)), 0);
// Now we should be in OD1
checkODState("OD1");
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.BLOCK, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Amount balance should be USD 749.85
assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(BigDecimal.valueOf(749.85)), 0);
// Now we should be in OD2
checkODState("OD2");
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Amount balance should be USD 999.80
assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(BigDecimal.valueOf(999.80)), 0);
// We should still be in OD2
checkODState("OD2");
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.TAG, NextEvent.BLOCK);
// Amount balance should be USD 1249.75
assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(BigDecimal.valueOf(1249.75)), 0);
// Now we should be in OD3
checkODState("OD3");
// Get all unpaid invoices and pay them to clear the overdue state
paymentPlugin.makeAllInvoicesFailWithError(false);
List<Invoice> unpaidInvoices = getUnpaidInvoicesOrderFromRecent();
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(0), NextEvent.BLOCK, NextEvent.TAG, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(1), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(2), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT, NextEvent.BLOCK);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(3), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT, NextEvent.BLOCK);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(4), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
// We should be clear now
checkODState(OverdueWrapper.CLEAR_STATE_NAME);
}
use of org.killbill.billing.overdue.config.DefaultOverdueConfig in project killbill by killbill.
the class TestOverdueIntegration method testOverdueStateWithNumberOfUnpaidInvoicesCondition.
@Test(groups = "slow", description = "Test overdue state with number of unpaid invoices condition")
public void testOverdueStateWithNumberOfUnpaidInvoicesCondition() throws Exception {
// 2012-05-01T00:03:42.000Z
clock.setTime(new DateTime(2012, 5, 1, 0, 3, 42, 0));
final DefaultOverdueConfig config = XMLLoader.getObjectFromString(Resources.getResource("overdueWithNumberOfUnpaidInvoicesCondition.xml").toExternalForm(), DefaultOverdueConfig.class);
overdueConfigCache.loadDefaultOverdueConfig(config);
setupAccount();
paymentPlugin.makeAllInvoicesFailWithError(true);
createBaseEntitlementAndCheckForCompletion(account.getId(), "externalKey", productName, ProductCategory.BASE, term, NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE);
// 2012-05-31 => DAY 30 have to get out of trial before first payment
addMonthsAndCheckForCompletion(1, NextEvent.PHASE, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Verify that number of unpaid invoices is 1
Assert.assertEquals(invoiceUserApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext).size(), 1);
// Should still be in clear state
checkODState(OverdueWrapper.CLEAR_STATE_NAME);
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.BLOCK, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Verify that number of unpaid invoices is 2
Assert.assertEquals(invoiceUserApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext).size(), 2);
// Now we should be in OD1
checkODState("OD1");
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.BLOCK, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Verify that number of unpaid invoices is 3
Assert.assertEquals(invoiceUserApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext).size(), 3);
// Now we should be in OD2
checkODState("OD2");
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR);
// Verify that number of unpaid invoices is 4
Assert.assertEquals(invoiceUserApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext).size(), 4);
// We should still be in OD2
checkODState("OD2");
// Add 1 month
addMonthsAndCheckForCompletion(1, NextEvent.INVOICE, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.PAYMENT_ERROR, NextEvent.INVOICE_PAYMENT_ERROR, NextEvent.TAG, NextEvent.BLOCK);
// Verify that number of unpaid invoices is 5
Assert.assertEquals(invoiceUserApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext).size(), 5);
// Now we should be in OD3
checkODState("OD3");
// Get all unpaid invoices and pay them to clear the overdue state
paymentPlugin.makeAllInvoicesFailWithError(false);
List<Invoice> unpaidInvoices = getUnpaidInvoicesOrderFromRecent();
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(0), NextEvent.BLOCK, NextEvent.TAG, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(1), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(2), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT, NextEvent.BLOCK);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(3), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT, NextEvent.BLOCK);
createPaymentAndCheckForCompletion(account, unpaidInvoices.get(4), NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
// We should be clear now
checkODState(OverdueWrapper.CLEAR_STATE_NAME);
}
use of org.killbill.billing.overdue.config.DefaultOverdueConfig in project killbill by killbill.
the class OverdueJson method toOverdueConfigWithValidation.
public static OverdueConfig toOverdueConfigWithValidation(final OverdueJson input) {
final DefaultOverdueConfig result = new DefaultOverdueConfig();
final DefaultOverdueStatesAccount overdueStateAccount = new DefaultOverdueStatesAccount();
result.setOverdueStates(overdueStateAccount);
final DefaultOverdueState[] states = new DefaultOverdueState[input.getOverdueStates().size()];
int i = 0;
int prevTimeSinceEarliestUnpaidInvoice = -1;
for (final OverdueStateConfigJson cur : input.getOverdueStates()) {
Preconditions.checkNotNull(cur.getName());
// We only support timeSinceEarliestUnpaidInvoiceEqualsOrExceeds condition (see #611)
Preconditions.checkNotNull(cur.getCondition());
Preconditions.checkNotNull(cur.getCondition().getTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds());
Preconditions.checkNotNull(cur.getCondition().getTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds().getUnit());
Preconditions.checkState(cur.getCondition().getTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds().getUnit() == TimeUnit.DAYS);
Preconditions.checkState(cur.getCondition().getTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds().getNumber() > 0);
final DefaultOverdueState state = new DefaultOverdueState();
state.setName(cur.getName());
state.setExternalMessage(cur.getExternalMessage());
state.setBlockChanges(cur.getBlockChanges());
state.setDisableEntitlement(cur.getDisableEntitlement());
state.setSubscriptionCancellationPolicy(cur.getSubscriptionCancellationPolicy());
state.setClearState(cur.isClearState());
state.setAutoReevaluationInterval(computeReevaluationInterval(cur.getAutoReevaluationIntervalDays(), prevTimeSinceEarliestUnpaidInvoice, cur.getCondition().getTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds().getNumber()));
state.setCondition(OverdueConditionJson.toOverdueCondition(cur.getCondition()));
states[i++] = state;
prevTimeSinceEarliestUnpaidInvoice = cur.getCondition().getTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds().getNumber();
}
overdueStateAccount.setAccountOverdueStates(states);
overdueStateAccount.setInitialReevaluationInterval(computeReevaluationInterval(null, prevTimeSinceEarliestUnpaidInvoice, 0));
return result;
}
Aggregations