| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Classes; |
| 4 |
|
| 5 |
use Better_Payment\Lite\Classes\Helper as ClassesHelper; |
| 6 |
use Better_Payment\Lite\Controller; |
| 7 |
use Better_Payment\Lite\Models\TransactionModel; |
| 8 |
use Better_Payment\Lite\Traits\Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Exit if accessed directly |
| 12 |
*/ |
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Export handler class |
| 19 |
* |
| 20 |
* @since 0.0.4 |
| 21 |
*/ |
| 22 |
class Export extends Controller{ |
| 23 |
|
| 24 |
use Helper; |
| 25 |
/** |
| 26 |
* Class constructor |
| 27 |
* |
| 28 |
* @since 0.0.4 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
public function export_transactions() { |
| 35 |
$response = []; |
| 36 |
|
| 37 |
if (!wp_verify_nonce($_REQUEST['nonce'], 'wp_rest')) { |
| 38 |
$response['message'] = __("Access Denied!", 'better-payment'); |
| 39 |
wp_send_json_error($response); |
| 40 |
} |
| 41 |
|
| 42 |
if (!current_user_can('manage_options')) { |
| 43 |
$response['message'] = __("Access Denied!", 'better-payment'); |
| 44 |
wp_send_json_error($response); |
| 45 |
} |
| 46 |
|
| 47 |
$transaction_model = new TransactionModel(); |
| 48 |
|
| 49 |
// Check if this is a filtered export |
| 50 |
$is_filtered = isset($_REQUEST['filtered']) && $_REQUEST['filtered'] === '1'; |
| 51 |
|
| 52 |
if ($is_filtered) { |
| 53 |
// Build filter arguments |
| 54 |
$filter_args = []; |
| 55 |
|
| 56 |
if (!empty($_REQUEST['search_text'])) { |
| 57 |
$filter_args['search_text'] = sanitize_text_field($_REQUEST['search_text']); |
| 58 |
} |
| 59 |
|
| 60 |
if (!empty($_REQUEST['payment_date_from'])) { |
| 61 |
$filter_args['payment_date_from'] = sanitize_text_field($_REQUEST['payment_date_from']); |
| 62 |
} |
| 63 |
|
| 64 |
if (!empty($_REQUEST['payment_date_to'])) { |
| 65 |
$filter_args['payment_date_to'] = sanitize_text_field($_REQUEST['payment_date_to']); |
| 66 |
} |
| 67 |
|
| 68 |
if (!empty($_REQUEST['source'])) { |
| 69 |
$filter_args['source'] = sanitize_text_field($_REQUEST['source']); |
| 70 |
} |
| 71 |
|
| 72 |
if (!empty($_REQUEST['currency'])) { |
| 73 |
$filter_args['currency'] = sanitize_text_field($_REQUEST['currency']); |
| 74 |
} |
| 75 |
|
| 76 |
if (!empty($_REQUEST['status'])) { |
| 77 |
$filter_args['status'] = sanitize_text_field($_REQUEST['status']); |
| 78 |
} |
| 79 |
|
| 80 |
$filter_args['per_page'] = '999999999'; |
| 81 |
$all_transactions = $transaction_model->get_transactions($filter_args); |
| 82 |
$filename = 'better-payment-transactions-filtered-' . date('Y-m-d') . '.csv'; |
| 83 |
} else { |
| 84 |
$all_transactions = $transaction_model->get_transactions(); |
| 85 |
$filename = 'better-payment-transactions-' . date('Y-m-d') . '.csv'; |
| 86 |
} |
| 87 |
|
| 88 |
return $this->transactions_array_to_csv_download($all_transactions, $filename); |
| 89 |
} |
| 90 |
|
| 91 |
public function transactions_array_to_csv_download($array, $filename = "export.csv", $delimiter=";") { |
| 92 |
$bp_helper_obj = new ClassesHelper(); |
| 93 |
$wp_date_format = get_option('date_format'); |
| 94 |
$wp_time_format = get_option('time_format'); |
| 95 |
|
| 96 |
$f = fopen('php://memory', 'w'); |
| 97 |
|
| 98 |
fputcsv($f, $this->export_transactions_heading(), $delimiter); |
| 99 |
|
| 100 |
foreach ($array as $line) { |
| 101 |
$line = (array)$line; |
| 102 |
unset($line['id']); |
| 103 |
unset($line['order_id']); |
| 104 |
unset($line['obj_id']); |
| 105 |
unset($line['refund_info']); |
| 106 |
unset($line['customer_info']); |
| 107 |
// unset($line['form_fields_info']); |
| 108 |
unset($line['referer']); |
| 109 |
|
| 110 |
|
| 111 |
$line['status'] = esc_html(ucfirst($bp_helper_obj->get_type_by_transaction_status($line['status'], 'v2'))); |
| 112 |
$line['source'] = esc_html( ucfirst( $line['source'] ) ); |
| 113 |
$line['payment_date'] = wp_date($wp_date_format.' '.$wp_time_format, strtotime($line['payment_date']));; |
| 114 |
|
| 115 |
$line['form_fields_info'] = $this->form_fields_info_formatted($line['form_fields_info']); |
| 116 |
|
| 117 |
$line['form_fields_info_name'] = isset($line['form_fields_info']['customer_name']) ? $line['form_fields_info']['customer_name'] : ''; |
| 118 |
$line['form_fields_info_email'] = isset($line['form_fields_info']['customer_email']) ? $line['form_fields_info']['customer_email'] : ''; |
| 119 |
$line['payment_type'] = ! empty( $line['form_fields_info']['subscription_id'] ) ? 'Subscription' : 'One Time'; |
| 120 |
|
| 121 |
unset($line['form_fields_info']['customer_name']); |
| 122 |
unset($line['form_fields_info']['customer_email']); |
| 123 |
|
| 124 |
$line['form_fields_info'] = array_filter($line['form_fields_info'], function($value) { |
| 125 |
return !empty($value); |
| 126 |
}); |
| 127 |
|
| 128 |
$line_formatted = array( |
| 129 |
$line['form_fields_info_name'], |
| 130 |
$line['form_fields_info_email'], |
| 131 |
$line['currency'] . ' ' . $line['amount'], |
| 132 |
$line['payment_type'], |
| 133 |
$line['transaction_id'], |
| 134 |
$line['source'], |
| 135 |
$line['status'], |
| 136 |
$line['payment_date'], |
| 137 |
! empty( $line['form_fields_info'] ) ? maybe_serialize( $line['form_fields_info'] ) : '', |
| 138 |
); |
| 139 |
|
| 140 |
fputcsv($f, $line_formatted, $delimiter); |
| 141 |
} |
| 142 |
|
| 143 |
// reset the file pointer to the start of the file |
| 144 |
fseek($f, 0); |
| 145 |
// tell the browser it's going to be a csv file |
| 146 |
header('Content-Type: text/csv'); |
| 147 |
// tell the browser we want to save it instead of displaying it |
| 148 |
header('Content-Disposition: attachment; filename="'.$filename.'";'); |
| 149 |
// make php send the generated csv lines to the browser |
| 150 |
|
| 151 |
fpassthru($f); |
| 152 |
exit; |
| 153 |
} |
| 154 |
|
| 155 |
public function export_transactions_heading(){ |
| 156 |
return array( |
| 157 |
esc_html('name'), |
| 158 |
esc_html('email'), |
| 159 |
esc_html('amount'), |
| 160 |
esc_html('payment_type'), |
| 161 |
esc_html('transaction_id'), |
| 162 |
esc_html('source'), |
| 163 |
esc_html('status'), |
| 164 |
esc_html('payment_date'), |
| 165 |
esc_html('form_fields_info'), |
| 166 |
// 'Order ID', |
| 167 |
// 'Customer Info', |
| 168 |
// 'Form Fields Info', |
| 169 |
// 'Currency', |
| 170 |
// 'Referer' |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
public function form_fields_info_formatted($form_fields_info){ |
| 175 |
$bp_form_fields_info = maybe_unserialize($form_fields_info); |
| 176 |
|
| 177 |
$bp_transaction_subscription_id = isset($bp_form_fields_info['subscription_id']) ? sanitize_text_field($bp_form_fields_info['subscription_id']) : ''; |
| 178 |
$bp_transaction_subscription_customer_id = isset($bp_form_fields_info['subscription_customer_id']) ? sanitize_text_field($bp_form_fields_info['subscription_customer_id']) : ''; |
| 179 |
$bp_transaction_subscription_plan_id = isset($bp_form_fields_info['subscription_plan_id']) ? sanitize_text_field($bp_form_fields_info['subscription_plan_id']) : ''; |
| 180 |
|
| 181 |
$subscription_interval = ! empty($bp_form_fields_info['subscription_interval']) ? sanitize_text_field( $bp_form_fields_info['subscription_interval'] ) : ''; |
| 182 |
$subscription_current_period_start = ! empty($bp_form_fields_info['subscription_current_period_start']) ? sanitize_text_field( $bp_form_fields_info['subscription_current_period_start'] ) : ''; |
| 183 |
$subscription_current_period_end = ! empty($bp_form_fields_info['subscription_current_period_end']) ? sanitize_text_field( $bp_form_fields_info['subscription_current_period_end'] ) : ''; |
| 184 |
$subscription_status = ! empty($bp_form_fields_info['subscription_status']) ? sanitize_text_field( $bp_form_fields_info['subscription_status'] ) : ''; |
| 185 |
$subscription_created_date = ! empty($bp_form_fields_info['subscription_created_date']) ? sanitize_text_field( $bp_form_fields_info['subscription_created_date'] ) : ''; |
| 186 |
$is_payment_split_payment = ! empty($bp_form_fields_info['is_payment_split_payment']) ? intval( $bp_form_fields_info['is_payment_split_payment'] ) : 0; |
| 187 |
|
| 188 |
$bp_transaction_customer_name = isset($bp_form_fields_info['primary_first_name']) ? sanitize_text_field($bp_form_fields_info['primary_first_name']) : ''; |
| 189 |
$bp_transaction_customer_name .= ' '; |
| 190 |
$bp_transaction_customer_name .= isset($bp_form_fields_info['primary_last_name']) ? sanitize_text_field($bp_form_fields_info['primary_last_name']) : ''; |
| 191 |
|
| 192 |
//legacy |
| 193 |
if( empty($bp_transaction_customer_name) || $bp_transaction_customer_name == ' ' ){ |
| 194 |
$bp_transaction_customer_name = isset($bp_form_fields_info['first_name']) ? sanitize_text_field($bp_form_fields_info['first_name']) : ''; |
| 195 |
$bp_transaction_customer_name .= ' '; |
| 196 |
$bp_transaction_customer_name .= isset($bp_form_fields_info['last_name']) ? sanitize_text_field($bp_form_fields_info['last_name']) : ''; |
| 197 |
} |
| 198 |
|
| 199 |
$bp_transaction_customer_email = isset($bp_form_fields_info['primary_email']) ? sanitize_text_field($bp_form_fields_info['primary_email']) : ''; |
| 200 |
//legacy |
| 201 |
if( empty($bp_transaction_customer_email) ){ |
| 202 |
$bp_transaction_customer_email = isset($bp_form_fields_info['email']) ? sanitize_text_field($bp_form_fields_info['email']) : ''; |
| 203 |
} |
| 204 |
|
| 205 |
return [ |
| 206 |
'customer_name' => $bp_transaction_customer_name, |
| 207 |
'customer_email' => $bp_transaction_customer_email, |
| 208 |
'subscription_id' => $bp_transaction_subscription_id, |
| 209 |
'subscription_customer_id' => $bp_transaction_subscription_customer_id, |
| 210 |
'subscription_plan_id' => $bp_transaction_subscription_plan_id, |
| 211 |
'subscription_interval' => $subscription_interval, |
| 212 |
'subscription_current_period_start' => $subscription_current_period_start, |
| 213 |
'subscription_current_period_end' => $subscription_current_period_end, |
| 214 |
'subscription_status' => $subscription_status, |
| 215 |
'subscription_created_date' => $subscription_created_date, |
| 216 |
'is_payment_split_payment' => $is_payment_split_payment, |
| 217 |
]; |
| 218 |
} |
| 219 |
|
| 220 |
} |