@Override public PaymentMethodJson apply(final PaymentMethod input) { return PaymentMethodJson.toPaymentMethodJson(account, input, accountAuditLogs); } }));
properties); return new PaymentMethodJson(in.getId().toString(), account.getId().toString(), isDefault, in.getPluginName(), pluginDetailJson, toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForPaymentMethod(in.getId())));
@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()); }
@GET @Path("/{paymentMethodId:" + UUID_PATTERN + "}") @Produces(APPLICATION_JSON) public Response getPaymentMethod(@PathParam("paymentMethodId") final String paymentMethodId, @QueryParam(QUERY_PAYMENT_METHOD_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException { final TenantContext tenantContext = context.createContext(request); final PaymentMethod paymentMethod = paymentApi.getPaymentMethodById(UUID.fromString(paymentMethodId), false, withPluginInfo, tenantContext); final Account account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext); final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext); final PaymentMethodJson json = PaymentMethodJson.toPaymentMethodJson(account, paymentMethod, accountAuditLogs); return Response.status(Status.OK).entity(json).build(); }
@Override public PaymentMethodJson apply(final PaymentMethod paymentMethod) { // Cache audit logs per account if (accountsAuditLogs.get().get(paymentMethod.getAccountId()) == null) { accountsAuditLogs.get().put(paymentMethod.getAccountId(), auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext)); } // Lookup the associated account(s) if (accounts.get(paymentMethod.getAccountId()) == null) { final Account account; try { account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext); accounts.put(paymentMethod.getAccountId(), account); } catch (final AccountApiException e) { log.warn("Unable to retrieve account", e); return null; } } return PaymentMethodJson.toPaymentMethodJson(accounts.get(paymentMethod.getAccountId()), paymentMethod, accountsAuditLogs.get().get(paymentMethod.getAccountId())); } },
@Override public PaymentMethodJson apply(final PaymentMethod paymentMethod) { // Cache audit logs per account if (accountsAuditLogs.get().get(paymentMethod.getAccountId()) == null) { accountsAuditLogs.get().put(paymentMethod.getAccountId(), auditUserApi.getAccountAuditLogs(paymentMethod.getAccountId(), auditMode.getLevel(), tenantContext)); } // Lookup the associated account(s) if (accounts.get(paymentMethod.getAccountId()) == null) { final Account account; try { account = accountUserApi.getAccountById(paymentMethod.getAccountId(), tenantContext); accounts.put(paymentMethod.getAccountId(), account); } catch (final AccountApiException e) { log.warn("Unable to retrieve account", e); return null; } } return PaymentMethodJson.toPaymentMethodJson(accounts.get(paymentMethod.getAccountId()), paymentMethod, accountsAuditLogs.get().get(paymentMethod.getAccountId())); } },