/** * Live courier rates pricing, as the checkout works it out (CheckoutQuoteService in PHP). * Used for the example price on the Checkout rates settings screen. */ import type { CheckoutRounding } from '../../../types/checkout-rates'; /** * Round a price in pounds the way the merchant chose. Works in whole pence, so float noise * never pushes a price up a step. * * @param price Price in pounds. * @param rounding Rounding option. * @return Rounded price in pounds. */ export function roundPrice(price: number, rounding: CheckoutRounding): number { let pence = Math.round(price * 100); if (rounding === 'up_10p') pence = Math.ceil(pence / 10) * 10; if (rounding === 'up_pound') pence = Math.ceil(pence / 100) * 100; if (rounding === 'end_99') pence = Math.ceil((pence + 1) / 100) * 100 - 1; return pence / 100; } /** * The customer's price from a Parcel2Go price: plus the percentage, plus the fixed amount, rounded. * Negative amounts take the change off (a price reduction); the price never goes below £0.00. * * @param base Parcel2Go price in pounds (before or with VAT). * @param percent Percentage to add (negative to take off). * @param fixed Pounds to add (negative to take off). * @param rounding Rounding option. * @return Customer's price in pounds. */ export function markedUpPrice( base: number, percent: number, fixed: number, rounding: CheckoutRounding ): number { return roundPrice(Math.max(0, base * (1 + percent / 100) + fixed), rounding); }