Last active
March 19, 2019 11:48
-
-
Save ibrahimlawal/af3c04af9515e2d7ae65df40fb9c5020 to your computer and use it in GitHub Desktop.
Add Paystack fees in CSharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class PaystackFees | |
{ | |
const Double DEFAULT_PERCENTAGE = 0.015; | |
const Double DEFAULT_ADDITIONAL_CHARGE = 10000; | |
const Double DEFAULT_THRESHOLD = 250000; | |
const Double DEFAULT_CAP = 200000; | |
private Double percentage; | |
private Double additional_charge; | |
private Double threshold; | |
private Double cap; | |
private Double chargeDivider; | |
private Double crossover; | |
private Double flatlinePlusCharge; | |
private Double flatline; | |
public PaystackFees() | |
{ | |
this.percentage = PaystackFees.DEFAULT_ADDITIONAL_CHARGE; | |
this.additional_charge = PaystackFees.DEFAULT_ADDITIONAL_CHARGE; | |
this.threshold = PaystackFees.DEFAULT_THRESHOLD; | |
this.cap = PaystackFees.DEFAULT_CAP; | |
this.__setup(); | |
} | |
public PaystackFees withPercentage(Double percentage) | |
{ | |
this.percentage = percentage; | |
this.__setup(); | |
return this; | |
} | |
public PaystackFees withAdditionalCharge(Double additional_charge) | |
{ | |
this.additional_charge = additional_charge; | |
this.__setup(); | |
return this; | |
} | |
public PaystackFees withThreshold(Double threshold) | |
{ | |
this.threshold = threshold; | |
this.__setup(); | |
return this; | |
} | |
public PaystackFees withCap(Double cap) | |
{ | |
this.cap = cap; | |
this.__setup(); | |
return this; | |
} | |
private void __setup() | |
{ | |
this.chargeDivider = this.__chargeDivider(); | |
this.crossover = this.__crossover(); | |
this.flatlinePlusCharge = this.__flatlinePlusCharge(); | |
this.flatline = this.__flatline(); | |
} | |
private Double __chargeDivider() | |
{ | |
return 1 - this.percentage; | |
} | |
private Double __crossover() | |
{ | |
return (this.threshold * this.chargeDivider) - this.additional_charge; | |
} | |
private Double __flatlinePlusCharge() | |
{ | |
return (this.cap - this.additional_charge) / this.percentage; | |
} | |
private Double __flatline() | |
{ | |
return this.flatlinePlusCharge - this.cap; | |
} | |
public int addFeeFor(int amountinkobo) | |
{ | |
if (amountinkobo > this.flatline) { | |
return Convert.ToInt32(Math.Ceiling(amountinkobo + this.cap)); | |
} else if (amountinkobo > this.crossover) { | |
return Convert.ToInt32(Math.Ceiling((amountinkobo + this.additional_charge) / this.chargeDivider)); | |
} else { | |
return Convert.ToInt32(Math.Ceiling(amountinkobo / this.chargeDivider)); | |
} | |
} | |
public int calculateFeeFor(int amountinkobo) | |
{ | |
Double fee = this.percentage * amountinkobo; | |
if (amountinkobo >= this.threshold) { | |
fee += this.additional_charge; | |
} | |
if (fee > this.cap) { | |
fee = this.cap; | |
} | |
return Convert.ToInt32(Math.Ceiling(fee)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// import PaystackFees somehow; | |
public class PaystackFeesSample; | |
{ | |
public static void Main() | |
{ | |
PaystackFees p = new PaystackFees(); | |
// you can configure to use default charge (1.5% with a additional 100ngn if above 2500) | |
// or a negotiated charge | |
int calc = p.withPercentage(0.015) // 0.015 (1.5%) | |
.withThreshold(250000) // 250000 (2,500 naira) | |
.withAdditionalCharge(10000) // 10000 (100 naira) | |
.withCap(200000) | |
.addFeeFor(10000); | |
Console.WriteLine(calc); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment