Skip to content

Commit cb0e288

Browse files
committed
Plans work now.
1 parent cb6fafe commit cb0e288

File tree

5 files changed

+116
-85
lines changed

5 files changed

+116
-85
lines changed

includes/class-paystack-forms-activator.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
class Paystack_Forms_Activator {
44

5-
/**
6-
* Short Description. (use period)
7-
*
8-
* Long Description.
9-
*
10-
* @since 1.0.0
11-
*/
125
public static function activate() {
136
global $wpdb;
147
$table_name = $wpdb->prefix . 'paystack_forms_payments';

includes/class-paystack-forms.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Paystack_Forms {
4343
public function __construct() {
4444

4545
$this->plugin_name = 'paystack-forms';
46-
$this->version = '1.0.1';
46+
$this->version = '1.0.4';
4747

4848
$this->load_dependencies();
4949
$this->set_locale();

paystack-forms.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ function insertTextarea(){
107107
QTags.addButton(
108108
"s_shortcode",
109109
"Insert Select Dropdown",
110-
insertSelect
110+
insertSelectb
111111
);
112-
function insertSelect(){
112+
function insertSelectb(){
113113
QTags.insertContent('[select name="Text Title" options="option 1,option 2,option 2"]');
114114
}
115115
QTags.addButton(
116-
"s_shortcode",
116+
"i_shortcode",
117117
"Insert File Upload",
118118
insertInput
119119
);

public/class-paystack-forms-public.php

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,28 @@ function send_receipt($id,$currency,$amount,$name,$email,$code,$metadata){
622622
wp_mail($user_email, $email_subject, $message,$headers);
623623

624624
}
625+
function fetch_plan($code){
626+
$mode = esc_attr( get_option('mode') );
627+
if ($mode == 'test') {
628+
$key = esc_attr( get_option('tsk') );
629+
}else{
630+
$key = esc_attr( get_option('lsk') );
631+
}
632+
$paystack_url = 'https://api.paystack.co/plan/' . $code;
633+
$headers = array(
634+
'Authorization' => 'Bearer ' . $key
635+
);
636+
$args = array(
637+
'headers' => $headers,
638+
'timeout' => 60
639+
);
640+
$request = wp_remote_get( $paystack_url, $args );
641+
if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
642+
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
643+
644+
}
645+
return $paystack_response;
646+
}
625647
function cf_shortcode($atts) {
626648
ob_start();
627649
if ( is_user_logged_in() ) {
@@ -642,7 +664,7 @@ function cf_shortcode($atts) {
642664
$loggedin = get_post_meta($id,'_loggedin',true);
643665
$txncharge = get_post_meta($id,'_txncharge',true);
644666
$currency = get_post_meta($id,'_currency',true);
645-
$recur = get_post_meta($id,'_recur',true);
667+
$recur = get_post_meta($id,'_recur',true);
646668
$recurplan = get_post_meta($id,'_recurplan',true);
647669
// print_r($loggedin);
648670
if ($loggedin == 'no') {
@@ -669,7 +691,7 @@ function cf_shortcode($atts) {
669691
echo '</p>';
670692
if ($recur != 'no') {
671693
if ($recur == 'optional') {
672-
echo '<p>Reccuring Payment<br />';
694+
echo '<p>Recuring Payment<br />';
673695
echo '<select class="form-control" name="pf-interval" style="width:100%;">
674696
<option value="no">None</option>
675697
<option value="hourly">Hourly</option>
@@ -681,9 +703,11 @@ function cf_shortcode($atts) {
681703
echo '</p>';
682704

683705
}else{
706+
$plan= fetch_plan($recurplan);
707+
$planamount = $plan->data->amount/100;
684708
echo '<input type="hidden" name="pf-plancode" value="' . $recurplan. '" />';
685709
// echo '<input type="number" name="pf-amount" class="form-control pf-number" id="pf-amount" required/>';
686-
echo '<p> Weekly Recuring Payment</p>';
710+
echo '<p>'.$plan->data->name.' '.$plan->data->interval. ' recuring payment - '.$plan->data->currency.number_format($planamount).'</p>';
687711
}
688712

689713
}
@@ -898,7 +922,7 @@ function paystack_submit_action() {
898922
if ($recur == 'optional') {
899923
$interval = $_POST['pf-interval'];
900924
if ($interval != 'no') {
901-
unset($metadata['pf-interval']);
925+
unset($metadata['pf-interval']);
902926
$mode = esc_attr( get_option('mode') );
903927
if ($mode == 'test') {
904928
$key = esc_attr( get_option('tsk') );
@@ -911,9 +935,11 @@ function paystack_submit_action() {
911935
'Content-Type' => 'application/json',
912936
'Authorization' => 'Bearer ' . $key
913937
);
938+
$amount = (int)str_replace(' ', '', $_POST["pf-amount"]);
939+
$koboamount = $amount*100;
914940
$body = array(
915941
'name' => $fullname.'_'.$code,
916-
'amount' => $_POST["pf-amount"],
942+
'amount' => $koboamount,
917943
'interval' => $interval
918944
);
919945

@@ -922,14 +948,18 @@ function paystack_submit_action() {
922948
'headers' => $headers,
923949
'timeout' => 60
924950
);
951+
// print_r($args);
952+
925953

926954
$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-
}
955+
// print_r($request);
956+
957+
if( ! is_wp_error( $request )) {
958+
// echo "string";
959+
$paystack_response = json_decode(wp_remote_retrieve_body($request));
960+
// print_r($paystack_response);
961+
$plancode = $paystack_response->data->plan_code;
962+
// echo $plancod;
933963

934964
}
935965
# code...
@@ -952,10 +982,6 @@ function paystack_submit_action() {
952982
'txn_code' => $code,
953983
'metadata' => json_encode($fixedmetadata)
954984
);
955-
// print_r($fixedmetadata);
956-
// print_r($_FILES);
957-
// die();
958-
959985
$exist = $wpdb->get_results("SELECT * FROM $table WHERE (post_id = '".$insert['post_id']."'
960986
AND email = '".$insert['email']."'
961987
AND user_id = '".$insert['pf-user_id']."'
@@ -1014,6 +1040,7 @@ function paystack_meta_as_custom_fields($metadata){
10141040

10151041
add_action( 'wp_ajax_paystack_confirm_payment', 'paystack_confirm_payment' );
10161042
add_action( 'wp_ajax_nopriv_paystack_confirm_payment', 'paystack_confirm_payment' );
1043+
10171044
function paystack_confirm_payment() {
10181045
if (trim($_POST['code']) == '') {
10191046
$response['error'] = true;

public/js/paystack-forms-public.js

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
11
(function( $ ) {
22
'use strict';
33

4-
/**
5-
* All of the code for your public-facing JavaScript source
6-
* should reside in this file.
7-
*
8-
* Note: It has been assumed you will write jQuery code here, so the
9-
* $ function reference has been prepared for usage within the scope
10-
* of this function.
11-
*
12-
* This enables you to define handlers, for when the DOM is ready:
13-
*
14-
* $(function() {
15-
*
16-
* });
17-
*
18-
* When the window is loaded:
19-
*
20-
* $( window ).load(function() {
21-
*
22-
* });
23-
*
24-
* ...and/or other possibilities.
25-
*
26-
* Ideally, it is not considered best practise to attach more than a
27-
* single DOM-ready or window-load handler for a particular page.
28-
* Although scripts in the WordPress core, Plugins and Themes may be
29-
* practising this, we should strive to set a better example in our own work.
30-
*/
4+
315
$(document).ready(function($) {
326

337
$('.pf-number').keydown(function(event) {
@@ -96,38 +70,75 @@
9670
var firstName = names[0] || "";
9771
var lastName = names[1] || "";
9872
// console.log(firstName+ " - "+lastName);
99-
var handler = PaystackPop.setup({
100-
key: settings.key,
101-
email: data.email,
102-
amount: data.total,
103-
firstname: firstName,
104-
lastname: lastName,
105-
ref: data.code,
106-
metadata: {'custom_fields': data.custom_fields},
107-
callback: function(response){
108-
$.blockUI({ message: 'Please wait...' });
109-
$.post($form.attr('action'), {'action':'paystack_confirm_payment','code':response.trxref}, function(newdata) {
110-
data = JSON.parse(newdata);
111-
if (data.result == 'success'){
112-
$('.paystack-form')[0].reset();
113-
$('html,body').animate({ scrollTop: $('.paystack-form').offset().top - 110 }, 500);
114-
115-
self.before('<pre>'+data.message+'</pre>');
116-
$(this).find("input, select, textarea").each(function() {
117-
$(this).css({ "border-color":"#d1d1d1" });
118-
});
119-
120-
$.unblockUI();
121-
}else{
122-
self.before('<pre>'+data.message+'</pre>');
123-
$.unblockUI();
124-
}
125-
});
126-
},
127-
onClose: function(){
128-
129-
}
130-
});
73+
if (data.plan == 'none') {
74+
var handler = PaystackPop.setup({
75+
key: settings.key,
76+
email: data.email,
77+
amount: data.total,
78+
firstname: firstName,
79+
lastname: lastName,
80+
ref: data.code,
81+
metadata: {'custom_fields': data.custom_fields},
82+
callback: function(response){
83+
$.blockUI({ message: 'Please wait...' });
84+
$.post($form.attr('action'), {'action':'paystack_confirm_payment','code':response.trxref}, function(newdata) {
85+
data = JSON.parse(newdata);
86+
if (data.result == 'success'){
87+
$('.paystack-form')[0].reset();
88+
$('html,body').animate({ scrollTop: $('.paystack-form').offset().top - 110 }, 500);
89+
90+
self.before('<pre>'+data.message+'</pre>');
91+
$(this).find("input, select, textarea").each(function() {
92+
$(this).css({ "border-color":"#d1d1d1" });
93+
});
94+
95+
$.unblockUI();
96+
}else{
97+
self.before('<pre>'+data.message+'</pre>');
98+
$.unblockUI();
99+
}
100+
});
101+
},
102+
onClose: function(){
103+
104+
}
105+
});
106+
107+
}else{
108+
var handler = PaystackPop.setup({
109+
key: settings.key,
110+
email: data.email,
111+
plan: data.plan,
112+
firstname: firstName,
113+
lastname: lastName,
114+
ref: data.code,
115+
metadata: {'custom_fields': data.custom_fields},
116+
callback: function(response){
117+
$.blockUI({ message: 'Please wait...' });
118+
$.post($form.attr('action'), {'action':'paystack_confirm_payment','code':response.trxref}, function(newdata) {
119+
data = JSON.parse(newdata);
120+
if (data.result == 'success'){
121+
$('.paystack-form')[0].reset();
122+
$('html,body').animate({ scrollTop: $('.paystack-form').offset().top - 110 }, 500);
123+
124+
self.before('<pre>'+data.message+'</pre>');
125+
$(this).find("input, select, textarea").each(function() {
126+
$(this).css({ "border-color":"#d1d1d1" });
127+
});
128+
129+
$.unblockUI();
130+
}else{
131+
self.before('<pre>'+data.message+'</pre>');
132+
$.unblockUI();
133+
}
134+
});
135+
},
136+
onClose: function(){
137+
138+
}
139+
});
140+
}
141+
131142
handler.openIframe();
132143
}else{
133144
alert(data.message);

0 commit comments

Comments
 (0)