public InvoicePaymentModelDao(final InvoicePayment invoicePayment) { this(invoicePayment.getId(), invoicePayment.getCreatedDate(), invoicePayment.getType(), invoicePayment.getInvoiceId(), invoicePayment.getPaymentId(), invoicePayment.getPaymentDate(), invoicePayment.getAmount(), invoicePayment.getCurrency(), invoicePayment.getProcessedCurrency(), invoicePayment.getPaymentCookieId(), invoicePayment.getLinkedInvoicePaymentId(), invoicePayment.isSuccess()); }
@Override public boolean apply(final InvoicePayment input) { return input.getPaymentId().equals(payment.getId()) && input.getType() == InvoicePaymentType.ATTEMPT; } }).orNull();
assertTrue(invoice1.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0); assertEquals(invoice1.getPayments().size(), 1); assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(BigDecimal.ZERO), 0); assertEquals(invoice1.getPayments().get(0).getCurrency(), Currency.USD); assertFalse(invoice1.getPayments().get(0).isSuccess()); assertNotNull(invoice1.getPayments().get(0).getPaymentId()); assertTrue(invoice2.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0); assertEquals(invoice2.getPayments().size(), 1); assertTrue(invoice2.getPayments().get(0).isSuccess());
Assert.assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(new BigDecimal("4.00")), 0); Assert.assertNull(invoice1.getPayments().get(0).getLinkedInvoicePaymentId()); Assert.assertEquals(invoice1.getPayments().get(0).getPaymentCookieId(), payment1.getTransactions().get(0).getExternalKey()); Assert.assertEquals(invoice1.getPayments().get(0).getPaymentId(), payment1.getId()); Assert.assertEquals(invoice1.getPayments().get(0).getType(), InvoicePaymentType.ATTEMPT); Assert.assertTrue(invoice1.getPayments().get(0).isSuccess()); Assert.assertEquals(invoice1.getPayments().get(1).getAmount().compareTo(new BigDecimal("6.00")), 0); Assert.assertNull(invoice1.getPayments().get(1).getLinkedInvoicePaymentId()); Assert.assertEquals(invoice1.getPayments().get(1).getPaymentCookieId(), payment2.getTransactions().get(0).getExternalKey()); Assert.assertEquals(invoice1.getPayments().get(1).getPaymentId(), payment2.getId()); Assert.assertEquals(invoice1.getPayments().get(1).getType(), InvoicePaymentType.ATTEMPT); Assert.assertTrue(invoice1.getPayments().get(1).isSuccess()); Assert.assertEquals(invoice1.getPayments().get(2).getAmount().compareTo(new BigDecimal("-4.00")), 0); Assert.assertEquals(invoice1.getPayments().get(2).getLinkedInvoicePaymentId(), invoice1.getPayments().get(0).getId()); Assert.assertEquals(invoice1.getPayments().get(2).getPaymentCookieId(), payment1.getTransactions().get(1).getExternalKey()); Assert.assertEquals(invoice1.getPayments().get(2).getPaymentId(), payment1.getId()); Assert.assertEquals(invoice1.getPayments().get(2).getType(), InvoicePaymentType.REFUND); Assert.assertTrue(invoice1.getPayments().get(2).isSuccess()); Assert.assertEquals(invoice1.getPayments().get(3).getAmount().compareTo(new BigDecimal("-6.00")), 0); Assert.assertEquals(invoice1.getPayments().get(3).getLinkedInvoicePaymentId(), invoice1.getPayments().get(1).getId()); Assert.assertEquals(invoice1.getPayments().get(3).getPaymentCookieId(), payment2.getTransactions().get(1).getExternalKey()); Assert.assertEquals(invoice1.getPayments().get(3).getPaymentId(), payment2.getId()); Assert.assertEquals(invoice1.getPayments().get(3).getType(), InvoicePaymentType.REFUND); Assert.assertTrue(invoice1.getPayments().get(3).isSuccess());
public PaymentTransaction checkRefund(final UUID paymentId, final CallContext context, ExpectedRefundCheck expected) throws PaymentApiException { final Payment payment = paymentApi.getPayment(paymentId, false, false, ImmutableList.<PluginProperty>of(), context); final PaymentTransaction refund = Iterables.tryFind(payment.getTransactions(), new Predicate<PaymentTransaction>() { @Override public boolean apply(final PaymentTransaction input) { return input.getTransactionType() == TransactionType.REFUND; } }).orNull(); Assert.assertNotNull(refund); final InvoicePayment refundInvoicePayment = getInvoicePaymentEntry(paymentId, InvoicePaymentType.REFUND, context); final InvoicePayment invoicePayment = getInvoicePaymentEntry(paymentId, InvoicePaymentType.ATTEMPT, context); Assert.assertEquals(refund.getPaymentId(), expected.getPaymentId()); Assert.assertEquals(refund.getCurrency(), expected.getCurrency()); Assert.assertEquals(refund.getAmount().compareTo(expected.getRefundAmount()), 0); Assert.assertEquals(refundInvoicePayment.getPaymentId(), paymentId); Assert.assertEquals(refundInvoicePayment.getLinkedInvoicePaymentId(), invoicePayment.getId()); Assert.assertEquals(refundInvoicePayment.getPaymentCookieId(), refund.getExternalKey()); Assert.assertEquals(refundInvoicePayment.getInvoiceId(), invoicePayment.getInvoiceId()); Assert.assertEquals(refundInvoicePayment.getAmount().compareTo(expected.getRefundAmount().negate()), 0); Assert.assertEquals(refundInvoicePayment.getCurrency(), expected.getCurrency()); return refund; }
private void verifyRefund(final Invoice invoice, final BigDecimal invoiceAmount, final BigDecimal refundAmount, final BigDecimal finalInvoiceAmount, final boolean adjusted, final Map<UUID, BigDecimal> invoiceItemIdsWithAmounts) throws InvoiceApiException { final InvoicePayment payment = createAndPersistPayment(invoiceInternalApi, clock, invoice.getId(), invoiceAmount, CURRENCY, internalCallContext); // Verify the initial invoice balance final BigDecimal initialInvoiceBalance = invoiceInternalApi.getInvoiceById(invoice.getId(), internalCallContext).getBalance(); Assert.assertEquals(initialInvoiceBalance.compareTo(BigDecimal.ZERO), 0); // Create a full refund with no adjustment final InvoicePayment refund = invoiceInternalApi.recordRefund(payment.getPaymentId(), refundAmount, adjusted, invoiceItemIdsWithAmounts, UUID.randomUUID().toString(), internalCallContext); Assert.assertEquals(refund.getAmount().compareTo(refundAmount.negate()), 0); Assert.assertEquals(refund.getCurrency(), CURRENCY); Assert.assertEquals(refund.getInvoiceId(), invoice.getId()); Assert.assertEquals(refund.getPaymentId(), payment.getPaymentId()); Assert.assertEquals(refund.getType(), InvoicePaymentType.REFUND); // Verify the current invoice balance final BigDecimal newInvoiceBalance = invoiceInternalApi.getInvoiceById(invoice.getId(), internalCallContext).getBalance(); Assert.assertEquals(newInvoiceBalance.compareTo(finalInvoiceAmount), 0); } }
public static BigDecimal computeInvoiceAmountRefunded(final Currency currency, @Nullable final Iterable<InvoicePayment> invoicePayments) { BigDecimal amountRefunded = BigDecimal.ZERO; if (invoicePayments == null || !invoicePayments.iterator().hasNext()) { return KillBillMoney.of(amountRefunded, currency); } for (final InvoicePayment invoicePayment : invoicePayments) { if (invoicePayment.isSuccess() == null || !invoicePayment.isSuccess()) { continue; } if (InvoicePaymentType.REFUND.equals(invoicePayment.getType()) || InvoicePaymentType.CHARGED_BACK.equals(invoicePayment.getType())) { amountRefunded = amountRefunded.add(invoicePayment.getAmount()); } } return KillBillMoney.of(amountRefunded, currency); } }
@Override public UUID apply(final InvoicePayment input) { return input.getPaymentId(); } }));
public static UUID getInvoiceId(final List<InvoicePayment> invoicePayments, final Payment payment) { final InvoicePayment invoicePayment = Iterables.tryFind(invoicePayments, new Predicate<InvoicePayment>() { @Override public boolean apply(final InvoicePayment input) { return input.getPaymentId().equals(payment.getId()) && input.getType() == InvoicePaymentType.ATTEMPT; } }).orNull(); return invoicePayment != null ? invoicePayment.getInvoiceId() : null; }
Assert.assertEquals(updateInvoice2.getPayments().get(0).getPaymentCookieId(), originalTransaction.getExternalKey()); Assert.assertEquals(updateInvoice3.getBalance().compareTo(BigDecimal.ZERO), 0); Assert.assertEquals(updateInvoice3.getPayments().size(), 1); Assert.assertEquals(updateInvoice3.getPayments().get(0).getPaymentCookieId(), originalTransaction.getExternalKey()); Assert.assertTrue(updateInvoice3.getPayments().get(0).isSuccess()); Assert.assertEquals(invoiceUserApi.getAccountBalance(account.getId(), callContext).compareTo(invoice2.getBalance()), 0);
@Override public boolean apply(final InvoicePayment input) { return input.getType() == InvoicePaymentType.ATTEMPT; } }).orNull();
Currency.USD, null, payment.getId()); invoiceUtil.createPayment(refund, context); final CreditBalanceAdjInvoiceItem cbaItem2 = new CreditBalanceAdjInvoiceItem(invoice1.getId(), accountId, new LocalDate(), rate2.negate(), Currency.USD);
@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; }
assertTrue(invoice1.getChargedAmount().compareTo(new BigDecimal("249.95")) == 0); assertEquals(invoice1.getPayments().size(), 1); assertEquals(invoice1.getPayments().get(0).getAmount().compareTo(new BigDecimal("249.95")), 0); assertEquals(invoice1.getPayments().get(0).getCurrency(), Currency.USD); assertFalse(invoice1.getPayments().get(0).isSuccess()); assertNotNull(invoice1.getPayments().get(0).getPaymentId());
public static BigDecimal computeInvoiceAmountPaid(final Currency currency, @Nullable final Iterable<InvoicePayment> invoicePayments) { BigDecimal amountPaid = BigDecimal.ZERO; if (invoicePayments == null || !invoicePayments.iterator().hasNext()) { return KillBillMoney.of(amountPaid, currency); } for (final InvoicePayment invoicePayment : invoicePayments) { if (!invoicePayment.isSuccess()) { continue; } if (InvoicePaymentType.ATTEMPT.equals(invoicePayment.getType())) { amountPaid = amountPaid.add(invoicePayment.getAmount()); } } return KillBillMoney.of(amountPaid, currency); }
payment.getPaymentExternalKey(), null, pluginProperties, callContext); return result != null ? uriBuilder.buildResponse(uriInfo, InvoicePaymentResource.class, "getInvoicePayment", result.getPaymentId(), request) : Response.status(Status.NO_CONTENT).build();
@Override public boolean apply(@Nullable final InvoicePayment invoicePayment) { return invoicePayment.getType() == type && invoicePayment.getPaymentId().equals(paymentId); } });
@Override public Map<UUID, BigDecimal> validateInvoiceItemAdjustments(final UUID paymentId, final Map<UUID, BigDecimal> idWithAmount, final InternalTenantContext context) throws InvoiceApiException { // We want to validate that only refund with invoice *item* adjustments are allowed (as opposed to refund with invoice adjustment) if (idWithAmount.size() == 0) { throw new InvoiceApiException(ErrorCode.INVOICE_ITEMS_ADJUSTMENT_MISSING); } final InvoicePayment invoicePayment = getInvoicePayment(paymentId, InvoicePaymentType.ATTEMPT, context); return dao.computeItemAdjustments(invoicePayment.getInvoiceId().toString(), idWithAmount, context); }