Search in sources :

Example 1 with CurrencyConversionException

use of org.killbill.billing.currency.api.CurrencyConversionException 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;
}
Also used : InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Rate(org.killbill.billing.currency.api.Rate) Currency(org.killbill.billing.catalog.api.Currency) CurrencyConversion(org.killbill.billing.currency.api.CurrencyConversion) CurrencyConversionException(org.killbill.billing.currency.api.CurrencyConversionException) DateTime(org.joda.time.DateTime)

Example 2 with CurrencyConversionException

use of org.killbill.billing.currency.api.CurrencyConversionException 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);
}
Also used : Rate(org.killbill.billing.currency.api.Rate) CurrencyConversionApi(org.killbill.billing.currency.api.CurrencyConversionApi) Currency(org.killbill.billing.catalog.api.Currency) CurrencyConversion(org.killbill.billing.currency.api.CurrencyConversion) CurrencyConversionException(org.killbill.billing.currency.api.CurrencyConversionException) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet)

Aggregations

DateTime (org.joda.time.DateTime)2 Currency (org.killbill.billing.catalog.api.Currency)2 CurrencyConversion (org.killbill.billing.currency.api.CurrencyConversion)2 CurrencyConversionException (org.killbill.billing.currency.api.CurrencyConversionException)2 Rate (org.killbill.billing.currency.api.Rate)2 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 CurrencyConversionApi (org.killbill.billing.currency.api.CurrencyConversionApi)1 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)1