Refine search
public void sendPayment(Author author, BigDecimal amount, String url) throws TransferFailedException { try { String note = "Commit payment:\n__" + author.getUsername() + "__ " + url; Transaction transaction = new Transaction(); transaction.setTo(author.getEmail()); transaction.setAmount(Money.of(CurrencyUnit.of("BTC"), amount, RoundingMode.DOWN)); transaction.setNotes(note); Transaction response = coinbase.sendMoney(transaction); if (response.getStatus() != Transaction.Status.COMPLETE) { throw new TransferFailedException(); } } catch (CoinbaseException | IOException e) { throw new TransferFailedException(e); } }
private boolean isSentTransaction(com.coinbase.api.entity.Transaction coinbaseTransaction) { BigDecimal amount = coinbaseTransaction.getAmount().getAmount(); return amount.compareTo(new BigDecimal(0.0)) < 0; }
public BigDecimal getAccountBalance() throws IOException, CoinbaseException { List<Account> accounts = coinbase.getAccounts().getAccounts(); Account primary = null; for (Account account : accounts) { if (account.isPrimary()) { primary = account; break; } } if (primary != null) return coinbase.getBalance(primary.getId()).getAmount(); else return new BigDecimal(0.0); } }
public String parseAmountInDollars(BigDecimal exchangeRate) { return coinbaseTransaction.getAmount().getAmount().abs() .multiply(exchangeRate) .setScale(2, RoundingMode.CEILING) .toPlainString(); }
@Override public Transfer buy(Money amount, String paymentMethodId, Boolean commit) throws CoinbaseException, IOException { URL buysUrl; try { buysUrl = new URL(_baseApiUrl, "buys"); } catch (MalformedURLException ex) { throw new AssertionError(ex); } Request request = newAccountSpecificRequest(); request.setQty(amount.getAmount().doubleValue()); request.setPaymentMethodId(paymentMethodId); request.setCurrency(amount.getCurrencyUnit().getCurrencyCode()); request.setCommit(commit); return post(buysUrl, request, TransferResponse.class).getTransfer(); }
public PriceRepresentation(BigDecimal amount, Currency currency, Locale locale) { MoneyFormatter formatter = new MoneyFormatterBuilder().appendAmount(MoneyAmountStyle.of(locale)).toFormatter(); CurrencyUnit currencyUnit = CurrencyUnit.of(currency); this.amount = formatter.withLocale(locale) .print(Money.of(currencyUnit, amount, RoundingMode.HALF_EVEN)); this.currency = new CurrencyRepresentation(currency, locale); if (amount.doubleValue() == amount.intValue()) { this.amountCompact = "" + amount.intValue(); } else { this.amountCompact = this.amount; } }
/** * Obtains an instance of {@code Money} from an amount in major units. * <p> * This allows you to create an instance with a specific currency and amount. * The amount is a whole number only. Thus you can initialise the value * 'USD 20', but not the value 'USD 20.32'. * For example, {@code ofMajor(USD, 25)} creates the instance {@code USD 25.00}. * * @param currency the currency, not null * @param amountMajor the amount of money in the major division of the currency * @return the new instance, never null */ public static Money ofMajor(CurrencyUnit currency, long amountMajor) { return Money.of(currency, BigDecimal.valueOf(amountMajor), RoundingMode.UNNECESSARY); }
@JsonProperty @JsonDeserialize(using=MoneyDeserializer.class) public void setAmount(Money amount) { _amount = amount; if (amount != null) { setAmountString(amount.getAmount().toPlainString()); setAmountCurrencyIso(amount.getCurrencyUnit().getCurrencyCode()); } else { setAmountString(null); setAmountCurrencyIso(null); } }
@Override public Quote getBuyQuote(Money amount, String paymentMethodId) throws IOException, CoinbaseException { String qtyParam; if(amount.getCurrencyUnit().getCode().equals("BTC")) { qtyParam = "qty"; } else { qtyParam = "native_qty"; } URL buyPriceUrl; try { buyPriceUrl = new URL( _baseApiUrl, "prices/buy?" + qtyParam +"=" + URLEncoder.encode(amount.getAmount().toPlainString(), "UTF-8") + (_accountId != null ? "&account_id=" + _accountId : "") + (paymentMethodId != null ? "&payment_method_id=" + paymentMethodId : "") ); } catch (MalformedURLException ex) { throw new AssertionError(ex); } return deserialize(doHttp(buyPriceUrl, "GET", null), Quote.class); }
public static String getAsString(Money money) { if (money == null) { return null; } return money.getAmount().toString() + " " + money.getCurrencyUnit().toString(); }
/** * Obtains an instance of {@code Money} from a {@code BigDecimal}. * <p> * This allows you to create an instance with a specific currency and amount. * No rounding is performed on the amount, so it must have a scale compatible * with the currency. * * @param currency the currency, not null * @param amount the amount of money, not null * @return the new instance, never null * @throws ArithmeticException if the scale exceeds the currency scale */ public static Money of(CurrencyUnit currency, BigDecimal amount) { MoneyUtils.checkNotNull(currency, "Currency must not be null"); MoneyUtils.checkNotNull(amount, "Amount must not be null"); if (amount.scale() > currency.getDecimalPlaces()) { throw new ArithmeticException("Scale of amount " + amount + " is greater than the scale of the currency " + currency); } return Money.of(currency, amount, RoundingMode.UNNECESSARY); }
/** * Converts this money to an instance of {@code Money}. * * @param roundingMode the rounding mode to use, not null * @return the money instance, never null * @throws ArithmeticException if the rounding fails */ public Money toMoney(RoundingMode roundingMode) { return Money.of(this, roundingMode); }
@Test public void shouldReturnZeroIfNotEligible() { when(paybackPolicy.isEligible(any(Account.class), any(Purchase.class))).thenReturn(FALSE); Money payback = merchant.calculatePaybackFor(account(), purchase()); assertThat(payback).isEqualTo(Money.zero(EUR)); } }
public Money calculatePaybackFor(Account account, Purchase purchase) { if (paybackPolicy.isEligible(account, purchase)) { return purchase.getAmount().multipliedBy(payback.asBigDecimal(), HALF_EVEN); } else { return Money.zero(EUR); } }
public static long toMinorUnits(final String currencyIsoCode, final BigDecimal amountBD, final RoundingMode roundingMode) { // The payment amount specified in minor units, without the decimal separator final CurrencyUnit currencyUnit = CurrencyUnit.of(currencyIsoCode); return Money.of(currencyUnit, amountBD, roundingMode).getAmountMinorLong(); } }
@Override public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { BigDecimal amount = BigDecimalType.INSTANCE.nullSafeGet(rs, names[0], session); String currencyCode = StringType.INSTANCE.nullSafeGet(rs, names[1], session); return amount == null && currencyCode == null ? null : Money.of(CurrencyUnit.getInstance(currencyCode), amount); }
/** * Obtains an instance of {@code Money} as the total value of * a possibly empty collection. * <p> * The amounts are added as though using {@link #plus(Money)} starting * from zero in the specified currency. * All amounts must be in the same currency. * * @param currency the currency to total in, not null * @param monies the monetary values to total, no null elements, not null * @return the total, never null * @throws CurrencyMismatchException if the currencies differ */ public static Money total(CurrencyUnit currency, Iterable<Money> monies) { return Money.zero(currency).plus(monies); }
@Override public Object getPropertyValue(Object component, int propertyIndex) throws HibernateException { if (component == null) { return null; } final Money money = (Money) component; switch (propertyIndex) { case 0: return money.getAmount(); case 1: return money.getCurrencyUnit().getCurrencyCode(); default: throw new HibernateException("Invalid property index [" + propertyIndex + "]"); } }
public Objective credit(Money amount) { savings = savings.plus(amount); return this; }
public Money calculatePaybackFor(Account account, Purchase purchase) { // TODO #3 ask the policy if the payback is available; if yes, then return calculated amount; otherwise return zero return purchase.getAmount().multipliedBy(payback.asBigDecimal(), HALF_EVEN); }