Search in sources :

Example 51 with CallContext

use of org.killbill.billing.util.callcontext.CallContext 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)

Example 52 with CallContext

use of org.killbill.billing.util.callcontext.CallContext in project killbill by killbill.

the class InvoiceResource method deleteCBA.

@TimedResource
@DELETE
@Path("/{invoiceId:" + UUID_PATTERN + "}" + "/{invoiceItemId:" + UUID_PATTERN + "}/cba")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Delete a CBA item")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id, invoice id or invoice item id supplied"), @ApiResponse(code = 404, message = "Account or invoice not found") })
public Response deleteCBA(@PathParam("invoiceId") final String invoiceId, @PathParam("invoiceItemId") final String invoiceItemId, @QueryParam(QUERY_ACCOUNT_ID) final String accountId, @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, InvoiceApiException {
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
    invoiceApi.deleteCBA(account.getId(), UUID.fromString(invoiceId), UUID.fromString(invoiceItemId), callContext);
    return Response.status(Status.OK).build();
}
Also used : Account(org.killbill.billing.account.api.Account) CallContext(org.killbill.billing.util.callcontext.CallContext) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) TimedResource(org.killbill.commons.metrics.TimedResource) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 53 with CallContext

use of org.killbill.billing.util.callcontext.CallContext in project killbill by killbill.

the class InvoiceResource method generateDryRunInvoice.

@TimedResource
@POST
@Path("/" + DRY_RUN)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Generate a dryRun invoice", response = InvoiceJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id or target datetime supplied") })
public Response generateDryRunInvoice(@Nullable final InvoiceDryRunJson dryRunSubscriptionSpec, @QueryParam(QUERY_ACCOUNT_ID) final String accountId, @Nullable @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.createContext(createdBy, reason, comment, request);
    final LocalDate inputDate;
    if (dryRunSubscriptionSpec != null) {
        if (DryRunType.UPCOMING_INVOICE.name().equals(dryRunSubscriptionSpec.getDryRunType())) {
            inputDate = null;
        } else if (DryRunType.SUBSCRIPTION_ACTION.name().equals(dryRunSubscriptionSpec.getDryRunType()) && dryRunSubscriptionSpec.getEffectiveDate() != null) {
            inputDate = dryRunSubscriptionSpec.getEffectiveDate();
        } else {
            inputDate = toLocalDate(targetDate);
        }
    } else {
        inputDate = toLocalDate(targetDate);
    }
    // On the other hand if body is not null, we are attempting a dryRun subscription operation
    if (dryRunSubscriptionSpec != null && dryRunSubscriptionSpec.getDryRunAction() != null) {
        if (SubscriptionEventType.START_BILLING.toString().equals(dryRunSubscriptionSpec.getDryRunAction())) {
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getProductName(), "DryRun subscription product category should be specified");
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getBillingPeriod(), "DryRun subscription billingPeriod should be specified");
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getProductCategory(), "DryRun subscription product category should be specified");
            if (dryRunSubscriptionSpec.getProductCategory().equals(ProductCategory.ADD_ON)) {
                verifyNonNullOrEmpty(dryRunSubscriptionSpec.getBundleId(), "DryRun bundle ID should be specified");
            }
        } else if (SubscriptionEventType.CHANGE.toString().equals(dryRunSubscriptionSpec.getDryRunAction())) {
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getProductName(), "DryRun subscription product category should be specified");
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getBillingPeriod(), "DryRun subscription billingPeriod should be specified");
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getSubscriptionId(), "DryRun subscriptionID should be specified");
        } else if (SubscriptionEventType.STOP_BILLING.toString().equals(dryRunSubscriptionSpec.getDryRunAction())) {
            verifyNonNullOrEmpty(dryRunSubscriptionSpec.getSubscriptionId(), "DryRun subscriptionID should be specified");
        }
    }
    final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), callContext);
    final DryRunArguments dryRunArguments = new DefaultDryRunArguments(dryRunSubscriptionSpec, account);
    try {
        final Invoice generatedInvoice = invoiceApi.triggerInvoiceGeneration(UUID.fromString(accountId), inputDate, dryRunArguments, callContext);
        return Response.status(Status.OK).entity(new InvoiceJson(generatedInvoice, true, null, null)).build();
    } catch (InvoiceApiException e) {
        if (e.getCode() == ErrorCode.INVOICE_NOTHING_TO_DO.getCode()) {
            return Response.status(Status.NOT_FOUND).build();
        }
        throw e;
    }
}
Also used : Account(org.killbill.billing.account.api.Account) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Invoice(org.killbill.billing.invoice.api.Invoice) DryRunArguments(org.killbill.billing.invoice.api.DryRunArguments) InvoiceJson(org.killbill.billing.jaxrs.json.InvoiceJson) CallContext(org.killbill.billing.util.callcontext.CallContext) LocalDate(org.joda.time.LocalDate) 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)

