Example #1
0
  @POST
  @Produces(APPLICATION_JSON)
  @Consumes(APPLICATION_JSON)
  @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENTS)
  public Response payAllInvoices(
      @PathParam("accountId") final String accountId,
      @QueryParam(QUERY_PAYMENT_EXTERNAL) @DefaultValue("false") final Boolean externalPayment,
      @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)
      throws AccountApiException, PaymentApiException {
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);

    final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
    final Collection<Invoice> unpaidInvoices =
        invoiceApi.getUnpaidInvoicesByAccountId(account.getId(), clock.getUTCToday(), callContext);
    for (final Invoice invoice : unpaidInvoices) {
      if (externalPayment) {
        paymentApi.createExternalPayment(
            account, invoice.getId(), invoice.getBalance(), callContext);
      } else {
        paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext);
      }
    }
    return Response.status(Status.OK).build();
  }
Example #2
0
  /*
   * ************************** REFUNDS ********************************
   */
  @GET
  @Path("/{accountId:" + UUID_PATTERN + "}/" + REFUNDS)
  @Produces(APPLICATION_JSON)
  public Response getRefunds(
      @PathParam("accountId") final String accountId,
      @javax.ws.rs.core.Context final HttpServletRequest request)
      throws AccountApiException, PaymentApiException {
    final TenantContext tenantContext = context.createContext(request);

    final Account account =
        accountUserApi.getAccountById(UUID.fromString(accountId), tenantContext);
    final List<Refund> refunds = paymentApi.getAccountRefunds(account, tenantContext);
    final List<RefundJson> result =
        new ArrayList<RefundJson>(
            Collections2.transform(
                refunds,
                new Function<Refund, RefundJson>() {
                  @Override
                  public RefundJson apply(Refund input) {
                    // TODO Return adjusted items and audits
                    return new RefundJson(input, null, null);
                  }
                }));

    return Response.status(Status.OK).entity(result).build();
  }
Example #3
0
  @GET
  @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS)
  @Produces(APPLICATION_JSON)
  public Response getPaymentMethods(
      @PathParam("accountId") final String accountId,
      @QueryParam(QUERY_PAYMENT_METHOD_PLUGIN_INFO) @DefaultValue("false")
          final Boolean withPluginInfo,
      @javax.ws.rs.core.Context final HttpServletRequest request)
      throws AccountApiException, PaymentApiException {
    final TenantContext tenantContext = context.createContext(request);

    final Account account =
        accountUserApi.getAccountById(UUID.fromString(accountId), tenantContext);
    final List<PaymentMethod> methods =
        paymentApi.getPaymentMethods(account, withPluginInfo, tenantContext);
    final List<PaymentMethodJson> json =
        new ArrayList<PaymentMethodJson>(
            Collections2.transform(
                methods,
                new Function<PaymentMethod, PaymentMethodJson>() {
                  @Override
                  public PaymentMethodJson apply(final PaymentMethod input) {
                    return PaymentMethodJson.toPaymentMethodJson(account, input);
                  }
                }));

    return Response.status(Status.OK).entity(json).build();
  }
Example #4
0
  @POST
  @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS)
  @Consumes(APPLICATION_JSON)
  @Produces(APPLICATION_JSON)
  public Response createPaymentMethod(
      final PaymentMethodJson json,
      @PathParam("accountId") final String accountId,
      @QueryParam(QUERY_PAYMENT_METHOD_IS_DEFAULT) @DefaultValue("false") final Boolean isDefault,
      @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false")
          final Boolean payAllUnpaidInvoices,
      @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 UriInfo uriInfo,
      @javax.ws.rs.core.Context final HttpServletRequest request)
      throws AccountApiException, PaymentApiException {
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);

    final PaymentMethod data = json.toPaymentMethod(accountId);
    final Account account = accountUserApi.getAccountById(data.getAccountId(), callContext);

    final boolean hasDefaultPaymentMethod = account.getPaymentMethodId() != null || isDefault;
    final Collection<Invoice> unpaidInvoices =
        payAllUnpaidInvoices
            ? invoiceApi.getUnpaidInvoicesByAccountId(
                account.getId(), clock.getUTCToday(), callContext)
            : Collections.<Invoice>emptyList();
    if (payAllUnpaidInvoices && unpaidInvoices.size() > 0 && !hasDefaultPaymentMethod) {
      return Response.status(Status.BAD_REQUEST).build();
    }

    final UUID paymentMethodId =
        paymentApi.addPaymentMethod(
            data.getPluginName(), account, isDefault, data.getPluginDetail(), callContext);
    if (payAllUnpaidInvoices && unpaidInvoices.size() > 0) {
      for (final Invoice invoice : unpaidInvoices) {
        paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext);
      }
    }
    return uriBuilder.buildResponse(
        PaymentMethodResource.class,
        "getPaymentMethod",
        paymentMethodId,
        uriInfo.getBaseUri().toString());
  }
