use of org.killbill.billing.currency.api.CurrencyConversion in project killbill by killbill.
the class DefaultInvoiceFormatter method getProcessedPaymentRate.
@Override
public String getProcessedPaymentRate() {
final Currency currency = getProcessedCurrency();
if (currency == null) {
return null;
}
// If there were multiple payments (and refunds) we pick chose the last one
DateTime latestPaymentDate = null;
final Iterator<InvoicePayment> paymentIterator = invoice.getPayments().iterator();
while (paymentIterator.hasNext()) {
final InvoicePayment cur = paymentIterator.next();
latestPaymentDate = latestPaymentDate != null && latestPaymentDate.isAfter(cur.getPaymentDate()) ? latestPaymentDate : cur.getPaymentDate();
}
try {
final CurrencyConversion conversion = currencyConversionApi.getCurrencyConversion(currency, latestPaymentDate);
for (final Rate rate : conversion.getRates()) {
if (rate.getCurrency() == getCurrency()) {
return rate.getValue().toString();
}
}
} catch (final CurrencyConversionException e) {
logger.warn("Failed to retrieve currency conversion rates for currency='{}', dateConversion='{}'", currency, latestPaymentDate, e);
return null;
}
logger.warn("Failed to retrieve currency conversion rates for currency='{}', dateConversion='{}'", currency, latestPaymentDate);
return null;
}
use of org.killbill.billing.currency.api.CurrencyConversion in project killbill by killbill.
the class TestInvoiceModuleNoDB method installCurrencyConversionApi.
private void installCurrencyConversionApi() {
final CurrencyConversionApi currencyConversionApi = Mockito.mock(CurrencyConversionApi.class);
final CurrencyConversion currencyConversion = Mockito.mock(CurrencyConversion.class);
final Set<Rate> rates = new HashSet<Rate>();
rates.add(new Rate() {
@Override
public Currency getBaseCurrency() {
return Currency.USD;
}
@Override
public Currency getCurrency() {
return Currency.BRL;
}
@Override
public BigDecimal getValue() {
return new BigDecimal("0.4234");
}
@Override
public DateTime getConversionDate() {
return new DateTime(DateTimeZone.UTC);
}
});
Mockito.when(currencyConversion.getRates()).thenReturn(rates);
try {
Mockito.when(currencyConversionApi.getCurrencyConversion(Mockito.<Currency>any(), Mockito.<DateTime>any())).thenReturn(currencyConversion);
} catch (CurrencyConversionException e) {
throw new RuntimeException(e);
}
bind(CurrencyConversionApi.class).toInstance(currencyConversionApi);
}
Aggregations