forked from yabacon/paystack-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFee.php
More file actions
117 lines (100 loc) · 3.14 KB
/
Fee.php
File metadata and controls
117 lines (100 loc) · 3.14 KB
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
<?php
namespace Yabacon\Paystack;
class Fee
{
const DEFAULT_PERCENTAGE = 0.015;
const DEFAULT_ADDITIONAL_CHARGE = 10000;
const DEFAULT_THRESHOLD = 250000;
const DEFAULT_CAP = 200000;
public static $default_percentage = Fee::DEFAULT_PERCENTAGE;
public static $default_additional_charge = Fee::DEFAULT_ADDITIONAL_CHARGE;
public static $default_threshold = Fee::DEFAULT_THRESHOLD;
public static $default_cap = Fee::DEFAULT_CAP;
private $percentage;
private $additional_charge;
private $threshold;
private $cap;
private $chargeDivider;
private $crossover;
private $flatlinePlusCharge;
private $flatline;
public function __construct()
{
$this->percentage = Fee::$default_percentage;
$this->additional_charge = Fee::$default_additional_charge;
$this->threshold = Fee::$default_threshold;
$this->cap = Fee::$default_cap;
$this->__setup();
}
public function withPercentage($percentage)
{
$this->percentage = $percentage;
$this->__setup();
}
public static function resetDefaults()
{
Fee::$default_percentage = Fee::DEFAULT_PERCENTAGE;
Fee::$default_additional_charge = Fee::DEFAULT_ADDITIONAL_CHARGE;
Fee::$default_threshold = Fee::DEFAULT_THRESHOLD;
Fee::$default_cap = Fee::DEFAULT_CAP;
}
public function withAdditionalCharge($additional_charge)
{
$this->additional_charge = $additional_charge;
$this->__setup();
}
public function withThreshold($threshold)
{
$this->threshold = $threshold;
$this->__setup();
}
public function withCap($cap)
{
$this->cap = $cap;
$this->__setup();
}
private function __setup()
{
$this->chargeDivider = $this->__chargeDivider();
$this->crossover = $this->__crossover();
$this->flatlinePlusCharge = $this->__flatlinePlusCharge();
$this->flatline = $this->__flatline();
}
private function __chargeDivider()
{
return 1 - $this->percentage;
}
private function __crossover()
{
return ($this->threshold * $this->chargeDivider) - $this->additional_charge;
}
private function __flatlinePlusCharge()
{
return ($this->cap - $this->additional_charge) / $this->percentage;
}
private function __flatline()
{
return $this->flatlinePlusCharge - $this->cap;
}
public function addFor($amountinkobo)
{
if ($amountinkobo > $this->flatline) {
return intval(ceil($amountinkobo + $this->cap));
} elseif ($amountinkobo > $this->crossover) {
return intval(ceil(($amountinkobo + $this->additional_charge) / $this->chargeDivider));
} else {
return intval(ceil($amountinkobo / $this->chargeDivider));
}
}
public function calculateFor($amountinkobo)
{
$fee = $this->percentage * $amountinkobo;
if ($amountinkobo >= $this->threshold) {
$fee += $this->additional_charge;
}
if ($fee > $this->cap) {
$fee = $this->cap;
}
return intval(ceil($fee));
}
}