Class SubscriptionOperationUtils

java.lang.Object
com.broadleafcommerce.subscriptionoperation.service.SubscriptionOperationUtils

public class SubscriptionOperationUtils extends Object
Author:
Nathan Moore (nathandmoore)
  • Constructor Details

    • SubscriptionOperationUtils

      public SubscriptionOperationUtils()
  • Method Details

    • isSubscriptionInGracePeriod

      public boolean isSubscriptionInGracePeriod(@NonNull @NonNull CancellationPolicyDetail cancellationPolicyDetail, @NonNull @NonNull Subscription subscription)
      Determines whether the given Subscription is in its cancellation grace period. This is based on the CancellationPolicyDetail that matches it.

      The grace period is calculated from the last auto-renewal period. For subscriptions with terms, this is from the end of the last term. Otherwise, this is from the end of the last billing period.

      Parameters:
      cancellationPolicyDetail - The CancellationPolicyDetail describing the grace period, if a grace period is supported.
      subscription - The Subscription that may be in the grace period.
      Returns:
      Whether the given subscription is in its cancellation policy's grace period.
    • isSubscriptionInGracePeriod

      public boolean isSubscriptionInGracePeriod(@NonNull @NonNull CancellationPolicyDetail cancellationPolicyDetail, @NonNull @NonNull SubscriptionWithItems subscription)
      See Also:
    • determineEndOfTermsDate

      public Instant determineEndOfTermsDate(@NonNull @NonNull Instant startOfTermsDate, @NonNull @NonNull String termDurationType, int termDurationLength, @Nullable String customerTimeZone)
    • getFreeTrialEndDate

      @Nullable public Instant getFreeTrialEndDate(@NonNull @NonNull Instant freeTrialStartDate, @NonNull @NonNull List<com.broadleafcommerce.order.common.domain.Adjustment> adjustments, @Nullable String customerTimezone, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Determines the free trial's end date based on the free trial start date & the free trial adjustment.
      Parameters:
      freeTrialStartDate - the free trial's start date.
      adjustments - the Adjustments containing a free trial.
      customerTimezone - the customer timezone if present
      contextInfo - context surrounding the multi-tenant state
      Returns:
      The free trial end date if a free trial adjustment is found, null otherwise.
    • getDelayedDateBasedOnFreeTrial

      public Instant getDelayedDateBasedOnFreeTrial(@NonNull @NonNull Instant originalDate, @NonNull @NonNull List<com.broadleafcommerce.order.common.domain.Adjustment> adjustments, @Nullable String timezoneString, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Delays the given Instant based on the free trial Adjustment, if any.
      Parameters:
      originalDate - the original date to delay.
      adjustments - the Adjustments to consider for delaying the date.
      timezoneString - the timezone for the customer if present
      contextInfo - context surrounding the multi-tenant state
      Returns:
      the delayed date based on the adjustments.
    • getPeriodStartDateAfterFreeTrialEnd

      public Instant getPeriodStartDateAfterFreeTrialEnd(@NonNull @NonNull SubscriptionPricingContext pricingContext, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Gets the start date of the next period after the free trial ends. Since the free trial end date is 1 nanosecond before the next day, we need to add 1 nanosecond to get the start date.
      Parameters:
      pricingContext - the current pricing context
      Returns:
      the start date of the next period after the free trial ends
    • determineStartDateOfNextPeriod

      public Instant determineStartDateOfNextPeriod(@NonNull @NonNull Instant periodStartDate, String periodType, int periodFrequency, boolean roundToMonthEnd, @Nullable Instant endOfTermDate, @Nullable String customerTimezone)
      Determines the Instant when the billing period subsequent to the given periodStartDate begins. By default, this adds the length of a period in ChronoUnit.MONTHS to the periodStartDate since that is the smallest typical unit for a billing period.
      Parameters:
      periodStartDate - A reference period start Instant from which to derive the previous period's start Instant.
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      roundToMonthEnd - Whether to round the day to the end of the month
      Returns:
      The Instant when the billing period subsequent to the given periodStartDate begins.
    • roundUpToTheEndOfMonth

      public Instant roundUpToTheEndOfMonth(@NonNull @NonNull Instant date, @Nullable String customerTimezone)
      Rounds the given Instant to the end of the month.
      Parameters:
      date - The Instant to round to the end of the month.
      customerTimezone - The customer's timezone ID.
      Returns:
      The Instant rounded to the end of the month.
    • getNumberOfPeriodsLeftInTerm

      public Optional<Long> getNumberOfPeriodsLeftInTerm(SubscriptionPricingContext pricingContext, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • getNumberOfPeriodsLeftInTerm

      public Optional<Long> getNumberOfPeriodsLeftInTerm(Instant periodStartDate, SubscriptionPricingContext pricingContext)
      Determines the number of billing periods left after the current that are outstanding. It is assumed that the term length is a multiple of the period length in months.
      Parameters:
      periodStartDate - the period start date truncated to DAYS to start the calculation
      pricingContext - The SubscriptionPricingContext
      Returns:
      The number billings left after the current that are unbilled.
    • getNumberOfPeriodsLeftInTerm

      public Optional<Long> getNumberOfPeriodsLeftInTerm(String periodType, int periodFrequency, Instant periodStartDate, @Nullable Instant termStartDate, @Nullable Instant termEndDate, @Nullable String customerTimezone)
      Determines the number of billing periods left after the current that are outstanding. It is assumed that the term length is a multiple of the period length in months.
      Parameters:
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      periodStartDate - the period start date truncated to DAYS to start the calculation
      termStartDate - The start of the subscription's term.
      termEndDate - The end of the subscription's term.
      customerTimezone - The customer's timezone, if present
      Returns:
      The number billings left after the current that are unbilled.
    • beginningOfNextDay

      public Instant beginningOfNextDay(@NonNull @NonNull Instant date, @Nullable String customerTimezone)
    • getEndOfPreviousDay

      public Instant getEndOfPreviousDay(@NonNull @NonNull Instant nextDay, @Nullable String customerTimezone)
    • getZoneId

      public ZoneId getZoneId(@Nullable String customerTimezone)
      Returns the ZoneId for the given customer timezone string. If the timezone is null, it defaults to ZoneOffset.UTC.
      Parameters:
      customerTimezone - the customer's timezone ID
      Returns:
      the resolved ZoneId
      Throws:
      IllegalArgumentException - if the provided timezone ID is invalid
    • delayDateBasedOnAdjustmentsForCustomLengthUnits

      protected Instant delayDateBasedOnAdjustmentsForCustomLengthUnits(@NonNull @NonNull String lengthUnits, int delayLength, ZonedDateTime originalNextBillZonedDateTime, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • determineEndOfTermsDateForCustomDurationType

      protected Instant determineEndOfTermsDateForCustomDurationType(@NonNull @NonNull Instant startOfTermsDate, String termDurationType, int termDurationLength)
    • getMonthsUntilEndOfTerm

      protected long getMonthsUntilEndOfTerm(@NonNull @NonNull Instant periodStartDate, @NonNull @NonNull Instant termEndDate, @Nullable String customerTimezone)
      Determines the number of months until the end of the subscription's term.
      Parameters:
      periodStartDate - The start of the subscription's term.
      termEndDate - The end of the subscription's term.
      Returns:
      The number of days in a subscription's terms.
    • getMonthsInPeriod

      protected long getMonthsInPeriod(@NonNull @NonNull String periodType, int periodFrequency)
      Determines the number of months in a billing period.
      Parameters:
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      Returns:
      The number of months in a billing period.
      See Also:
    • getMonthsInCustomPeriodType

      protected int getMonthsInCustomPeriodType(String periodType, int periodFrequency)
      Hook point for handling custom DefaultSubscriptionPeriodType values.
      Parameters:
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      Returns:
      The number of months in a billing period.
      See Also:
    • determineNumberOfDaysInFullPeriod

      protected long determineNumberOfDaysInFullPeriod(@NonNull @NonNull Instant periodStartDate, @NonNull @NonNull SubscriptionPricingContext pricingContext, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)