private Payment setTerminalStateOnRetryWithAccountLocked(final Account account, final Invoice invoice, final PaymentModelDao payment, final BigDecimal requestedAmount, final String terminalStateReason, final InternalCallContext context) {
final PaymentStatus paymentStatus;
switch (payment.getPaymentStatus()) {
case PAYMENT_FAILURE:
paymentStatus = PaymentStatus.PAYMENT_FAILURE_ABORTED;
break;
case PLUGIN_FAILURE:
case UNKNOWN:
paymentStatus = PaymentStatus.PLUGIN_FAILURE_ABORTED;
break;
case AUTO_PAY_OFF:
default:
throw new IllegalStateException("Unexpected payment status for retry " + payment.getPaymentStatus());
}
final PaymentAttemptModelDao attempt = new PaymentAttemptModelDao(account.getId(), invoice.getId(), payment.getId(), account.getPaymentMethodId(), clock.getUTCNow(),
requestedAmount, invoice.getCurrency());
paymentDao.updatePaymentWithNewAttempt(payment.getId(), attempt, context);
paymentDao.updatePaymentAndAttemptOnCompletion(payment.getId(), paymentStatus, requestedAmount, account.getCurrency(), attempt.getId(), null, terminalStateReason, context);
final List<PaymentAttemptModelDao> allAttempts = paymentDao.getAttemptsForPayment(payment.getId(), context);
return new DefaultPayment(payment, null, allAttempts, Collections.<RefundModelDao>emptyList());
}