Search in sources :

Example 1 with InvoiceItemJson

use of org.killbill.billing.jaxrs.json.InvoiceItemJson in project killbill by killbill.

the class InvoiceResource method createExternalCharges.

@TimedResource
@POST
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@Path("/" + CHARGES + "/{accountId:" + UUID_PATTERN + "}")
@ApiOperation(value = "Create external charge(s)", response = InvoiceItemJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response createExternalCharges(final Iterable<InvoiceItemJson> externalChargesJson, @PathParam("accountId") final String accountId, @QueryParam(QUERY_REQUESTED_DT) final String requestedDateTimeString, @QueryParam(QUERY_PAY_INVOICE) @DefaultValue("false") final Boolean payInvoice, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUTO_COMMIT) @DefaultValue("false") final Boolean autoCommit, @QueryParam(QUERY_PAYMENT_EXTERNAL_KEY) final String paymentExternalKey, @QueryParam(QUERY_TRANSACTION_EXTERNAL_KEY) final String transactionExternalKey, @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, InvoiceApiException, PaymentApiException {
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
    final Iterable<InvoiceItem> sanitizedExternalChargesJson = validateSanitizeAndTranformInputItems(account.getCurrency(), externalChargesJson);
    // Get the effective date of the external charge, in the account timezone
    final LocalDate requestedDate = toLocalDateDefaultToday(account, requestedDateTimeString, callContext);
    final List<InvoiceItem> createdExternalCharges = invoiceApi.insertExternalCharges(account.getId(), requestedDate, sanitizedExternalChargesJson, autoCommit, callContext);
    // if all createdExternalCharges point to the same invoiceId, use the provided paymentExternalKey and / or transactionExternalKey
    final boolean haveSameInvoiceId = Iterables.all(createdExternalCharges, new Predicate<InvoiceItem>() {

        @Override
        public boolean apply(final InvoiceItem input) {
            return input.getInvoiceId().equals(createdExternalCharges.get(0).getInvoiceId());
        }
    });
    if (payInvoice) {
        final Collection<UUID> paidInvoices = new HashSet<UUID>();
        for (final InvoiceItem externalCharge : createdExternalCharges) {
            if (!paidInvoices.contains(externalCharge.getInvoiceId())) {
                paidInvoices.add(externalCharge.getInvoiceId());
                final Invoice invoice = invoiceApi.getInvoice(externalCharge.getInvoiceId(), callContext);
                createPurchaseForInvoice(account, invoice.getId(), invoice.getBalance(), account.getPaymentMethodId(), false, (haveSameInvoiceId && paymentExternalKey != null) ? paymentExternalKey : null, (haveSameInvoiceId && transactionExternalKey != null) ? transactionExternalKey : null, pluginProperties, callContext);
            }
        }
    }
    final List<InvoiceItemJson> createdExternalChargesJson = Lists.<InvoiceItem, InvoiceItemJson>transform(createdExternalCharges, new Function<InvoiceItem, InvoiceItemJson>() {

        @Override
        public InvoiceItemJson apply(final InvoiceItem input) {
            return new InvoiceItemJson(input);
        }
    });
    return Response.status(Status.OK).entity(createdExternalChargesJson).build();
}
Also used : Account(org.killbill.billing.account.api.Account) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) Invoice(org.killbill.billing.invoice.api.Invoice) InvoiceItemJson(org.killbill.billing.jaxrs.json.InvoiceItemJson) CallContext(org.killbill.billing.util.callcontext.CallContext) LocalDate(org.joda.time.LocalDate) PluginProperty(org.killbill.billing.payment.api.PluginProperty) UUID(java.util.UUID) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DefaultPlanPhasePriceOverride(org.killbill.billing.catalog.DefaultPlanPhasePriceOverride) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with InvoiceItemJson

use of org.killbill.billing.jaxrs.json.InvoiceItemJson in project killbill by killbill.

the class InvoicePaymentResource method createRefundWithAdjustments.

@TimedResource
@POST
@Path("/{paymentId:" + UUID_PATTERN + "}/" + REFUNDS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Refund a payment, and adjust the invoice if needed")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid payment id supplied"), @ApiResponse(code = 404, message = "Account or payment not found") })
public Response createRefundWithAdjustments(final InvoicePaymentTransactionJson json, @PathParam("paymentId") final String paymentId, @QueryParam(QUERY_PAYMENT_EXTERNAL) @DefaultValue("false") final Boolean externalPayment, @QueryParam(QUERY_PAYMENT_METHOD_ID) @DefaultValue("") final String paymentMethodId, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @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 PaymentApiException, AccountApiException {
    verifyNonNullOrEmpty(json, "InvoicePaymentTransactionJson body should be specified");
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final UUID paymentUuid = UUID.fromString(paymentId);
    final Payment payment = paymentApi.getPayment(paymentUuid, false, false, ImmutableList.<PluginProperty>of(), callContext);
    final Account account = accountUserApi.getAccountById(payment.getAccountId(), callContext);
    final Iterable<PluginProperty> pluginProperties;
    final String transactionExternalKey = json.getTransactionExternalKey() != null ? json.getTransactionExternalKey() : UUIDs.randomUUID().toString();
    final String paymentExternalKey = json.getPaymentExternalKey() != null ? json.getPaymentExternalKey() : UUIDs.randomUUID().toString();
    if (json.isAdjusted() != null && json.isAdjusted()) {
        if (json.getAdjustments() != null && json.getAdjustments().size() > 0) {
            final Map<UUID, BigDecimal> adjustments = new HashMap<UUID, BigDecimal>();
            for (final InvoiceItemJson item : json.getAdjustments()) {
                adjustments.put(UUID.fromString(item.getInvoiceItemId()), item.getAmount());
            }
            pluginProperties = extractPluginProperties(pluginPropertiesString, new PluginProperty("IPCD_REFUND_WITH_ADJUSTMENTS", "true", false), new PluginProperty("IPCD_REFUND_IDS_AMOUNTS", adjustments, false));
        } else {
            pluginProperties = extractPluginProperties(pluginPropertiesString, new PluginProperty("IPCD_REFUND_WITH_ADJUSTMENTS", "true", false));
        }
    } else {
        pluginProperties = extractPluginProperties(pluginPropertiesString);
    }
    final Payment result;
    if (externalPayment) {
        UUID externalPaymentMethodId = Strings.isNullOrEmpty(paymentMethodId) ? null : UUID.fromString(paymentMethodId);
        final Collection<PluginProperty> pluginPropertiesForExternalRefund = new LinkedList<PluginProperty>();
        Iterables.addAll(pluginPropertiesForExternalRefund, pluginProperties);
        pluginPropertiesForExternalRefund.add(new PluginProperty("IPCD_PAYMENT_ID", paymentUuid, false));
        result = paymentApi.createCreditWithPaymentControl(account, externalPaymentMethodId, null, json.getAmount(), account.getCurrency(), paymentExternalKey, transactionExternalKey, pluginPropertiesForExternalRefund, createInvoicePaymentControlPluginApiPaymentOptions(true), callContext);
    } else {
        result = paymentApi.createRefundWithPaymentControl(account, payment.getId(), json.getAmount(), account.getCurrency(), transactionExternalKey, pluginProperties, createInvoicePaymentControlPluginApiPaymentOptions(false), callContext);
    }
    return uriBuilder.buildResponse(uriInfo, InvoicePaymentResource.class, "getInvoicePayment", result.getId(), request);
}
Also used : Account(org.killbill.billing.account.api.Account) HashMap(java.util.HashMap) InvoiceItemJson(org.killbill.billing.jaxrs.json.InvoiceItemJson) CallContext(org.killbill.billing.util.callcontext.CallContext) BigDecimal(java.math.BigDecimal) LinkedList(java.util.LinkedList) PluginProperty(org.killbill.billing.payment.api.PluginProperty) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) UUID(java.util.UUID) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 UUID (java.util.UUID)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Account (org.killbill.billing.account.api.Account)2 InvoiceItemJson (org.killbill.billing.jaxrs.json.InvoiceItemJson)2 PluginProperty (org.killbill.billing.payment.api.PluginProperty)2 CallContext (org.killbill.billing.util.callcontext.CallContext)2 TimedResource (org.killbill.commons.metrics.TimedResource)2 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 LocalDate (org.joda.time.LocalDate)1 DefaultPlanPhasePriceOverride (org.killbill.billing.catalog.DefaultPlanPhasePriceOverride)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1 Invoice (org.killbill.billing.invoice.api.Invoice)1