public PaymentApiException(final InvoiceApiException e) { super(e, e.getCode(), e.getMessage()); }
public PaymentApiException(final InvoiceApiException e) { super(e, e.getCode(), e.getMessage()); }
@TimedResource @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Trigger an invoice generation", response = InvoiceJson.class) @ApiResponses(value = {@ApiResponse(code = 201, message = "Created invoice successfully"), @ApiResponse(code = 400, message = "Invalid account id or target datetime supplied")}) public Response createFutureInvoice(@ApiParam(required=true) @QueryParam(QUERY_ACCOUNT_ID) final UUID accountId, @QueryParam(QUERY_TARGET_DATE) final String targetDate, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws AccountApiException, InvoiceApiException { final CallContext callContext = context.createCallContextWithAccountId(accountId, createdBy, reason, comment, request); final LocalDate inputDate = toLocalDate(targetDate); try { final Invoice generatedInvoice = invoiceApi.triggerInvoiceGeneration(accountId, inputDate, callContext); return uriBuilder.buildResponse(uriInfo, InvoiceResource.class, "getInvoice", generatedInvoice.getId(), request); } catch (InvoiceApiException e) { if (e.getCode() == ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) { return Response.status(Status.NOT_FOUND).build(); } throw e; } }
public void handleParentInvoiceCommitmentEvent(final UUID invoiceId, final UUID userToken, final Long accountRecordId, final Long tenantRecordId) { try { final InternalCallContext context = internalCallContextFactory.createInternalCallContext(tenantRecordId, accountRecordId, "Commit Invoice", CallOrigin.INTERNAL, UserType.SYSTEM, userToken); invoiceApi.commitInvoice(invoiceId, context); } catch (final InvoiceApiException e) { // In case we commit parent invoice earlier we expect to see an INVOICE_INVALID_STATUS status if (ErrorCode.INVOICE_INVALID_STATUS.getCode() != e.getCode()) { log.error(e.getMessage()); } } }
@Override public void write(final OutputStream output) throws IOException, WebApplicationException { try { final JsonGenerator generator = mapper.getFactory().createGenerator(output); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.writeStartObject(); while (iterator.hasNext()) { final Tag tag = iterator.next(); final UUID accountId = tag.getObjectId(); try { invoiceUserApi.triggerInvoiceGeneration(accountId, clock.getUTCToday(), callContext); generator.writeStringField(accountId.toString(), OK); } catch (final InvoiceApiException e) { if (e.getCode() != ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) { log.warn("Unable to trigger invoice generation for accountId='{}'", accountId); } generator.writeStringField(accountId.toString(), ErrorCode.fromCode(e.getCode()).toString()); } } generator.writeEndObject(); generator.close(); } finally { // In case the client goes away (IOException), make sure to close the underlying DB connection tags.close(); } } };
@Test(groups = "slow") public void testCreateRefundOnNonExistingPayment() throws Exception { try { invoiceDao.createRefund(UUID.randomUUID(), BigDecimal.TEN, false, ImmutableMap.<UUID, BigDecimal>of(), null, context); Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_PAYMENT_BY_ATTEMPT_NOT_FOUND.getCode()); } }
@Override public Response toResponse(final InvoiceApiException exception) { if (exception.getCode() == ErrorCode.INVOICE_ACCOUNT_ID_INVALID.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_INVALID_DATE_SEQUENCE.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_INVALID_TRANSITION.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_NO_ACCOUNT_ID_FOR_SUBSCRIPTION_ID.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_NO_SUCH_CREDIT.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_NOT_FOUND.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_PAYMENT_BY_ATTEMPT_NOT_FOUND.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_PAYMENT_NOT_FOUND.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_TARGET_DATE_TOO_FAR_IN_THE_FUTURE.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.CREDIT_AMOUNT_INVALID.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_ITEM_ADJUSTMENT_AMOUNT_SHOULD_BE_POSITIVE.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_ITEM_NOT_FOUND.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.INVOICE_NO_SUCH_EXTERNAL_CHARGE.getCode()) { return buildBadRequestResponse(exception, uriInfo);
@Test(groups = "slow") public void testCantAdjustInvoiceWithNegativeAmount() throws Exception { try { invoiceUserApi.insertCreditForInvoice(accountId, invoiceId, BigDecimal.TEN.negate(), clock.getUTCToday(), accountCurrency, null, null, null, callContext); Assert.fail("Should not have been able to adjust an invoice with a negative amount"); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CREDIT_AMOUNT_INVALID.getCode()); } }
@Test(groups = "slow") public void testCantAdjustInvoiceItemWithNegativeAmount() throws Exception { final InvoiceItem invoiceItem = invoiceUserApi.getInvoice(invoiceId, callContext).getInvoiceItems().get(0); try { invoiceUserApi.insertInvoiceItemAdjustment(accountId, invoiceId, invoiceItem.getId(), clock.getUTCToday(), BigDecimal.TEN.negate(), accountCurrency, null, null, null, callContext); Assert.fail("Should not have been able to adjust an item with a negative amount"); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ITEM_ADJUSTMENT_AMOUNT_SHOULD_BE_POSITIVE.getCode()); } }
@Test(groups = "fast") public void testComputePositiveRefundAmount() throws Exception { // Verify the cases with no adjustment first final Map<UUID, BigDecimal> noItemAdjustment = ImmutableMap.<UUID, BigDecimal>of(); verifyComputedRefundAmount(null, null, noItemAdjustment, BigDecimal.ZERO); verifyComputedRefundAmount(null, BigDecimal.ZERO, noItemAdjustment, BigDecimal.ZERO); verifyComputedRefundAmount(BigDecimal.TEN, null, noItemAdjustment, BigDecimal.TEN); verifyComputedRefundAmount(BigDecimal.TEN, BigDecimal.ONE, noItemAdjustment, BigDecimal.ONE); try { verifyComputedRefundAmount(BigDecimal.ONE, BigDecimal.TEN, noItemAdjustment, BigDecimal.TEN); Assert.fail("Shouldn't have been able to compute a refund amount"); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.REFUND_AMOUNT_TOO_HIGH.getCode()); } // Try with adjustments now final Map<UUID, BigDecimal> itemAdjustments = ImmutableMap.<UUID, BigDecimal>of(UUID.randomUUID(), BigDecimal.ONE, UUID.randomUUID(), BigDecimal.TEN, UUID.randomUUID(), BigDecimal.ZERO); verifyComputedRefundAmount(new BigDecimal("100"), new BigDecimal("11"), itemAdjustments, new BigDecimal("11")); try { verifyComputedRefundAmount(new BigDecimal("100"), BigDecimal.TEN, itemAdjustments, BigDecimal.TEN); Assert.fail("Shouldn't have been able to compute a refund amount"); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.REFUND_AMOUNT_DONT_MATCH_ITEMS_TO_ADJUST.getCode()); } }
Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_NOT_FOUND.getCode()); Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_INVALID_NUMBER.getCode()); Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_NUMBER_NOT_FOUND.getCode()); Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CHARGE_BACK_DOES_NOT_EXIST.getCode()); Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ITEM_NOT_FOUND.getCode()); Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ITEM_NOT_FOUND.getCode());
@Test(groups = "slow") public void testVoidInvoiceThatIsPaid() throws Exception { InternalCallContext context = internalCallContextFactory.createInternalCallContext(accountId, callContext); // Verify the initial invoice balance final BigDecimal invoiceBalance = invoiceUserApi.getInvoice(invoiceId, callContext).getBalance(); Assert.assertEquals(invoiceBalance.compareTo(BigDecimal.ZERO), 1); // Verify the initial account balance final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext); Assert.assertEquals(accountBalance, invoiceBalance); // create payment final InvoicePayment payment = new DefaultInvoicePayment(InvoicePaymentType.ATTEMPT, UUID.randomUUID(), invoiceId, new DateTime(), invoiceBalance, Currency.USD, Currency.USD, null, true); invoiceUtil.createPayment(payment, context); // try to void invoice, it should fail try { invoiceUserApi.voidInvoice(invoiceId, callContext); Assert.fail("Should fail to void invoice that is already paid"); } catch (final InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CAN_NOT_VOID_INVOICE_THAT_IS_PAID.getCode()); } } }
protected void checkNoMoreInvoiceToGenerate(final Account account) { busHandler.pushExpectedEvent(NextEvent.NULL_INVOICE); try { invoiceUserApi.triggerInvoiceGeneration(account.getId(), clock.getUTCToday(), callContext); fail("Should not have generated an extra invoice"); } catch (final InvoiceApiException e) { assertListenerStatus(); assertEquals(e.getCode(), ErrorCode.INVOICE_NOTHING_TO_DO.getCode()); } }
fail(); } catch (final InvoiceApiException e) { assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode()); assertTrue(e.getCause().getMessage().startsWith("Missing subscription id"));
fail(); } catch (final InvoiceApiException e) { assertEquals(e.getCode(), ErrorCode.UNEXPECTED_ERROR.getCode()); assertTrue(e.getCause().getMessage().startsWith("Missing cancelledItem"));
fail(); } catch (final InvoiceApiException e) { assertEquals(e.getCode(), INVOICE_NOTHING_TO_DO.getCode());
fail(); } catch (final InvoiceApiException e) { assertEquals(e.getCode(), INVOICE_NOTHING_TO_DO.getCode());
Assert.fail("Should fail to add external charge on already committed invoice"); } catch (final InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ALREADY_COMMITTED.getCode()); Assert.fail("Should fail to add credit on already committed invoice"); } catch (final InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_ALREADY_COMMITTED.getCode());
Assert.fail(); } catch (InvoiceApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.INVOICE_WOULD_BE_NEGATIVE.getCode());