Skip to content

Commit cc96cef

Browse files
committed
Adding multiple amount options in progress
1 parent 26063b1 commit cc96cef

File tree

3 files changed

+120
-60
lines changed

3 files changed

+120
-60
lines changed

admin/class-paystack-forms-admin.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ function kkd_pff_paystack_editor_add_form_data() {
287287
$filelimit = get_post_meta($post->ID, '_filelimit', true);
288288
$redirect = get_post_meta($post->ID, '_redirect', true);
289289
$minimum = get_post_meta($post->ID, '_minimum', true);
290+
$usevariableamount = get_post_meta($post->ID, '_usevariableamount', true);
291+
$variableamount = get_post_meta($post->ID, '_variableamount', true);
290292

291293
if ($amount == "") {$amount = 0;}
292294
if ($filelimit == "") {$filelimit = 2;}
@@ -295,18 +297,29 @@ function kkd_pff_paystack_editor_add_form_data() {
295297
if ($currency == "") {$currency = 'NGN';}
296298
if ($txncharge == "") {$txncharge = 'merchant';}
297299
if ($minimum == "") {$minimum = 0;}
300+
if ($uservariableamount == "") {$uservariableamount = 0;}
301+
if ($variableamount == "") {$variableamount = '';}
298302
// Echo out the field
299-
echo '<p>Currency:</p>';
303+
304+
305+
echo '<p>Currency:</p>';
300306
echo '<input type="text" name="_currency" value="' . $currency . '" class="widefat" />
301307
<small>We currently support only payments in Naira(NGN).</small>';
302-
echo '<p>Amount to be paid(Set 0 for customer input):</p>';
308+
echo '<p>Amount to be paid(Set 0 for customer input):</p>';
303309
echo '<input type="number" name="_amount" value="' . $amount . '" class="widefat pf-number" />';
304310
if ($minimum == 1) {
305311
echo '<br><label><input name="_minimum" type="checkbox" value="1" checked> Make amount minimum payable </label>';
306312
}else{
307313
echo '<br><label><input name="_minimum" type="checkbox" value="1"> Make amount minimum payable </label>';
308314
}
309-
echo '<p>Pay button Description:</p>';
315+
echo '<p>Variable Dropdown Amount:<code><label>Format(option:amount): Option 1:10000,Option 2:3000 Separate options with "," </code></label></p>';
316+
echo '<input type="text" name="_variableamount" value="' . $variableamount . '" class="widefat " />';
317+
if ($usevariableamount == 1) {
318+
echo '<br><label><input name="_usevariableamount" type="checkbox" value="1" checked> Use dropdown amount option </label>';
319+
}else{
320+
echo '<br><label><input name="_usevariableamount" type="checkbox" value="1" > Use dropdown amount option </label>';
321+
}
322+
echo '<p>Pay button Description:</p>';
310323
echo '<input type="text" name="_paybtn" value="' . $paybtn . '" class="widefat" />';
311324
echo '<p>Transaction Charges:</p>';
312325
echo '<select class="form-control" name="_txncharge" id="parent_id" style="width:100%;">
@@ -475,6 +488,10 @@ function kkd_pff_paystack_save_data($post_id, $post) {
475488

476489
$form_meta['_amount'] = $_POST['_amount'];
477490
$form_meta['_minimum'] = $_POST['_minimum'];
491+
492+
$form_meta['_variableamount'] = $_POST['_variableamount'];
493+
$form_meta['_usevariableamount'] = $_POST['_usevariableamount'];
494+
478495
$form_meta['_paybtn'] = $_POST['_paybtn'];
479496
$form_meta['_currency'] = $_POST['_currency'];
480497
$form_meta['_successmsg'] = $_POST['_successmsg'];
@@ -498,7 +515,6 @@ function kkd_pff_paystack_save_data($post_id, $post) {
498515
$form_meta['_subaccount'] = $_POST['_subaccount'];
499516
$form_meta['_txnbearer'] = $_POST['_txnbearer'];
500517
$form_meta['_merchantamount'] = $_POST['_merchantamount'];
501-
502518
// Add values of $form_meta as custom fields
503519

504520
foreach ($form_meta as $key => $value) { // Cycle through the $form_meta array!

public/class-paystack-forms-public.php

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,19 @@ function kkd_pff_paystack_form_shortcode($atts) {
723723
$useagreement = get_post_meta($id,'_useagreement',true);
724724
$agreementlink = get_post_meta($id,'_agreementlink',true);
725725
$minimum = get_post_meta($id,'_minimum',true);
726-
if ($minimum == "") {$minimum = 0;}
727-
726+
$variableamount = get_post_meta($id,'_variableamount',true);
727+
$usevariableamount = get_post_meta($id,'_usevariableamount',true);
728+
if ($minimum == "") {$minimum = 0;}
729+
if ($usevariableamount == "") {$usevariableamount = 0;}
730+
731+
if ($usevariableamount == 1) {
732+
// $variableamount = '3GB Etisalat:10000';
733+
$paymentoptions = explode(',', $variableamount);
734+
echo "<pre>";
735+
print_r($paymentoptions);
736+
echo "</pre>";
737+
// die();
738+
}
728739
$showbtn = true;
729740
$planerrorcode = 'Input Correct Recurring Plan Code';
730741
if ($recur == 'plan') {
@@ -740,12 +751,10 @@ function kkd_pff_paystack_form_shortcode($atts) {
740751
}
741752

742753
}
743-
// print_r($loggedin);
744754
if ((($user_id != 0) && ($loggedin == 'yes')) || $loggedin == 'no') {
745755

746756
echo "<h1 id='pf-form".$id."'>".$obj->post_title."</h1>";
747-
// echo get_site_url().'/paystackinvoice/?code=ddddddd';
748-
echo '<form version="2.0.7" enctype="multipart/form-data" action="' . admin_url('admin-ajax.php') . '" url="' . admin_url() . '" method="post" class="paystack-form j-forms" novalidate>
757+
echo '<form version="2.0.8" enctype="multipart/form-data" action="' . admin_url('admin-ajax.php') . '" url="' . admin_url() . '" method="post" class="paystack-form j-forms" novalidate>
749758
<div class="j-row">';
750759
echo '<input type="hidden" name="action" value="kkd_pff_paystack_submit_action">';
751760
echo '<input type="hidden" name="pf-id" value="' . $id . '" />';
@@ -756,9 +765,6 @@ function kkd_pff_paystack_form_shortcode($atts) {
756765
<div class="input">
757766
<input type="text" name="pf-fname" placeholder="First & Last Name" value="' . $fullname. '"
758767
';
759-
// if($loggedin == 'yes'){
760-
// echo 'readonly ';
761-
// }
762768

763769
echo' required>
764770
</div>
@@ -778,36 +784,55 @@ function kkd_pff_paystack_form_shortcode($atts) {
778784
echo '<div class="span12 unit">
779785
<label class="label">Amount ('.$currency.') <span>*</span></label>
780786
<div class="input">';
781-
// echo "<pre>".$minimum. "</pre>";
782-
if ($minimum == 1) {
783-
echo '<small> Minimum payable amount <b style="font-size:87% !important;">'.$currency.' '.number_format($amount).'</b></small>';
784-
}
785-
if ($recur == 'plan') {
786-
if ($showbtn) {
787-
echo '<input type="text" name="pf-amount" value="'.$planamount.'" id="pf-amount" readonly required/>';
788-
}else{
789-
echo '<div class="span12 unit">
790-
<label class="label" style="font-size:18px;font-weight:600;line-height: 20px;">'.$planerrorcode.'</label>
791-
</div>';
792-
}
793-
}elseif($recur == 'optional'){
794-
echo '<input type="text" name="pf-amount" class="pf-number" id="pf-amount" value="0" required/>';
795-
}else{
796-
if ($amount == 0) {
797-
echo '<input type="text" name="pf-amount" class="pf-number" value="0" id="pf-amount" required/>';
798-
}elseif($amount != 0 && $minimum == 1){
799-
echo '<input type="text" name="pf-amount" value="'.$amount.'" id="pf-amount" required/>';
800-
}else{
801-
echo '<input type="text" name="pf-amount" value="'.$amount.'" id="pf-amount" readonly required/>';
787+
if ($usevariableamount == 0) {
788+
if ($minimum == 1) {
789+
echo '<small> Minimum payable amount <b style="font-size:87% !important;">'.$currency.' '.number_format($amount).'</b></small>';
790+
}
791+
if ($recur == 'plan') {
792+
if ($showbtn) {
793+
echo '<input type="text" name="pf-amount" value="'.$planamount.'" id="pf-amount" readonly required/>';
794+
}else{
795+
echo '<div class="span12 unit">
796+
<label class="label" style="font-size:18px;font-weight:600;line-height: 20px;">'.$planerrorcode.'</label>
797+
</div>';
798+
}
799+
}elseif($recur == 'optional'){
800+
echo '<input type="text" name="pf-amount" class="pf-number" id="pf-amount" value="0" required/>';
801+
}else{
802+
if ($amount == 0) {
803+
echo '<input type="text" name="pf-amount" class="pf-number" value="0" id="pf-amount" required/>';
804+
}elseif($amount != 0 && $minimum == 1){
805+
echo '<input type="text" name="pf-amount" value="'.$amount.'" id="pf-amount" required/>';
806+
}else{
807+
echo '<input type="text" name="pf-amount" value="'.$amount.'" id="pf-amount" readonly required/>';
808+
}
809+
}
810+
}else{
811+
if ($usevariableamount == "") {
812+
echo "Form Error, set variable amount string";
813+
}else{
814+
if (count($paymentoptions) > 0) {
815+
echo '<div class="select">
816+
<input type="hidden" id="pf-vname" />
817+
<select class="form-control" id="pf-vamount" name="pf-amount">';
818+
$max = $quantity+1;
819+
foreach ($paymentoptions as $key => $paymentoption) {
820+
list($a,$b) = explode(':', $paymentoption);
821+
echo '<option value="'.$b.'" data-name="'.$a.'">'.$a.'('.number_format($b).')</option>';
822+
823+
}
824+
echo '</select> <i></i> </div>';
825+
802826
}
803-
}
804-
if ($txncharge != 'merchant' && $recur != 'plan') {
805-
echo '<small>Transaction Charge: <b class="pf-txncharge">NGN1000</b>, Total:<b class="pf-txntotal">NGN1000</b></small>';
806-
}
827+
}
828+
}
829+
if ($txncharge != 'merchant' && $recur != 'plan') {
830+
echo '<small>Transaction Charge: <b class="pf-txncharge"></b>, Total:<b class="pf-txntotal"></b></small>';
831+
}
807832

808833
echo '</div>
809834
</div>';
810-
if ($recur == 'no' && $usequantity == 'yes') {
835+
if ($recur == 'no' && $usequantity == 'yes' && $amount != 0) {
811836
echo '<div class="span12 unit">
812837
<label class="label">Quantity</label>
813838
<div class="select">
@@ -1115,6 +1140,8 @@ function kkd_pff_paystack_submit_action() {
11151140

11161141
$txncharge = get_post_meta($_POST["pf-id"],'_txncharge',true);
11171142
$minimum = get_post_meta($_POST["pf-id"],'_minimum',true);
1143+
$variableamount = get_post_meta($_POST["pf-id"],'_variableamount',true);
1144+
$usevariableamount = get_post_meta($_POST["pf-id"],'_usevariableamount',true);
11181145
$amount = (int)str_replace(' ', '', $_POST["pf-amount"]);//User input
11191146
$originalamount = $amount;
11201147
$quantity = 1;

public/js/paystack-forms-public.js

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
$(document).ready(function($) {
66
var international_card = false;
7-
var amountField = $('#pf-amount');
7+
if( $('#pf-vamount').length ){
8+
var amountField = $('#pf-vamount');
9+
10+
}else{
11+
var amountField = $('#pf-amount');
12+
13+
}
814
var max = 10;
915
amountField.keydown(function(e) {
1016
format_validate(max, e);
@@ -43,25 +49,29 @@
4349
$(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
4450
})
4551
}
46-
function calculateFees() {
52+
function calculateFees(transaction_amount) {
4753
setTimeout(function() {
48-
var transaction_amount = parseInt(amountField.val());
49-
var multiplier = 0.015;
50-
var fees = multiplier * transaction_amount;
51-
var extrafee = 0;
52-
if (fees > 2000) {
53-
var fees = 2000;
54-
}else{
55-
if (transaction_amount > 2500) {fees += 100};
56-
}
57-
var total = transaction_amount + fees;
54+
transaction_amount = transaction_amount || parseInt(amountField.val());
55+
if( $('#pf-vamount').length ){
56+
var name = $('#pf-vamount option:selected').attr('data-name');
57+
$('#pf-vname').val(name);
58+
}
59+
var multiplier = 0.015;
60+
var fees = multiplier * transaction_amount;
61+
var extrafee = 0;
62+
if (fees > 2000) {
63+
var fees = 2000;
64+
}else{
65+
if (transaction_amount > 2500) {fees += 100};
66+
}
67+
var total = transaction_amount + fees;
5868
// console.log(transaction_amount);
5969
if (transaction_amount == '' || transaction_amount == 0 || transaction_amount.length == 0 || transaction_amount == null || isNaN (transaction_amount)) {
60-
var total = 0;
61-
var fees = 0;
62-
}
63-
$(".pf-txncharge").hide().html("NGN"+fees.toFixed(2)).show().digits();
64-
$(".pf-txntotal").hide().html("NGN"+total.toFixed(2)).show().digits();
70+
var total = 0;
71+
var fees = 0;
72+
}
73+
$(".pf-txncharge").hide().html("NGN"+fees.toFixed(2)).show().digits();
74+
$(".pf-txntotal").hide().html("NGN"+total.toFixed(2)).show().digits();
6575
}, 100);
6676
}
6777

@@ -79,12 +89,19 @@
7989
}
8090
}
8191
});
82-
$('#pf-quantity').on('change', function() {
83-
var unit = $('#pf-qamount').val();
84-
var quant = this.value;
85-
var newvalue = unit*quant;
92+
$('#pf-quantity,#pf-vamount').on('change', function() {
93+
if( $('#pf-vamount').length ){
94+
var amountField = $('#pf-vamount');
95+
96+
}else{
97+
var amountField = $('#pf-qamount');
98+
99+
}
100+
var unit = amountField.val();
101+
var quant = $('#pf-quantity').val();
102+
var newvalue = unit * quant;
86103
$('#pf-amount').val(newvalue);
87-
calculateFees();
104+
calculateFees(newvalue);
88105
});
89106
function validateEmail(email) {
90107
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

0 commit comments

Comments
 (0)