| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Classes; |
| 4 |
|
| 5 |
use Better_Payment\Lite\Controller; |
| 6 |
use Better_Payment\Lite\Models\TransactionModel; |
| 7 |
use Better_Payment\Lite\Traits\Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Exit if accessed directly |
| 11 |
*/ |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Export handler class |
| 18 |
* |
| 19 |
* @since 0.0.4 |
| 20 |
*/ |
| 21 |
class Export extends Controller{ |
| 22 |
|
| 23 |
use Helper; |
| 24 |
/** |
| 25 |
* Class constructor |
| 26 |
* |
| 27 |
* @since 0.0.4 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
public function export_transactions() { |
| 34 |
$response = []; |
| 35 |
|
| 36 |
if (!wp_verify_nonce($_REQUEST['nonce'], 'better_payment_admin_nonce')) { |
| 37 |
$response['message'] = __("Access Denied!", 'better-payment'); |
| 38 |
wp_send_json_error($response); |
| 39 |
} |
| 40 |
|
| 41 |
if (!current_user_can('manage_options')) { |
| 42 |
$response['message'] = __("Access Denied!", 'better-payment'); |
| 43 |
wp_send_json_error($response); |
| 44 |
} |
| 45 |
|
| 46 |
$transaction_model = new TransactionModel(); |
| 47 |
$all_transactions = $transaction_model->get_transactions(); |
| 48 |
|
| 49 |
$filename = 'better-payment-transactions-' . date('Y-m-d') . '.csv'; |
| 50 |
return $this->transactions_array_to_csv_download($all_transactions, $filename); |
| 51 |
} |
| 52 |
|
| 53 |
public function transactions_array_to_csv_download($array, $filename = "export.csv", $delimiter=";") { |
| 54 |
$bp_helper_obj = new Better_Payment_Helper(); |
| 55 |
$wp_date_format = get_option('date_format'); |
| 56 |
$wp_time_format = get_option('time_format'); |
| 57 |
|
| 58 |
$f = fopen('php://memory', 'w'); |
| 59 |
|
| 60 |
fputcsv($f, $this->export_transactions_heading(), $delimiter); |
| 61 |
|
| 62 |
foreach ($array as $line) { |
| 63 |
$line = (array)$line; |
| 64 |
unset($line['id']); |
| 65 |
unset($line['order_id']); |
| 66 |
unset($line['obj_id']); |
| 67 |
unset($line['refund_info']); |
| 68 |
unset($line['customer_info']); |
| 69 |
// unset($line['form_fields_info']); |
| 70 |
unset($line['referer']); |
| 71 |
|
| 72 |
|
| 73 |
$line['status'] = esc_html(ucfirst($bp_helper_obj->get_type_by_transaction_status($line['status'], 'v2'))); |
| 74 |
$line['source'] = esc_html( ucfirst( $line['source'] ) ); |
| 75 |
$line['payment_date'] = wp_date($wp_date_format.' '.$wp_time_format, strtotime($line['payment_date']));; |
| 76 |
|
| 77 |
$line['form_fields_info'] = $this->form_fields_info_formatted($line['form_fields_info']); |
| 78 |
|
| 79 |
$line['form_fields_info_name'] = isset($line['form_fields_info']['customer_name']) ? $line['form_fields_info']['customer_name'] : ''; |
| 80 |
$line['form_fields_info_email'] = isset($line['form_fields_info']['customer_email']) ? $line['form_fields_info']['customer_email'] : ''; |
| 81 |
|
| 82 |
$line_formatted = array( |
| 83 |
$line['form_fields_info_name'], |
| 84 |
$line['form_fields_info_email'], |
| 85 |
$line['currency'] . ' ' . $line['amount'], |
| 86 |
$line['transaction_id'], |
| 87 |
$line['source'], |
| 88 |
$line['status'], |
| 89 |
$line['payment_date'], |
| 90 |
); |
| 91 |
|
| 92 |
fputcsv($f, $line_formatted, $delimiter); |
| 93 |
} |
| 94 |
|
| 95 |
// reset the file pointer to the start of the file |
| 96 |
fseek($f, 0); |
| 97 |
// tell the browser it's going to be a csv file |
| 98 |
header('Content-Type: text/csv'); |
| 99 |
// tell the browser we want to save it instead of displaying it |
| 100 |
header('Content-Disposition: attachment; filename="'.$filename.'";'); |
| 101 |
// make php send the generated csv lines to the browser |
| 102 |
|
| 103 |
fpassthru($f); |
| 104 |
exit; |
| 105 |
} |
| 106 |
|
| 107 |
public function export_transactions_heading(){ |
| 108 |
return array( |
| 109 |
'Name', |
| 110 |
'Email Address', |
| 111 |
'Amount', |
| 112 |
'Transaction ID', |
| 113 |
'Source', |
| 114 |
'Status', |
| 115 |
'Date', |
| 116 |
// 'Order ID', |
| 117 |
// 'Customer Info', |
| 118 |
// 'Form Fields Info', |
| 119 |
// 'Currency', |
| 120 |
// 'Referer' |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
public function form_fields_info_formatted($form_fields_info){ |
| 125 |
$bp_form_fields_info = maybe_unserialize($form_fields_info); |
| 126 |
|
| 127 |
$bp_transaction_customer_name = isset($bp_form_fields_info['primary_first_name']) ? sanitize_text_field($bp_form_fields_info['primary_first_name']) : ''; |
| 128 |
$bp_transaction_customer_name .= ' '; |
| 129 |
$bp_transaction_customer_name .= isset($bp_form_fields_info['primary_last_name']) ? sanitize_text_field($bp_form_fields_info['primary_last_name']) : ''; |
| 130 |
|
| 131 |
//legacy |
| 132 |
if( empty($bp_transaction_customer_name) || $bp_transaction_customer_name == ' ' ){ |
| 133 |
$bp_transaction_customer_name = isset($bp_form_fields_info['first_name']) ? sanitize_text_field($bp_form_fields_info['first_name']) : ''; |
| 134 |
$bp_transaction_customer_name .= ' '; |
| 135 |
$bp_transaction_customer_name .= isset($bp_form_fields_info['last_name']) ? sanitize_text_field($bp_form_fields_info['last_name']) : ''; |
| 136 |
} |
| 137 |
|
| 138 |
$bp_transaction_customer_email = isset($bp_form_fields_info['primary_email']) ? sanitize_text_field($bp_form_fields_info['primary_email']) : ''; |
| 139 |
//legacy |
| 140 |
if( empty($bp_transaction_customer_email) ){ |
| 141 |
$bp_transaction_customer_email = isset($bp_form_fields_info['email']) ? sanitize_text_field($bp_form_fields_info['email']) : ''; |
| 142 |
} |
| 143 |
|
| 144 |
return [ |
| 145 |
'customer_name' => $bp_transaction_customer_name, |
| 146 |
'customer_email' => $bp_transaction_customer_email |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
} |