diff --git a/admin/class-paystack-forms-cpt.php b/admin/class-paystack-forms-cpt.php new file mode 100644 index 0000000..69d1a46 --- /dev/null +++ b/admin/class-paystack-forms-cpt.php @@ -0,0 +1,333 @@ +plugin_name = $plugin_name; + $this->version = $version; + } + + public function register_paystack_form_cpt() { + + $labels = array( + 'name' => _x( 'Paystack Forms', 'paystack_form', 'payment-forms-for-paystack' ), + 'singular_name' => _x( 'Paystack Form', 'paystack_form', 'payment-forms-for-paystack' ), + 'add_new' => _x( 'Add New', 'paystack_form', 'payment-forms-for-paystack' ), + 'add_new_item' => _x( 'Add Paystack Form', 'paystack_form', 'payment-forms-for-paystack' ), + 'edit_item' => _x( 'Edit Paystack Form', 'paystack_form', 'payment-forms-for-paystack' ), + 'new_item' => _x( 'Paystack Form', 'paystack_form', 'payment-forms-for-paystack' ), + 'view_item' => _x( 'View Paystack Form', 'paystack_form', 'payment-forms-for-paystack' ), + 'all_items' => _x( 'All Forms', 'paystack_form', 'payment-forms-for-paystack' ), + 'search_items' => _x( 'Search Paystack Forms', 'paystack_form', 'payment-forms-for-paystack' ), + 'not_found' => _x( 'No Paystack Forms found', 'paystack_form', 'payment-forms-for-paystack' ), + 'not_found_in_trash' => _x( 'No Paystack Forms found in Trash', 'paystack_form', 'payment-forms-for-paystack' ), + 'parent_item_colon' => _x( 'Parent Paystack Form:', 'paystack_form', 'payment-forms-for-paystack' ), + 'menu_name' => _x( 'Paystack Forms', 'paystack_form', 'payment-forms-for-paystack' ), + ); + + $args = array( + 'labels' => $labels, + 'hierarchical' => true, + 'description' => __( 'Paystack Forms filterable by genre', 'payment-forms-for-paystack' ), + 'supports' => array( 'title', 'editor' ), + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_position' => 5, + 'menu_icon' => plugins_url( '../images/logo.png', __FILE__ ), + 'show_in_nav_menus' => false, + 'publicly_queryable' => false, + 'exclude_from_search' => true, + 'has_archive' => false, + 'query_var' => true, + 'can_export' => true, + 'rewrite' => false, + 'comments' => false, + 'capability_type' => 'post', + ); + + register_post_type( 'paystack_form', $args ); + } + + public function save_payment_form_data( $post_id ) { + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return; + } + + if ( empty( $_POST['payment_forms_for_paystack_nonce'] ) ) { + return; + } + + if ( ! wp_verify_nonce( $_POST['payment_forms_for_paystack_nonce'], 'payment-forms-for-paystack-nonce' ) ) { + return; + } + + $post = get_post( $post_id ); + + if ( empty( $post ) ) { + return; + } + + // Is the user allowed to edit the post or page? + if ( ! current_user_can( 'edit_post', $post->ID ) ) { + return; + } + + $form_meta['_inventory'] = $_POST['_inventory'] ?? '0'; + $form_meta['_useinventory'] = $_POST['_inventory'] ?? '0'; + $form_meta['_amount'] = $_POST['_amount']; + $form_meta['_hidetitle'] = $_POST['_hidetitle'] ?? '0'; + $form_meta['_minimum'] = $_POST['_minimum'] ?? '0'; + + $form_meta['_variableamount'] = $_POST['_variableamount']; + $form_meta['_usevariableamount'] = $_POST['_usevariableamount'] ?? '0'; + + $form_meta['_paybtn'] = $_POST['_paybtn']; + $form_meta['_currency'] = $_POST['_currency']; + $form_meta['_successmsg'] = $_POST['_successmsg']; + $form_meta['_txncharge'] = $_POST['_txncharge']; + $form_meta['_loggedin'] = $_POST['_loggedin']; + $form_meta['_filelimit'] = $_POST['_filelimit']; + $form_meta['_redirect'] = $_POST['_redirect']; + + $form_meta['_subject'] = $_POST['_subject']; + $form_meta['_merchant'] = $_POST['_merchant']; + $form_meta['_heading'] = $_POST['_heading']; + $form_meta['_message'] = $_POST['_message']; + $form_meta['_sendreceipt'] = $_POST['_sendreceipt']; + $form_meta['_sendinvoice'] = $_POST['_sendinvoice']; + + $form_meta['_recur'] = $_POST['_recur']; + $form_meta['_recurplan'] = $_POST['_recurplan']; + $form_meta['_usequantity'] = $_POST['_usequantity']; + $form_meta['_quantity'] = $_POST['_quantity'] ?? '1'; + $form_meta['_sold'] = $_POST['_sold'] ?? '0'; + $form_meta['_quantityunit'] = $_POST['_quantityunit'] ?? __( 'Quantity', 'payment-forms-for-paystack' ); + + $form_meta['_useagreement'] = $_POST['_useagreement']; + $form_meta['_agreementlink'] = $_POST['_agreementlink']; + $form_meta['_subaccount'] = $_POST['_subaccount']; + $form_meta['_txnbearer'] = $_POST['_txnbearer']; + $form_meta['_merchantamount'] = $_POST['_merchantamount']; + // Add values of $form_meta as custom fields + + //Custom Plan with Start Date + $form_meta['_startdate_days'] = $_POST['_startdate_days']; + $form_meta['_startdate_plan_code'] = $_POST['_startdate_plan_code']; + $form_meta['_startdate_enabled'] = $_POST['_startdate_enabled'] ?? '0'; + + foreach ( $form_meta as $key => $value ) { // Cycle through the $form_meta array! + if ( 'revision' === $post->post_type ) { + return; // Don't store custom data twice + } + $value = implode( ',', (array) $value ); // If $value is an array, make it a CSV (unlikely) + if ( get_post_meta( $post->ID, $key, false ) ) { // If the custom field already has a value + update_post_meta( $post->ID, $key, $value ); + } else { // If the custom field doesn't have a value + add_post_meta( $post->ID, $key, $value ); + } + if ( ! $value ) { + delete_post_meta( $post->ID, $key ); // Delete if blank + } + } + } + + public function disable_wyswyg( $default ) { + + global $post_type; + + if ( 'paystack_form' === $post_type ) { + echo ''; + add_filter( 'user_can_richedit', '__return_false', 50 ); + remove_action( 'media_buttons', 'media_buttons' ); + add_filter( 'quicktags_settings', array( $this, 'remove_fullscreen_quicktag' ) ); + } + + return $default; + } + + public function remove_fullscreen_quicktag( $qt_init ) { + $qt_init['buttons'] = 'fullscreen'; + return $qt_init; + } + + public function add_help_section_meta_box( $post ) { + do_meta_boxes( null, 'paystack_payment_forms_cpt_meta_box', $post ); + } + + public function help_section_meta_box() { + ?> + +
+ required="required" to the shortcode', 'payment-forms-for-paystack' ), + array( + 'code' => array(), + ) + ) + ?> +
++ [text name="Full Name" required="required" ]', 'payment-forms-for-paystack' ), + array( + 'code' => array(), + ) + ) + ?> +
++ Warning: Using the file input field may cause data overload on your server. Be sure you have enough server space before using it. You also have the ability to set file upload limits', 'payment-forms-for-paystack' ), + array( + 'strong' => array( + 'style' => array(), + ), + ) + ) + ?> +
++ + + + +
+ + version, + true, + ); + } +} diff --git a/admin/js/quicktags.js b/admin/js/quicktags.js new file mode 100644 index 0000000..1d5f7e2 --- /dev/null +++ b/admin/js/quicktags.js @@ -0,0 +1,93 @@ +//this function is used to retrieve the selected text from the text editor +function getSel() { + var txtarea = document.getElementById( "content" ); + var start = txtarea.selectionStart; + var finish = txtarea.selectionEnd; + return txtarea.value.substring( start, finish ); +} + +QTags.addButton( + "t_shortcode", + "Insert Text", + insertText +); + +function insertText() { + QTags.insertContent( '[text name="Text Title"]' ); +} +QTags.addButton( + "ta_shortcode", + "Insert Textarea", + insertTextarea +); + +function insertTextarea() { + QTags.insertContent( '[textarea name="Text Title"]' ); +} +QTags.addButton( + "s_shortcode", + "Insert Select Dropdown", + insertSelectb +); + +function insertSelectb() { + QTags.insertContent( '[select name="Text Title" options="option 1,option 2,option 3"]' ); +} +QTags.addButton( + "r_shortcode", + "Insert Radio Options", + insertRadiob +); + +function insertRadiob() { + QTags.insertContent( '[radio name="Text Title" options="option 1,option 2,option 3"]' ); +} +QTags.addButton( + "cb_shortcode", + "Insert Checkbox Options", + insertCheckboxb +); + +function insertCheckboxb() { + QTags.insertContent( '[checkbox name="Text Title" options="option 1,option 2,option 3"]' ); +} +QTags.addButton( + "dp_shortcode", + "Insert Datepicker", + insertDatepickerb +); + +function insertDatepickerb() { + QTags.insertContent( '[datepicker name="Datepicker Title"]' ); +} +QTags.addButton( + "i_shortcode", + "Insert File Upload", + insertInput +); + +function insertInput() { + QTags.insertContent( '[input name="File Name"]' ); +} +QTags.addButton( + "ngs_shortcode", + "Insert Nigerian States", + insertSelectStates +); + +function insertSelectStates() { + QTags.insertContent( + '[select name="State" options="Abia,Adamawa,Akwa Ibom,Anambra,Bauchi,Bayelsa,Benue,Borno,Cross River,Delta,Ebonyi,Edo,Ekiti,Enugu,FCT,Gombe,Imo,Jigawa,Kaduna,Kano,Katsina,Kebbi,Kogi,Kwara,Lagos,Nasarawa,Niger,Ogun,Ondo,Osun,Oyo,Plateau,Rivers,Sokoto,Taraba,Yobe,Zamfara"]' + ); +} +QTags.addButton( + "ctys_shortcode", + "Insert All Countries", + insertSelectCountries +); + +function insertSelectCountries() { + QTags.insertContent( + '[select name="country" options="Afghanistan, Albania, Algeria, American Samoa, Andorra, Angola, Anguilla, Antarctica, Antigua and Barbuda, Argentina, Armenia, Aruba, Australia, Austria, Azerbaijan, Bahamas, Bahrain, Bangladesh, Barbados, Belarus, Belgium, Belize, Benin, Bermuda, Bhutan, Bolivia, Bosnia and Herzegovina, Botswana, Bouvet Island, Brazil, British Indian Ocean Territory, Brunei Darussalam, Bulgaria, Burkina Faso, Burundi, Cambodia, Cameroon, Canada, Cape Verde, Cayman Islands, Central African Republic, Chad, Chile, China, Christmas Island, Cocos (Keeling) Islands, Colombia, Comoros, Congo, Congo, The Democratic Republic of The, Cook Islands, Costa Rica, Cote D’ivoire, Croatia, Cuba, Cyprus, Czech Republic, Denmark, Djibouti, Dominica, Dominican Republic, Ecuador, Egypt, El Salvador, Equatorial Guinea, Eritrea, Estonia, Ethiopia, Falkland Islands (Malvinas), Faroe Islands, Fiji, Finland, France, French Guiana, French Polynesia, French Southern Territories, Gabon, Gambia, Georgia, Germany, Ghana, Gibraltar, Greece, Greenland, Grenada, Guadeloupe, Guam, Guatemala, Guinea, Guinea-bissau, Guyana, Haiti, Heard Island and Mcdonald Islands, Holy See (Vatican City State), Honduras, Hong Kong, Hungary, Iceland, India, Indonesia, Iran, Islamic Republic of, Iraq, Ireland, Israel, Italy, Jamaica, Japan, Jordan, Kazakhstan, Kenya, Kiribati, Korea, Democratic People’s Republic of, Korea, Republic of, Kuwait, Kyrgyzstan, Lao People’s Democratic Republic, Latvia, Lebanon, Lesotho, Liberia, Libyan Arab Jamahiriya, Liechtenstein, Lithuania, Luxembourg, Macao, Macedonia, The Former Yugoslav Republic of, Madagascar, Malawi, Malaysia, Maldives, Mali, Malta, Marshall Islands, Martinique, Mauritania, Mauritius, Mayotte, Mexico, Micronesia, Federated States of, Moldova, Republic of, Monaco, Mongolia, Montserrat, Morocco, Mozambique, Myanmar, Namibia, Nauru, Nepal, Netherlands, Netherlands Antilles, New Caledonia, New Zealand, Nicaragua, Niger, Nigeria, Niue, Norfolk Island, Northern Mariana Islands, Norway, Oman, Pakistan, Palau, Palestinian Territory, Occupied, Panama, Papua New Guinea, Paraguay, Peru, Philippines, Pitcairn, Poland, Portugal, Puerto Rico, Qatar, Reunion, Romania, Russian Federation, Rwanda, Saint Helena, Saint Kitts and Nevis, Saint Lucia, Saint Pierre and Miquelon, Saint Vincent and The Grenadines, Samoa, San Marino, Sao Tome and Principe, Saudi Arabia, Senegal, Serbia and Montenegro, Seychelles, Sierra Leone, Singapore, Slovakia, Slovenia, Solomon Islands, Somalia, South Africa, South Georgia and The South Sandwich Islands, Spain, Sri Lanka, Sudan, Suriname, Svalbard and Jan Mayen, Swaziland, Sweden, Switzerland, Syrian Arab Republic, Taiwan, Province of China, Tajikistan, Tanzania, United Republic of, Thailand, Timor-leste, Togo, Tokelau, Tonga, Trinidad and Tobago, Tunisia, Turkey, Turkmenistan, Turks and Caicos Islands, Tuvalu, Uganda, Ukraine, United Arab Emirates, United Kingdom, United States, United States Minor Outlying Islands, Uruguay, Uzbekistan, Vanuatu, Venezuela, Viet Nam, Virgin Islands, British, Virgin Islands, U.S., Wallis and Futuna, Western Sahara, Yemen, Zambia, Zimbabwe"] ' + ); +} \ No newline at end of file diff --git a/admin/partials/metaboxes/agreement-checkbox.php b/admin/partials/metaboxes/agreement-checkbox.php new file mode 100644 index 0000000..7555744 --- /dev/null +++ b/admin/partials/metaboxes/agreement-checkbox.php @@ -0,0 +1,35 @@ +ID, '_useagreement', true ); +$agreementlink = get_post_meta( $post->ID, '_agreementlink', true ); + +if ( empty( $useagreement ) ) { + $useagreement = 'no'; +} +if ( empty( $agreementlink ) ) { + $agreementlink = ''; +} +?> + ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
++ +
+ ++ + here for more information.', 'payment-forms-for-paystack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ) + ?> + +
++ +
+ + + ++ +
+ ++ +
+ ++ +
+ ++ + Configure', 'payment-forms-for-paystack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ), + esc_url( get_admin_url() . 'edit.php?post_type=paystack_form&page=class-paystack-forms-admin.php#paystack_setting_fees' ) + ); + ?> + +
++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
++ +
+ + + + + ++
+ +
+ ++
++ +
+ ++ + Quantity', 'payment-forms-for-paystack' ), + array( + 'code' => array(), + ) + ) + ?> + +
++ +
+ ++ +
+ ++ +
+ ++ +
+ ++ +
+ ++
++ +
+ ++ +
+ ++ +
+ ++ here.', 'payment-forms-for-paystack' ), + array( + 'a' => array( + 'href' => array(), + 'target' => array(), + ), + ) + ) + ?> +
+ + +|
-
-
-
-
-
|
-
|
-
-
-
-
-
|
-
|
-
-
-
-
-
-
-
|
-
|
-
-
-
-
-
|
-
|
-
-
-
-
-
-
-
|
-
| - - | -
|
-
-
-
-
-
-
-
-
-
|
-
|
-
-
-
-
-
-
-
-
|
-
| - - - | -
|
-
-
-
-
-
-
-
|
-
| - - | -
|
-
-
-
-
-
-
-
-
-
|
-
|
-
-
-
-
-
-
-
-
|
-
| - - - | -
|
-
-
-
-
-
-
-
|
-
"; - // print_r($paymentoptions); - // echo ""; - // die(); - } - $showbtn = true; - $planerrorcode = 'Input Correct Recurring Plan Code'; - if ($recur == 'plan') { - if ($recurplan == '' || $recurplan == null) { - $showbtn = false; - } else { - $plan = kkd_pff_paystack_fetch_plan($recurplan); - if (isset($plan->data->amount)) { - $planamount = $plan->data->amount/100; - } else { - $showbtn = false; - } - } - } - if ((($user_id != 0) && ($loggedin == 'yes')) || $loggedin == 'no') { - if ($hidetitle != 1) { - echo "
';
- // print_r($_POST);
-
- $fixedmetadata = kkd_pff_paystack_meta_as_custom_fields($metadata);
- // print_r($fixedmetadata );
- $filelimit = get_post_meta($_POST["pf-id"], '_filelimit', true);
- $currency = get_post_meta($_POST["pf-id"], '_currency', true);
- $formamount = get_post_meta($_POST["pf-id"], '_amount', true);/// From form
- $recur = get_post_meta($_POST["pf-id"], '_recur', true);
- $subaccount = get_post_meta($_POST["pf-id"], '_subaccount', true);
- $txnbearer = get_post_meta($_POST["pf-id"], '_txnbearer', true);
- $transaction_charge = get_post_meta($_POST["pf-id"], '_merchantamount', true);
- $transaction_charge = $transaction_charge*100;
-
- $txncharge = get_post_meta($_POST["pf-id"], '_txncharge', true);
- $minimum = get_post_meta($_POST["pf-id"], '_minimum', true);
- $variableamount = get_post_meta($_POST["pf-id"], '_variableamount', true);
- $usevariableamount = get_post_meta($_POST["pf-id"], '_usevariableamount', true);
- $amount = (int)str_replace(' ', '', $_POST["pf-amount"]);
- $variablename = $_POST["pf-vname"];
- // pf-vname
- $originalamount = $amount;
- $quantity = 1;
- $usequantity = get_post_meta($_POST["pf-id"], '_usequantity', true);
-
- if (($recur == 'no') && ($formamount != 0)) {
- $amount = (int)str_replace(' ', '', $formamount);
- }
- if ($minimum == 1 && $formamount != 0) {
- if ($originalamount < $formamount) {
- $amount = $formamount;
- } else {
- $amount = $originalamount;
- }
- }
- if ($usevariableamount == 1) {
- $paymentoptions = explode(',', $variableamount);
- if (count($paymentoptions) > 0) {
- foreach ($paymentoptions as $key => $paymentoption) {
- list($a, $b) = explode(':', $paymentoption);
- if ($variablename == $a) {
- $amount = $b;
- }
- }
- }
- }
- $fixedmetadata[] = array(
- 'display_name' => 'Unit Price',
- 'variable_name' => 'Unit_Price',
- 'type' => 'text',
- 'value' => $currency.number_format($amount)
- );
- if ($usequantity != 'no') {
- $quantity = $_POST["pf-quantity"];
- $unitamount = (int)str_replace(' ', '', $amount);
- $amount = $quantity*$unitamount;
- }
-
-
-
- if ($txncharge == 'customer') {
- $amount = kkd_pff_paystack_add_paystack_charge($amount);
- }
- $maxFileSize = $filelimit * 1024 * 1024;
-
- if (!empty($_FILES)) {
- foreach ($_FILES as $keyname => $value) {
- if ($value['size'] > 0) {
- if ($value['size'] > $maxFileSize) {
- $response['result'] = 'failed';
- $response['message'] = 'Max upload size is '.$filelimit."MB";
- exit(json_encode($response));
- } else {
- $attachment_id = media_handle_upload($keyname, $_POST["pf-id"]);
- $url = wp_get_attachment_url($attachment_id);
- $fixedmetadata[] = array(
- 'display_name' => ucwords(str_replace("_", " ", $keyname)),
- 'variable_name' => $keyname,
- 'type' => 'link',
- 'value' => $url
- );
- }
- } else {
- $fixedmetadata[] = array(
- 'display_name' => ucwords(str_replace("_", " ", $keyname)),
- 'variable_name' => $keyname,
- 'type' => 'text',
- 'value' => 'No file Uploaded'
- );
- }
- }
- }
- $plancode = 'none';
- if ($recur != 'no') {
- if ($recur == 'optional') {
- $interval = $_POST['pf-interval'];
- if ($interval != 'no') {
- unset($metadata['pf-interval']);
- $mode = esc_attr(get_option('mode'));
- if ($mode == 'test') {
- $key = esc_attr(get_option('tsk'));
- } else {
- $key = esc_attr(get_option('lsk'));
- }
- $koboamount = $amount*100;
- //Create Plan
- $paystack_url = 'https://api.paystack.co/plan';
- $check_url = 'https://api.paystack.co/plan?amount='.$koboamount.'&interval='.$interval;
- $headers = array(
- 'Content-Type' => 'application/json',
- 'Authorization' => 'Bearer ' . $key
- );
-
- $checkargs = array(
- 'headers' => $headers,
- 'timeout' => 60
- );
- // Check if plan exist
- $checkrequest = wp_remote_get($check_url, $checkargs);
- if (!is_wp_error($checkrequest)) {
- $response = json_decode(wp_remote_retrieve_body($checkrequest));
- if ($response->meta->total >= 1) {
- $plan = $response->data[0];
- $plancode = $plan->plan_code;
- $fixedmetadata[] = array(
- 'display_name' => 'Plan Interval',
- 'variable_name' => 'Plan Interval',
- 'type' => 'text',
- 'value' => $plan->interval
- );
- } else {
- //Create Plan
- $body = array(
- 'name' => $currency.number_format($originalamount).' ['.$currency.number_format($amount).'] - '.$interval,
- 'amount' => $koboamount,
- 'interval' => $interval
- );
- $args = array(
- 'body' => json_encode($body),
- 'headers' => $headers,
- 'timeout' => 60
- );
-
- $request = wp_remote_post($paystack_url, $args);
- if (! is_wp_error($request)) {
- $paystack_response = json_decode(wp_remote_retrieve_body($request));
- $plancode = $paystack_response->data->plan_code;
- $fixedmetadata[] = array(
- 'display_name' => 'Plan Interval',
- 'variable_name' => 'Plan Interval',
- 'type' => 'text',
- 'value' => $paystack_response->data->interval
- );
- }
- }
- }
- }
- } else {
- //Use Plan Code
- $plancode = $_POST['pf-plancode'];
- unset($metadata['pf-plancode']);
- }
- }
-
- if ($plancode != 'none') {
- $fixedmetadata[] = array(
- 'display_name' => 'Plan',
- 'variable_name' => 'Plan',
- 'type' => 'text',
- 'value' => $plancode
- );
- }
-
- $insert = array(
- 'post_id' => strip_tags($_POST["pf-id"], ""),
- 'email' => strip_tags($_POST["pf-pemail"], ""),
- 'user_id' => strip_tags($_POST["pf-user_id"], ""),
- 'amount' => strip_tags($amount, ""),
- 'plan' => strip_tags($plancode, ""),
- 'ip' => kkd_pff_paystack_get_the_user_ip(),
- 'txn_code' => $code,
- 'metadata' => json_encode($fixedmetadata)
- );
- $exist = $wpdb->get_results(
- "SELECT * FROM $table WHERE (post_id = '".$insert['post_id']."'
- AND email = '".$insert['email']."'
- AND user_id = '".$insert['user_id']."'
- AND amount = '".$insert['amount']."'
- AND plan = '".$insert['plan']."'
- AND ip = '".$insert['ip']."'
- AND paid = '0'
- AND metadata = '". $insert['metadata'] ."')"
- );
- if (count($exist) > 0) {
- // $insert['txn_code'] = $code;
- // $insert['plan'] = $exist[0]->plan;
- $wpdb->update($table, array( 'txn_code' => $code,'plan' =>$insert['plan']), array('id'=>$exist[0]->id));
- } else {
- $wpdb->insert(
- $table,
- $insert
- );
- // if("yes" == get_post_meta($insert['post_id'],'_sendinvoice',true)){
- kkd_pff_paystack_send_invoice($currency, $insert['amount'], $fullname, $insert['email'], $code);
- // }
- }
- if ($subaccount == "" || !isset($subaccount)) {
- $subaccount = null;
- $txnbearer = null;
- $transaction_charge = null;
- }
- if ($transaction_charge == "" || $transaction_charge == 0 || $transaction_charge == null) {
- $transaction_charge = null;
- }
-
- $amount = floatval($insert['amount'])*100;
- $response = array(
- 'result' => 'success',
- 'code' => $insert['txn_code'],
- 'plan' => $insert['plan'],
- 'quantity' => $quantity,
- 'email' => $insert['email'],
- 'name' => $fullname,
- 'total' => round($amount),
- 'custom_fields' => $fixedmetadata,
- 'currency' => $currency,
- 'subaccount' => $subaccount,
- 'txnbearer' => $txnbearer,
- 'transaction_charge' => $transaction_charge
- );
- // print_r($response);
- echo json_encode($response, JSON_NUMERIC_CHECK);
-
- die();
-}
-
-function kkd_pff_paystack_meta_as_custom_fields($metadata)
-{
- $custom_fields = array();
- foreach ($metadata as $key => $value) {
- if (is_array($value)) {
- $value = implode(', ', $value);
- }
- if ($key == 'pf-fname') {
- $custom_fields[] = array(
- 'display_name' => 'Full Name',
- 'variable_name' => 'Full_Name',
- 'type' => 'text',
- 'value' => $value
- );
- } elseif ($key == 'pf-plancode') {
- $custom_fields[] = array(
- 'display_name' => 'Plan',
- 'variable_name' => 'Plan',
- 'type' => 'text',
- 'value' => $value
- );
- } elseif ($key == 'pf-vname') {
- $custom_fields[] = array(
- 'display_name' => 'Payment Option',
- 'variable_name' => 'Payment Option',
- 'type' => 'text',
- 'value' => $value
- );
- } elseif ($key == 'pf-interval') {
- $custom_fields[] = array(
- 'display_name' => 'Plan Interval',
- 'variable_name' => 'Plan Interval',
- 'type' => 'text',
- 'value' => $value
- );
- } elseif ($key == 'pf-quantity') {
- $custom_fields[] = array(
- 'display_name' => 'Quantity',
- 'variable_name' => 'Quantity',
- 'type' => 'text',
- 'value' => $value
- );
- } else {
- $custom_fields[] = array(
- 'display_name' => ucwords(str_replace("_", " ", $key)),
- 'variable_name' => $key,
- 'type' => 'text',
- 'value' => $value
- );
- }
- }
- return $custom_fields;
-}
-
-add_action('wp_ajax_kkd_pff_paystack_confirm_payment', 'kkd_pff_paystack_confirm_payment');
-add_action('wp_ajax_nopriv_kkd_pff_paystack_confirm_payment', 'kkd_pff_paystack_confirm_payment');
-
-function kkd_pff_paystack_confirm_payment()
-{
- if (trim($_POST['code']) == '') {
- $response['error'] = true;
- $response['error_message'] = "Did you make a payment?";
-
- exit(json_encode($response));
- }
- global $wpdb;
- $table = $wpdb->prefix.KKD_PFF_PAYSTACK_TABLE;
- $code = $_POST['code'];
- $record = $wpdb->get_results("SELECT * FROM $table WHERE (txn_code = '".$code."')");
- if (array_key_exists("0", $record)) {
- $payment_array = $record[0];
- $amount = get_post_meta($payment_array->post_id, '_amount', true);
- $recur = get_post_meta($payment_array->post_id, '_recur', true);
- $currency = get_post_meta($payment_array->post_id, '_currency', true);
- $txncharge = get_post_meta($payment_array->post_id, '_txncharge', true);
- $redirect = get_post_meta($payment_array->post_id, '_redirect', true);
- $minimum = get_post_meta($payment_array->post_id, '_minimum', true);
- $usevariableamount = get_post_meta($payment_array->post_id, '_usevariableamount', true);
- $variableamount = get_post_meta($payment_array->post_id, '_variableamount', true);
-
- if ($minimum == 1 && $amount != 0) {
- if ($payment_array->amount < $formamount) {
- $amount = $formamount;
- } else {
- $amount = $payment_array->amount;
- }
- }
- $oamount = $amount;
- $mode = esc_attr(get_option('mode'));
- if ($mode == 'test') {
- $key = esc_attr(get_option('tsk'));
- } else {
- $key = esc_attr(get_option('lsk'));
- }
- $paystack_url = 'https://api.paystack.co/transaction/verify/' . $code;
- $headers = array(
- 'Authorization' => 'Bearer ' . $key
- );
- $args = array(
- 'headers' => $headers,
- 'timeout' => 60
- );
- $request = wp_remote_get($paystack_url, $args);
- if (! is_wp_error($request) && 200 == wp_remote_retrieve_response_code($request)) {
- $paystack_response = json_decode(wp_remote_retrieve_body($request));
- if ('success' == $paystack_response->data->status) {
- $customer_code = $paystack_response->data->customer->customer_code;
- $amount_paid = $paystack_response->data->amount / 100;
- $paystack_ref = $paystack_response->data->reference;
- $paid_at = $paystack_response->data->transaction_date;
- if ($recur == 'optional' || $recur == 'plan') {
- $wpdb->update($table, array( 'paid' => 1,'amount' => $amount_paid, 'paid_at' => $paid_at), array('txn_code'=>$paystack_ref));
- $thankyou = get_post_meta($payment_array->post_id, '_successmsg', true);
- $message = $thankyou;
- $result = "success";
- } else {
- if ($amount == 0 || $usevariableamount == 1) {
- $wpdb->update($table, array( 'paid' => 1,'amount' => $amount_paid, 'paid_at' => $paid_at), array('txn_code'=>$paystack_ref));
- $thankyou = get_post_meta($payment_array->post_id, '_successmsg', true);
- $message = $thankyou;
- $result = "success";
- // kkd_pff_paystack_send_receipt($currency,$amount,$name,$payment_array->email,$code,$metadata)
- } else {
- $usequantity = get_post_meta($payment_array->post_id, '_usequantity', true);
- if ($usequantity == 'no') {
- $oamount = (int)str_replace(' ', '', $amount);
- } else {
- $quantity = $_POST["quantity"];
- $unitamount = (int)str_replace(' ', '', $amount);
- $oamount = $quantity*$unitamount;
- }
- if ($txncharge == 'customer') {
- if ($minimum == 0 && $amount != 0) {
- $oamount = kkd_pff_paystack_add_paystack_charge($oamount);
- }
- }
-
- // if ($txncharge == 'customer') {
- // $amount = kkd_pff_paystack_add_paystack_charge($amount);
- // }
- if ($oamount != $amount_paid) {
- // echo $oamount. ' - '.$amount_paid;
- $message = "Invalid amount Paid. Amount required is ".$currency."".number_format($oamount)."";
- $result = "failed";
- } else {
- $wpdb->update($table, array( 'paid' => 1, 'paid_at' => $paid_at), array('txn_code'=>$paystack_ref));
- $thankyou = get_post_meta($payment_array->post_id, '_successmsg', true);
- $message = $thankyou;
- $result = "success";
- }
- }
- }
- } else {
- $message = "Transaction Failed/Invalid Code";
- $result = "failed";
- }
- } else {
- $message = "Payment Verifiction Failed";
- $result = "failed";
- }
- } else {
- $message = "Payment Verification Failed.";
- $result = "failed";
- }
-
- if ($result == 'success') {
- ///
- //Create Plan
- $enabled_custom_plan = get_post_meta($payment_array->post_id, '_startdate_enabled', true);
- if ($enabled_custom_plan == 1) {
- $mode = esc_attr(get_option('mode'));
- if ($mode == 'test') {
- $key = esc_attr(get_option('tsk'));
- } else {
- $key = esc_attr(get_option('lsk'));
- }
- //Create Plan
- $paystack_url = 'https://api.paystack.co/subscription';
- $headers = array(
- 'Content-Type' => 'application/json',
- 'Authorization' => 'Bearer ' . $key
- );
- $custom_plan = get_post_meta($payment_array->post_id, '_startdate_plan_code', true);
- $days = get_post_meta($payment_array->post_id, '_startdate_days', true);
-
- $start_date = date("c", strtotime("+".$days." days"));
- $body = array(
- 'start_date' => $start_date,
- 'plan' => $custom_plan,
- 'customer' => $customer_code
- );
- $args = array(
- 'body' => json_encode($body),
- 'headers' => $headers,
- 'timeout' => 60
- );
-
- $request = wp_remote_post($paystack_url, $args);
- if (! is_wp_error($request)) {
- $paystack_response = json_decode(wp_remote_retrieve_body($request));
- $plancode = $paystack_response->data->subscription_code;
- // $message.= $message.'Subscribed
'.$plancode.'sssss';
- }
- }
-
- $sendreceipt = get_post_meta($payment_array->post_id, '_sendreceipt', true);
- if ($sendreceipt == 'yes') {
- $decoded = json_decode($payment_array->metadata);
- $fullname = $decoded[0]->value;
- kkd_pff_paystack_send_receipt($payment_array->post_id, $currency, $amount_paid, $fullname, $payment_array->email, $paystack_ref, $payment_array->metadata);
- kkd_pff_paystack_send_receipt_owner($payment_array->post_id, $currency, $amount_paid, $fullname, $payment_array->email, $paystack_ref, $payment_array->metadata);
- }
- }
- $response = array(
- 'result' => $result,
- 'message' => $message,
- );
- if ($result == 'success' && $redirect != '') {
- $response['result'] = 'success2';
- $response['link'] = $redirect;
- }
-
-
- echo json_encode($response);
-
- die();
-}
-
-
-add_action('wp_ajax_kkd_pff_paystack_retry_action', 'kkd_pff_paystack_retry_action');
-add_action('wp_ajax_nopriv_kkd_pff_paystack_retry_action', 'kkd_pff_paystack_retry_action');
-function kkd_pff_paystack_retry_action()
-{
- if (trim($_POST['code']) == '') {
- $response['result'] = 'failed';
- $response['message'] = 'Cde is required';
-
- // Exit here, for not processing further because of the error
- exit(json_encode($response));
- }
- do_action('kkd_pff_paystack_before_save');
-
- global $wpdb;
- $code = $_POST['code'];
- $newcode = kkd_pff_paystack_generate_code();
- $newcode = $newcode.'_2';
- $insert = array();
- $table = $wpdb->prefix.KKD_PFF_PAYSTACK_TABLE;
- $record = $wpdb->get_results("SELECT * FROM $table WHERE (txn_code = '".$code."')");
- if (array_key_exists("0", $record)) {
- $dbdata = $record[0];
- $plan = $dbdata->plan;
- $quantity = 1;
- $wpdb->update($table, array( 'txn_code_2' => $newcode), array('txn_code' => $code));
-
- $currency = get_post_meta($dbdata->post_id, '_currency', true);
- $subaccount = get_post_meta($dbdata->post_id, '_subaccount', true);
- $txnbearer = get_post_meta($dbdata->post_id, '_txnbearer', true);
- $transaction_charge = get_post_meta($dbdata->post_id, '_merchantamount', true);
- $transaction_charge = $transaction_charge*100;
- $fixedmetadata = kkd_pff_paystack_meta_as_custom_fields($dbdata->metadata);
- $nmeta = json_decode($dbdata->metadata);
- foreach ($nmeta as $nkey => $nvalue) {
- if ($nvalue->variable_name == 'Quantity') {
- $quantity = $nvalue->value;
- }
- if ($nvalue->variable_name == 'Full_Name') {
- $fullname = $nvalue->value;
- }
- }
- }
- if ($subaccount == "" || !isset($subaccount)) {
- $subaccount = null;
- $txnbearer = null;
- $transaction_charge = null;
- }
- if ($transaction_charge == "" || $transaction_charge == 0 || $transaction_charge == null || !isset($transaction_charge)) {
- $transaction_charge = null;
- }
- $response = array(
- 'result' => 'success',
- 'code' => $newcode,
- 'plan' => $plan,
- 'quantity' => $quantity,
- 'email' => $dbdata->email,
- 'name' => $fullname,
- 'total' => $dbdata->amount*100,
- 'custom_fields' => $fixedmetadata,
- 'subaccount' => $subaccount,
- 'txnbearer' => $txnbearer,
- 'transaction_charge' => $transaction_charge
- );
- echo json_encode($response);
-
- die();
-}
-add_action('wp_ajax_kkd_pff_paystack_rconfirm_payment', 'kkd_pff_paystack_rconfirm_payment');
-add_action('wp_ajax_nopriv_kkd_pff_paystack_rconfirm_payment', 'kkd_pff_paystack_rconfirm_payment');
-
-function kkd_pff_paystack_rconfirm_payment()
-{
- if (trim($_POST['code']) == '') {
- $response['error'] = true;
- $response['error_message'] = "Did you make a payment?";
-
- exit(json_encode($response));
- }
- global $wpdb;
- $table = $wpdb->prefix.KKD_PFF_PAYSTACK_TABLE;
- $code = $_POST['code'];
- $record = $wpdb->get_results("SELECT * FROM $table WHERE (txn_code_2 = '".$code."')");
- if (array_key_exists("0", $record)) {
- $payment_array = $record[0];
- $amount = get_post_meta($payment_array->post_id, '_amount', true);
- $recur = get_post_meta($payment_array->post_id, '_recur', true);
- $currency = get_post_meta($payment_array->post_id, '_currency', true);
- $txncharge = get_post_meta($payment_array->post_id, '_txncharge', true);
- $redirect = get_post_meta($payment_array->post_id, '_redirect', true);
-
-
- $mode = esc_attr(get_option('mode'));
- if ($mode == 'test') {
- $key = esc_attr(get_option('tsk'));
- } else {
- $key = esc_attr(get_option('lsk'));
- }
- $paystack_url = 'https://api.paystack.co/transaction/verify/' . $code;
- $headers = array(
- 'Authorization' => 'Bearer ' . $key
- );
- $args = array(
- 'headers' => $headers,
- 'timeout' => 60
- );
- $request = wp_remote_get($paystack_url, $args);
- if (! is_wp_error($request) && 200 == wp_remote_retrieve_response_code($request)) {
- $paystack_response = json_decode(wp_remote_retrieve_body($request));
- if ('success' == $paystack_response->data->status) {
- $amount_paid = $paystack_response->data->amount / 100;
- $paystack_ref = $paystack_response->data->reference;
- $paid_at = $paystack_response->data->transaction_date;
- if ($recur == 'optional' || $recur == 'plan') {
- $wpdb->update($table, array( 'paid' => 1,'amount' =>$amount_paid,'paid_at' => $paid_at), array('txn_code_2'=>$paystack_ref));
- $thankyou = get_post_meta($payment_array->post_id, '_successmsg', true);
- $message = $thankyou;
- $result = "success";
- } else {
- if ($amount == 0) {
- $wpdb->update($table, array( 'paid' => 1,'amount' =>$amount_paid,'paid_at' => $paid_at), array('txn_code_2'=>$paystack_ref));
- $thankyou = get_post_meta($payment_array->post_id, '_successmsg', true);
- $message = $thankyou;
- $result = "success";
- // kkd_pff_paystack_send_receipt($currency,$amount,$name,$payment_array->email,$code,$metadata)
- } else {
- $usequantity = get_post_meta($payment_array->post_id, '_usequantity', true);
- if ($usequantity == 'no') {
- $amount = (int)str_replace(' ', '', $amount);
- } else {
- $quantity = $_POST["quantity"];
- $unitamount = (int)str_replace(' ', '', $amount);
- $amount = $quantity*$unitamount;
- }
-
-
- if ($txncharge == 'customer') {
- $amount = kkd_pff_paystack_add_paystack_charge($amount);
- }
- if ($amount != $amount_paid) {
- $message = "Invalid amount Paid. Amount required is ".$currency."".number_format($amount)."";
- $result = "failed";
- } else {
- $wpdb->update($table, array( 'paid' => 1, 'paid_at' => $paid_at), array('txn_code_2'=>$paystack_ref));
- $thankyou = get_post_meta($payment_array->post_id, '_successmsg', true);
- $message = $thankyou;
- $result = "success";
- }
- }
- }
- } else {
- $message = "Transaction Failed/Invalid Code";
- $result = "failed";
- }
- }
- } else {
- $message = "Payment Verification Failed.";
- $result = "failed";
- }
-
- if ($result == 'success') {
- $sendreceipt = get_post_meta($payment_array->post_id, '_sendreceipt', true);
- if ($sendreceipt == 'yes') {
- $decoded = json_decode($payment_array->metadata);
- $fullname = $decoded[0]->value;
- kkd_pff_paystack_send_receipt($payment_array->post_id, $currency, $amount_paid, $fullname, $payment_array->email, $paystack_ref, $payment_array->metadata);
- kkd_pff_paystack_send_receipt_owner($payment_array->post_id, $currency, $amount_paid, $fullname, $payment_array->email, $paystack_ref, $payment_array->metadata);
- }
- }
- $response = array(
- 'result' => $result,
- 'message' => $message,
- );
- if ($result == 'success' && $redirect != '') {
- $response['result'] = 'success2';
- $response['link'] = $redirect;
- }
-
-
- echo json_encode($response);
-
- die();
-}
diff --git a/public/class-paystack-plugin-tracker.php b/public/class-paystack-plugin-tracker.php
index 30ecbe1..50f032a 100644
--- a/public/class-paystack-plugin-tracker.php
+++ b/public/class-paystack-plugin-tracker.php
@@ -1,38 +1,29 @@
plugin_name = $plugin;
- $this->public_key = $pk;
- }
+namespace Paystack_Plugins\Payment_Forms;
+class Paystack_Plugin_Tracker {
- function log_transaction_success($trx_ref){
- //send reference to logger along with plugin name and public key
- $url = "https://plugin-tracker.paystackintegrations.com/log/charge_success";
+ public static function log_transaction( $txn_ref ) {
- $fields = [
- 'plugin_name' => $this->plugin_name,
- 'transaction_reference' => $trx_ref,
- 'public_key' => $this->public_key
- ];
+ $public_key = paystack_forms_get_public_key();
- $fields_string = http_build_query($fields);
+ if ( empty( $public_key ) ) {
+ return;
+ }
- $ch = curl_init();
+ $url = 'https://plugin-tracker.paystackintegrations.com/log/charge_success';
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_POST, true);
- curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
+ $body = array(
+ 'public_key' => $public_key,
+ 'plugin_name' => 'pff-paystack',
+ 'transaction_reference' => $txn_ref,
+ );
- curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
+ $args = array(
+ 'body' => $body,
+ );
- //execute post
- $result = curl_exec($ch);
- // echo $result;
- }
-}
+ wp_remote_post( $url, $args );
+ }
+}