-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
92 lines (69 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
const PayStack = require('./src/PayStack/index.js')
PayStack.prototype.version = '0.3.0'
const Fees = function (cap, additionalCharge, percentage, threshold) {
this.additionalCharge = additionalCharge || Fees.INIT_ADDITIONAL_CHARGE
this.percentage = percentage || Fees.INIT_PERCENT
this.threshold = threshold || Fees.INIT_THRESHOLD
this.cap = cap || Fees.INIT_CAP
this.compute()
}
Fees.INIT_THRESHOLD = 250000
Fees.INIT_CAP = 200000
Fees.INIT_PERCENT = 0.015
Fees.INIT_ADDITIONAL_CHARGE = 10000
Fees.prototype.resetDefaults = function () {
}
Fees.prototype.withAdditionalCharge = function (additionalCharge) {
this.additionalCharge = additionalCharge
return this
}
Fees.prototype.withThreshold = function (threshold) {
this.threshold = threshold
return this
}
Fees.prototype.withCap = function (cap) {
this.cap = cap
return this
}
Fees.prototype.compute = function () {
this.chargeDivider = this.__chargeDivider()
this.crossover = this.__crossover()
this.flatlinePlusCharge = this.__flatlinePlusCharge()
this.flatline = this.__flatline()
}
Fees.prototype.__chargeDivider = function () {
return 1 - this.percentage
}
Fees.prototype.__crossover = function () {
return (this.threshold * this.chargeDivider) - this.additionalCharge
}
Fees.prototype.__flatlinePlusCharge = function () {
return (this.cap - this.additionalCharge) / this.percentage
}
Fees.prototype.__flatline = function () {
return this.flatlinePlusCharge - this.cap
}
Fees.prototype.addFor = function (amountinkobo) {
this.compute()
if (amountinkobo > this.flatline) {
return parseInt(Math.ceil(amountinkobo + this.cap))
} else if (amountinkobo > this.crossover) {
return parseInt(Math.ceil((amountinkobo + this.additionalCharge) / this.chargeDivider))
} else {
return parseInt(Math.ceil(amountinkobo / this.chargeDivider))
}
}
Fees.prototype.calculateFor = function (amountinkobo) {
this.compute()
let fee = this.percentage * amountinkobo
if (amountinkobo >= this.threshold) {
fee += this.additionalCharge
}
if (fee > this.cap) {
fee = this.cap
}
return parseInt(Math.ceil(fee))
}
PayStack.Fees = Fees
module.exports = PayStack