-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPaystackFees.js
222 lines (206 loc) · 6.92 KB
/
PaystackFees.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
try {
require('@hapi/joi'); // eslint-disable-line global-require
} catch (error) {
process.emitWarning('Please install peer dependency - @hapi/joi - so that validations can run.');
throw error;
}
const Joi = require('@hapi/joi');
/**
* A class for managing Paystack fees in your application.
*
* Installation
* ============
* **Peer Dependency (required)**
*
* Install Joi which we have employed for validating parameters sent to functions in this library
* ```
* npm install --save @hapi/joi
* ```
* Now install the module
*```
* npm install --save paystack-fees
*```
*
*
* Sample Usage
* ============
* ```javascript
* const PaystackFees = require('paystack-fees');
* // The lines below will create a new object that calculates fees based on the fee
* // schedule for USD payments. The cap is set to an arbitrarily high number since there
* // is no cap.
* const paystackFees = (new PaystackFees())
* .withPercentage(0.035)
* .withAdditionalCharge(0)
* .withCap(1000000000000)
* .withThreshold(0);
*
* // you can now calculate fees for an amount by calling the `calculateFor` function
* const feesFor1000USD = paystackFees.calculateFor(100000);
* // or the amount to be sent when we intend to be settled a target amount
* const amountToSendToBeSettled50USD = paystackFees.addTo(5000);
* ```
*/
class PaystackFees {
constructor() {
this.percentage = PaystackFees.DEFAULT_LOCAL_NGN_PERCENTAGE;
this.additionalCharge = PaystackFees.DEFAULT_LOCAL_NGN_ADDITIONAL_CHARGE;
this.threshold = PaystackFees.DEFAULT_LOCAL_NGN_THRESHOLD;
this.cap = PaystackFees.DEFAULT_LOCAL_NGN_CAP;
}
get chargeDivider() {
return 1 - this.percentage;
}
get crossover() {
return this.threshold * this.chargeDivider - this.additionalCharge;
}
get flatlinePlusCharge() {
return (this.cap - this.additionalCharge) / this.percentage;
}
get flatline() {
return this.flatlinePlusCharge - this.cap;
}
/**
* @description set the percentage
* @throws if percentage sent is invalid
* @param {number} percentage - positive number less than 1
* @returns the current PaystackFees object
*/
withPercentage(percentage) {
Joi.assert(
{ percentage },
Joi.object({
percentage: Joi.number()
.positive()
.less(1)
.allow(0)
.required(),
}),
);
this.percentage = percentage;
return this;
}
/**
* @description set the additional charge which will be added if the amount is over threshold
* @throws if additional charge sent is invalid
* @param {number} additionalCharge - 0 or more
* @returns the current PaystackFees object
*/
withAdditionalCharge(additionalCharge) {
Joi.assert(
{ additionalCharge },
Joi.object({
additionalCharge: Joi.number()
.positive()
.integer()
.allow(0)
.required(),
}),
);
this.additionalCharge = additionalCharge;
return this;
}
/**
* @description set the threshold, beyond which additional charge
* will be added to fees.
* @throws if threshold sent is invalid
* @param {number} threshold - 0 or more
* @returns the current PaystackFees object
*/
withThreshold(threshold) {
Joi.assert(
{ threshold },
Joi.object({
threshold: Joi.number()
.positive()
.integer()
.allow(0)
.required(),
}),
);
this.threshold = threshold;
return this;
}
/**
* @description set the cap
* @throws if cap sent is invalid
* @param {number} cap - positive number greater than or equal to 1
* @returns the current PaystackFees object
*/
withCap(cap) {
Joi.assert(
{ cap },
Joi.object({
cap: Joi.number()
.positive()
.integer()
.required(),
}),
);
this.cap = cap;
return this;
}
/**
* @description calculate amount to be sent to paystack to be settled the amount provided
* @throws if amountInLowerDenomination sent is invalid
* @param {number} amountInLowerDenomination - The amount we want to be settled after
* paystack deducts fees
* @returns amount you should send in lower denomination
* @example paystackFee.addTo(10000) // add fees so we can be settled 100 in higher denomination
*/
addTo(amountInLowerDenomination) {
Joi.assert(
{ amountInLowerDenomination },
Joi.object({
amountInLowerDenomination: Joi.number()
.positive()
.integer()
.allow(0)
.required(),
}),
);
if (this.percentage === 0) {
return amountInLowerDenomination + Math.min(this.cap, this.additionalCharge);
}
if (amountInLowerDenomination > this.flatline) {
return amountInLowerDenomination + this.cap;
}
if (amountInLowerDenomination > this.crossover) {
return Math.ceil(
(amountInLowerDenomination + this.additionalCharge) / this.chargeDivider,
);
}
return Math.ceil(amountInLowerDenomination / this.chargeDivider) || 1;
}
/**
* @description Calculates the fee for an amount in lower denomination
* @throws if amountInLowerDenomination sent is invalid
* @param {number} amountInLowerDenomination - The amount we want to send to paystack
* @returns fees in lower denomination
* @example
* // calculate the charge that will be deducted if
* // a local cards pays 100 naira
* const amountInKobo = 10000; // 10000 kobo is 100 naira
* paystackFee.calculateFor(amountInKobo);
*/
calculateFor(amountInLowerDenomination) {
Joi.assert(
{ amountInLowerDenomination },
Joi.object({
amountInLowerDenomination: Joi.number()
.positive()
.integer()
.allow(0)
.required(),
}),
);
const flat = amountInLowerDenomination > this.threshold ? this.additionalCharge : 0;
const fees = Math.ceil(this.percentage * amountInLowerDenomination + flat);
return Math.min(fees, this.cap);
}
}
PaystackFees.DEFAULT_LOCAL_NGN_PERCENTAGE = 0.015;
PaystackFees.DEFAULT_LOCAL_NGN_ADDITIONAL_CHARGE = 10000;
PaystackFees.DEFAULT_LOCAL_NGN_THRESHOLD = 250000;
PaystackFees.DEFAULT_LOCAL_NGN_CAP = 200000;
module.exports = PaystackFees;