Search in sources :

Example 6 with OverdueState

use of org.killbill.billing.overdue.api.OverdueState in project killbill by killbill.

the class OverdueStateApplicator method apply.

public void apply(final DateTime effectiveDate, final OverdueStateSet overdueStateSet, final BillingState billingState, final ImmutableAccountData account, final OverdueState previousOverdueState, final OverdueState nextOverdueState, final InternalCallContext context) throws OverdueException, OverdueApiException {
    try {
        if (isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF(context)) {
            log.debug("OverdueStateApplicator: apply returns because account (recordId={}) is set with OVERDUE_ENFORCEMENT_OFF", context.getAccountRecordId());
            return;
        }
        log.debug("OverdueStateApplicator: time={}, previousState={}, nextState={}, billingState={}", effectiveDate, previousOverdueState, nextOverdueState, billingState);
        final OverdueState firstOverdueState = overdueStateSet.getFirstState();
        final boolean conditionForNextNotfication = !nextOverdueState.isClearState() || // We did not reach the first state yet but we have an unpaid invoice
        (firstOverdueState != null && billingState != null && billingState.getDateOfEarliestUnpaidInvoice() != null);
        if (conditionForNextNotfication) {
            final Period reevaluationInterval = getReevaluationInterval(overdueStateSet, nextOverdueState);
            // If there is no configuration in the config, we assume this is because the overdue conditions are not time based and so there is nothing to retry
            if (reevaluationInterval == null) {
                log.debug("OverdueStateApplicator <notificationQ>: missing InitialReevaluationInterval from config, NOT inserting notification for account {}", account.getId());
            } else {
                log.debug("OverdueStateApplicator <notificationQ>: inserting notification for account={}, time={}", account.getId(), effectiveDate.plus(reevaluationInterval));
                createFutureNotification(account, effectiveDate.plus(reevaluationInterval), context);
            }
        } else if (nextOverdueState.isClearState()) {
            clearFutureNotification(account, context);
        }
        if (previousOverdueState.getName().equals(nextOverdueState.getName())) {
            log.debug("OverdueStateApplicator is no-op: previousState={}, nextState={}", previousOverdueState, nextOverdueState);
            return;
        }
        cancelSubscriptionsIfRequired(effectiveDate, account, nextOverdueState, context);
        sendEmailIfRequired(account.getId(), billingState, nextOverdueState, context);
        avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(account, previousOverdueState, nextOverdueState, context);
        // Make sure to store the new state last here: the entitlement DAO will send a BlockingTransitionInternalEvent
        // on the bus to which invoice will react. We need the latest state (including AUTO_INVOICE_OFF tag for example)
        // to be present in the database first.
        storeNewState(effectiveDate, account, nextOverdueState, context);
    } catch (final AccountApiException e) {
        throw new OverdueException(e);
    }
    final OverdueChangeInternalEvent event;
    try {
        event = createOverdueEvent(account, previousOverdueState.getName(), nextOverdueState.getName(), isBlockBillingTransition(previousOverdueState, nextOverdueState), isUnblockBillingTransition(previousOverdueState, nextOverdueState), context);
    } catch (final BlockingApiException e) {
        log.warn("Failed to create OverdueChangeInternalEvent for accountId='{}'", account.getId(), e);
        return;
    }
    try {
        bus.post(event);
    } catch (final Exception e) {
        log.warn("Failed to post event {}", event, e);
    }
}
Also used : AccountApiException(org.killbill.billing.account.api.AccountApiException) Period(org.joda.time.Period) OverdueException(org.killbill.billing.overdue.config.api.OverdueException) OverdueChangeInternalEvent(org.killbill.billing.events.OverdueChangeInternalEvent) BlockingApiException(org.killbill.billing.entitlement.api.BlockingApiException) OverdueState(org.killbill.billing.overdue.api.OverdueState) TagApiException(org.killbill.billing.util.api.TagApiException) EntitlementApiException(org.killbill.billing.entitlement.api.EntitlementApiException) BlockingApiException(org.killbill.billing.entitlement.api.BlockingApiException) AccountApiException(org.killbill.billing.account.api.AccountApiException) EmailApiException(org.killbill.billing.util.email.EmailApiException) OverdueException(org.killbill.billing.overdue.config.api.OverdueException) MustacheException(com.samskivert.mustache.MustacheException) IOException(java.io.IOException) OverdueApiException(org.killbill.billing.overdue.api.OverdueApiException)

Example 7 with OverdueState

use of org.killbill.billing.overdue.api.OverdueState in project killbill by killbill.

the class AccountResource method getOverdueAccount.

/*
     * ************************** OVERDUE ********************************
     */
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + OVERDUE)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve overdue state for account", response = OverdueStateJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getOverdueAccount(@PathParam("accountId") final String accountId, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, OverdueException, OverdueApiException {
    final TenantContext tenantContext = context.createContext(request);
    final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), tenantContext);
    final OverdueState overdueState = overdueApi.getOverdueStateFor(account.getId(), tenantContext);
    return Response.status(Status.OK).entity(new OverdueStateJson(overdueState, paymentConfig)).build();
}
Also used : Account(org.killbill.billing.account.api.Account) OverdueStateJson(org.killbill.billing.jaxrs.json.OverdueStateJson) TenantContext(org.killbill.billing.util.callcontext.TenantContext) OverdueState(org.killbill.billing.overdue.api.OverdueState) Path(javax.ws.rs.Path) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

OverdueState (org.killbill.billing.overdue.api.OverdueState)7 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Account (org.killbill.billing.account.api.Account)3 DefaultOverdueConfig (org.killbill.billing.overdue.config.DefaultOverdueConfig)3 Test (org.testng.annotations.Test)3 BlockingState (org.killbill.billing.entitlement.api.BlockingState)2 MustacheException (com.samskivert.mustache.MustacheException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Period (org.joda.time.Period)1 AccountApiException (org.killbill.billing.account.api.AccountApiException)1 ImmutableAccountData (org.killbill.billing.account.api.ImmutableAccountData)1 BlockingApiException (org.killbill.billing.entitlement.api.BlockingApiException)1 EntitlementApiException (org.killbill.billing.entitlement.api.EntitlementApiException)1 OverdueChangeInternalEvent (org.killbill.billing.events.OverdueChangeInternalEvent)1