Skip to content

Commit cb6fafe

Browse files
committed
Creation of subscription and use of subscription
1 parent 07f73b8 commit cb6fafe

File tree

3 files changed

+93
-16
lines changed

3 files changed

+93
-16
lines changed

admin/class-paystack-forms-admin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ function wpt_recur_data() {
353353
if ($recurplan == "") {$recurplan = '';}
354354
// Echo out the field
355355
echo '<p>Reccuring Payment:</p>';
356-
echo '<select class="form-control" name="_recur" id="parent_id" style="width:100%;">
356+
echo '<select class="form-control" name="_recur" style="width:100%;">
357357
<option value="no" '.txncheck('no',$recur).'>None</option>
358-
<option value="custom" '.txncheck('yes',$recur).'>User Sets Interval</option>
359-
<option value="plan" '.txncheck('yes',$recur).'>Paystack Plan</option>
358+
<option value="optional" '.txncheck('optional',$recur).'>Optional Recurring</option>
359+
<option value="plan" '.txncheck('plan',$recur).'>Paystack Plan</option>
360360
</select>';
361-
echo '<p>Paystack Recur Plan:</p>';
361+
echo '<p>Paystack Recur Plan code:</p>';
362362
echo '<input type="text" name="_recurplan" value="' . $recurplan . '" class="widefat" />
363363
<small>Plan amount must match amount on extra form description.</small>';
364364

includes/class-paystack-forms-activator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public static function activate() {
1717
id int(11) NOT NULL AUTO_INCREMENT,
1818
post_id int(11) NOT NULL,
1919
user_id int(11) NOT NULL,
20-
email varchar(255) DEFAULT '' NOT NULL,
20+
email varchar(255) DEFAULT '' NOT NULL,
21+
plan varchar(255) DEFAULT '' NOT NULL,
2122
metadata text,
2223
paid int(1) NOT NULL DEFAULT '0',
2324
txn_code varchar(255) DEFAULT '' NOT NULL,

public/class-paystack-forms-public.php

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -642,18 +642,21 @@ function cf_shortcode($atts) {
642642
$loggedin = get_post_meta($id,'_loggedin',true);
643643
$txncharge = get_post_meta($id,'_txncharge',true);
644644
$currency = get_post_meta($id,'_currency',true);
645+
$recur = get_post_meta($id,'_recur',true);
646+
$recurplan = get_post_meta($id,'_recurplan',true);
645647
// print_r($loggedin);
646648
if ($loggedin == 'no') {
647649
echo "<h1 id='pf-form".$id."'>".$obj->post_title."</h1>";
648650
echo '<form enctype="multipart/form-data" class="paystack-form" action="' . admin_url('admin-ajax.php') . '" url="' . admin_url() . '" method="post">';
649651
echo '<input type="hidden" name="action" value="paystack_submit_action">';
650652
echo '<input type="hidden" name="pf-id" value="' . $id . '" />';
651653
echo '<input type="hidden" name="pf-user_id" value="' . $user_id. '" />';
654+
echo '<input type="hidden" name="pf-recur" value="' . $recur. '" />';
652655
echo '<p>';
653-
echo 'Full Name (required)<br />';
656+
echo 'Full Name (required)<br />';
654657
echo '<input type="text" name="pf-fname" class="form-control" required/>';
655658
echo '</p>';
656-
echo 'Email (required)<br />';
659+
echo 'Email (required)<br />';
657660
echo '<input type="email" name="pf-pemail" class="form-control" id="pf-email" required/>';
658661
echo '</p>';
659662
echo '<p>';
@@ -664,6 +667,26 @@ function cf_shortcode($atts) {
664667
echo '<input type="number" name="pf-amount" value="'.$amount.'" id="pf-amount" readonly required/>';
665668
}
666669
echo '</p>';
670+
if ($recur != 'no') {
671+
if ($recur == 'optional') {
672+
echo '<p>Reccuring Payment<br />';
673+
echo '<select class="form-control" name="pf-interval" style="width:100%;">
674+
<option value="no">None</option>
675+
<option value="hourly">Hourly</option>
676+
<option value="daily">Daily</option>
677+
<option value="weekly">Weekly</option>
678+
<option value="monthly">Monthly</option>
679+
<option value="annually">Annually</option>
680+
</select>';
681+
echo '</p>';
682+
683+
}else{
684+
echo '<input type="hidden" name="pf-plancode" value="' . $recurplan. '" />';
685+
// echo '<input type="number" name="pf-amount" class="form-control pf-number" id="pf-amount" required/>';
686+
echo '<p> Weekly Recuring Payment</p>';
687+
}
688+
689+
}
667690
echo(do_shortcode($obj->post_content));
668691

669692
// echo '<br /><p>Transaction charge:'.$currency.'<b class="txn_charge">13,000</b></p>';
@@ -825,7 +848,9 @@ function paystack_submit_action() {
825848
$table = $wpdb->prefix."paystack_forms_payments";
826849
$metadata = $_POST;
827850
$fullname = $_POST['pf-fname'];
851+
$recur = $_POST['pf-recur'];
828852
unset($metadata['action']);
853+
unset($metadata['pf-recur']);
829854
unset($metadata['pf-id']);
830855
unset($metadata['pf-pemail']);
831856
unset($metadata['pf-amount']);
@@ -868,16 +893,65 @@ function paystack_submit_action() {
868893

869894
}
870895
}
896+
$plancode = 'none';
897+
if($recur != 'no'){
898+
if ($recur == 'optional') {
899+
$interval = $_POST['pf-interval'];
900+
if ($interval != 'no') {
901+
unset($metadata['pf-interval']);
902+
$mode = esc_attr( get_option('mode') );
903+
if ($mode == 'test') {
904+
$key = esc_attr( get_option('tsk') );
905+
}else{
906+
$key = esc_attr( get_option('lsk') );
907+
}
908+
//Create Plan
909+
$paystack_url = 'https://api.paystack.co/plan';
910+
$headers = array(
911+
'Content-Type' => 'application/json',
912+
'Authorization' => 'Bearer ' . $key
913+
);
914+
$body = array(
915+
'name' => $fullname.'_'.$code,
916+
'amount' => $_POST["pf-amount"],
917+
'interval' => $interval
918+
);
919+
920+
$args = array(
921+
'body' => json_encode( $body ),
922+
'headers' => $headers,
923+
'timeout' => 60
924+
);
925+
926+
$request = wp_remote_post( $paystack_url, $args );
927+
if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
928+
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
929+
if ( 'Plan created' == $paystack_response->message ) {
930+
$plancode = $paystack_response->data->plan_code;
931+
// $paystack_ref = $paystack_response->data->reference;
932+
}
933+
934+
}
935+
# code...
936+
}
871937

938+
// $plancode = 'optionalcode';
939+
}else{
940+
//Use Plan Code
941+
$plancode = $_POST['pf-plancode'];
942+
unset($metadata['pf-plancode']);
943+
}
944+
}
872945
$insert = array(
873-
'post_id' => strip_tags($_POST["pf-id"], ""),
874-
'email' => strip_tags($_POST["pf-pemail"], ""),
875-
'user_id' => strip_tags($_POST["pf-user_id"], ""),
876-
'amount' => strip_tags($_POST["pf-amount"], ""),
877-
'ip' => get_the_user_ip(),
878-
'txn_code' => $code,
879-
'metadata' => json_encode($fixedmetadata)
880-
);
946+
'post_id' => strip_tags($_POST["pf-id"], ""),
947+
'email' => strip_tags($_POST["pf-pemail"], ""),
948+
'user_id' => strip_tags($_POST["pf-user_id"], ""),
949+
'amount' => strip_tags($_POST["pf-amount"], ""),
950+
'plan' => strip_tags($plancode, ""),
951+
'ip' => get_the_user_ip(),
952+
'txn_code' => $code,
953+
'metadata' => json_encode($fixedmetadata)
954+
);
881955
// print_r($fixedmetadata);
882956
// print_r($_FILES);
883957
// die();
@@ -886,6 +960,7 @@ function paystack_submit_action() {
886960
AND email = '".$insert['email']."'
887961
AND user_id = '".$insert['pf-user_id']."'
888962
AND amount = '".$insert['amount']."'
963+
AND plan = '".$insert['plan']."'
889964
AND ip = '".$insert['ip']."'
890965
AND paid = '0'
891966
AND metadata = '". $insert['metadata'] ."')");
@@ -902,7 +977,8 @@ function paystack_submit_action() {
902977

903978
$response = array(
904979
'result' => 'success',
905-
'code' => $insert['txn_code'],
980+
'code' => $insert['txn_code'],
981+
'plan' => $insert['plan'],
906982
'email' => $insert['email'],
907983
'name' => $fullname,
908984
'total' => $insert['amount']*100,

0 commit comments

Comments
 (0)