| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\Classes; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use FluentForm\App\Models\OrderItem; |
| 10 |
use FluentForm\App\Models\Subscription; |
| 11 |
use FluentForm\App\Models\Transaction; |
| 12 |
use FluentForm\App\Modules\Acl\Acl; |
| 13 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 14 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 15 |
|
| 16 |
class PaymentEntries |
| 17 |
{ |
| 18 |
|
| 19 |
public function init() |
| 20 |
{ |
| 21 |
add_action('fluentform/render_payment_entries', array($this, 'loadApp')); |
| 22 |
add_action('wp_ajax_fluentform_get_payments', array($this, 'getPayments')); |
| 23 |
add_action('wp_ajax_fluentform-do_entry_bulk_actions_payment', array($this, 'handleBulkAction')); |
| 24 |
add_action('wp_ajax_fluentform_get_all_payments_entries_filters', array($this, 'getFilters')); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
public function loadApp() |
| 29 |
{ |
| 30 |
wp_enqueue_style('ff-payment-entries', fluentFormMix('css/payment_entries.css'), [], FLUENTFORM_VERSION); |
| 31 |
wp_enqueue_script('ff-payment-entries', fluentFormMix('js/payment_entries.js'), ['jquery'], FLUENTFORM_VERSION, true); |
| 32 |
$settingsUrl = admin_url('admin.php?page=fluent_forms_settings#payments/general_settings'); |
| 33 |
do_action_deprecated( |
| 34 |
'fluentform_global_menu', |
| 35 |
[ |
| 36 |
], |
| 37 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 38 |
'fluentform/global_menu', |
| 39 |
'Use fluentform/global_menu instead of fluentform_global_menu.' |
| 40 |
); |
| 41 |
do_action('fluentform/global_menu'); |
| 42 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $settingsUrl is escaped via esc_url |
| 43 |
echo '<div id="ff_payment_entries"><ff-payment-entries settings_url="'.esc_url($settingsUrl).'"></ff-payment-entries><global-search></global-search></div>'; |
| 44 |
} |
| 45 |
|
| 46 |
public function getPayments() |
| 47 |
{ |
| 48 |
$request = wpFluentForm()->request; |
| 49 |
$attributes = $request->all(); |
| 50 |
|
| 51 |
// Sanitize request data |
| 52 |
$sanitizeMap = [ |
| 53 |
'form_id' => function ($value) { |
| 54 |
return Acl::normalizeFormId($value); |
| 55 |
}, |
| 56 |
'per_page' => 'intval', |
| 57 |
'payment_statuses' => 'sanitize_text_field', |
| 58 |
'payment_types' => 'sanitize_text_field', |
| 59 |
'payment_methods' => 'sanitize_text_field', |
| 60 |
]; |
| 61 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 62 |
|
| 63 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by Acl::verify() below |
| 64 |
Acl::verify('fluentform_view_payments', ArrayHelper::get($attributes, 'form_id')); |
| 65 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by Acl::verify() above |
| 66 |
$perPage = ArrayHelper::get($attributes, 'per_page', 10); |
| 67 |
if(!$perPage) { |
| 68 |
$perPage = 10; |
| 69 |
} |
| 70 |
$paymentsQuery = Transaction::select([ |
| 71 |
'fluentform_transactions.id', |
| 72 |
'fluentform_transactions.form_id', |
| 73 |
'fluentform_transactions.submission_id', |
| 74 |
'fluentform_transactions.transaction_type', |
| 75 |
'fluentform_transactions.payment_method', |
| 76 |
'fluentform_transactions.payment_mode', |
| 77 |
'fluentform_transactions.charge_id', |
| 78 |
'fluentform_transactions.card_brand', |
| 79 |
'fluentform_transactions.payment_total', |
| 80 |
'fluentform_transactions.created_at', |
| 81 |
'fluentform_transactions.payer_name', |
| 82 |
'fluentform_transactions.status', |
| 83 |
'fluentform_transactions.currency', |
| 84 |
'fluentform_forms.title' |
| 85 |
]) |
| 86 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_transactions.form_id') |
| 87 |
->orderBy('fluentform_transactions.id', 'DESC'); |
| 88 |
|
| 89 |
if ($selectedFormId = ArrayHelper::get($attributes, 'form_id')) { |
| 90 |
$paymentsQuery = $paymentsQuery->where('fluentform_transactions.form_id', $selectedFormId); |
| 91 |
} |
| 92 |
|
| 93 |
$allowFormIds = apply_filters('fluentform/current_user_allowed_forms', false); |
| 94 |
if (false !== $allowFormIds && is_array($allowFormIds)) { |
| 95 |
$paymentsQuery = $paymentsQuery->whereIn('fluentform_transactions.form_id', $allowFormIds ?: [0]); |
| 96 |
} |
| 97 |
if ($paymentStatus = ArrayHelper::get($attributes, 'payment_statuses')) { |
| 98 |
$paymentsQuery = $paymentsQuery->where('fluentform_transactions.status', $paymentStatus); |
| 99 |
} |
| 100 |
if ($paymentTypes = ArrayHelper::get($attributes, 'payment_types')) { |
| 101 |
$paymentsQuery = $paymentsQuery->where('fluentform_transactions.transaction_type', $paymentTypes); |
| 102 |
} |
| 103 |
if ($paymentMethods = ArrayHelper::get($attributes, 'payment_methods')) { |
| 104 |
$paymentsQuery = $paymentsQuery->where('fluentform_transactions.payment_method', $paymentMethods); |
| 105 |
} |
| 106 |
$paymentsPaginate = $paymentsQuery->paginate($perPage); |
| 107 |
|
| 108 |
$payments = $paymentsPaginate->items(); |
| 109 |
foreach ($payments as $payment) { |
| 110 |
$payment->formatted_payment_total = PaymentHelper::formatMoney($payment->payment_total, $payment->currency); |
| 111 |
$payment->entry_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id='.$payment->form_id.'#/entries/'.$payment->submission_id); |
| 112 |
if($payment->payment_method == 'test') { |
| 113 |
$payment->payment_method = 'offline'; |
| 114 |
} |
| 115 |
} |
| 116 |
wp_send_json_success([ |
| 117 |
'payments' => $payments, |
| 118 |
'total' => $paymentsPaginate->total(), |
| 119 |
'current_page' => $paymentsPaginate->currentPage(), |
| 120 |
'per_page' => $paymentsPaginate->perPage(), |
| 121 |
'last_page' => $paymentsPaginate->lastPage() |
| 122 |
]); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
public function handleBulkAction() |
| 127 |
{ |
| 128 |
$request = wpFluentForm()->request; |
| 129 |
$attributes = $request->all(); |
| 130 |
|
| 131 |
$sanitizeMap = [ |
| 132 |
'entries' => function($value) { |
| 133 |
if (is_array($value)) { |
| 134 |
return array_map('intval', $value); |
| 135 |
} |
| 136 |
return []; |
| 137 |
}, |
| 138 |
'action_type' => 'sanitize_text_field', |
| 139 |
]; |
| 140 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 141 |
|
| 142 |
$entries = ArrayHelper::get($attributes, 'entries', []); |
| 143 |
$actionType = ArrayHelper::get($attributes, 'action_type'); |
| 144 |
if (!$actionType || !count($entries)) { |
| 145 |
wp_send_json_error([ |
| 146 |
'message' => __('Please select entries & action first', 'fluentform') |
| 147 |
], 400); |
| 148 |
} |
| 149 |
|
| 150 |
$message = __("Invalid action", 'fluentform'); |
| 151 |
$statusCode = 400; |
| 152 |
// permanently delete payment entries from transactions |
| 153 |
if ($actionType == 'delete_items') { |
| 154 |
// get submission ids to delete order items |
| 155 |
$transactionData = Transaction::select(['id', 'form_id', 'submission_id']) |
| 156 |
->whereIn('id', $entries) |
| 157 |
->get(); |
| 158 |
|
| 159 |
$this->authorizeTransactionForms($transactionData); |
| 160 |
|
| 161 |
$submission_ids = []; |
| 162 |
|
| 163 |
foreach ($transactionData as $transactionDatum) { |
| 164 |
$submission_ids[] = $transactionDatum->submission_id; |
| 165 |
} |
| 166 |
|
| 167 |
try { |
| 168 |
if (!$submission_ids || count($transactionData) === 0 ) { |
| 169 |
throw new \Exception(__('Invalid transaction id', 'fluentform')); |
| 170 |
} |
| 171 |
do_action_deprecated( |
| 172 |
'fluentform_before_entry_payment_deleted', |
| 173 |
[ |
| 174 |
$entries, |
| 175 |
$transactionData |
| 176 |
], |
| 177 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 178 |
'fluentform/before_entry_payment_deleted', |
| 179 |
'Use fluentform/before_entry_payment_deleted instead of fluentform_before_entry_payment_deleted.' |
| 180 |
); |
| 181 |
do_action('fluentform/before_entry_payment_deleted', $entries, $transactionData); |
| 182 |
|
| 183 |
//delete data from transaction table |
| 184 |
Transaction::whereIn('id', $entries)->delete(); |
| 185 |
|
| 186 |
//delete data from order table |
| 187 |
OrderItem::whereIn('submission_id', $submission_ids)->delete(); |
| 188 |
|
| 189 |
// delete data from subscriptions table |
| 190 |
Subscription::whereIn('submission_id', $submission_ids)->delete(); |
| 191 |
|
| 192 |
//add log in each form that payment record has been deleted |
| 193 |
foreach ($transactionData as $data){ |
| 194 |
$logData = [ |
| 195 |
'parent_source_id' => $data->form_id, |
| 196 |
'source_type' => 'submission_item', |
| 197 |
'source_id' => $data->submission_id, |
| 198 |
'component' => 'payment', |
| 199 |
'status' => 'info', |
| 200 |
'title' => __('Payment data successfully deleted', 'fluentform'), |
| 201 |
'description' => __('Payment record cleared from transaction history and order items', 'fluentform'), |
| 202 |
]; |
| 203 |
do_action('fluentform/log_data', $logData); |
| 204 |
} |
| 205 |
do_action_deprecated( |
| 206 |
'fluentform_after_entry_payment_deleted', |
| 207 |
[ |
| 208 |
$entries, |
| 209 |
$transactionData |
| 210 |
], |
| 211 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 212 |
'fluentform/after_entry_payment_deleted', |
| 213 |
'Use fluentform/after_entry_payment_deleted instead of fluentform_after_entry_payment_deleted.' |
| 214 |
); |
| 215 |
do_action('fluentform/after_entry_payment_deleted', $entries, $transactionData); |
| 216 |
$message = __('Selected entries successfully deleted', 'fluentform'); |
| 217 |
$statusCode = 200; |
| 218 |
|
| 219 |
} catch (\Exception $exception) { |
| 220 |
$message = $exception->getMessage(); |
| 221 |
$statusCode = 400; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
wp_send_json_success([ |
| 226 |
'message' => $message |
| 227 |
], $statusCode); |
| 228 |
} |
| 229 |
|
| 230 |
private function authorizeTransactionForms($transactionData) |
| 231 |
{ |
| 232 |
Acl::verify('fluentform_forms_manager'); |
| 233 |
|
| 234 |
$formIds = []; |
| 235 |
|
| 236 |
foreach ($transactionData as $transaction) { |
| 237 |
$formIds[(int) $transaction->form_id] = true; |
| 238 |
} |
| 239 |
|
| 240 |
foreach (array_keys($formIds) as $formId) { |
| 241 |
if (!Acl::hasPermission('fluentform_forms_manager', $formId)) { |
| 242 |
wp_send_json_error([ |
| 243 |
'message' => __('You do not have permission to perform this action.', 'fluentform') |
| 244 |
], 422); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
public function getFilters() |
| 250 |
{ |
| 251 |
$request = wpFluentForm()->request; |
| 252 |
$attributes = $request->all(); |
| 253 |
|
| 254 |
$sanitizeMap = [ |
| 255 |
'form_id' => function ($value) { |
| 256 |
return Acl::normalizeFormId($value); |
| 257 |
}, |
| 258 |
]; |
| 259 |
$attributes = fluentform_backend_sanitizer($attributes, $sanitizeMap); |
| 260 |
|
| 261 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by Acl::verify() below |
| 262 |
Acl::verify('fluentform_view_payments', ArrayHelper::get($attributes, 'form_id')); |
| 263 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified by Acl::verify() above |
| 264 |
$selectedFormId = ArrayHelper::get($attributes, 'form_id'); |
| 265 |
$allowFormIds = apply_filters('fluentform/current_user_allowed_forms', false); |
| 266 |
|
| 267 |
$transactionTypesQuery = Transaction::select('transaction_type'); |
| 268 |
if ($selectedFormId) { |
| 269 |
$transactionTypesQuery = $transactionTypesQuery->where('form_id', $selectedFormId); |
| 270 |
} |
| 271 |
if (false !== $allowFormIds && is_array($allowFormIds)) { |
| 272 |
$transactionTypesQuery = $transactionTypesQuery->whereIn('form_id', $allowFormIds ?: [0]); |
| 273 |
} |
| 274 |
|
| 275 |
$transactionTypes = $transactionTypesQuery |
| 276 |
->groupBy('transaction_type') |
| 277 |
->get(); |
| 278 |
// Define transaction type labels |
| 279 |
$transactionTypeLabels = [ |
| 280 |
'onetime' => __('Charge', 'fluentform'), |
| 281 |
'refund' => __('Refund', 'fluentform'), |
| 282 |
'subscription' => __('Subscription', 'fluentform'), |
| 283 |
'renewal' => __('Renewal', 'fluentform') |
| 284 |
]; |
| 285 |
|
| 286 |
$formattedTransactionTypes = []; |
| 287 |
foreach ($transactionTypes as $type) { |
| 288 |
$label = ArrayHelper::get($transactionTypeLabels, $type->transaction_type, ucfirst($type->transaction_type)); |
| 289 |
$formattedTransactionTypes[] = [ |
| 290 |
'value' => $label, |
| 291 |
'key' => $type->transaction_type |
| 292 |
]; |
| 293 |
} |
| 294 |
|
| 295 |
$statusesQuery = Transaction::select('status'); |
| 296 |
if ($selectedFormId) { |
| 297 |
$statusesQuery = $statusesQuery->where('form_id', $selectedFormId); |
| 298 |
} |
| 299 |
if (false !== $allowFormIds && is_array($allowFormIds)) { |
| 300 |
$statusesQuery = $statusesQuery->whereIn('form_id', $allowFormIds ?: [0]); |
| 301 |
} |
| 302 |
|
| 303 |
$statuses = $statusesQuery |
| 304 |
->groupBy('status') |
| 305 |
->get(); |
| 306 |
$statusTypes = PaymentHelper::getPaymentStatuses(); |
| 307 |
$formattedStatuses = []; |
| 308 |
foreach ($statuses as $status) { |
| 309 |
$formattedStatuses[] = ArrayHelper::get($statusTypes, $status->status, $status->status); |
| 310 |
} |
| 311 |
$formsQuery = Transaction::select('fluentform_transactions.form_id', 'fluentform_forms.title'); |
| 312 |
if ($selectedFormId) { |
| 313 |
$formsQuery = $formsQuery->where('fluentform_transactions.form_id', $selectedFormId); |
| 314 |
} |
| 315 |
if (false !== $allowFormIds && is_array($allowFormIds)) { |
| 316 |
$formsQuery = $formsQuery->whereIn('fluentform_transactions.form_id', $allowFormIds ?: [0]); |
| 317 |
} |
| 318 |
|
| 319 |
$forms = $formsQuery |
| 320 |
->groupBy('fluentform_transactions.form_id') |
| 321 |
->orderBy('fluentform_transactions.form_id', 'DESC') |
| 322 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_transactions.form_id') |
| 323 |
->get(); |
| 324 |
|
| 325 |
$formattedForms = []; |
| 326 |
foreach ($forms as $form) { |
| 327 |
$formattedForms[] = [ |
| 328 |
'form_id' => $form->form_id, |
| 329 |
'title' => $form->title |
| 330 |
]; |
| 331 |
} |
| 332 |
|
| 333 |
$paymentMethodsQuery = Transaction::select('payment_method'); |
| 334 |
if ($selectedFormId) { |
| 335 |
$paymentMethodsQuery = $paymentMethodsQuery->where('form_id', $selectedFormId); |
| 336 |
} |
| 337 |
if (false !== $allowFormIds && is_array($allowFormIds)) { |
| 338 |
$paymentMethodsQuery = $paymentMethodsQuery->whereIn('form_id', $allowFormIds ?: [0]); |
| 339 |
} |
| 340 |
|
| 341 |
$paymentMethods = $paymentMethodsQuery |
| 342 |
->groupBy('payment_method') |
| 343 |
->get(); |
| 344 |
|
| 345 |
$formattedMethods = []; |
| 346 |
foreach ($paymentMethods as $method) { |
| 347 |
if(!$method->payment_method){ |
| 348 |
continue; |
| 349 |
} |
| 350 |
if ($method->payment_method == 'test') { |
| 351 |
$formattedMethods[] = ['value' => __('Offline', 'fluentform'), 'key' => $method->payment_method]; |
| 352 |
} else { |
| 353 |
$formattedMethods[] = ['value' => ucfirst($method->payment_method), 'key' => $method->payment_method]; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
wp_send_json_success([ |
| 358 |
'available_payment_types' => $formattedTransactionTypes, |
| 359 |
'available_statuses' => $formattedStatuses, |
| 360 |
'available_forms' => $formattedForms, |
| 361 |
'available_methods' => array_filter($formattedMethods), |
| 362 |
]); |
| 363 |
|
| 364 |
} |
| 365 |
} |
| 366 |
|