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; }
@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); }
@TimedResource @GET @Path("/{paymentId:" + UUID_PATTERN + "}/") @Produces(APPLICATION_JSON) @ApiOperation(value = "Retrieve a payment by id", response = InvoicePaymentJson.class) @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid payment id supplied"), @ApiResponse(code = 404, message = "Payment not found")}) public Response getInvoicePayment(@PathParam("paymentId") final UUID paymentId, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException { final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString); final TenantContext tenantContext = context.createTenantContextNoAccountId(request); final Payment payment = paymentApi.getPayment(paymentId, withPluginInfo, withAttempts, pluginProperties, tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(payment.getAccountId(), auditMode.getLevel(), tenantContext); final List<InvoicePayment> invoicePayments = invoicePaymentApi.getInvoicePayments(paymentId, tenantContext); final InvoicePayment invoicePayment = Iterables.tryFind(invoicePayments, new Predicate<InvoicePayment>() { @Override public boolean apply(final InvoicePayment input) { return input.getType() == InvoicePaymentType.ATTEMPT; } }).orNull(); final UUID invoiceId = invoicePayment != null ? invoicePayment.getInvoiceId() : null; final InvoicePaymentJson result = new InvoicePaymentJson(payment, invoiceId, accountAuditLogs); return Response.status(Response.Status.OK).entity(result).build(); }
final UUID invoiceId = originalInvoicePaymentAttempt != null ? originalInvoicePaymentAttempt.getInvoiceId() : null; if (invoiceId == null) { return Response.status(Status.NOT_FOUND).build();
public static InvoicePayment createAndPersistPayment(final InvoiceInternalApi invoicePaymentApi, final Clock clock, final UUID invoiceId, final BigDecimal amount, final Currency currency, final InternalCallContext callContext) throws InvoiceApiException { final InvoicePayment payment = Mockito.mock(InvoicePayment.class); Mockito.when(payment.getId()).thenReturn(UUID.randomUUID()); Mockito.when(payment.getType()).thenReturn(InvoicePaymentType.ATTEMPT); Mockito.when(payment.getInvoiceId()).thenReturn(invoiceId); Mockito.when(payment.getPaymentId()).thenReturn(UUID.randomUUID()); Mockito.when(payment.getPaymentCookieId()).thenReturn(UUID.randomUUID().toString()); final DateTime utcNow = clock.getUTCNow(); Mockito.when(payment.getPaymentDate()).thenReturn(utcNow); Mockito.when(payment.getAmount()).thenReturn(amount); Mockito.when(payment.getCurrency()).thenReturn(currency); Mockito.when(payment.getProcessedCurrency()).thenReturn(currency); Mockito.when(payment.isSuccess()).thenReturn(true); invoicePaymentApi.recordPaymentAttemptCompletion(payment.getInvoiceId(), payment.getAmount(), payment.getCurrency(), payment.getProcessedCurrency(), payment.getPaymentId(), payment.getPaymentCookieId(), payment.getPaymentDate(), payment.isSuccess(), callContext); return payment; } }
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 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()); }
private void checkPayment(final UUID accountId, final Payment payment, final CallContext context, final ExpectedPaymentCheck expected) { Assert.assertEquals(payment.getAccountId(), accountId); Assert.assertEquals(payment.getCurrency(), expected.getCurrency()); if (expected.getInvoiceId() != null) { for (final InvoicePayment invoicePayment : invoicePaymentApi.getInvoicePayments(payment.getId(), context)) { Assert.assertEquals(invoicePayment.getInvoiceId(), expected.getInvoiceId()); } } final PaymentTransaction transaction = getPurchaseTransaction(payment); Assert.assertTrue(transaction.getAmount().compareTo(expected.getAmount()) == 0, "Actual amount " + transaction.getAmount() + ", expected amount " + expected.getAmount()); Assert.assertEquals(transaction.getTransactionStatus(), expected.getStatus()); Assert.assertEquals(transaction.getEffectiveDate().toLocalDate().compareTo(expected.getPaymentDate()), 0, "Actual date " + transaction.getEffectiveDate() + ", expected date " + expected.getPaymentDate()); auditChecker.checkPaymentCreated(payment, context); }