Example 54 with CallContext

use of org.killbill.billing.util.callcontext.CallContext in project killbill by killbill.

the class InvoiceResource method uploadTemplateResource.

private Response uploadTemplateResource(final String templateResource, @Nullable final String localeStr, final boolean deleteIfExists, final TenantKey tenantKey, final String getMethodStr, final String createdBy, final String reason, final String comment, final HttpServletRequest request, final UriInfo uriInfo) throws Exception {
    final String tenantKeyStr;
    if (localeStr != null) {
        // Validation purpose:  Will throw bad stream
        final InputStream stream = new ByteArrayInputStream(templateResource.getBytes());
        new PropertyResourceBundle(stream);
        final Locale locale = localeStr != null ? LocaleUtils.toLocale(localeStr) : defaultLocale;
        tenantKeyStr = LocaleUtils.localeString(locale, tenantKey.toString());
    } else {
        tenantKeyStr = tenantKey.toString();
    }
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    if (!tenantApi.getTenantValuesForKey(tenantKeyStr, callContext).isEmpty()) {
        if (deleteIfExists) {
            tenantApi.deleteTenantKey(tenantKeyStr, callContext);
        } else {
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    tenantApi.addTenantKeyValue(tenantKeyStr, templateResource, callContext);
    return uriBuilder.buildResponse(uriInfo, InvoiceResource.class, getMethodStr, localeStr, request);
}
Also used : Locale(java.util.Locale) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CallContext(org.killbill.billing.util.callcontext.CallContext) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 55 with CallContext

use of org.killbill.billing.util.callcontext.CallContext in project killbill by killbill.

the class InvoiceResource method createInstantPayment.

@TimedResource
@POST
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@Path("/{invoiceId:" + UUID_PATTERN + "}/" + PAYMENTS)
@ApiOperation(value = "Trigger a payment for invoice")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id or invoice id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response createInstantPayment(final InvoicePaymentJson payment, @QueryParam(QUERY_PAYMENT_EXTERNAL) @DefaultValue("false") final Boolean externalPayment, @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 HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws AccountApiException, PaymentApiException {
    verifyNonNullOrEmpty(payment, "InvoicePaymentJson body should be specified");
    verifyNonNullOrEmpty(payment.getAccountId(), "InvoicePaymentJson accountId needs to be set", payment.getTargetInvoiceId(), "InvoicePaymentJson targetInvoiceId needs to be set", payment.getPurchasedAmount(), "InvoicePaymentJson purchasedAmount needs to be set");
    Preconditions.checkArgument(!externalPayment || payment.getPaymentMethodId() == null, "InvoicePaymentJson should not contain a paymentMethodId when this is an external payment");
    final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    final Account account = accountUserApi.getAccountById(UUID.fromString(payment.getAccountId()), callContext);
    final UUID paymentMethodId = externalPayment ? null : (payment.getPaymentMethodId() != null ? UUID.fromString(payment.getPaymentMethodId()) : account.getPaymentMethodId());
    final UUID invoiceId = UUID.fromString(payment.getTargetInvoiceId());
    final Payment result = createPurchaseForInvoice(account, invoiceId, payment.getPurchasedAmount(), paymentMethodId, externalPayment, (payment.getPaymentExternalKey() != null) ? payment.getPaymentExternalKey() : null, null, pluginProperties, callContext);
    return result != null ? uriBuilder.buildResponse(uriInfo, InvoicePaymentResource.class, "getInvoicePayment", result.getId(), request) : Response.status(Status.NO_CONTENT).build();
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Account(org.killbill.billing.account.api.Account) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment) UUID(java.util.UUID) CallContext(org.killbill.billing.util.callcontext.CallContext) 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)

Aggregations

CallContext (org.killbill.billing.util.callcontext.CallContext)82 ApiOperation (io.swagger.annotations.ApiOperation)51 ApiResponses (io.swagger.annotations.ApiResponses)51 Produces (javax.ws.rs.Produces)48 TimedResource (org.killbill.commons.metrics.TimedResource)48 Consumes (javax.ws.rs.Consumes)47 Path (javax.ws.rs.Path)43 UUID (java.util.UUID)40 Account (org.killbill.billing.account.api.Account)38 POST (javax.ws.rs.POST)35 PluginProperty (org.killbill.billing.payment.api.PluginProperty)29 LocalDate (org.joda.time.LocalDate)16 Payment (org.killbill.billing.payment.api.Payment)15 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)14 DefaultCallContext (org.killbill.billing.callcontext.DefaultCallContext)11 Invoice (org.killbill.billing.invoice.api.Invoice)10 PaymentOptions (org.killbill.billing.payment.api.PaymentOptions)10 PUT (javax.ws.rs.PUT)9 Entitlement (org.killbill.billing.entitlement.api.Entitlement)9 DELETE (javax.ws.rs.DELETE)7