Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best pattern for a monthly billing cycle

I wrote some code for my new billing system. The purpose is to bill the customer on the same day each month. (not the 1st or last day of the month)

static bool NeedToBill(DateTime planLastBilled, DateTime cycleDate)
    {
        // is today the same date as the cycleDate AND is was the planLastBilled not the same day as today?
        if (DateTime.UtcNow.Day.Equals(cycleDate.Day) && !DateTime.UtcNow.Day.Equals(planLastBilled))
            return true;
        else
            return false;
    } 

The 2 pitfalls are:

  1. If his cycleDate.Day is the 31 and the current month only has 29 days
  2. cycleDate is Feb 29 2012 - he will only get billed on leap years

Is there a common best practice here?

so it seems like there's a bunch things to check

  1. has this account already been billed this month?
  2. does the cycle day exists in the current month
  3. is the cycle day greater than or equal to the current date (this is ideal if the transaction failed the day before)

Thanks!

like image 603
aron Avatar asked Mar 22 '11 16:03

aron


2 Answers

Only allow the choice of a billing day between 1 - 28. In my experience this is how most credit card / loan companies deal with it when given a choice.

like image 70
m.edmondson Avatar answered Oct 23 '22 18:10

m.edmondson


What does the same day each month mean?

If I am a customer, I want to be billed on the 16th each month. No problem. If I want to be billed on the 31st on each month the obvious issue is not all months have 31 days as you've pointed out in your question.

Why not check the current month for the number of days. If it has less than 31 days, make the last day of the month the bill date.

Is there more to the problem?

like image 37
Chuck Conway Avatar answered Oct 23 '22 18:10

Chuck Conway