Last active
April 6, 2020 00:57
-
-
Save ibrahimlawal/a383fc4682c92aff94aa9a5ad8635346 to your computer and use it in GitHub Desktop.
A php class to add paystack's local charge to a transaction total. Totally flops if the user pays with a foreign card.
This file contains hidden or 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
<?php | |
class PaystackFees | |
{ | |
const DEFAULT_PERCENTAGE = 0.015; | |
const DEFAULT_ADDITIONAL_CHARGE = 10000; | |
const DEFAULT_THRESHOLD = 250000; | |
const DEFAULT_CAP = 200000; | |
public static $default_percentage = PaystackFees::DEFAULT_PERCENTAGE; | |
public static $default_additional_charge = PaystackFees::DEFAULT_ADDITIONAL_CHARGE; | |
public static $default_threshold = PaystackFees::DEFAULT_THRESHOLD; | |
public static $default_cap = PaystackFees::DEFAULT_CAP; | |
private $percentage; | |
private $additional_charge; | |
private $threshold; | |
private $cap; | |
private $chargeDivider; | |
private $crossover; | |
private $flatlinePlusCharge; | |
private $flatline; | |
public function __construct() | |
{ | |
$this->percentage = PaystackFees::$default_percentage; | |
$this->additional_charge = PaystackFees::$default_additional_charge; | |
$this->threshold = PaystackFees::$default_threshold; | |
$this->cap = PaystackFees::$default_cap; | |
$this->__setup(); | |
} | |
public function withPercentage($percentage) | |
{ | |
$this->percentage = $percentage; | |
$this->__setup(); | |
return $this; | |
} | |
public static function resetDefaults() | |
{ | |
PaystackFees::$default_percentage = PaystackFees::DEFAULT_PERCENTAGE; | |
PaystackFees::$default_additional_charge = PaystackFees::DEFAULT_ADDITIONAL_CHARGE; | |
PaystackFees::$default_threshold = PaystackFees::DEFAULT_THRESHOLD; | |
PaystackFees::$default_cap = PaystackFees::DEFAULT_CAP; | |
} | |
public function withAdditionalCharge($additional_charge) | |
{ | |
$this->additional_charge = $additional_charge; | |
$this->__setup(); | |
return $this; | |
} | |
public function withThreshold($threshold) | |
{ | |
$this->threshold = $threshold; | |
$this->__setup(); | |
return $this; | |
} | |
public function withCap($cap) | |
{ | |
$this->cap = $cap; | |
$this->__setup(); | |
return $this; | |
} | |
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)); | |
} | |
} |
This file contains hidden or 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
<?php | |
require "PaystackFees.php"; | |
// you can configure to use default charge (1.5% with a additional 100ngn if above 2500) | |
// or a negotiated charge | |
$calc = new PaystackFees()) | |
->withPercentage($fee_percent) // 0.015 (1.5%) | |
->withThreshold(intval($charge_tresh)) // 250000 (2,500 naira) | |
->withAdditionalCharge(intval($flat * 100)) // 10000 (100 naira) | |
->withCap($cap); // 200000 (2,000 naira) | |
// after configuring, you can add the charge to an kobo value | |
// will return the amount to request so Paystack will settle with the supplied amount | |
$amount = $calc->addFor($original_amount); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment