Skip to content

Commit 25910e1

Browse files
committed
quantity field
1 parent a13d522 commit 25910e1

File tree

4 files changed

+136
-56
lines changed

4 files changed

+136
-56
lines changed

.DS_Store

0 Bytes
Binary file not shown.

admin/class-paystack-forms-admin.php

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -258,57 +258,58 @@ function kkd_pff_paystack_editor_add_extra_metaboxes() {
258258
add_meta_box('kkd_pff_paystack_editor_add_form_data', 'Extra Form Description', 'kkd_pff_paystack_editor_add_form_data', 'paystack_form', 'normal', 'default');
259259
add_meta_box('kkd_pff_paystack_editor_add_recur_data', 'Recurring Payment', 'kkd_pff_paystack_editor_add_recur_data', 'paystack_form', 'side', 'default');
260260
add_meta_box('kkd_pff_paystack_editor_add_email_data', 'Email Receipt Settings', 'kkd_pff_paystack_editor_add_email_data', 'paystack_form', 'normal', 'default');
261-
261+
add_meta_box('kkd_pff_paystack_editor_add_quantity_data', 'Quantity Payment', 'kkd_pff_paystack_editor_add_quantity_data', 'paystack_form', 'side', 'default');
262+
262263
}
263264

264265

265266
function kkd_pff_paystack_editor_add_form_data() {
266-
global $post;
267-
268-
// Noncename needed to verify where the data originated
269-
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
270-
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
271-
272-
// Get the location data if its already been entered
273-
$amount = get_post_meta($post->ID, '_amount', true);
274-
$paybtn = get_post_meta($post->ID, '_paybtn', true);
275-
$successmsg = get_post_meta($post->ID, '_successmsg', true);
276-
$txncharge = get_post_meta($post->ID, '_txncharge', true);
277-
$loggedin = get_post_meta($post->ID, '_loggedin', true);
278-
$currency = get_post_meta($post->ID, '_currency', true);
279-
$filelimit = get_post_meta($post->ID, '_filelimit', true);
280-
281-
if ($amount == "") {$amount = 0;}
282-
if ($filelimit == "") {$filelimit = 2;}
283-
if ($paybtn == "") {$paybtn = 'Pay';}
284-
if ($successmsg == "") {$successmsg = 'Thank you for paying!';}
285-
if ($currency == "") {$currency = 'NGN';}
286-
if ($txncharge == "") {$txncharge = 'merchant';}
287-
// Echo out the field
288-
echo '<p>Currency:</p>';
289-
echo '<input type="text" name="_currency" value="' . $currency . '" class="widefat" />
290-
<small>We currently support only payments in Naira(NGN).</small>';
291-
echo '<p>Amount to be paid(Set 0 for customer input):</p>';
292-
echo '<input type="number" name="_amount" value="' . $amount . '" class="widefat pf-number" />';
293-
echo '<p>Pay button Description:</p>';
294-
echo '<input type="text" name="_paybtn" value="' . $paybtn . '" class="widefat" />';
295-
echo '<p>Transaction Charges:</p>';
296-
echo '<select class="form-control" name="_txncharge" id="parent_id" style="width:100%;">
297-
<option value="merchant"'.kkd_pff_paystack_txncheck('merchant',$txncharge).'>Merchant Pays(Include in fee)</option>
298-
<option value="customer" '.kkd_pff_paystack_txncheck('customer',$txncharge).'>Client Pays(Extra Fee added) - 1.55%+ NGN100 if above NGN2,500 </option>
299-
</select>';
300-
echo '<p>User logged In:</p>';
301-
echo '<select class="form-control" name="_loggedin" id="parent_id" style="width:100%;">
302-
<option value="no" '.kkd_pff_paystack_txncheck('no',$loggedin).'>User must not be logged in</option>
303-
<option value="yes"'.kkd_pff_paystack_txncheck('yes',$loggedin).'>User must be logged In</option>
304-
</select>';
305-
echo '<p>Success Message after Payment</p>';
306-
echo '<textarea rows="3" name="_successmsg" class="widefat" >'.$successmsg.'</textarea>';
307-
echo '<p>File Upload Limit(MB):</p>';
308-
echo '<input ttype="number" name="_filelimit" value="' . $filelimit . '" class="widefat pf-number" />';
267+
global $post;
268+
269+
// Noncename needed to verify where the data originated
270+
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
271+
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
272+
273+
// Get the location data if its already been entered
274+
$amount = get_post_meta($post->ID, '_amount', true);
275+
$paybtn = get_post_meta($post->ID, '_paybtn', true);
276+
$successmsg = get_post_meta($post->ID, '_successmsg', true);
277+
$txncharge = get_post_meta($post->ID, '_txncharge', true);
278+
$loggedin = get_post_meta($post->ID, '_loggedin', true);
279+
$currency = get_post_meta($post->ID, '_currency', true);
280+
$filelimit = get_post_meta($post->ID, '_filelimit', true);
281+
282+
if ($amount == "") {$amount = 0;}
283+
if ($filelimit == "") {$filelimit = 2;}
284+
if ($paybtn == "") {$paybtn = 'Pay';}
285+
if ($successmsg == "") {$successmsg = 'Thank you for paying!';}
286+
if ($currency == "") {$currency = 'NGN';}
287+
if ($txncharge == "") {$txncharge = 'merchant';}
288+
// Echo out the field
289+
echo '<p>Currency:</p>';
290+
echo '<input type="text" name="_currency" value="' . $currency . '" class="widefat" />
291+
<small>We currently support only payments in Naira(NGN).</small>';
292+
echo '<p>Amount to be paid(Set 0 for customer input):</p>';
293+
echo '<input type="number" name="_amount" value="' . $amount . '" class="widefat pf-number" />';
294+
echo '<p>Pay button Description:</p>';
295+
echo '<input type="text" name="_paybtn" value="' . $paybtn . '" class="widefat" />';
296+
echo '<p>Transaction Charges:</p>';
297+
echo '<select class="form-control" name="_txncharge" id="parent_id" style="width:100%;">
298+
<option value="merchant"'.kkd_pff_paystack_txncheck('merchant',$txncharge).'>Merchant Pays(Include in fee)</option>
299+
<option value="customer" '.kkd_pff_paystack_txncheck('customer',$txncharge).'>Client Pays(Extra Fee added) - 1.55%+ NGN100 if above NGN2,500 </option>
300+
</select>';
301+
echo '<p>User logged In:</p>';
302+
echo '<select class="form-control" name="_loggedin" id="parent_id" style="width:100%;">
303+
<option value="no" '.kkd_pff_paystack_txncheck('no',$loggedin).'>User must not be logged in</option>
304+
<option value="yes"'.kkd_pff_paystack_txncheck('yes',$loggedin).'>User must be logged In</option>
305+
</select>';
306+
echo '<p>Success Message after Payment</p>';
307+
echo '<textarea rows="3" name="_successmsg" class="widefat" >'.$successmsg.'</textarea>';
308+
echo '<p>File Upload Limit(MB):</p>';
309+
echo '<input ttype="number" name="_filelimit" value="' . $filelimit . '" class="widefat pf-number" />';
309310

310311
}
311-
function kkd_pff_paystack_editor_add_email_data() {
312+
function kkd_pff_paystack_editor_add_email_data() {
312313
global $post;
313314

314315
// Noncename needed to verify where the data originated
@@ -339,7 +340,7 @@ function kkd_pff_paystack_editor_add_email_data() {
339340
echo '<textarea rows="6" name="_message" class="widefat" >'.$message.'</textarea>';
340341

341342
}
342-
function kkd_pff_paystack_editor_add_recur_data() {
343+
function kkd_pff_paystack_editor_add_recur_data() {
343344
global $post;
344345

345346
// Noncename needed to verify where the data originated
@@ -363,8 +364,32 @@ function kkd_pff_paystack_editor_add_recur_data() {
363364
echo '<input type="text" name="_recurplan" value="' . $recurplan . '" class="widefat" />
364365
<small>Plan amount must match amount on extra form description.</small>';
365366

366-
}
367-
function kkd_pff_paystack_save_data($post_id, $post) {
367+
}
368+
function kkd_pff_paystack_editor_add_quantity_data() {
369+
global $post;
370+
371+
// Noncename needed to verify where the data originated
372+
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
373+
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
374+
375+
// Get the location data if its already been entered
376+
$usequantity = get_post_meta($post->ID, '_usequantity', true);
377+
$quantity = get_post_meta($post->ID, '_quantity', true);
378+
379+
if ($usequantity == "") {$usequantity = 'no';}
380+
if ($quantity == "") {$quantity = '10';}
381+
// Echo out the field
382+
echo '<small>Allow your users pay in multiple quantity</small><p>Quantity Payment:</p>';
383+
echo '<select class="form-control" name="_usequantity" style="width:100%;">
384+
<option value="no" '.kkd_pff_paystack_txncheck('no',$usequantity).'>No</option>
385+
<option value="yes" '.kkd_pff_paystack_txncheck('yes',$usequantity).'>Yes</option>
386+
</select>';
387+
echo '<p>Paystack Recur Plan code:</p>';
388+
echo '<input type="text" name="_quantity" value="' . $quantity . '" class="widefat pf-number" />
389+
<small>Your users only get to pay in quantities if the from amount is not set to zero and recur is set to none.</small>';
390+
391+
}
392+
function kkd_pff_paystack_save_data($post_id, $post) {
368393

369394
if ( !wp_verify_nonce( @$_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
370395
return $post->ID;
@@ -390,6 +415,8 @@ function kkd_pff_paystack_save_data($post_id, $post) {
390415
///
391416
$form_meta['_recur'] = $_POST['_recur'];
392417
$form_meta['_recurplan'] = $_POST['_recurplan'];
418+
$form_meta['_usequantity'] = $_POST['_usequantity'];
419+
$form_meta['_quantity'] = $_POST['_quantity'];
393420

394421
// Add values of $form_meta as custom fields
395422

public/class-paystack-forms-public.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,16 +581,18 @@ function kkd_pff_paystack_form_shortcode($atts) {
581581
if ($id != 0) {
582582
$obj = get_post($id);
583583
if ($obj->post_type == 'paystack_form') {
584-
$amount = get_post_meta($id,'_amount',true);
585-
$thankyou = get_post_meta($id,'_successmsg',true);
584+
$amount = get_post_meta($id,'_amount',true);
585+
$thankyou = get_post_meta($id,'_successmsg',true);
586586
$paybtn = get_post_meta($id,'_paybtn',true);
587587
$loggedin = get_post_meta($id,'_loggedin',true);
588588
$txncharge = get_post_meta($id,'_txncharge',true);
589589
$currency = get_post_meta($id,'_currency',true);
590-
$recur = get_post_meta($id,'_recur',true);
591-
$recurplan = get_post_meta($id,'_recurplan',true);
592-
$showbtn = true;
593-
$planerrorcode = 'Input Correct Recurring Plan Code';
590+
$recur = get_post_meta($id,'_recur',true);
591+
$recurplan = get_post_meta($id,'_recurplan',true);
592+
$usequantity = get_post_meta($id,'_usequantity',true);
593+
$quantity = get_post_meta($id,'_quantity',true);
594+
$showbtn = true;
595+
$planerrorcode = 'Input Correct Recurring Plan Code';
594596
if ($recur == 'plan') {
595597
if ($recurplan == '' || $recurplan == null) {
596598
$showbtn = false;
@@ -664,6 +666,21 @@ function kkd_pff_paystack_form_shortcode($atts) {
664666

665667
echo '</div>
666668
</div>';
669+
if ($usequantity == 'yes') {
670+
echo '<div class="span12 unit">
671+
<label class="label">Quantity</label>
672+
<div class="select">
673+
<input type="hidden" value="'.$amount.'" id="pf-qamount"/>
674+
<select class="form-control" id="pf-quantity" name="pf-quantity" >';
675+
$max = $quantity+1;
676+
for ($i=1; $i < $max; $i++) {
677+
echo ' <option value="'.$i.'">'.$i.'</ption>';
678+
}
679+
echo '</select>
680+
<i></i>
681+
</div>
682+
</div>';
683+
}
667684

668685
if ($recur == 'optional') {
669686
echo '<div class="span12 unit">
@@ -901,8 +918,16 @@ function kkd_pff_paystack_submit_action() {
901918
$txncharge = get_post_meta($_POST["pf-id"],'_txncharge',true);
902919
$amount = (int)str_replace(' ', '', $_POST["pf-amount"]);
903920
$originalamount = $amount;
921+
$quantity = 1;
922+
$usequantity = get_post_meta($_POST["pf-id"],'_usequantity',true);
904923
if(($recur == 'no') && ($formamount != 0)){
905-
$amount = (int)str_replace(' ', '', $formamount);
924+
if ($usequantity == 'no') {
925+
$amount = (int)str_replace(' ', '', $formamount);
926+
}else{
927+
$quantity = $_POST["pf-quantity"];
928+
$unitamount = (int)str_replace(' ', '', $formamount);
929+
$amount = $quantity*$unitamount;
930+
}
906931
}
907932
if ($txncharge == 'customer') {
908933
$percent = (1.55/100)*$amount;
@@ -1065,6 +1090,7 @@ function kkd_pff_paystack_submit_action() {
10651090
'result' => 'success',
10661091
'code' => $insert['txn_code'],
10671092
'plan' => $insert['plan'],
1093+
'quantity' => $quantity,
10681094
'email' => $insert['email'],
10691095
'name' => $fullname,
10701096
'total' => $insert['amount']*100,
@@ -1097,6 +1123,13 @@ function kkd_pff_paystack_meta_as_custom_fields($metadata){
10971123
'display_name' => 'Plan Interval',
10981124
'variable_name' => 'Plan Interval',
10991125
'type' => 'text',
1126+
'value' => $value
1127+
];
1128+
}elseif ($key == 'pf-quantity') {
1129+
$custom_fields[] = [
1130+
'display_name' => 'Plan Quantity',
1131+
'variable_name' => 'Plan Quantity',
1132+
'type' => 'text',
11001133
'value' => $value
11011134
];
11021135
}else{
@@ -1169,6 +1202,16 @@ function kkd_pff_paystack_confirm_payment() {
11691202
$result = "success";
11701203
// kkd_pff_paystack_send_receipt($currency,$amount,$name,$payment_array->email,$code,$metadata)
11711204
}else{
1205+
$usequantity = get_post_meta($payment_array->post_id,'_usequantity',true);
1206+
if ($usequantity == 'no') {
1207+
$amount = (int)str_replace(' ', '', $amount);
1208+
}else{
1209+
$quantity = $_POST["quantity"];
1210+
$unitamount = (int)str_replace(' ', '', $amount);
1211+
$amount = $quantity*$unitamount;
1212+
}
1213+
1214+
11721215
if ($txncharge == 'customer') {
11731216
$percent = (1.55/100)*$amount;
11741217
if ($percent > 2000) {

public/js/paystack-forms-public.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@
7979
}
8080
}
8181
});
82+
$('#pf-quantity').on('change', function() {
83+
var unit = $('#pf-qamount').val();
84+
var quant = this.value;
85+
var newvalue = unit*quant;
86+
$('#pf-amount').val(newvalue);
87+
calculateFees();
88+
89+
// alert( this.value ); // or $(this).val()
90+
});
8291
function validateEmail(email) {
8392
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,}))$/;
8493
return re.test(email);
@@ -133,6 +142,7 @@
133142
var names = data.name.split(' ');
134143
var firstName = names[0] || "";
135144
var lastName = names[1] || "";
145+
var quantity =data.quantity;
136146
// console.log(firstName+ " - "+lastName);
137147
if (data.plan == 'none') {
138148
var handler = PaystackPop.setup({
@@ -145,7 +155,7 @@
145155
metadata: {'custom_fields': data.custom_fields},
146156
callback: function(response){
147157
$.blockUI({ message: 'Please wait...' });
148-
$.post($form.attr('action'), {'action':'kkd_pff_paystack_confirm_payment','code':response.trxref}, function(newdata) {
158+
$.post($form.attr('action'), {'action':'kkd_pff_paystack_confirm_payment','code':response.trxref,'quantity':quantity}, function(newdata) {
149159
data = JSON.parse(newdata);
150160
if (data.result == 'success'){
151161
$('.paystack-form')[0].reset();

0 commit comments

Comments
 (0)