Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/purchases/charge-amount.ts
Views: 687
import { round2up } from "@cocalc/util/misc";12export default function getChargeAmount({3cost,4balance,5minBalance,6minPayment,7}: {8cost: number;9balance: number;10minBalance: number;11minPayment: number;12}): {13amountDue: number;14chargeAmount: number;15cureAmount: number;16minimumPaymentCharge: number;17} {18// Figure out what the amount due is, not worrying about the minPayment (we do that below).19let amountDue = cost;2021// Sometimes for weird reasons the balance goes below the minimum allowed balance,22// so if that happens we correct that here.23const cureAmount = Math.max(minBalance - balance, 0);24// get back up to the minimum balance -- this should never be required,25// but sometimes it is, e.g., maybe there is a race condition with purchases26// or an admin explicitly increases the minimum balance27amountDue += cureAmount;2829const availableCredit = balance - minBalance + cureAmount;30const appliedCredit = Math.min(availableCredit, amountDue);31if (availableCredit > 0) {32// We extend a little bit of credit to this user, because they33// have a minBalance below 0:34amountDue -= appliedCredit;35}3637const minimumPaymentCharge =38amountDue > 0 ? Math.max(amountDue, minPayment) - amountDue : 0;3940// amount due can never be negative.41// We always round up though -- if the user owes us 1.053 cents and we charge 1.05, then42// they still owe 0.003 and the purchase fails!43amountDue = Math.max(0, round2up(amountDue));4445// amount you actually have to pay, due to our min payment requirement46const chargeAmount = amountDue == 0 ? 0 : Math.max(amountDue, minPayment);4748return {49amountDue,50chargeAmount,51cureAmount,52minimumPaymentCharge,53};54}555657