Search in sources :

Example 1 with SubscriptionUsageRecordJson

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

the class UsageResource method recordUsage.

@TimedResource
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Record usage for a subscription")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid subscription (e.g. inactive)") })
public Response recordUsage(final SubscriptionUsageRecordJson json, @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 EntitlementApiException, AccountApiException, UsageApiException {
    verifyNonNullOrEmpty(json, "SubscriptionUsageRecordJson body should be specified");
    verifyNonNullOrEmpty(json.getSubscriptionId(), "SubscriptionUsageRecordJson subscriptionId needs to be set", json.getUnitUsageRecords(), "SubscriptionUsageRecordJson unitUsageRecords needs to be set");
    Preconditions.checkArgument(!json.getUnitUsageRecords().isEmpty());
    for (final UnitUsageRecordJson unitUsageRecordJson : json.getUnitUsageRecords()) {
        verifyNonNullOrEmpty(unitUsageRecordJson.getUnitType(), "UnitUsageRecordJson unitType need to be set");
        Preconditions.checkArgument(Iterables.size(unitUsageRecordJson.getUsageRecords()) > 0, "UnitUsageRecordJson usageRecords must have at least one element.");
        for (final UsageRecordJson usageRecordJson : unitUsageRecordJson.getUsageRecords()) {
            verifyNonNull(usageRecordJson.getAmount(), "UsageRecordJson amount needs to be set");
            verifyNonNull(usageRecordJson.getRecordDate(), "UsageRecordJson recordDate needs to be set");
        }
    }
    final CallContext callContext = context.createContext(createdBy, reason, comment, request);
    // Verify subscription exists..
    final Entitlement entitlement = entitlementApi.getEntitlementForId(UUID.fromString(json.getSubscriptionId()), callContext);
    if (entitlement.getState() != EntitlementState.ACTIVE) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    final SubscriptionUsageRecord record = json.toSubscriptionUsageRecord();
    usageUserApi.recordRolledUpUsage(record, callContext);
    return Response.status(Status.CREATED).build();
}
Also used : UnitUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson) UnitUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson) UsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UsageRecordJson) SubscriptionUsageRecordJson(org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson) Entitlement(org.killbill.billing.entitlement.api.Entitlement) SubscriptionUsageRecord(org.killbill.billing.usage.api.SubscriptionUsageRecord) CallContext(org.killbill.billing.util.callcontext.CallContext) 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)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Entitlement (org.killbill.billing.entitlement.api.Entitlement)1 SubscriptionUsageRecordJson (org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson)1 UnitUsageRecordJson (org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UnitUsageRecordJson)1 UsageRecordJson (org.killbill.billing.jaxrs.json.SubscriptionUsageRecordJson.UsageRecordJson)1 SubscriptionUsageRecord (org.killbill.billing.usage.api.SubscriptionUsageRecord)1 CallContext (org.killbill.billing.util.callcontext.CallContext)1 TimedResource (org.killbill.commons.metrics.TimedResource)1