use of org.killbill.billing.invoice.api.InvoiceApiException in project killbill by killbill.
the class InvoicePaymentControlPluginApi method computeRefundAmount.
private BigDecimal computeRefundAmount(final UUID paymentId, @Nullable final BigDecimal specifiedRefundAmount, final Map<UUID, BigDecimal> invoiceItemIdsWithAmounts, final InternalTenantContext context) throws PaymentControlApiException {
if (specifiedRefundAmount != null) {
if (specifiedRefundAmount.compareTo(BigDecimal.ZERO) <= 0) {
throw new PaymentControlApiException("Failed to compute refund: ", new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, "You need to specify a positive refund amount"));
}
return specifiedRefundAmount;
}
try {
final List<InvoiceItem> items = invoiceApi.getInvoiceForPaymentId(paymentId, context).getInvoiceItems();
BigDecimal amountFromItems = BigDecimal.ZERO;
for (final UUID itemId : invoiceItemIdsWithAmounts.keySet()) {
final BigDecimal specifiedItemAmount = invoiceItemIdsWithAmounts.get(itemId);
final BigDecimal itemAmount = getAmountFromItem(items, itemId);
if (specifiedItemAmount != null && (specifiedItemAmount.compareTo(BigDecimal.ZERO) <= 0 || specifiedItemAmount.compareTo(itemAmount) > 0)) {
throw new PaymentControlApiException("Failed to compute refund: ", new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, "You need to specify a valid invoice item amount"));
}
amountFromItems = amountFromItems.add(MoreObjects.firstNonNull(specifiedItemAmount, itemAmount));
}
return amountFromItems;
} catch (final InvoiceApiException e) {
throw new PaymentControlApiException(e);
}
}
use of org.killbill.billing.invoice.api.InvoiceApiException in project killbill by killbill.
the class TestDefaultInvoiceDaoUnit method testComputePositiveRefundAmount.
@Test(groups = "fast")
public void testComputePositiveRefundAmount() throws Exception {
// Verify the cases with no adjustment first
final Map<UUID, BigDecimal> noItemAdjustment = ImmutableMap.<UUID, BigDecimal>of();
verifyComputedRefundAmount(null, null, noItemAdjustment, BigDecimal.ZERO);
verifyComputedRefundAmount(null, BigDecimal.ZERO, noItemAdjustment, BigDecimal.ZERO);
verifyComputedRefundAmount(BigDecimal.TEN, null, noItemAdjustment, BigDecimal.TEN);
verifyComputedRefundAmount(BigDecimal.TEN, BigDecimal.ONE, noItemAdjustment, BigDecimal.ONE);
try {
verifyComputedRefundAmount(BigDecimal.ONE, BigDecimal.TEN, noItemAdjustment, BigDecimal.TEN);
Assert.fail("Shouldn't have been able to compute a refund amount");
} catch (InvoiceApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.REFUND_AMOUNT_TOO_HIGH.getCode());
}
// Try with adjustments now
final Map<UUID, BigDecimal> itemAdjustments = ImmutableMap.<UUID, BigDecimal>of(UUID.randomUUID(), BigDecimal.ONE, UUID.randomUUID(), BigDecimal.TEN, UUID.randomUUID(), BigDecimal.ZERO);
verifyComputedRefundAmount(new BigDecimal("100"), new BigDecimal("11"), itemAdjustments, new BigDecimal("11"));
try {
verifyComputedRefundAmount(new BigDecimal("100"), BigDecimal.TEN, itemAdjustments, BigDecimal.TEN);
Assert.fail("Shouldn't have been able to compute a refund amount");
} catch (InvoiceApiException e) {
Assert.assertEquals(e.getCode(), ErrorCode.REFUND_AMOUNT_DONT_MATCH_ITEMS_TO_ADJUST.getCode());
}
}
use of org.killbill.billing.invoice.api.InvoiceApiException in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testTooManyFixedInvoiceItemsForGivenSubscriptionAndStartDatePostMerge.
// Simulate a bug in the generator where two fixed items for the same day and subscription end up in the resulting items
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testTooManyFixedInvoiceItemsForGivenSubscriptionAndStartDatePostMerge() throws InvoiceApiException {
final Multimap<UUID, LocalDate> createdItemsPerDayPerSubscription = LinkedListMultimap.<UUID, LocalDate>create();
final LocalDate startDate = new LocalDate("2016-01-01");
final Collection<InvoiceItem> resultingItems = new LinkedList<InvoiceItem>();
final InvoiceItem fixedPriceInvoiceItem = new FixedPriceInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), null, account.getId(), subscription.getBundleId(), subscription.getId(), "planName", "phaseName", "description", startDate, BigDecimal.ONE, account.getCurrency());
resultingItems.add(fixedPriceInvoiceItem);
resultingItems.add(fixedPriceInvoiceItem);
try {
fixedAndRecurringInvoiceItemGenerator.safetyBounds(resultingItems, createdItemsPerDayPerSubscription, internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
}
resultingItems.clear();
for (int i = 0; i < 2; i++) {
resultingItems.add(new FixedPriceInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), null, account.getId(), subscription.getBundleId(), subscription.getId(), "planName", "phaseName", "description", startDate, // Amount shouldn't have any effect
BigDecimal.ONE.add(new BigDecimal(i)), account.getCurrency()));
}
try {
fixedAndRecurringInvoiceItemGenerator.safetyBounds(resultingItems, createdItemsPerDayPerSubscription, internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
}
}
use of org.killbill.billing.invoice.api.InvoiceApiException in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testOverlappingItemsWithTooManyRepairs.
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testOverlappingItemsWithTooManyRepairs() throws InvoiceApiException {
final LocalDate startDate = new LocalDate("2016-01-01");
final BillingEventSet events = new MockBillingEventSet();
final BigDecimal amount = BigDecimal.TEN;
final MockInternationalPrice price = new MockInternationalPrice(new DefaultPrice(amount, account.getCurrency()));
final Plan plan = new MockPlan("my-plan");
final PlanPhase planPhase = new MockPlanPhase(price, null, BillingPeriod.MONTHLY, PhaseType.EVERGREEN);
final BillingEvent event = invoiceUtil.createMockBillingEvent(account, subscription, startDate.toDateTimeAtStartOfDay(), plan, planPhase, null, amount, account.getCurrency(), BillingPeriod.MONTHLY, 1, BillingMode.IN_ADVANCE, "Billing Event Desc", 1L, SubscriptionBaseTransitionType.CREATE);
events.add(event);
// Simulate a previous mis-bill: existing item is for [2016-01-01,2016-01-30], proposed will be for [2016-01-01,2016-02-01]
final List<Invoice> existingInvoices = new LinkedList<Invoice>();
final Invoice invoice = new DefaultInvoice(account.getId(), clock.getUTCToday(), startDate, account.getCurrency());
invoice.addInvoiceItem(new RecurringInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), subscription.getBundleId(), subscription.getId(), event.getPlan().getName(), event.getPlanPhase().getName(), startDate, startDate.plusDays(29), amount, amount, account.getCurrency()));
// But the system has already repaired it
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
// Twice!
invoice.addInvoiceItem(new RepairAdjInvoiceItem(UUID.randomUUID(), startDate.toDateTimeAtStartOfDay(), invoice.getId(), account.getId(), startDate, startDate.plusDays(29), // Note! The amount will not matter
BigDecimal.ONE.negate(), account.getCurrency(), invoice.getInvoiceItems().get(0).getId()));
existingInvoices.add(invoice);
try {
final List<InvoiceItem> generatedItems = fixedAndRecurringInvoiceItemGenerator.generateItems(account, UUID.randomUUID(), events, existingInvoices, startDate, account.getCurrency(), new HashMap<UUID, SubscriptionFutureNotificationDates>(), internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
assertTrue(e.getCause().getMessage().startsWith("Too many repairs"));
}
}
use of org.killbill.billing.invoice.api.InvoiceApiException in project killbill by killbill.
the class TestFixedAndRecurringInvoiceItemGenerator method testTooManyRecurringInvoiceItemsForGivenSubscriptionAndServicePeriodPostMerge.
// Simulate a bug in the generator where two recurring items for the same service period and subscription end up in the resulting items
@Test(groups = "fast", description = "https://github.com/killbill/killbill/issues/664")
public void testTooManyRecurringInvoiceItemsForGivenSubscriptionAndServicePeriodPostMerge() throws InvoiceApiException {
final Multimap<UUID, LocalDate> createdItemsPerDayPerSubscription = LinkedListMultimap.<UUID, LocalDate>create();
final LocalDate startDate = new LocalDate("2016-01-01");
final Collection<InvoiceItem> resultingItems = new LinkedList<InvoiceItem>();
final InvoiceItem recurringInvoiceItem = new RecurringInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), null, account.getId(), subscription.getBundleId(), subscription.getId(), "planName", "phaseName", startDate, startDate.plusMonths(1), BigDecimal.ONE, BigDecimal.ONE, account.getCurrency());
resultingItems.add(recurringInvoiceItem);
resultingItems.add(recurringInvoiceItem);
try {
fixedAndRecurringInvoiceItemGenerator.safetyBounds(resultingItems, createdItemsPerDayPerSubscription, internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
}
resultingItems.clear();
for (int i = 0; i < 2; i++) {
resultingItems.add(new RecurringInvoiceItem(UUID.randomUUID(), clock.getUTCNow(), null, account.getId(), subscription.getBundleId(), subscription.getId(), "planName", "phaseName", startDate, startDate.plusMonths(1), // Amount shouldn't have any effect
BigDecimal.TEN, BigDecimal.ONE, account.getCurrency()));
}
try {
fixedAndRecurringInvoiceItemGenerator.safetyBounds(resultingItems, createdItemsPerDayPerSubscription, internalCallContext);
fail();
} catch (final InvoiceApiException e) {
assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode());
}
}
Aggregations