PaymentLog allocateTokensWithRetries(PaymentLog paymentLog)
throws Throwable {
if (paymentLog.getUsdAmount() == null) {
throw new IllegalArgumentException("PaymentLog's amount in USD must not be null.");
}
if (paymentLog.getBlockTime() == null) {
throw new IllegalArgumentException("PaymentLog's block time must not be null.");
}
LOG.debug("Calling token allocation with {} USD for {} transaction {}.",
paymentLog.getUsdAmount().toPlainString(), paymentLog.getCurrency(),
paymentLog.getTransactionId());
try {
PaymentLog updatedPaymentLog = retryer.call(
() -> monitorService.allocateTokens(paymentLog));
LOG.debug("Allocated {} tomics for {} transaction {}.",
updatedPaymentLog.getAllocatedTomics(), updatedPaymentLog.getCurrency(),
updatedPaymentLog.getTransactionId());
return updatedPaymentLog;
} catch (Throwable e) {
LOG.error("Failed to distribute payment to tiers for {} transaction {}.", paymentLog.getCurrency().name(), paymentLog.getTransactionId(), e.getCause());
RefundReason reason = RefundReason.TOKEN_ALLOCATION_FAILED;
monitorService.createRefundEntryForPaymentLogAndCommit(paymentLog, reason);
throw e;
}
}