killbill-uncached
Changes
pom.xml 2(+1 -1)
Details
diff --git a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java
index 461eff6..a7dbf1f 100644
--- a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java
+++ b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/InvoiceResource.java
@@ -459,25 +459,12 @@ public class InvoiceResource extends JaxRsResourceBase {
final CallContext callContext = context.createContext(createdBy, reason, comment, request);
final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
-
- // TODO Get rid of that check once we truly support multiple currencies per account
- // See discussion https://github.com/killbill/killbill/commit/942e214d49e9c7ed89da76d972ee017d2d3ade58#commitcomment-6045547
- final Set<Currency> currencies = new HashSet<Currency>(Lists.<InvoiceItemJson, Currency>transform(ImmutableList.<InvoiceItemJson>copyOf(externalChargesJson),
- new Function<InvoiceItemJson, Currency>() {
- @Override
- public Currency apply(final InvoiceItemJson input) {
- return input.getCurrency();
- }
- }
- ));
- if (currencies.size() != 1 || !currencies.iterator().next().equals(account.getCurrency())) {
- throw new InvoiceApiException(ErrorCode.CURRENCY_INVALID, currencies.iterator().next(), account.getCurrency());
- }
+ final Iterable<InvoiceItemJson> sanitizedExternalChargesJson = cloneRefundItemsWithValidCurrency(account.getCurrency(), externalChargesJson);
// Get the effective date of the external charge, in the account timezone
final LocalDate requestedDate = toLocalDate(account, requestedDateTimeString, callContext);
- final Iterable<InvoiceItem> externalCharges = Iterables.<InvoiceItemJson, InvoiceItem>transform(externalChargesJson,
+ final Iterable<InvoiceItem> externalCharges = Iterables.<InvoiceItemJson, InvoiceItem>transform(sanitizedExternalChargesJson,
new Function<InvoiceItemJson, InvoiceItem>() {
@Override
public InvoiceItem apply(final InvoiceItemJson invoiceItemJson) {
@@ -509,6 +496,40 @@ public class InvoiceResource extends JaxRsResourceBase {
return Response.status(Status.OK).entity(createdExternalChargesJson).build();
}
+ private Iterable<InvoiceItemJson> cloneRefundItemsWithValidCurrency(final Currency accountCurrency, final Iterable<InvoiceItemJson> inputItems) throws InvoiceApiException {
+ try {
+ return Iterables.transform(inputItems, new Function<InvoiceItemJson, InvoiceItemJson>() {
+ @Override
+ public InvoiceItemJson apply(final InvoiceItemJson input) {
+ if (input.getCurrency() != null) {
+ if (!input.getCurrency().equals(accountCurrency)) {
+ throw new IllegalArgumentException(input.getCurrency().toString());
+ }
+ return input;
+ } else {
+ return new InvoiceItemJson(null,
+ input.getInvoiceId(),
+ null, input.getAccountId(),
+ input.getBundleId(),
+ input.getSubscriptionId(),
+ input.getPlanName(),
+ input.getPhaseName(),
+ input.getUsageName(),
+ input.getItemType(),
+ input.getDescription(),
+ input.getStartDate(),
+ input.getEndDate(),
+ input.getAmount(),
+ accountCurrency,
+ null);
+ }
+ }
+ });
+ } catch (IllegalArgumentException e) {
+ throw new InvoiceApiException(ErrorCode.CURRENCY_INVALID, accountCurrency, e.getMessage());
+ }
+ }
+
@Timed
@GET
@Path("/{invoiceId:" + UUID_PATTERN + "}/" + PAYMENTS)
pom.xml 2(+1 -1)
diff --git a/pom.xml b/pom.xml
index a731cc5..57731da 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,7 +21,7 @@
<parent>
<artifactId>killbill-oss-parent</artifactId>
<groupId>org.kill-bill.billing</groupId>
- <version>0.41</version>
+ <version>0.42</version>
</parent>
<artifactId>killbill</artifactId>
<version>0.15.4-SNAPSHOT</version>
diff --git a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestInvoice.java b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestInvoice.java
index c48147d..0c65c04 100644
--- a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestInvoice.java
+++ b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestInvoice.java
@@ -20,6 +20,7 @@ package org.killbill.billing.jaxrs;
import java.math.BigDecimal;
import java.math.RoundingMode;
+import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -351,6 +352,42 @@ public class TestInvoice extends TestJaxrsBase {
assertEquals(killBillClient.getInvoicesForAccount(accountJson.getAccountId()).size(), 3);
}
+
+ @Test(groups = "slow", description = "Can create multiple external charges")
+ public void testExternalCharges() throws Exception {
+ final Account accountJson = createAccountNoPMBundleAndSubscriptionAndWaitForFirstInvoice();
+
+ // Get the invoices
+ assertEquals(killBillClient.getInvoicesForAccount(accountJson.getAccountId()).size(), 2);
+
+ // Post an external charge
+ final BigDecimal chargeAmount = BigDecimal.TEN;
+
+ final List<InvoiceItem> externalCharges = new ArrayList<InvoiceItem>();
+
+ // Does not pass currency to test on purpose that we will default to account currency
+ final InvoiceItem externalCharge1 = new InvoiceItem();
+ externalCharge1.setAccountId(accountJson.getAccountId());
+ externalCharge1.setAmount(chargeAmount);
+ externalCharge1.setDescription(UUID.randomUUID().toString());
+ externalCharges.add(externalCharge1);
+
+ final InvoiceItem externalCharge2 = new InvoiceItem();
+ externalCharge2.setAccountId(accountJson.getAccountId());
+ externalCharge2.setAmount(chargeAmount);
+ externalCharge2.setCurrency(Currency.valueOf(accountJson.getCurrency()));
+ externalCharge2.setDescription(UUID.randomUUID().toString());
+ externalCharges.add(externalCharge2);
+
+ final List<InvoiceItem> createdExternalCharges = killBillClient.createExternalCharges(externalCharges, clock.getUTCNow(), false, createdBy, reason, comment);
+ assertEquals(createdExternalCharges.size(), 2);
+ assertEquals(createdExternalCharges.get(0).getCurrency().toString(), accountJson.getCurrency());
+ assertEquals(createdExternalCharges.get(1).getCurrency().toString(), accountJson.getCurrency());
+
+ // Verify the total number of invoices
+ assertEquals(killBillClient.getInvoicesForAccount(accountJson.getAccountId()).size(), 3);
+ }
+
@Test(groups = "slow", description = "Can create an external charge and trigger a payment")
public void testExternalChargeOnNewInvoiceWithAutomaticPayment() throws Exception {
final Account accountJson = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice();