Skip to content

Commit 1383b9a

Browse files
authored
Merge pull request #50 from PaystackHQ/fix/convenience-fee-error
Fix/convenience fee error
2 parents 898970d + 347fd4d commit 1383b9a

File tree

3 files changed

+383
-301
lines changed

3 files changed

+383
-301
lines changed

admin/class-paystack-forms-admin.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ function kkd_pff_paystack_setting_page()
8080
</tr>
8181

8282
</table>
83-
84-
<table class="form-table paystack_setting_page">
85-
86-
<hr>
87-
83+
84+
<hr>
85+
86+
<table class="form-table paystack_setting_page" id="paystack_setting_fees">
8887
<h2>Fees Settings</h2>
8988

9089
<tr valign="top">
@@ -400,12 +399,14 @@ function kkd_pff_paystack_editor_add_form_data()
400399
}
401400
echo '<p>Pay button Description:</p>';
402401
echo '<input type="text" name="_paybtn" value="' . $paybtn . '" class="widefat" />';
403-
echo '<p>Add Convenience Fee:</p>';
402+
echo '<p>Add Extra Charge:</p>';
404403
echo '<select class="form-control" name="_txncharge" id="parent_id" style="width:100%;">
405404
<option value="merchant"'.kkd_pff_paystack_txncheck('merchant', $txncharge).'>No, do not add</option>
406405
<option value="customer" '.kkd_pff_paystack_txncheck('customer', $txncharge).'>Yes, add it</option>
407406
</select>
408-
<br><small>This adds an estimate of Paystack transaction local fee to the transaction amount. This cushions the effect of the fees on the merchant</small>';
407+
<br><small>This allows you include an extra charge to cushion the effect of the transaction fee. <a href="';
408+
echo get_admin_url() . "edit.php?post_type=paystack_form&page=class-paystack-forms-admin.php#paystack_setting_fees";
409+
echo '"><em>Configure</em></a></small>';
409410
echo '<p>User logged In:</p>';
410411
echo '<select class="form-control" name="_loggedin" id="parent_id" style="width:100%;">
411412
<option value="no" '.kkd_pff_paystack_txncheck('no', $loggedin).'>User must not be logged in</option>

public/class-paystack-forms-public.php

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,82 @@ public function enqueue_scripts()
6262
define('KKD_PFF_PAYSTACK_FLATLINE_AMOUNT_PLUS_CHARGE', intval((KKD_PFF_PAYSTACK_LOCAL_CAP-KKD_PFF_PAYSTACK_ADDITIONAL_CHARGE)/KKD_PFF_PAYSTACK_PERCENTAGE));
6363
define('KKD_PFF_PAYSTACK_FLATLINE_AMOUNT', KKD_PFF_PAYSTACK_FLATLINE_AMOUNT_PLUS_CHARGE - KKD_PFF_PAYSTACK_LOCAL_CAP);
6464

65-
function kkd_pff_paystack_add_paystack_charge($amount)
65+
class Kkd_Pff_Paystack_PaystackCharge
6666
{
67-
// $amountinkobo = $amount; // * 100;
68-
$charge = 0;
69-
$amount = intval($amount);
70-
if ($amount <= 2500) {
71-
$charge = floatval($amount*KKD_PFF_PAYSTACK_PERCENTAGE);
72-
} else {
73-
$charge = floatval($amount*KKD_PFF_PAYSTACK_PERCENTAGE)+100;
67+
public $percentage;
68+
public $additional_charge;
69+
public $crossover_total;
70+
public $cap;
71+
72+
public $charge_divider;
73+
public $crossover;
74+
public $flatline_plus_charge;
75+
public $flatline;
76+
77+
public function __construct($percentage = 0.015, $additional_charge = 10000, $crossover_total = 250000, $cap = 200000)
78+
{
79+
$this->percentage = $percentage;
80+
$this->additional_charge = $additional_charge;
81+
$this->crossover_total = $crossover_total;
82+
$this->cap = $cap;
83+
$this->__setup();
7484
}
75-
// echo $charge;
76-
if ($charge > 2000) {
77-
$charge = 2000;
85+
86+
private function __setup()
87+
{
88+
$this->charge_divider = $this->__charge_divider();
89+
$this->crossover = $this->__crossover();
90+
$this->flatline_plus_charge = $this->__flatline_plus_charge();
91+
$this->flatline = $this->__flatline();
92+
}
93+
94+
private function __charge_divider()
95+
{
96+
return floatval(1 - $this->percentage);
97+
}
98+
99+
private function __crossover()
100+
{
101+
return ceil(($this->crossover_total * $this->charge_divider) - $this->additional_charge);
102+
}
103+
104+
private function __flatline_plus_charge()
105+
{
106+
return floor(($this->cap - $this->additional_charge) / $this->percentage);
107+
}
108+
109+
private function __flatline()
110+
{
111+
return $this->flatline_plus_charge - $this->cap;
78112
}
79-
$amount += $charge;
80-
return $amount;
81-
// if ($amountinkobo > KKD_PFF_PAYSTACK_FLATLINE_AMOUNT)
82-
// return ($amountinkobo + KKD_PFF_PAYSTACK_LOCAL_CAP)/100;
83-
// elseif ($amountinkobo > KKD_PFF_PAYSTACK_CROSSOVER_AMOUNT)
84-
// return (intval(($amountinkobo + KKD_PFF_PAYSTACK_ADDITIONAL_CHARGE) / KKD_PFF_PAYSTACK_CHARGE_DIVIDER))/100;
85-
// else
86-
// return (intval($amountinkobo / KKD_PFF_PAYSTACK_CHARGE_DIVIDER))/100;
113+
114+
public function add_for_kobo($amountinkobo)
115+
{
116+
if ($amountinkobo > $this->flatline) {
117+
return $amountinkobo + $this->cap;
118+
} elseif ($amountinkobo > $this->crossover) {
119+
return ceil(($amountinkobo + $this->additional_charge) / $this->charge_divider);
120+
} else {
121+
return ceil($amountinkobo / $this->charge_divider);
122+
}
123+
}
124+
125+
public function add_for_ngn($amountinngn)
126+
{
127+
return $this->add_for_kobo(ceil($amountinngn * 100)) / 100;
128+
}
129+
}
130+
131+
function kkd_pff_paystack_add_paystack_charge($amount)
132+
{
133+
$feeSettings = Kkd_Pff_Paystack_Public::fetchFeeSettings();
134+
$pc = new Kkd_Pff_Paystack_PaystackCharge(
135+
$feeSettings['prc'],
136+
$feeSettings['adc'],
137+
$feeSettings['ths'],
138+
$feeSettings['cap']
139+
);
140+
return $pc->add_for_ngn($amount);
87141
}
88142

89143
add_filter("wp_mail_content_type", "kkd_pff_paystack_mail_content_type");
@@ -777,6 +831,14 @@ function kkd_pff_paystack_form_shortcode($atts)
777831
echo '<input type="hidden" name="pf-id" value="' . $id . '" />';
778832
echo '<input type="hidden" name="pf-user_id" value="' . $user_id. '" />';
779833
echo '<input type="hidden" name="pf-recur" value="' . $recur. '" />';
834+
echo '<input type="hidden" name="pf-currency" id="pf-currency" value="' . $currency. '" />';
835+
$feeSettings = Kkd_Pff_Paystack_Public::fetchFeeSettings();
836+
echo '<script>window.KKD_PAYSTACK_CHARGE_SETTINGS={
837+
percentage:'.$feeSettings['prc'].',
838+
additional_charge:'.$feeSettings['adc'].',
839+
threshold:'.$feeSettings['ths'].',
840+
cap:'.$feeSettings['cap'].'
841+
}</script>';
780842
echo '<div class="span12 unit">
781843
<label class="label">Full Name <span>*</span></label>
782844
<div class="input">
@@ -794,10 +856,9 @@ function kkd_pff_paystack_form_shortcode($atts)
794856
if ($loggedin == 'yes') {
795857
echo 'readonly ';
796858
}
797-
798859
echo' required>
799860
</div>
800-
</div>';
861+
</div>';
801862
echo '<div class="span12 unit">
802863
<label class="label">Amount ('.$currency;
803864
if ($minimum == 0 && $amount != 0 && $usequantity == 'yes') {
@@ -850,8 +911,8 @@ function kkd_pff_paystack_form_shortcode($atts)
850911
}
851912
}
852913
}
853-
if ($txncharge != 'merchant' && $recur != 'plan') {
854-
echo '<small>Transaction Charge: <b class="pf-txncharge"></b>, Total:<b class="pf-txntotal"></b></small>';
914+
if ($txncharge != 'merchant' && $recur != 'plan' && $usequantity!=="yes") {
915+
echo '<small>Transaction Charge: <b class="pf-txncharge"></b>, Total: <b class="pf-txntotal"></b></small>';
855916
}
856917

857918
echo '<span id="pf-min-val-warn" style="color: red; font-size: 13px;"></span>
@@ -877,11 +938,14 @@ function kkd_pff_paystack_form_shortcode($atts)
877938
<label class="label">Total ('.$currency;
878939
echo') <span>*</span></label>
879940
<div class="input">
880-
<input type="text" id="pf-total" name="pf-total" placeholder="" value="" disabled>
881-
</div>
941+
<input type="text" id="pf-total" name="pf-total" placeholder="" value="" disabled>';
942+
if ($txncharge != 'merchant' && $recur != 'plan') {
943+
echo '<small>Transaction Charge: <b class="pf-txncharge"></b>, Total: <b class="pf-txntotal"></b></small>';
944+
}
945+
echo '</div>
882946
</div>';
883947
}
884-
948+
885949
if ($recur == 'optional') {
886950
echo '<div class="span12 unit">
887951
<label class="label">Recurring Payment</label>

0 commit comments

Comments
 (0)