Last active
March 23, 2019 07:21
-
-
Save ibrahimlawal/9ca180ab8cec24e9ed2f0e8828b552ab to your computer and use it in GitHub Desktop.
Add Paystack fees in Javascript
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
function PaystackFee() { | |
this.DEFAULT_PERCENTAGE = 0.015; | |
this.DEFAULT_ADDITIONAL_CHARGE = 10000; | |
this.DEFAULT_THRESHOLD = 250000; | |
this.DEFAULT_CAP = 200000; | |
this.percentage = this.DEFAULT_PERCENTAGE; | |
this.additional_charge = this.DEFAULT_ADDITIONAL_CHARGE; | |
this.threshold = this.DEFAULT_THRESHOLD; | |
this.cap = this.DEFAULT_CAP; | |
this.chargeDivider = 0; | |
this.crossover = 0; | |
this.flatlinePlusCharge = 0; | |
this.flatline = 0; | |
this.withPercentage = function (percentage) { | |
this.percentage = percentage; | |
return this.__setup(); | |
}; | |
this.withAdditionalCharge = function (additional_charge) { | |
this.additional_charge = additional_charge; | |
return this.__setup(); | |
}; | |
this.withThreshold = function (threshold) { | |
this.threshold = threshold; | |
return this.__setup(); | |
}; | |
this.withCap = function (cap) { | |
this.cap = cap; | |
return this.__setup(); | |
}; | |
this.__setup = function () { | |
this.chargeDivider = this.__chargeDivider(); | |
this.crossover = this.__crossover(); | |
this.flatlinePlusCharge = this.__flatlinePlusCharge(); | |
this.flatline = this.__flatline(); | |
return this; | |
}; | |
this.__chargeDivider = function () { | |
return 1 - this.percentage; | |
}; | |
this.__crossover = function () { | |
return (this.threshold * this.chargeDivider) - this.additional_charge; | |
}; | |
this.__flatlinePlusCharge = function () { | |
return (this.cap - this.additional_charge) / this.percentage; | |
}; | |
this.__flatline = function () { | |
return this.flatlinePlusCharge - this.cap; | |
}; | |
this.addFor = function (amountinkobo) { | |
if (amountinkobo > this.flatline) | |
return parseInt(Math.round(amountinkobo + this.cap)); | |
else if (amountinkobo > this.crossover) | |
return parseInt(Math.ceil((amountinkobo + this.additional_charge) / this.chargeDivider)); | |
else | |
return parseInt(Math.ceil(amountinkobo / this.chargeDivider)); | |
}; | |
this.__setup = function () { | |
this.chargeDivider = this.__chargeDivider(); | |
this.crossover = this.__crossover(); | |
this.flatlinePlusCharge = this.__flatlinePlusCharge(); | |
this.flatline = this.__flatline(); | |
}; | |
this.__setup(); | |
} |
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
var obj = new PaystackFee(); | |
console.log( obj.addFor(9850)); | |
// to add foreign payment costs... | |
obj.withAdditionalCharge(10000) | |
.withThreshold(250000) | |
.withCap(10000000000000) | |
.withPercentage(0.039); | |
console.log(obj.addFor(530000)); | |
console.log(obj.addFor(975000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment