Skip to content

Commit 4e0523c

Browse files
committed
Csv export
1 parent 8328220 commit 4e0523c

File tree

5 files changed

+219
-86
lines changed

5 files changed

+219
-86
lines changed

admin/class-paystack-forms-admin.php

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function kkd_pff_paystack_setting_page() {
3434
<h1>Paystack Forms API KEYS Settings!</h1>
3535
<h3>Paystack</h3>
3636

37-
<h4>Optional: To avoid situations where bad network makes it impossible to verify transactions, set your webhook URL <a href="https://dashboard.paystack.co/#/settings/developer">here</a> to the URL below<strong style="color: red"><pre><code><?php echo get_site_url().'/kkd/wpffp/webhook/'; ?></code></pre></strong></
37+
<h4>Optional: To avoid situations where bad network makes it impossible to verify transactions, set your webhook URL <a href="https://dashboard.paystack.co/#/settings/developer">here</a> to the URL below<strong style="color: red"><pre><code><?php echo get_site_url().'/kkd/wpffp/webhook/'; ?></code></pre></strong></
3838
<form method="post" action="options.php">
3939
<?php settings_fields( 'kkd-pff-paystack-settings-group' ); do_settings_sections( 'kkd-pff-paystack-settings-group' ); ?>
4040
<table class="form-table paystack_setting_page">
@@ -171,7 +171,7 @@ function kkd_pff_paystack_edit_dashboard_header_columns( $columns ) {
171171
$columns = array(
172172
'cb' => '<input type="checkbox" />',
173173
'title' => __( 'Name' ),
174-
'shortcode' => __( 'Shortcode' ),
174+
'shortcode' => __( 'Shortcode' ),
175175
'payments' => __( 'Payments' ),
176176
'date' => __( 'Date' )
177177
);
@@ -524,11 +524,21 @@ function kkd_pff_paystack_payment_submissions(){
524524
$loggedin = get_post_meta($id,'_loggedin',true);
525525
$txncharge = get_post_meta($id,'_txncharge',true);
526526

527-
echo "<title>".$obj->post_title." Payments </title>";
528-
echo "<h1>".$obj->post_title." Payments</h1>";
529527
$exampleListTable = new Kkd_Pff_Paystack_Payments_List_Table();
530528
$exampleListTable->prepare_items();
531529
?>
530+
<div id="welcome-panel" class="welcome-panel">
531+
<div class="welcome-panel-content">
532+
<h1 style="margin: 0px;"><?php echo $obj->post_title; ?> Payments </h1>
533+
<p class="about-description">All payments made for this form</p>
534+
<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
535+
<input type="hidden" name="action" value="kkd_pff_export_excel">
536+
<input type="hidden" name="form_id" value="<?php echo $id; ?>">
537+
<button type="submit" class="button button-primary button-hero load-customize" >Export Data to Excel</button>
538+
</form>
539+
<br><br>
540+
</div>
541+
</div>
532542
<div class="wrap">
533543
<div id="icon-users" class="icon32"></div>
534544
<?php $exampleListTable->display(); ?>
@@ -537,6 +547,110 @@ function kkd_pff_paystack_payment_submissions(){
537547

538548
}
539549
}
550+
add_action( 'admin_post_kkd_pff_export_excel', 'Kkd_pff_export_excel' );
551+
552+
function Kkd_pff_export_excel() {
553+
global $wpdb;
554+
555+
$post_id = $_POST['form_id'];
556+
$obj = get_post($post_id);
557+
$csv_output = "";
558+
559+
560+
$table = $wpdb->prefix.KKD_PFF_PAYSTACK_TABLE;
561+
$data = array();
562+
$alldbdata = $wpdb->get_results("SELECT * FROM $table WHERE (post_id = '".$post_id."' AND paid = '1') ORDER BY `id` ASC");
563+
$i = 0;
564+
565+
if (count($alldbdata) > 0) {
566+
$header = $alldbdata[0];
567+
$csv_output .= "#,";
568+
$csv_output .= "Email,";
569+
$csv_output .= "Amount,";
570+
$csv_output .= "Reference,";
571+
$new = json_decode($header->metadata);
572+
$text = '';
573+
if (array_key_exists("0", $new)) {
574+
foreach ($new as $key => $item) {
575+
$csv_output .= $item->display_name.",";
576+
}
577+
}else{
578+
if (count($new) > 0) {
579+
foreach ($new as $key => $item) {
580+
$csv_output .= $key.",";
581+
}
582+
}
583+
}
584+
$csv_output .= "\n";
585+
586+
587+
}
588+
foreach ($alldbdata as $key => $dbdata) {
589+
$newkey = $key+1;
590+
if ($dbdata->txn_code_2 != "") {
591+
$txn_code = $dbdata->txn_code_2;
592+
}else{
593+
$txn_code = $dbdata->txn_code;
594+
}
595+
// $csv_output .= $txn_code;//.",";
596+
// $csv_output .= "\n";
597+
// $i++;
598+
// $data[] = array(
599+
// 'id' => $newkey,
600+
// 'email' => '<a href="mailto:'.$dbdata->email.'">'.$dbdata->email.'</a>',
601+
// 'amount' => $currency.'<b>'.number_format($dbdata->amount).'</b>',
602+
// 'txn_code' => $txn_code,
603+
// 'metadata' => format_data($dbdata->metadata),
604+
// 'date' => $dbdata->created_at
605+
// );
606+
$csv_output .= $newkey.",";
607+
$csv_output .= $dbdata->email.",";
608+
$csv_output .= $currency.' '.$dbdata->amount.",";
609+
$csv_output .= $txn_code.",";
610+
$new = json_decode($header->metadata);
611+
$text = '';
612+
if (array_key_exists("0", $new)) {
613+
foreach ($new as $key => $item) {
614+
$csv_output .= $item->value.",";
615+
}
616+
}else{
617+
if (count($new) > 0) {
618+
foreach ($new as $key => $item) {
619+
$csv_output .= $item.",";
620+
}
621+
}
622+
}
623+
$csv_output .= "\n";
624+
}
625+
626+
// $link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
627+
// mysql_select_db($db) or die("Can not connect.");
628+
629+
// $result = mysql_query("SHOW COLUMNS FROM ".$table."");
630+
// if (mysql_num_rows($result) > 0) {
631+
// while ($row = mysql_fetch_assoc($result)) {
632+
// $csv_output .= $row[‘Field’]."; ";
633+
// $i++;
634+
// }
635+
// }
636+
637+
// $values = mysql_query("SELECT * FROM ".$table."");
638+
// while ($rowr = mysql_fetch_row($values)) {
639+
// for ($j=0;$j<$i;$j++) {
640+
// $csv_output .= $rowr[$j]."; ";
641+
// }
642+
// $csv_output .= "\n";
643+
// }
644+
645+
$filename = $obj->post_title."_payments_".date("Y-m-d_H-i",time());
646+
header("Content-type: application/vnd.ms-excel");
647+
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
648+
header( "Content-disposition: filename=".$filename.".csv");
649+
print $csv_output;
650+
exit;
651+
652+
// Handle request then generate response using echo or leaving PHP and using HTML
653+
}
540654
class Kkd_Pff_Paystack_Wp_List_Table{
541655
public function __construct(){
542656
add_action( 'admin_menu', array($this, 'add_menu_example_list_table_page' ));
@@ -591,7 +705,7 @@ public function prepare_items(){
591705

592706
global $wpdb;
593707

594-
$table = $wpdb->prefix.KKD_PFF_PAYSTACK_TABLE;
708+
$table = $wpdb->prefix.KKD_PFF_PAYSTACK_TABLE;
595709
$data = array();
596710
$alldbdata = $wpdb->get_results("SELECT * FROM $table WHERE (post_id = '".$post_id."' AND paid = '1')");
597711

@@ -603,8 +717,8 @@ public function prepare_items(){
603717
$txn_code = $dbdata->txn_code;
604718
}
605719
$data[] = array(
606-
'id' => $newkey,
607-
'email' => '<a href="mailto:'.$dbdata->email.'">'.$dbdata->email.'</a>',
720+
'id' => $newkey,
721+
'email' => '<a href="mailto:'.$dbdata->email.'">'.$dbdata->email.'</a>',
608722
'amount' => $currency.'<b>'.number_format($dbdata->amount).'</b>',
609723
'txn_code' => $txn_code,
610724
'metadata' => format_data($dbdata->metadata),

includes/class-paystack-forms.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private function load_dependencies() {
144144
* side of the site.
145145
*/
146146
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-paystack-forms-public.php';
147-
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-paystack-forms-webhook.php';
147+
// require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/paystack-webhook.php';
148148

149149
$this->loader = new Kkd_Pff_Paystack_Loader();
150150

includes/paystack-webhook.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
// require_once('../../../../wp-load.php');
3+
4+
5+
6+
if ( ( strtoupper( $_SERVER['REQUEST_METHOD'] ) != 'POST' ) || ! array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
7+
exit;
8+
}
9+
10+
$json = file_get_contents( "php://input" );
11+
12+
// validate event do all at once to avoid timing attack
13+
if ( $_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac( 'sha512', $json, $this->secret_key ) ) {
14+
exit;
15+
}
16+
17+
$event = json_decode( $json );
18+
19+
if ( 'charge.success' == $event->event ) {
20+
21+
http_response_code( 200 );
22+
23+
$order_details = explode( '_', $event->data->reference );
24+
25+
$order_id = (int) $order_details[0];
26+
27+
$order = wc_get_order($order_id);
28+
29+
$paystack_txn_ref = get_post_meta( $order_id, '_paystack_txn_ref', true );
30+
31+
if ( $event->data->reference != $paystack_txn_ref ) {
32+
exit;
33+
}
34+
35+
if ( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
36+
exit;
37+
}
38+
39+
$order_total = $order->get_total();
40+
41+
$amount_paid = $event->data->amount / 100;
42+
43+
$paystack_ref = $event->data->reference;
44+
45+
// check if the amount paid is equal to the order amount.
46+
if ( $order_total != $amount_paid ) {
47+
48+
$order->update_status( 'on-hold', '' );
49+
50+
add_post_meta( $order_id, '_transaction_id', $paystack_ref, true );
51+
52+
$notice = 'Thank you for shopping with us.<br />Your payment transaction was successful, but the amount paid is not the same as the total order amount.<br />Your order is currently on-hold.<br />Kindly contact us for more information regarding your order and payment status.';
53+
$notice_type = 'notice';
54+
55+
// Add Customer Order Note
56+
$order->add_order_note( $notice, 1 );
57+
58+
// Add Admin Order Note
59+
$order->add_order_note('<strong>Look into this order</strong><br />This order is currently on hold.<br />Reason: Amount paid is less than the total order amount.<br />Amount Paid was <strong>&#8358;'.$amount_paid.'</strong> while the total order amount is <strong>&#8358;'.$order_total.'</strong><br />Paystack Transaction Reference: '.$paystack_ref );
60+
61+
$order->reduce_order_stock();
62+
63+
wc_add_notice( $notice, $notice_type );
64+
65+
wc_empty_cart();
66+
67+
} else {
68+
69+
$order->payment_complete( $paystack_ref );
70+
71+
$order->add_order_note( sprintf( 'Payment via Paystack successful (Transaction Reference: %s)', $paystack_ref ) );
72+
73+
wc_empty_cart();
74+
75+
}
76+
77+
$this->save_card_details( $event, $order->get_user_id(), $order_id );
78+
79+
exit;
80+
}
81+
82+
exit;
83+
84+
85+
86+
87+
?>

paystack-forms.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,13 @@ function kkd_pff_paystack_invoice_url_rewrite(){
158158
$wp_rewrite->non_wp_rules['paystackinvoice/$'] = $plugin_url;
159159
file_put_contents(ABSPATH.'.htaccess', $wp_rewrite->mod_rewrite_rules() );
160160
}
161+
162+
add_action( 'init', 'kkd_pff_paystack_webhook_url_rewrite' );
163+
function kkd_pff_paystack_webhook_url_rewrite(){
164+
global $wp_rewrite;
165+
$plugin_url = plugins_url( 'includes/paystack-webhook.php', __FILE__ );
166+
$plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );
167+
$wp_rewrite->non_wp_rules['kkd_wppf_webhoook$'] = $plugin_url;
168+
$wp_rewrite->non_wp_rules['kkd_wppf_webhoook/$'] = $plugin_url;
169+
file_put_contents(ABSPATH.'.htaccess', $wp_rewrite->mod_rewrite_rules() );
170+
}

public/class-paystack-forms-webhook.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)