← back to StudentLoanTracker

src/lib/calculators/paye-calculator.ts

81 lines

/**
 * PAYE Plan Calculator (Pay As You Earn)
 *
 * Key rules:
 * - 10% of discretionary income (AGI - 150% poverty line)
 * - Payment capped at 10-year Standard payment
 * - 20-year forgiveness (240 months)
 * - Only available to "new borrowers" as of Oct 1, 2007 with loans after Oct 1, 2011
 * - Interest capitalizes if payment doesn't cover interest
 * - Cap on capitalized interest: original balance (subsidized loans, first 3 years)
 */

import { LoanInputs, PlanResult, MonthlySnapshot } from './types';
import { getDiscretionaryIncome, getPovertyGuideline, POVERTY_MULTIPLIERS } from './poverty-guidelines';

const PAYE_PAYMENT_RATE = 0.10;
const FORGIVENESS_MONTHS = 240; // 20 years

export function calculatePAYE(inputs: LoanInputs): PlanResult {
  const { totalBalance, weightedRate, agi, familySize, state } = inputs;

  const povertyGuideline = getPovertyGuideline(familySize, state);
  const discretionaryIncome = getDiscretionaryIncome(
    agi, familySize, state, POVERTY_MULTIPLIERS.PAYE
  );

  const monthlyPayment = Math.max(0, (discretionaryIncome * PAYE_PAYMENT_RATE) / 12);

  const monthlyRate = weightedRate / 12;
  const standardPayment =
    monthlyRate > 0
      ? (totalBalance * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -120))
      : totalBalance / 120;

  const cappedPayment = Math.min(monthlyPayment, standardPayment);

  let balance = totalBalance;
  let totalPaid = 0;
  let totalInterest = 0;
  const schedule: MonthlySnapshot[] = [];

  for (let month = 1; month <= FORGIVENESS_MONTHS; month++) {
    const interestThisMonth = balance * monthlyRate;
    const payment = Math.min(cappedPayment, balance + interestThisMonth);
    const principalPaid = payment - interestThisMonth;

    totalPaid += payment;
    totalInterest += interestThisMonth;

    if (principalPaid >= 0) {
      balance = Math.max(0, balance - principalPaid);
    } else {
      // Interest capitalizes
      balance += Math.abs(principalPaid);
    }

    schedule.push({
      month,
      payment: Math.round(payment * 100) / 100,
      principal: Math.round(Math.max(0, principalPaid) * 100) / 100,
      interest: Math.round(interestThisMonth * 100) / 100,
      balance: Math.round(balance * 100) / 100,
    });

    if (balance <= 0) break;
  }

  return {
    planName: 'PAYE',
    monthlyPayment: Math.round(cappedPayment * 100) / 100,
    totalPaid: Math.round(totalPaid * 100) / 100,
    totalInterest: Math.round(totalInterest * 100) / 100,
    forgivenessAmount: Math.round(Math.max(0, balance) * 100) / 100,
    forgivenessMonths: FORGIVENESS_MONTHS,
    repaymentMonths: schedule.length,
    discretionaryIncome: Math.round(discretionaryIncome * 100) / 100,
    povertyGuideline,
    paymentSchedule: schedule,
  };
}