Skip to content

Commit a1e882e

Browse files
committed
Check for existing plan before plan creation
1 parent ff85539 commit a1e882e

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

public/class-paystack-forms-public.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ function kkd_pff_paystack_submit_action() {
900900

901901
$txncharge = get_post_meta($_POST["pf-id"],'_txncharge',true);
902902
$amount = (int)str_replace(' ', '', $_POST["pf-amount"]);
903-
903+
$originalamount = $amount;
904904
if(($recur == 'no') && ($formamount != 0)){
905905
$amount = (int)str_replace(' ', '', $formamount);
906906
}
@@ -960,47 +960,62 @@ function kkd_pff_paystack_submit_action() {
960960
}else{
961961
$key = esc_attr( get_option('lsk') );
962962
}
963+
$koboamount = $amount*100;
963964
//Create Plan
964965
$paystack_url = 'https://api.paystack.co/plan';
966+
$check_url = 'https://api.paystack.co/plan?amount='.$koboamount.'&interval='.$interval;
965967
$headers = array(
966968
'Content-Type' => 'application/json',
967969
'Authorization' => 'Bearer ' . $key
968970
);
969-
$koboamount = $amount*100;
970-
$body = array(
971-
'name' => $fullname.'_'.$code,
972-
'amount' => $koboamount,
973-
'interval' => $interval
974-
);
975971

976-
$args = array(
977-
'body' => json_encode( $body ),
972+
$checkargs = array(
978973
'headers' => $headers,
979974
'timeout' => 60
980975
);
981-
// print_r($args);
982-
983-
984-
$request = wp_remote_post( $paystack_url, $args );
985-
// print_r($request);
976+
// Check if plan exist
977+
$checkrequest = wp_remote_get( $check_url, $checkargs );
978+
if(!is_wp_error($checkrequest)) {
979+
$response = json_decode(wp_remote_retrieve_body($checkrequest));
980+
if ($response->meta->total >= 1) {
981+
$plan = $response->data[0];
982+
$plancode = $plan->plan_code;
983+
$fixedmetadata[] = [
984+
'display_name' => 'Plan Interval',
985+
'variable_name' => 'Plan Interval',
986+
'type' => 'text',
987+
'value' => $plan->interval
988+
];
989+
}else{
990+
//Create Plan
991+
$body = array(
992+
'name' => $currency.$originalamount.' ['.$currency.$amount.'] - '.$interval,
993+
'amount' => $koboamount,
994+
'interval' => $interval
995+
);
996+
$args = array(
997+
'body' => json_encode( $body ),
998+
'headers' => $headers,
999+
'timeout' => 60
1000+
);
1001+
1002+
$request = wp_remote_post( $paystack_url, $args );
1003+
if( ! is_wp_error( $request )) {
1004+
$paystack_response = json_decode(wp_remote_retrieve_body($request));
1005+
$plancode = $paystack_response->data->plan_code;
1006+
$fixedmetadata[] = [
1007+
'display_name' => 'Plan Interval',
1008+
'variable_name' => 'Plan Interval',
1009+
'type' => 'text',
1010+
'value' => $paystack_response->data->interval
1011+
];
9861012

987-
if( ! is_wp_error( $request )) {
988-
// echo "string";
989-
$paystack_response = json_decode(wp_remote_retrieve_body($request));
990-
// print_r($paystack_response);
991-
$plancode = $paystack_response->data->plan_code;
992-
$fixedmetadata[] = [
993-
'display_name' => 'Plan Interval',
994-
'variable_name' => 'Plan Interval',
995-
'type' => 'text',
996-
'value' => $paystack_response->data->interval
997-
];
1013+
}
1014+
}
9981015

9991016
}
1000-
# code...
1001-
}
10021017

1003-
// $plancode = 'optionalcode';
1018+
}
10041019
}else{
10051020
//Use Plan Code
10061021
$plancode = $_POST['pf-plancode'];

public/js/paystack-forms-public.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
if (transaction_amount > 2500) {extrafee = 100};
5656
}
5757
var total = transaction_amount + fees + extrafee;
58-
console.log(transaction_amount);
58+
// console.log(transaction_amount);
5959
if (transaction_amount == '' || transaction_amount == 0 || transaction_amount.length == 0 || transaction_amount == null || isNaN (transaction_amount)) {
6060
var total = 0;
6161
var fees = 0;

0 commit comments

Comments
 (0)