Functional Weave
Code in TypeScript

subscriptions.dunning-schedule@1.0.0

README.md

1,399 bytes · view raw

# subscriptions.dunning-schedule

When a renewal payment fails, the retry policy says when to try again: "retry
3, 5 and 7 days later" is the usual shape. This turns a policy and a failure
date into the actual dates.

Every offset is counted from the date of the first failure, not from the
previous retry: [3, 5, 7] means three, five and seven days after the failure.
Some billing dashboards state a policy as gaps between attempts instead
("2 days after the previous attempt"); add the gaps up to get offsets. The offsets must
be whole days of 1 or more and strictly increasing, so the schedule is always
ascending and never retries twice on one day.

With `businessDays` true, the offsets are counted in working days using
dates.add-business-days: weekends and the listed holidays are skipped, and a
failure on a Saturday retried 1 business day later is retried on Monday. Pass
bank holidays (dates.bank-holidays) when retries should land on days the
banks process card and Direct Debit payments. With `businessDays` false the
offsets are plain calendar days and `holidays` is ignored.

What happens after the last retry (cancel, mark unpaid, pause) is a policy
decision for the caller; this only computes when the retries fall. An empty
policy gives an empty schedule.

Errors: an offset below 1, offsets that do not increase, an impossible date,
and (in business-day mode) a malformed holiday.