Example #5
0
  @PUT
  @Consumes(APPLICATION_JSON)
  @Produces(APPLICATION_JSON)
  @Path(
      "/{accountId:"
          + UUID_PATTERN
          + "}/"
          + PAYMENT_METHODS
          + "/{paymentMethodId:"
          + UUID_PATTERN
          + "}/"
          + PAYMENT_METHODS_DEFAULT_PATH_POSTFIX)
  public Response setDefaultPaymentMethod(
      @PathParam("accountId") final String accountId,
      @PathParam("paymentMethodId") final String paymentMethodId,
      @QueryParam(QUERY_PAY_ALL_UNPAID_INVOICES) @DefaultValue("false")
          final Boolean payAllUnpaidInvoices,
      @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)
      throws AccountApiException, PaymentApiException {
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);

    final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
    paymentApi.setDefaultPaymentMethod(account, UUID.fromString(paymentMethodId), callContext);

    if (payAllUnpaidInvoices) {
      final Collection<Invoice> unpaidInvoices =
          invoiceApi.getUnpaidInvoicesByAccountId(
              account.getId(), clock.getUTCToday(), callContext);
      for (final Invoice invoice : unpaidInvoices) {
        paymentApi.createPayment(account, invoice.getId(), invoice.getBalance(), callContext);
      }
    }
    return Response.status(Status.OK).build();
  }
Example #6
0
 @GET
 @Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENTS)
 @Produces(APPLICATION_JSON)
 public Response getPayments(
     @PathParam("accountId") final String accountId,
     @javax.ws.rs.core.Context final HttpServletRequest request)
     throws PaymentApiException {
   final List<Payment> payments =
       paymentApi.getAccountPayments(UUID.fromString(accountId), context.createContext(request));
   final List<PaymentJson> result = new ArrayList<PaymentJson>(payments.size());
   for (final Payment payment : payments) {
     result.add(new PaymentJson(payment, null));
   }
   return Response.status(Status.OK).entity(result).build();
 }
Example #7
0
  @GET
  @Path("/{accountId:" + UUID_PATTERN + "}/" + TIMELINE)
  @Produces(APPLICATION_JSON)
  public Response getAccountTimeline(
      @PathParam("accountId") final String accountIdString,
      @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode,
      @javax.ws.rs.core.Context final HttpServletRequest request)
      throws AccountApiException, PaymentApiException, SubscriptionApiException {
    final TenantContext tenantContext = context.createContext(request);

    final UUID accountId = UUID.fromString(accountIdString);
    final Account account = accountUserApi.getAccountById(accountId, tenantContext);

    // Get the invoices
    final List<Invoice> invoices = invoiceApi.getInvoicesByAccount(account.getId(), tenantContext);
    final AuditLogsForInvoices invoicesAuditLogs =
        auditUserApi.getAuditLogsForInvoices(invoices, auditMode.getLevel(), tenantContext);

    // Get the payments
    final List<Payment> payments = paymentApi.getAccountPayments(accountId, tenantContext);
    final AuditLogsForPayments paymentsAuditLogs =
        auditUserApi.getAuditLogsForPayments(payments, auditMode.getLevel(), tenantContext);

    // Get the refunds
    final List<Refund> refunds = paymentApi.getAccountRefunds(account, tenantContext);
    final AuditLogsForRefunds refundsAuditLogs =
        auditUserApi.getAuditLogsForRefunds(refunds, auditMode.getLevel(), tenantContext);
    final Multimap<UUID, Refund> refundsByPayment = ArrayListMultimap.<UUID, Refund>create();
    for (final Refund refund : refunds) {
      refundsByPayment.put(refund.getPaymentId(), refund);
    }

    // Get the chargebacks
    final List<InvoicePayment> chargebacks =
        invoicePaymentApi.getChargebacksByAccountId(accountId, tenantContext);
    final AuditLogsForInvoicePayments chargebacksAuditLogs =
        auditUserApi.getAuditLogsForInvoicePayments(
            chargebacks, auditMode.getLevel(), tenantContext);
    final Multimap<UUID, InvoicePayment> chargebacksByPayment =
        ArrayListMultimap.<UUID, InvoicePayment>create();
    for (final InvoicePayment chargeback : chargebacks) {
      chargebacksByPayment.put(chargeback.getPaymentId(), chargeback);
    }

    // Get the bundles
    final List<SubscriptionBundle> bundles =
        subscriptionApi.getSubscriptionBundlesForAccountId(account.getId(), tenantContext);
    final AuditLogsForBundles bundlesAuditLogs =
        auditUserApi.getAuditLogsForBundles(bundles, auditMode.getLevel(), tenantContext);

    final AccountTimelineJson json =
        new AccountTimelineJson(
            account,
            invoices,
            payments,
            bundles,
            refundsByPayment,
            chargebacksByPayment,
            invoicesAuditLogs,
            paymentsAuditLogs,
            refundsAuditLogs,
            chargebacksAuditLogs,
            bundlesAuditLogs);
    return Response.status(Status.OK).entity(json).build();
  }