| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\MCP\Support; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
use FluentForm\App\Modules\Acl\Acl; |
| 8 |
use FluentForm\App\Modules\Payments\Orders\OrderData; |
| 9 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 10 |
|
| 11 |
/** |
| 12 |
* Default payment provider for the MCP submission seams. |
| 13 |
* |
| 14 |
* Listens on fluentform/mcp_submission_data (entry) and |
| 15 |
* fluentform/mcp_submission_rows (list) to attach a compact payment block from |
| 16 |
* the core payment tables. The entry tools authorize on entry-view permission, |
| 17 |
* so THIS listener owns the payments boundary: nothing is injected unless the |
| 18 |
* user holds fluentform_view_payments for the entry's form. Fail-closed — any |
| 19 |
* error returns the payload unchanged. An addon that already populated |
| 20 |
* 'payment' wins; this provider never overwrites. |
| 21 |
*/ |
| 22 |
class PaymentDataProvider |
| 23 |
{ |
| 24 |
public static function register() |
| 25 |
{ |
| 26 |
add_filter('fluentform/mcp_submission_data', [__CLASS__, 'addEntryPayment'], 10, 2); |
| 27 |
add_filter('fluentform/mcp_submission_rows', [__CLASS__, 'addRowPayments'], 10, 3); |
| 28 |
} |
| 29 |
|
| 30 |
public static function addEntryPayment($data, $submission) |
| 31 |
{ |
| 32 |
if (isset($data['payment'])) { |
| 33 |
return $data; |
| 34 |
} |
| 35 |
|
| 36 |
try { |
| 37 |
$formId = isset($submission->form_id) ? (int) $submission->form_id : 0; |
| 38 |
if (!$formId || !Acl::hasPermission('fluentform_view_payments', $formId)) { |
| 39 |
return $data; |
| 40 |
} |
| 41 |
|
| 42 |
$payment = self::buildPayment($submission); |
| 43 |
if ($payment) { |
| 44 |
$data['payment'] = $payment; |
| 45 |
} |
| 46 |
} catch (\Throwable $e) { |
| 47 |
return $data; |
| 48 |
} |
| 49 |
|
| 50 |
return $data; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Attach payment summaries to the supplied submission rows. |
| 55 |
* |
| 56 |
* @param iterable<\FluentForm\App\Models\Submission|\stdClass> $items |
| 57 |
*/ |
| 58 |
public static function addRowPayments($rows, $items, $formId) |
| 59 |
{ |
| 60 |
try { |
| 61 |
$formId = (int) $formId; |
| 62 |
if (!$formId || !Acl::hasPermission('fluentform_view_payments', $formId)) { |
| 63 |
return $rows; |
| 64 |
} |
| 65 |
|
| 66 |
// The seam passes the page's Submission models (payment columns |
| 67 |
// included), so the per-row summary needs no query of its own. |
| 68 |
$map = []; |
| 69 |
foreach ($items as $submission) { |
| 70 |
if (!isset($submission->payment_status) || !$submission->payment_status) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
$map[(int) $submission->id] = [ |
| 74 |
'status' => $submission->payment_status, |
| 75 |
'total' => isset($submission->payment_total) |
| 76 |
? PaymentHelper::formatMoney($submission->payment_total, isset($submission->currency) ? $submission->currency : '') |
| 77 |
: null, |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
if (!$map) { |
| 82 |
return $rows; |
| 83 |
} |
| 84 |
|
| 85 |
foreach ($rows as &$row) { |
| 86 |
$id = isset($row['id']) ? (int) $row['id'] : 0; |
| 87 |
if ($id && isset($map[$id]) && !isset($row['payment'])) { |
| 88 |
$row['payment'] = $map[$id]; |
| 89 |
} |
| 90 |
} |
| 91 |
unset($row); |
| 92 |
} catch (\Throwable $e) { |
| 93 |
return $rows; |
| 94 |
} |
| 95 |
|
| 96 |
return $rows; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Compact, agent-sized block: status, formatted total, currency, method, |
| 101 |
* transaction count, and subscription status/interval. Never raw cents, |
| 102 |
* payment_note, vendor_response, or original_plan. |
| 103 |
* |
| 104 |
* @param \FluentForm\App\Models\Submission|\stdClass $submission |
| 105 |
*/ |
| 106 |
private static function buildPayment($submission) |
| 107 |
{ |
| 108 |
// The submission row already carries the payment columns; when none is |
| 109 |
// set this is a non-payment entry — skip both transaction queries. |
| 110 |
$hasPaymentColumns = (isset($submission->payment_status) && $submission->payment_status) |
| 111 |
|| isset($submission->payment_total) |
| 112 |
|| (isset($submission->payment_type) && $submission->payment_type); |
| 113 |
if (!$hasPaymentColumns) { |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
$transactions = OrderData::getTransactions($submission->id); |
| 118 |
list($subscriptions) = OrderData::getSubscriptionsAndPaymentTotal($submission); |
| 119 |
|
| 120 |
// count(), not empty(): ->get() returns a Collection and |
| 121 |
// empty(Collection) is always false — an empty() guard would fabricate |
| 122 |
// a payment block on every non-payment entry. |
| 123 |
$hasStatus = isset($submission->payment_status) && $submission->payment_status; |
| 124 |
if (!$hasStatus && 0 === count($transactions) && 0 === count($subscriptions)) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
|
| 128 |
$currency = isset($submission->currency) ? $submission->currency : ''; |
| 129 |
|
| 130 |
$payment = [ |
| 131 |
'status' => $hasStatus ? $submission->payment_status : null, |
| 132 |
'total' => isset($submission->payment_total) ? PaymentHelper::formatMoney($submission->payment_total, $currency) : null, |
| 133 |
'currency' => $currency, |
| 134 |
'payment_method' => isset($submission->payment_method) ? $submission->payment_method : null, |
| 135 |
'transaction_count' => count($transactions), |
| 136 |
]; |
| 137 |
|
| 138 |
if (count($subscriptions)) { |
| 139 |
$subscription = $subscriptions[0]; |
| 140 |
$payment['subscription'] = [ |
| 141 |
'status' => isset($subscription->status) ? $subscription->status : null, |
| 142 |
'billing_interval' => isset($subscription->billing_interval) ? $subscription->billing_interval : null, |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
return $payment; |
| 147 |
} |
| 148 |
} |
| 149 |
|