Skip to content

Instantly share code, notes, and snippets.

@Mastersam07
Created December 20, 2024 10:30
Show Gist options
  • Save Mastersam07/3ce77386a5bfe4ab041eab23038fe314 to your computer and use it in GitHub Desktop.
Save Mastersam07/3ce77386a5bfe4ab041eab23038fe314 to your computer and use it in GitHub Desktop.
Paystack fee
class PaystackSetup {
private readonly decimalFee: number = 0.015; // 1.5%
private readonly feeCap: number = 2000; // Maximum fee per transaction
private readonly flatFee: number = 100; // Flat fee applied for transactions >= NGN 2,500
/// Calculates the total price to be charged to the customer, including transaction fees.
calculateTotalPrice(price: number): number {
if (price < 2500) {
return this.calculateBelow2500(price);
}
return this.calculateAbove2500(price);
}
/// Calculates the final amount for transactions below NGN 2,500.
private calculateBelow2500(price: number): number {
const applicableFees = this.decimalFee * price;
if (applicableFees > this.feeCap) {
// If percentage fee exceeds the fee cap, add the fee cap to the price
return this.roundToTwoDecimalPlaces(price + this.feeCap);
}
// Calculate the price including only the percentage fee
return this.roundToTwoDecimalPlaces(price / (1 - this.decimalFee));
}
/// Calculates the final amount for transactions of NGN 2,500 and above.
private calculateAbove2500(price: number): number {
const applicableFees = (this.decimalFee * price) + this.flatFee;
if (applicableFees > this.feeCap) {
// If combined fees exceed the fee cap, add the fee cap to the price
return this.roundToTwoDecimalPlaces(price + this.feeCap);
}
// Calculate the price including both percentage and flat fees
return this.roundToTwoDecimalPlaces((price + this.flatFee) / (1 - this.decimalFee));
}
/// Rounds a number to two decimal places.
private roundToTwoDecimalPlaces(value: number): number {
return Math.ceil(value * 100) / 100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment